From fa851f29c53c81733407178f3a55d0129cfd51c3 Mon Sep 17 00:00:00 2001 From: Mohamed Gharib Date: Sun, 18 Jun 2023 23:45:01 +0300 Subject: [PATCH] [v2] Add -devtools production build flag (#2725) * [v2] Add devtools production build flag * Update changelog * Fix changelog spacing --------- Co-authored-by: Lea Anthony --- v2/cmd/wails/build.go | 2 ++ v2/cmd/wails/flags/build.go | 1 + v2/cmd/wails/flags/dev.go | 1 + v2/internal/app/app.go | 4 ++++ v2/internal/app/app_dev.go | 5 ++++- v2/internal/app/app_devtools.go | 7 +++++++ v2/internal/app/app_devtools_not.go | 7 +++++++ v2/internal/app/app_production.go | 3 +++ v2/internal/frontend/desktop/darwin/Application.h | 2 +- v2/internal/frontend/desktop/darwin/Application.m | 4 ++-- v2/internal/frontend/desktop/darwin/WailsContext.h | 2 +- v2/internal/frontend/desktop/darwin/WailsContext.m | 2 +- v2/internal/frontend/desktop/darwin/frontend.go | 9 ++++++++- v2/internal/frontend/desktop/darwin/main.m | 4 ++-- v2/internal/frontend/desktop/darwin/window.go | 8 ++++---- v2/internal/frontend/desktop/linux/frontend.go | 9 ++++++++- v2/internal/frontend/desktop/linux/window.go | 8 +++++--- v2/internal/frontend/desktop/windows/frontend.go | 10 ++++++++-- v2/pkg/commands/build/base.go | 5 +++++ v2/pkg/commands/build/build.go | 1 + website/docs/reference/cli.mdx | 3 ++- website/docs/tutorials/helloworld.mdx | 1 + website/src/pages/changelog.mdx | 4 ++++ 23 files changed, 82 insertions(+), 20 deletions(-) create mode 100644 v2/internal/app/app_devtools.go create mode 100644 v2/internal/app/app_devtools_not.go diff --git a/v2/cmd/wails/build.go b/v2/cmd/wails/build.go index 5d284e9a2..1c6b791ec 100644 --- a/v2/cmd/wails/build.go +++ b/v2/cmd/wails/build.go @@ -57,6 +57,7 @@ func buildApplication(f *flags.Build) error { OutputFile: f.OutputFilename, CleanBinDirectory: f.Clean, Mode: f.GetBuildMode(), + Devtools: f.Debug || f.Devtools, Pack: !f.NoPackage, LDFlags: f.LdFlags, Compiler: f.Compiler, @@ -82,6 +83,7 @@ func buildApplication(f *flags.Build) error { {"Compiler", f.GetCompilerPath()}, {"Skip Bindings", bool2Str(f.SkipBindings)}, {"Build Mode", f.GetBuildModeAsString()}, + {"Devtools", bool2Str(buildOptions.Devtools)}, {"Frontend Directory", projectOptions.GetFrontendDir()}, {"Obfuscated", bool2Str(f.Obfuscated)}, } diff --git a/v2/cmd/wails/flags/build.go b/v2/cmd/wails/flags/build.go index d36ceef6a..974d9c3ad 100644 --- a/v2/cmd/wails/flags/build.go +++ b/v2/cmd/wails/flags/build.go @@ -35,6 +35,7 @@ type Build struct { ForceBuild bool `name:"f" description:"Force build of application"` UpdateWailsVersionGoMod bool `name:"u" description:"Updates go.mod to use the same Wails version as the CLI"` Debug bool `description:"Builds the application in debug mode"` + Devtools bool `description:"Enable Devtools in productions, Already enabled in debug mode (-debug)"` NSIS bool `description:"Generate NSIS installer for Windows"` TrimPath bool `description:"Remove all file system paths from the resulting executable"` WindowsConsole bool `description:"Keep the console when building for Windows"` diff --git a/v2/cmd/wails/flags/dev.go b/v2/cmd/wails/flags/dev.go index 885e0cead..6d4f02f95 100644 --- a/v2/cmd/wails/flags/dev.go +++ b/v2/cmd/wails/flags/dev.go @@ -120,6 +120,7 @@ func (d *Dev) GenerateBuildOptions() *build.Options { result := &build.Options{ OutputType: "dev", Mode: build.Dev, + Devtools: true, Arch: runtime.GOARCH, Pack: true, Platform: runtime.GOOS, diff --git a/v2/internal/app/app.go b/v2/internal/app/app.go index f2821aaba..226b5a0be 100644 --- a/v2/internal/app/app.go +++ b/v2/internal/app/app.go @@ -2,6 +2,7 @@ package app import ( "context" + "github.com/wailsapp/wails/v2/internal/frontend" "github.com/wailsapp/wails/v2/internal/logger" "github.com/wailsapp/wails/v2/internal/menumanager" @@ -20,6 +21,9 @@ type App struct { // Indicates if the app is in debug mode debug bool + // Indicates if the devtools is enabled + devtools bool + // OnStartup/OnShutdown startupCallback func(ctx context.Context) shutdownCallback func(ctx context.Context) diff --git a/v2/internal/app/app_dev.go b/v2/internal/app/app_dev.go index 38aada698..7cdc627f1 100644 --- a/v2/internal/app/app_dev.go +++ b/v2/internal/app/app_dev.go @@ -42,7 +42,9 @@ func (a *App) Run() error { func CreateApp(appoptions *options.App) (*App, error) { var err error - ctx := context.WithValue(context.Background(), "debug", true) + ctx := context.Background() + ctx = context.WithValue(ctx, "debug", true) + ctx = context.WithValue(ctx, "devtools", true) // Set up logger myLogger := logger.New(appoptions.Logger) @@ -228,6 +230,7 @@ func CreateApp(appoptions *options.App) (*App, error) { startupCallback: appoptions.OnStartup, shutdownCallback: appoptions.OnShutdown, debug: true, + devtools: true, } result.options = appoptions diff --git a/v2/internal/app/app_devtools.go b/v2/internal/app/app_devtools.go new file mode 100644 index 000000000..e28a1e080 --- /dev/null +++ b/v2/internal/app/app_devtools.go @@ -0,0 +1,7 @@ +//go:build devtools + +package app + +func IsDevtoolsEnabled() bool { + return true +} diff --git a/v2/internal/app/app_devtools_not.go b/v2/internal/app/app_devtools_not.go new file mode 100644 index 000000000..a2cfd4e5d --- /dev/null +++ b/v2/internal/app/app_devtools_not.go @@ -0,0 +1,7 @@ +//go:build !devtools + +package app + +func IsDevtoolsEnabled() bool { + return false +} diff --git a/v2/internal/app/app_production.go b/v2/internal/app/app_production.go index afb67bdb3..2dfad6015 100644 --- a/v2/internal/app/app_production.go +++ b/v2/internal/app/app_production.go @@ -34,7 +34,9 @@ func CreateApp(appoptions *options.App) (*App, error) { options.MergeDefaults(appoptions) debug := IsDebug() + devtools := IsDevtoolsEnabled() ctx = context.WithValue(ctx, "debug", debug) + ctx = context.WithValue(ctx, "devtools", devtools) // Set up logger myLogger := logger.New(appoptions.Logger) @@ -93,6 +95,7 @@ func CreateApp(appoptions *options.App) (*App, error) { startupCallback: appoptions.OnStartup, shutdownCallback: appoptions.OnShutdown, debug: debug, + devtools: devtools, options: appoptions, } diff --git a/v2/internal/frontend/desktop/darwin/Application.h b/v2/internal/frontend/desktop/darwin/Application.h index e418168e6..5e6aa0657 100644 --- a/v2/internal/frontend/desktop/darwin/Application.h +++ b/v2/internal/frontend/desktop/darwin/Application.h @@ -17,7 +17,7 @@ #define WindowStartsMinimised 2 #define WindowStartsFullscreen 3 -WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent, int debug, int windowStartState, int startsHidden, int minWidth, int minHeight, int maxWidth, int maxHeight, bool fraudulentWebsiteWarningEnabled); +WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent, int devtools, int windowStartState, int startsHidden, int minWidth, int minHeight, int maxWidth, int maxHeight, bool fraudulentWebsiteWarningEnabled); void Run(void*, const char* url); void SetTitle(void* ctx, const char *title); diff --git a/v2/internal/frontend/desktop/darwin/Application.m b/v2/internal/frontend/desktop/darwin/Application.m index ab951714d..f79b0b8f2 100644 --- a/v2/internal/frontend/desktop/darwin/Application.m +++ b/v2/internal/frontend/desktop/darwin/Application.m @@ -13,13 +13,13 @@ #import "WailsMenu.h" #import "WailsMenuItem.h" -WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent, int debug, int windowStartState, int startsHidden, int minWidth, int minHeight, int maxWidth, int maxHeight, bool fraudulentWebsiteWarningEnabled) { +WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent, int devtools, int windowStartState, int startsHidden, int minWidth, int minHeight, int maxWidth, int maxHeight, bool fraudulentWebsiteWarningEnabled) { [NSApplication sharedApplication]; WailsContext *result = [WailsContext new]; - result.debug = debug; + result.devtools = devtools; if ( windowStartState == WindowStartsFullscreen ) { fullscreen = 1; diff --git a/v2/internal/frontend/desktop/darwin/WailsContext.h b/v2/internal/frontend/desktop/darwin/WailsContext.h index 1e48b2182..f39514d9f 100644 --- a/v2/internal/frontend/desktop/darwin/WailsContext.h +++ b/v2/internal/frontend/desktop/darwin/WailsContext.h @@ -44,7 +44,7 @@ @property bool alwaysOnTop; -@property bool debug; +@property bool devtools; @property (retain) WKUserContentController* userContentController; diff --git a/v2/internal/frontend/desktop/darwin/WailsContext.m b/v2/internal/frontend/desktop/darwin/WailsContext.m index 29fa99317..fffbe25c5 100644 --- a/v2/internal/frontend/desktop/darwin/WailsContext.m +++ b/v2/internal/frontend/desktop/darwin/WailsContext.m @@ -225,7 +225,7 @@ typedef void (^schemeTaskCaller)(id); [userContentController addScriptMessageHandler:self name:@"external"]; config.userContentController = userContentController; self.userContentController = userContentController; - if (self.debug) { + if (self.devtools) { [config.preferences setValue:@YES forKey:@"developerExtrasEnabled"]; } else { // Disable default context menus diff --git a/v2/internal/frontend/desktop/darwin/frontend.go b/v2/internal/frontend/desktop/darwin/frontend.go index b714a2c3b..c0dd27029 100644 --- a/v2/internal/frontend/desktop/darwin/frontend.go +++ b/v2/internal/frontend/desktop/darwin/frontend.go @@ -47,6 +47,7 @@ type Frontend struct { frontendOptions *options.App logger *logger.Logger debug bool + devtools bool // Assets assets *assetserver.AssetServer @@ -151,12 +152,18 @@ func (f *Frontend) WindowSetDarkTheme() { func (f *Frontend) Run(ctx context.Context) error { f.ctx = ctx + var _debug = ctx.Value("debug") + var _devtools = ctx.Value("devtools") + if _debug != nil { f.debug = _debug.(bool) } + if _devtools != nil { + f.devtools = _devtools.(bool) + } - mainWindow := NewWindow(f.frontendOptions, f.debug) + mainWindow := NewWindow(f.frontendOptions, f.debug, f.devtools) f.mainWindow = mainWindow f.mainWindow.Center() diff --git a/v2/internal/frontend/desktop/darwin/main.m b/v2/internal/frontend/desktop/darwin/main.m index d2f39bccb..48f4f66ad 100644 --- a/v2/internal/frontend/desktop/darwin/main.m +++ b/v2/internal/frontend/desktop/darwin/main.m @@ -215,10 +215,10 @@ int main(int argc, const char * argv[]) { int hideWindowOnClose = 0; const char* appearance = "NSAppearanceNameDarkAqua"; int windowIsTranslucent = 1; - int debug = 1; + int devtools = 1; int windowStartState = 0; int startsHidden = 0; - WailsContext *result = Create("OI OI!",400,400, frameless, resizable, fullscreen, fullSizeContent, hideTitleBar, titlebarAppearsTransparent, hideTitle, useToolbar, hideToolbarSeparator, webviewIsTransparent, alwaysOnTop, hideWindowOnClose, appearance, windowIsTranslucent, debug, windowStartState, + WailsContext *result = Create("OI OI!",400,400, frameless, resizable, fullscreen, fullSizeContent, hideTitleBar, titlebarAppearsTransparent, hideTitle, useToolbar, hideToolbarSeparator, webviewIsTransparent, alwaysOnTop, hideWindowOnClose, appearance, windowIsTranslucent, devtools, windowStartState, startsHidden, 400, 400, 600, 600, false); SetBackgroundColour(result, 255, 0, 0, 255); void *m = NewMenu(""); diff --git a/v2/internal/frontend/desktop/darwin/window.go b/v2/internal/frontend/desktop/darwin/window.go index 8b4c77799..c4e9dcdbd 100644 --- a/v2/internal/frontend/desktop/darwin/window.go +++ b/v2/internal/frontend/desktop/darwin/window.go @@ -40,7 +40,7 @@ func bool2Cint(value bool) C.int { return C.int(0) } -func NewWindow(frontendOptions *options.App, debugMode bool) *Window { +func NewWindow(frontendOptions *options.App, debug bool, devtools bool) *Window { c := NewCalloc() defer c.Free() @@ -51,7 +51,7 @@ func NewWindow(frontendOptions *options.App, debugMode bool) *Window { alwaysOnTop := bool2Cint(frontendOptions.AlwaysOnTop) hideWindowOnClose := bool2Cint(frontendOptions.HideWindowOnClose) startsHidden := bool2Cint(frontendOptions.StartHidden) - debug := bool2Cint(debugMode) + devtoolsEnabled := bool2Cint(devtools) var fullSizeContent, hideTitleBar, hideTitle, useToolbar, webviewIsTransparent C.int var titlebarAppearsTransparent, hideToolbarSeparator, windowIsTranslucent C.int @@ -86,7 +86,7 @@ func NewWindow(frontendOptions *options.App, debugMode bool) *Window { } var context *C.WailsContext = C.Create(title, width, height, frameless, resizable, fullscreen, fullSizeContent, hideTitleBar, titlebarAppearsTransparent, hideTitle, useToolbar, hideToolbarSeparator, webviewIsTransparent, - alwaysOnTop, hideWindowOnClose, appearance, windowIsTranslucent, debug, windowStartState, startsHidden, + alwaysOnTop, hideWindowOnClose, appearance, windowIsTranslucent, devtoolsEnabled, windowStartState, startsHidden, minWidth, minHeight, maxWidth, maxHeight, enableFraudulentWebsiteWarnings) // Create menu @@ -114,7 +114,7 @@ func NewWindow(frontendOptions *options.App, debugMode bool) *Window { result.SetApplicationMenu(frontendOptions.Menu) } - if debugMode && frontendOptions.Debug.OpenInspectorOnStartup { + if debug && frontendOptions.Debug.OpenInspectorOnStartup { showInspector(result.context) } return result diff --git a/v2/internal/frontend/desktop/linux/frontend.go b/v2/internal/frontend/desktop/linux/frontend.go index 58b8d746b..14c202d82 100644 --- a/v2/internal/frontend/desktop/linux/frontend.go +++ b/v2/internal/frontend/desktop/linux/frontend.go @@ -106,6 +106,7 @@ type Frontend struct { frontendOptions *options.App logger *logger.Logger debug bool + devtools bool // Assets assets *assetserver.AssetServer @@ -176,10 +177,16 @@ func NewFrontend(ctx context.Context, appoptions *options.App, myLogger *logger. go result.startMessageProcessor() var _debug = ctx.Value("debug") + var _devtools = ctx.Value("devtools") + if _debug != nil { result.debug = _debug.(bool) } - result.mainWindow = NewWindow(appoptions, result.debug) + if _devtools != nil { + result.devtools = _devtools.(bool) + } + + result.mainWindow = NewWindow(appoptions, result.debug, result.devtools) C.install_signal_handlers() diff --git a/v2/internal/frontend/desktop/linux/window.go b/v2/internal/frontend/desktop/linux/window.go index 9cc86afbb..79fc46b6f 100644 --- a/v2/internal/frontend/desktop/linux/window.go +++ b/v2/internal/frontend/desktop/linux/window.go @@ -37,6 +37,7 @@ func gtkBool(input bool) C.gboolean { type Window struct { appoptions *options.App debug bool + devtools bool gtkWindow unsafe.Pointer contentManager unsafe.Pointer webview unsafe.Pointer @@ -54,12 +55,13 @@ func bool2Cint(value bool) C.int { return C.int(0) } -func NewWindow(appoptions *options.App, debug bool) *Window { +func NewWindow(appoptions *options.App, debug bool, devtools bool) *Window { validateWebKit2Version(appoptions) result := &Window{ appoptions: appoptions, debug: debug, + devtools: devtools, minHeight: appoptions.MinHeight, minWidth: appoptions.MinWidth, maxHeight: appoptions.MaxHeight, @@ -95,8 +97,8 @@ func NewWindow(appoptions *options.App, debug bool) *Window { defer C.free(unsafe.Pointer(buttonPressedName)) C.ConnectButtons(unsafe.Pointer(webview)) - if debug { - C.DevtoolsEnabled(unsafe.Pointer(webview), C.int(1), C.bool(appoptions.Debug.OpenInspectorOnStartup)) + if devtools { + C.DevtoolsEnabled(unsafe.Pointer(webview), C.int(1), C.bool(debug && appoptions.Debug.OpenInspectorOnStartup)) } else { C.DisableContextMenu(unsafe.Pointer(webview)) } diff --git a/v2/internal/frontend/desktop/windows/frontend.go b/v2/internal/frontend/desktop/windows/frontend.go index c4186eda6..ef6b9d575 100644 --- a/v2/internal/frontend/desktop/windows/frontend.go +++ b/v2/internal/frontend/desktop/windows/frontend.go @@ -48,6 +48,7 @@ type Frontend struct { logger *logger.Logger chromium *edge.Chromium debug bool + devtools bool // Assets assets *assetserver.AssetServer @@ -142,9 +143,14 @@ func (f *Frontend) Run(ctx context.Context) error { f.mainWindow = mainWindow var _debug = ctx.Value("debug") + var _devtools = ctx.Value("devtools") + if _debug != nil { f.debug = _debug.(bool) } + if _devtools != nil { + f.devtools = _devtools.(bool) + } f.WindowCenter() f.setupChromium() @@ -489,11 +495,11 @@ func (f *Frontend) setupChromium() { if err != nil { log.Fatal(err) } - err = settings.PutAreDefaultContextMenusEnabled(f.debug) + err = settings.PutAreDefaultContextMenusEnabled(f.devtools) if err != nil { log.Fatal(err) } - err = settings.PutAreDevToolsEnabled(f.debug) + err = settings.PutAreDevToolsEnabled(f.devtools) if err != nil { log.Fatal(err) } diff --git a/v2/pkg/commands/build/base.go b/v2/pkg/commands/build/base.go index fbae6ce7e..abfbafff5 100644 --- a/v2/pkg/commands/build/base.go +++ b/v2/pkg/commands/build/base.go @@ -234,6 +234,11 @@ func (b *BaseBuilder) CompileProject(options *Options) error { tags.Add("debug") } + // This options allows you to enable devtools in production build (not dev build as it's always enabled there) + if options.Devtools { + tags.Add("devtools") + } + if options.Obfuscated { tags.Add("obfuscated") } diff --git a/v2/pkg/commands/build/build.go b/v2/pkg/commands/build/build.go index cad768fa2..85b3e4f5b 100644 --- a/v2/pkg/commands/build/build.go +++ b/v2/pkg/commands/build/build.go @@ -40,6 +40,7 @@ type Options struct { Logger *clilogger.CLILogger // All output to the logger OutputType string // EG: desktop, server.... Mode Mode // release or dev + Devtools bool // Enable devtools in production ProjectData *project.Project // The project data Pack bool // Create a package for the app after building Platform string // The platform to build for diff --git a/website/docs/reference/cli.mdx b/website/docs/reference/cli.mdx index 8b2f5b405..9dcc25bc9 100644 --- a/website/docs/reference/cli.mdx +++ b/website/docs/reference/cli.mdx @@ -56,7 +56,8 @@ If you are unsure about a template, inspect `package.json` and `wails.json` for |:---------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------| | -clean | Cleans the `build/bin` directory | | | -compiler "compiler" | Use a different go compiler to build, eg go1.15beta1 | go | -| -debug | Retains debug information in the application. Allows the use of the devtools in the application window | | +| -debug | Retains debug information in the application and shows the debug console. Allows the use of the devtools in the application window | | +| -devtools | Allows the use of the devtools in the application window in production (when -debug is not used) | | | -dryrun | Prints the build command without executing it | | | -f | Force build application | | | -garbleargs | Arguments to pass to garble | `-literals -tiny -seed=random` | diff --git a/website/docs/tutorials/helloworld.mdx b/website/docs/tutorials/helloworld.mdx index c7d4d01c5..f9e789bb1 100644 --- a/website/docs/tutorials/helloworld.mdx +++ b/website/docs/tutorials/helloworld.mdx @@ -73,6 +73,7 @@ App Type: desktop Platforms: windows/amd64 Compiler: C:\Users\leaan\go\go1.18.3\bin\go.exe Build Mode: Production +Devtools: false Skip Frontend: false Compress: false Package: true diff --git a/website/src/pages/changelog.mdx b/website/src/pages/changelog.mdx index 921521e85..4118bea99 100644 --- a/website/src/pages/changelog.mdx +++ b/website/src/pages/changelog.mdx @@ -18,6 +18,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Avoid app crashing when the Linux GTK key is empty by @aminya in [PR](https://github.com/wailsapp/wails/pull/2672) +### Added + +- Added `-devtools` production build flag. Added by @mmghv in [PR](https://github.com/wailsapp/wails/pull/2725) + ### Changed - Now uses new `go-webview2` module. Added by @leaanthony in [PR](https://github.com/wailsapp/wails/pull/2687).