diff --git a/v2/cmd/wails/internal/commands/build/build.go b/v2/cmd/wails/internal/commands/build/build.go index 1ea3ec18..01e3b911 100644 --- a/v2/cmd/wails/internal/commands/build/build.go +++ b/v2/cmd/wails/internal/commands/build/build.go @@ -57,6 +57,11 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) { keepAssets := false command.BoolFlag("k", "Keep generated assets", &keepAssets) + appleIdentity := "" + if runtime.GOOS == "darwin" { + command.StringFlag("sign", "Signs your app with the given identity.", &appleIdentity) + } + command.Action(func() error { // Create logger @@ -72,6 +77,11 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) { app.PrintBanner() } + // Ensure package is used with apple identity + if appleIdentity != "" && pack == false { + return fmt.Errorf("must use `-package` flag when using `-sign`") + } + task := fmt.Sprintf("Building %s Application", strings.Title(outputType)) logger.Println(task) logger.Println(strings.Repeat("-", len(task))) @@ -84,14 +94,15 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) { // Create BuildOptions buildOptions := &build.Options{ - Logger: logger, - OutputType: outputType, - Mode: mode, - Pack: pack, - Platform: platform, - LDFlags: ldflags, - Compiler: compilerCommand, - KeepAssets: keepAssets, + Logger: logger, + OutputType: outputType, + Mode: mode, + Pack: pack, + Platform: platform, + LDFlags: ldflags, + Compiler: compilerCommand, + KeepAssets: keepAssets, + AppleIdentity: appleIdentity, } return doBuild(buildOptions) diff --git a/v2/pkg/commands/build/build.go b/v2/pkg/commands/build/build.go index 8ed28131..b5163da1 100644 --- a/v2/pkg/commands/build/build.go +++ b/v2/pkg/commands/build/build.go @@ -38,6 +38,7 @@ type Options struct { BuildDirectory string // Directory to use for building the application CompiledBinary string // Fully qualified path to the compiled binary KeepAssets bool // /Keep the generated assets/files + AppleIdentity string } // GetModeAsString returns the current mode as a string diff --git a/v2/pkg/commands/build/packager_darwin.go b/v2/pkg/commands/build/packager_darwin.go index 99f38305..127d37ae 100644 --- a/v2/pkg/commands/build/packager_darwin.go +++ b/v2/pkg/commands/build/packager_darwin.go @@ -2,9 +2,11 @@ package build import ( "bytes" + "fmt" "image" "io/ioutil" "os" + "os/exec" "path" "path/filepath" "strings" @@ -52,6 +54,12 @@ func packageApplication(options *Options) error { return err } + // Sign app if needed + err = signApplication(options) + if err != nil { + return err + } + return nil } @@ -176,3 +184,21 @@ func processApplicationIcon(resourceDir string, iconsDir string) (err error) { }() return icns.Encode(dest, srcImg) } + +func signApplication(options *Options) error { + bundlename := filepath.Join(options.BuildDirectory, options.ProjectData.Name+".app") + identity := fmt.Sprintf(`"%s"`, options.AppleIdentity) + cmd := exec.Command("codesign", "--deep", "--force", "--verbose", "--sign", identity, bundlename) + var stdo, stde bytes.Buffer + cmd.Stdout = &stdo + cmd.Stderr = &stde + + // Run command + err := cmd.Run() + + // Format error if we have one + if err != nil { + return fmt.Errorf("%s\n%s", err, string(stde.Bytes())) + } + return nil +}