From 86c1ea5e6a24d01159ab0fe8e77ab25c2ca5d194 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sat, 27 Mar 2021 20:59:14 +1100 Subject: [PATCH] Initial support for compression --- v2/cmd/wails/internal/commands/build/build.go | 10 ++++++++ v2/pkg/commands/build/base.go | 23 +++++++++++++++++++ v2/pkg/commands/build/build.go | 3 ++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/v2/cmd/wails/internal/commands/build/build.go b/v2/cmd/wails/internal/commands/build/build.go index 390ba4597..0300b36fd 100644 --- a/v2/cmd/wails/internal/commands/build/build.go +++ b/v2/cmd/wails/internal/commands/build/build.go @@ -39,6 +39,9 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) { compilerCommand := "go" command.StringFlag("compiler", "Use a different go compiler to build, eg go1.15beta1", &compilerCommand) + compress := false + command.BoolFlag("compress", "Compress final binary", &compress) + // Setup Platform flag platform := runtime.GOOS command.StringFlag("platform", "Platform to target", &platform) @@ -114,6 +117,10 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) { return fmt.Errorf("platform %s is not supported", platform) } + if compress && platform == "darwin/universal" { + println("Warning: compress flag unsupported for universal binaries. Ignoring.") + compress = false + } // Create BuildOptions buildOptions := &build.Options{ Logger: logger, @@ -127,6 +134,7 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) { KeepAssets: keepAssets, AppleIdentity: appleIdentity, Verbosity: verbosity, + Compress: compress, } // Calculate platform and arch @@ -147,10 +155,12 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) { } // Write out the system information + fmt.Fprintf(w, "\n") fmt.Fprintf(w, "App Type: \t%s\n", buildOptions.OutputType) fmt.Fprintf(w, "Platform: \t%s\n", buildOptions.Platform) fmt.Fprintf(w, "Arch: \t%s\n", buildOptions.Arch) fmt.Fprintf(w, "Compiler: \t%s\n", buildOptions.Compiler) + fmt.Fprintf(w, "Compress: \t%t\n", buildOptions.Compress) fmt.Fprintf(w, "Build Mode: \t%s\n", buildModeText) fmt.Fprintf(w, "Package: \t%t\n", buildOptions.Pack) fmt.Fprintf(w, "Clean Build Dir: \t%t\n", buildOptions.CleanBuildDirectory) diff --git a/v2/pkg/commands/build/base.go b/v2/pkg/commands/build/base.go index c712ba898..fb4b3aa94 100644 --- a/v2/pkg/commands/build/base.go +++ b/v2/pkg/commands/build/base.go @@ -10,6 +10,8 @@ import ( "runtime" "strings" + "github.com/pkg/errors" + "github.com/leaanthony/slicer" "github.com/wailsapp/wails/v2/internal/assetdb" "github.com/wailsapp/wails/v2/internal/fs" @@ -291,6 +293,27 @@ func (b *BaseBuilder) CompileProject(options *Options) error { return fmt.Errorf("%s\n%s", err, string(stde.Bytes())) } + if !options.Compress { + return nil + } + + // Do we have upx installed? + if !shell.CommandExists("upx") { + println("Warning: Cannot compress binary: upx not found") + return nil + } + + if verbose { + println(" Compressing with:", "upx", "--best", "--no-color", "--no-progress", options.CompiledBinary) + } + + output, err := exec.Command(options.BuildDirectory, "upx", "--best", "--no-color", "--no-progress", options.CompiledBinary).Output() + if err != nil { + return errors.Wrap(err, "Error during compression:") + } + if verbose { + println(output) + } return nil } diff --git a/v2/pkg/commands/build/build.go b/v2/pkg/commands/build/build.go index 3e4ea58a8..2937e5a87 100644 --- a/v2/pkg/commands/build/build.go +++ b/v2/pkg/commands/build/build.go @@ -42,8 +42,9 @@ type Options struct { BuildDirectory string // Directory to use for building the application CleanBuildDirectory bool // Indicates if the build directory should be cleaned before building CompiledBinary string // Fully qualified path to the compiled binary - KeepAssets bool // /Keep the generated assets/files + KeepAssets bool // Keep the generated assets/files Verbosity int // Verbosity level (0 - silent, 1 - default, 2 - verbose) + Compress bool // Compress the final binary AppleIdentity string }