mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 19:31:20 +08:00
Add pre-build hooks. Print command output when verbosity flag set. (#1578)
* Add pre-build hooks. Print command output when verbosity flag set. * Refactor build hooks
This commit is contained in:
parent
8e43206ae5
commit
f46e825d24
@ -51,15 +51,16 @@ type Project struct {
|
|||||||
// RunNonNativeBuildHooks will run build hooks though they are defined for a GOOS which is not equal to the host os
|
// RunNonNativeBuildHooks will run build hooks though they are defined for a GOOS which is not equal to the host os
|
||||||
RunNonNativeBuildHooks bool `json:"runNonNativeBuildHooks"`
|
RunNonNativeBuildHooks bool `json:"runNonNativeBuildHooks"`
|
||||||
|
|
||||||
// Post build hooks for different targets, the hooks are executed in the following order
|
// Build hooks for different targets, the hooks are executed in the following order
|
||||||
// Key: GOOS/GOARCH - Executed at build level after a build of the specific platform and arch
|
// Key: GOOS/GOARCH - Executed at build level before/after a build of the specific platform and arch
|
||||||
// Key: GOOS/* - Executed at build level after a build of the specific platform
|
// Key: GOOS/* - Executed at build level before/after a build of the specific platform
|
||||||
// Key: */* - Executed at build level after a build
|
// Key: */* - Executed at build level before/after a build
|
||||||
// The following keys are not yet supported.
|
// The following keys are not yet supported.
|
||||||
// Key: GOOS - Executed at platform level after all builds of the specific platform
|
// Key: GOOS - Executed at platform level before/after all builds of the specific platform
|
||||||
// Key: * - Executed at platform level after all builds of a platform
|
// Key: * - Executed at platform level before/after all builds of a platform
|
||||||
// Key: [empty] - Executed at global level after all builds of all platforms
|
// Key: [empty] - Executed at global level before/after all builds of all platforms
|
||||||
PostBuildHooks map[string]string `json:"postBuildHooks"`
|
PostBuildHooks map[string]string `json:"postBuildHooks"`
|
||||||
|
PreBuildHooks map[string]string `json:"preBuildHooks"`
|
||||||
|
|
||||||
// The application author
|
// The application author
|
||||||
Author Author
|
Author Author
|
||||||
|
@ -115,6 +115,16 @@ func Build(options *Options) (string, error) {
|
|||||||
// Initialise Builder
|
// Initialise Builder
|
||||||
builder.SetProjectData(projectData)
|
builder.SetProjectData(projectData)
|
||||||
|
|
||||||
|
hookArgs := map[string]string{
|
||||||
|
"${platform}": options.Platform + "/" + options.Arch,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, hook := range []string{options.Platform + "/" + options.Arch, options.Platform + "/*", "*/*"} {
|
||||||
|
if err := execPreBuildHook(outputLogger, options, hook, hookArgs); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !options.IgnoreFrontend || options.ForceBuild {
|
if !options.IgnoreFrontend || options.ForceBuild {
|
||||||
err = builder.BuildFrontend(outputLogger)
|
err = builder.BuildFrontend(outputLogger)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -215,10 +225,7 @@ func Build(options *Options) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
compileBinary := options.CompiledBinary
|
compileBinary := options.CompiledBinary
|
||||||
hookArgs := map[string]string{
|
hookArgs["${bin}"] = compileBinary
|
||||||
"${platform}": options.Platform + "/" + options.Arch,
|
|
||||||
"${bin}": compileBinary,
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, hook := range []string{options.Platform + "/" + options.Arch, options.Platform + "/*", "*/*"} {
|
for _, hook := range []string{options.Platform + "/" + options.Arch, options.Platform + "/*", "*/*"} {
|
||||||
if err := execPostBuildHook(outputLogger, options, hook, hookArgs); err != nil {
|
if err := execPostBuildHook(outputLogger, options, hook, hookArgs); err != nil {
|
||||||
@ -229,19 +236,33 @@ func Build(options *Options) (string, error) {
|
|||||||
return compileBinary, nil
|
return compileBinary, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func execPreBuildHook(outputLogger *clilogger.CLILogger, options *Options, hookIdentifier string, argReplacements map[string]string) error {
|
||||||
|
preBuildHook := options.ProjectData.PreBuildHooks[hookIdentifier]
|
||||||
|
if preBuildHook == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return executeBuildHook(outputLogger, options, hookIdentifier, argReplacements, preBuildHook, "pre")
|
||||||
|
}
|
||||||
|
|
||||||
func execPostBuildHook(outputLogger *clilogger.CLILogger, options *Options, hookIdentifier string, argReplacements map[string]string) error {
|
func execPostBuildHook(outputLogger *clilogger.CLILogger, options *Options, hookIdentifier string, argReplacements map[string]string) error {
|
||||||
postBuildHook := options.ProjectData.PostBuildHooks[hookIdentifier]
|
postBuildHook := options.ProjectData.PostBuildHooks[hookIdentifier]
|
||||||
if postBuildHook == "" {
|
if postBuildHook == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return executeBuildHook(outputLogger, options, hookIdentifier, argReplacements, postBuildHook, "post")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func executeBuildHook(outputLogger *clilogger.CLILogger, options *Options, hookIdentifier string, argReplacements map[string]string, buildHook string, hookName string) error {
|
||||||
if !options.ProjectData.RunNonNativeBuildHooks {
|
if !options.ProjectData.RunNonNativeBuildHooks {
|
||||||
if hookIdentifier == "" {
|
if hookIdentifier == "" {
|
||||||
// That's the global hook
|
// That's the global hook
|
||||||
} else {
|
} else {
|
||||||
platformOfHook := strings.Split(hookIdentifier, "/")[0]
|
platformOfHook := strings.Split(hookIdentifier, "/")[0]
|
||||||
if platformOfHook == "*" {
|
if platformOfHook == "*" {
|
||||||
// Thats OK, we don't have a specific platform of the hook
|
// That's OK, we don't have a specific platform of the hook
|
||||||
} else if platformOfHook == runtime.GOOS {
|
} else if platformOfHook == runtime.GOOS {
|
||||||
// The hook is for host platform
|
// The hook is for host platform
|
||||||
} else {
|
} else {
|
||||||
@ -252,8 +273,8 @@ func execPostBuildHook(outputLogger *clilogger.CLILogger, options *Options, hook
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
outputLogger.Print(" - Executing post build hook '%s': ", hookIdentifier)
|
outputLogger.Print(" - Executing %s build hook '%s': ", hookName, hookIdentifier)
|
||||||
args := strings.Split(postBuildHook, " ")
|
args := strings.Split(buildHook, " ")
|
||||||
for i, arg := range args {
|
for i, arg := range args {
|
||||||
newArg := argReplacements[arg]
|
newArg := argReplacements[arg]
|
||||||
if newArg == "" {
|
if newArg == "" {
|
||||||
@ -266,7 +287,10 @@ func execPostBuildHook(outputLogger *clilogger.CLILogger, options *Options, hook
|
|||||||
outputLogger.Println("%s", strings.Join(args, " "))
|
outputLogger.Println("%s", strings.Join(args, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
_, stderr, err := shell.RunCommand(options.BuildDirectory, args[0], args[1:]...)
|
stdout, stderr, err := shell.RunCommand(options.BuildDirectory, args[0], args[1:]...)
|
||||||
|
if options.Verbosity == VERBOSE {
|
||||||
|
println(stdout)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%s - %s", err.Error(), stderr)
|
return fmt.Errorf("%s - %s", err.Error(), stderr)
|
||||||
}
|
}
|
||||||
|
@ -23,11 +23,16 @@ The project config resides in the `wails.json` file in the project directory. Th
|
|||||||
"devServer": "[Address to bind the wails dev sever to. Default: localhost:34115]",
|
"devServer": "[Address to bind the wails dev sever to. Default: localhost:34115]",
|
||||||
"appargs": "[Arguments passed to the application in shell style when in dev mode]",
|
"appargs": "[Arguments passed to the application in shell style when in dev mode]",
|
||||||
"runNonNativeBuildHooks": false, // Defines if build hooks should be run though they are defined for an OS other than the host OS.
|
"runNonNativeBuildHooks": false, // Defines if build hooks should be run though they are defined for an OS other than the host OS.
|
||||||
"postBuildHooks": {
|
"preBuildHooks": {
|
||||||
"GOOS/GOARCH": "[The command that will be executed after a build of the specified GOOS/GOARCH: ${platform} is replaced with the "GOOS/GOARCH" and ${bin} with the path to the compiled binary. The "GOOS/GOARCH" hook is executed before the "GOOS/*" and "*/*" hook.]",
|
"GOOS/GOARCH": "[The command that will be executed before a build of the specified GOOS/GOARCH: ${platform} is replaced with the "GOOS/GOARCH". The "GOOS/GOARCH" hook is executed before the "GOOS/*" and "*/*" hook.]",
|
||||||
"GOOS/*": "[The command that will be executed after a build of the specified GOOS: ${platform} is replaced with the "GOOS/GOARCH" and ${bin} with the path to the compiled binary. The "GOOS/*" hook is executed before the "*/*" hook.]",
|
"GOOS/*": "[The command that will be executed before a build of the specified GOOS: ${platform} is replaced with the "GOOS/GOARCH". The "GOOS/*" hook is executed before the "*/*" hook.]",
|
||||||
"*/*": "[The command that will be executed after every build: ${platform} is replaced with the "GOOS/GOARCH" and ${bin} with the path to the compiled binary.]"
|
"*/*": "[The command that will be executed before every build: ${platform} is replaced with the "GOOS/GOARCH".]"
|
||||||
},
|
},
|
||||||
|
"postBuildHooks": {
|
||||||
|
"GOOS/GOARCH": "[The command that will be executed after a build of the specified GOOS/GOARCH: ${platform} is replaced with the "GOOS/GOARCH" and ${bin} with the path to the compiled binary. The "GOOS/GOARCH" hook is executed before the "GOOS/*" and "*/*" hook.]",
|
||||||
|
"GOOS/*": "[The command that will be executed after a build of the specified GOOS: ${platform} is replaced with the "GOOS/GOARCH" and ${bin} with the path to the compiled binary. The "GOOS/*" hook is executed before the "*/*" hook.]",
|
||||||
|
"*/*": "[The command that will be executed after every build: ${platform} is replaced with the "GOOS/GOARCH" and ${bin} with the path to the compiled binary.]"
|
||||||
|
},
|
||||||
"info": { // Data used to populate manifests and version info.
|
"info": { // Data used to populate manifests and version info.
|
||||||
"companyName": "[The company name. Default: [The project name]]",
|
"companyName": "[The company name. Default: [The project name]]",
|
||||||
"productName": "[The product name. Default: [The project name]]",
|
"productName": "[The product name. Default: [The project name]]",
|
||||||
|
Loading…
Reference in New Issue
Block a user