From 0413b240dd160acf122984615861d5b2abee0fbb Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Wed, 4 Sep 2024 11:23:39 -0700 Subject: [PATCH] Only copy the relevant wavesrv binary when packaging for a specific architecture (#316) This change shaves ~20 MB off the download size by only copying over the wavesrv binary that is relevant for whichever architecture we're currently packaging. This is only relevant for macOS at the moment, though it can also apply to Windows when we get multi-arch builds working. This required renaming our Go binaries from .amd64 to .x64 to comply with electron-builder's naming conventions. --- Taskfile.yml | 10 ++++++---- electron-builder.config.cjs | 13 ++++++++++--- emain/platform.ts | 2 +- pkg/remote/connutil.go | 2 +- pkg/util/shellutil/shellutil.go | 18 +++++++++++------- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index 47428ebc..999de850 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -97,11 +97,11 @@ tasks: vars: - ARCHS cmds: - - cmd: CGO_ENABLED=1 GOARCH={{.ARCH}} go build -tags "osusergo,netgo,sqlite_omit_load_extension" -ldflags "{{.GO_LDFLAGS}} -X main.BuildTime=$({{.DATE}} +'%Y%m%d%H%M') -X main.WaveVersion={{.VERSION}}" -o dist/bin/wavesrv.{{.ARCH}}{{exeExt}} cmd/server/main-server.go + - cmd: CGO_ENABLED=1 GOARCH={{.GOARCH}} go build -tags "osusergo,netgo,sqlite_omit_load_extension" -ldflags "{{.GO_LDFLAGS}} -X main.BuildTime=$({{.DATE}} +'%Y%m%d%H%M') -X main.WaveVersion={{.VERSION}}" -o dist/bin/wavesrv.{{if eq .GOARCH "amd64"}}x64{{else}}{{.GOARCH}}{{end}}{{exeExt}} cmd/server/main-server.go for: var: ARCHS split: "," - as: ARCH + as: GOARCH sources: - "cmd/server/*.go" - "pkg/**/*.go" @@ -159,6 +159,8 @@ tasks: vars: EXT: sh: echo {{if eq .GOOS "windows"}}.exe{{end}} + NORMALIZEDARCH: + sh: echo {{if eq .GOARCH "amd64"}}x64{{else}}{{.GOARCH}}{{end}} requires: vars: - GOOS @@ -168,9 +170,9 @@ tasks: - "cmd/wsh/**/*.go" - "pkg/**/*.go" generates: - - dist/bin/wsh-{{.VERSION}}-{{.GOOS}}.{{.GOARCH}}{{.EXT}} + - dist/bin/wsh-{{.VERSION}}-{{.GOOS}}.{{.NORMALIZEDARCH}}{{.EXT}} cmds: - - (CGO_ENABLED=0 GOOS={{.GOOS}} GOARCH={{.GOARCH}} go build -ldflags="-s -w -X main.BuildTime=$({{.DATE}} +'%Y%m%d%H%M') -X main.WaveVersion={{.VERSION}}" -o dist/bin/wsh-{{.VERSION}}-{{.GOOS}}.{{.GOARCH}}{{.EXT}} cmd/wsh/main-wsh.go) + - (CGO_ENABLED=0 GOOS={{.GOOS}} GOARCH={{.GOARCH}} go build -ldflags="-s -w -X main.BuildTime=$({{.DATE}} +'%Y%m%d%H%M') -X main.WaveVersion={{.VERSION}}" -o dist/bin/wsh-{{.VERSION}}-{{.GOOS}}.{{.NORMALIZEDARCH}}{{.EXT}} cmd/wsh/main-wsh.go) deps: - go:mod:tidy internal: true diff --git a/electron-builder.config.cjs b/electron-builder.config.cjs index d99501b7..75fae151 100644 --- a/electron-builder.config.cjs +++ b/electron-builder.config.cjs @@ -17,7 +17,7 @@ const config = { { from: "./dist", to: "./dist", - filter: ["**/*"], + filter: ["**/*", "!bin/*", "bin/wavesrv.${arch}*", "bin/wsh*"], }, { from: ".", @@ -51,10 +51,17 @@ const config = { teamId: process.env.APPLE_TEAM_ID, } : false, + mergeASARs: true, + singleArchFiles: "dist/bin/wavesrv.*", binaries: fs - .readdirSync("dist/bin", { recursive: true, withFileTypes: true }) + .readdirSync("./dist/bin", { recursive: true, withFileTypes: true }) .filter((f) => f.isFile() && (f.name.startsWith("wavesrv") || f.name.includes("darwin"))) - .map((f) => path.resolve(f.parentPath, f.name)), + .map((f) => { + const resolvedPath = path.resolve(f.parentPath ?? f.path, f.name); + console.log("resolvedPath", resolvedPath); + return resolvedPath; + }) + .filter((path) => path), }, linux: { executableName: pkg.productName, diff --git a/emain/platform.ts b/emain/platform.ts index cd70004a..c910f640 100644 --- a/emain/platform.ts +++ b/emain/platform.ts @@ -18,7 +18,7 @@ if (isDevVite) { app.setName(isDev ? "TheNextWave (Dev)" : "TheNextWave"); const unamePlatform = process.platform; -const unameArch: string = process.arch === "x64" ? "amd64" : process.arch; +const unameArch: string = process.arch; keyutil.setKeyUtilPlatform(unamePlatform); ipcMain.on("get-is-dev", (event) => { diff --git a/pkg/remote/connutil.go b/pkg/remote/connutil.go index de58ad9f..ab0d1d57 100644 --- a/pkg/remote/connutil.go +++ b/pkg/remote/connutil.go @@ -193,7 +193,7 @@ func GetClientArch(client *ssh.Client) (string, error) { formatted := strings.ToLower(string(out)) formatted = strings.TrimSpace(formatted) if formatted == "x86_64" { - return "amd64", nil + return "x64", nil } return formatted, nil } diff --git a/pkg/util/shellutil/shellutil.go b/pkg/util/shellutil/shellutil.go index 5c6e16f5..f755dd8f 100644 --- a/pkg/util/shellutil/shellutil.go +++ b/pkg/util/shellutil/shellutil.go @@ -209,12 +209,19 @@ func GetZshZDotDir() string { return filepath.Join(wavebase.GetWaveHomeDir(), ZshIntegrationDir) } -func GetWshBinaryPath(version string, goos string, goarch string) string { +func GetWshBaseName(version string, goos string, goarch string) string { ext := "" + if goarch == "amd64" { + goarch = "x64" + } if goos == "windows" { ext = ".exe" } - return filepath.Join(os.Getenv(WaveAppPathVarName), AppPathBinDir, fmt.Sprintf("wsh-%s-%s.%s%s", version, goos, goarch, ext)) + return fmt.Sprintf("wsh-%s-%s.%s%s", version, goos, goarch, ext) +} + +func GetWshBinaryPath(version string, goos string, goarch string) string { + return filepath.Join(os.Getenv(WaveAppPathVarName), AppPathBinDir, GetWshBaseName(version, goos, goarch)) } func InitRcFiles(waveHome string, wshBinDir string) error { @@ -288,6 +295,7 @@ func initCustomShellStartupFilesInternal() error { } // copy the correct binary to bin + wshBaseName := GetWshBaseName(wavebase.WaveVersion, runtime.GOOS, runtime.GOARCH) wshFullPath := GetWshBinaryPath(wavebase.WaveVersion, runtime.GOOS, runtime.GOARCH) if _, err := os.Stat(wshFullPath); err != nil { log.Printf("error (non-fatal), could not resolve wsh binary %q: %v\n", wshFullPath, err) @@ -301,14 +309,10 @@ func initCustomShellStartupFilesInternal() error { if err != nil { return fmt.Errorf("error copying wsh binary to bin: %v", err) } - log.Printf("wsh binary successfully %q copied to %q\n", computeWshBaseName(), wshDstPath) + log.Printf("wsh binary successfully %q copied to %q\n", wshBaseName, wshDstPath) return nil } -func computeWshBaseName() string { - return fmt.Sprintf("wsh-%s-%s.%s", wavebase.WaveVersion, runtime.GOOS, runtime.GOARCH) -} - func toPwshEnvVarRef(input string) string { return strings.Replace(input, "$", "$env:", -1) }