5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 13:02:04 +08:00

[v2] Add -devtools production build flag (#2725)

* [v2] Add devtools production build flag

* Update changelog

* Fix changelog spacing

---------

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
This commit is contained in:
Mohamed Gharib 2023-06-18 23:45:01 +03:00 committed by GitHub
parent 31a5c90673
commit fa851f29c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 82 additions and 20 deletions

View File

@ -57,6 +57,7 @@ func buildApplication(f *flags.Build) error {
OutputFile: f.OutputFilename, OutputFile: f.OutputFilename,
CleanBinDirectory: f.Clean, CleanBinDirectory: f.Clean,
Mode: f.GetBuildMode(), Mode: f.GetBuildMode(),
Devtools: f.Debug || f.Devtools,
Pack: !f.NoPackage, Pack: !f.NoPackage,
LDFlags: f.LdFlags, LDFlags: f.LdFlags,
Compiler: f.Compiler, Compiler: f.Compiler,
@ -82,6 +83,7 @@ func buildApplication(f *flags.Build) error {
{"Compiler", f.GetCompilerPath()}, {"Compiler", f.GetCompilerPath()},
{"Skip Bindings", bool2Str(f.SkipBindings)}, {"Skip Bindings", bool2Str(f.SkipBindings)},
{"Build Mode", f.GetBuildModeAsString()}, {"Build Mode", f.GetBuildModeAsString()},
{"Devtools", bool2Str(buildOptions.Devtools)},
{"Frontend Directory", projectOptions.GetFrontendDir()}, {"Frontend Directory", projectOptions.GetFrontendDir()},
{"Obfuscated", bool2Str(f.Obfuscated)}, {"Obfuscated", bool2Str(f.Obfuscated)},
} }

View File

@ -35,6 +35,7 @@ type Build struct {
ForceBuild bool `name:"f" description:"Force build of application"` 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"` 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"` 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"` NSIS bool `description:"Generate NSIS installer for Windows"`
TrimPath bool `description:"Remove all file system paths from the resulting executable"` TrimPath bool `description:"Remove all file system paths from the resulting executable"`
WindowsConsole bool `description:"Keep the console when building for Windows"` WindowsConsole bool `description:"Keep the console when building for Windows"`

View File

@ -120,6 +120,7 @@ func (d *Dev) GenerateBuildOptions() *build.Options {
result := &build.Options{ result := &build.Options{
OutputType: "dev", OutputType: "dev",
Mode: build.Dev, Mode: build.Dev,
Devtools: true,
Arch: runtime.GOARCH, Arch: runtime.GOARCH,
Pack: true, Pack: true,
Platform: runtime.GOOS, Platform: runtime.GOOS,

View File

@ -2,6 +2,7 @@ package app
import ( import (
"context" "context"
"github.com/wailsapp/wails/v2/internal/frontend" "github.com/wailsapp/wails/v2/internal/frontend"
"github.com/wailsapp/wails/v2/internal/logger" "github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/internal/menumanager" "github.com/wailsapp/wails/v2/internal/menumanager"
@ -20,6 +21,9 @@ type App struct {
// Indicates if the app is in debug mode // Indicates if the app is in debug mode
debug bool debug bool
// Indicates if the devtools is enabled
devtools bool
// OnStartup/OnShutdown // OnStartup/OnShutdown
startupCallback func(ctx context.Context) startupCallback func(ctx context.Context)
shutdownCallback func(ctx context.Context) shutdownCallback func(ctx context.Context)

View File

@ -42,7 +42,9 @@ func (a *App) Run() error {
func CreateApp(appoptions *options.App) (*App, error) { func CreateApp(appoptions *options.App) (*App, error) {
var err 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 // Set up logger
myLogger := logger.New(appoptions.Logger) myLogger := logger.New(appoptions.Logger)
@ -228,6 +230,7 @@ func CreateApp(appoptions *options.App) (*App, error) {
startupCallback: appoptions.OnStartup, startupCallback: appoptions.OnStartup,
shutdownCallback: appoptions.OnShutdown, shutdownCallback: appoptions.OnShutdown,
debug: true, debug: true,
devtools: true,
} }
result.options = appoptions result.options = appoptions

View File

@ -0,0 +1,7 @@
//go:build devtools
package app
func IsDevtoolsEnabled() bool {
return true
}

View File

@ -0,0 +1,7 @@
//go:build !devtools
package app
func IsDevtoolsEnabled() bool {
return false
}

View File

@ -34,7 +34,9 @@ func CreateApp(appoptions *options.App) (*App, error) {
options.MergeDefaults(appoptions) options.MergeDefaults(appoptions)
debug := IsDebug() debug := IsDebug()
devtools := IsDevtoolsEnabled()
ctx = context.WithValue(ctx, "debug", debug) ctx = context.WithValue(ctx, "debug", debug)
ctx = context.WithValue(ctx, "devtools", devtools)
// Set up logger // Set up logger
myLogger := logger.New(appoptions.Logger) myLogger := logger.New(appoptions.Logger)
@ -93,6 +95,7 @@ func CreateApp(appoptions *options.App) (*App, error) {
startupCallback: appoptions.OnStartup, startupCallback: appoptions.OnStartup,
shutdownCallback: appoptions.OnShutdown, shutdownCallback: appoptions.OnShutdown,
debug: debug, debug: debug,
devtools: devtools,
options: appoptions, options: appoptions,
} }

View File

@ -17,7 +17,7 @@
#define WindowStartsMinimised 2 #define WindowStartsMinimised 2
#define WindowStartsFullscreen 3 #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 Run(void*, const char* url);
void SetTitle(void* ctx, const char *title); void SetTitle(void* ctx, const char *title);

View File

@ -13,13 +13,13 @@
#import "WailsMenu.h" #import "WailsMenu.h"
#import "WailsMenuItem.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]; [NSApplication sharedApplication];
WailsContext *result = [WailsContext new]; WailsContext *result = [WailsContext new];
result.debug = debug; result.devtools = devtools;
if ( windowStartState == WindowStartsFullscreen ) { if ( windowStartState == WindowStartsFullscreen ) {
fullscreen = 1; fullscreen = 1;

View File

@ -44,7 +44,7 @@
@property bool alwaysOnTop; @property bool alwaysOnTop;
@property bool debug; @property bool devtools;
@property (retain) WKUserContentController* userContentController; @property (retain) WKUserContentController* userContentController;

View File

@ -225,7 +225,7 @@ typedef void (^schemeTaskCaller)(id<WKURLSchemeTask>);
[userContentController addScriptMessageHandler:self name:@"external"]; [userContentController addScriptMessageHandler:self name:@"external"];
config.userContentController = userContentController; config.userContentController = userContentController;
self.userContentController = userContentController; self.userContentController = userContentController;
if (self.debug) { if (self.devtools) {
[config.preferences setValue:@YES forKey:@"developerExtrasEnabled"]; [config.preferences setValue:@YES forKey:@"developerExtrasEnabled"];
} else { } else {
// Disable default context menus // Disable default context menus

View File

@ -47,6 +47,7 @@ type Frontend struct {
frontendOptions *options.App frontendOptions *options.App
logger *logger.Logger logger *logger.Logger
debug bool debug bool
devtools bool
// Assets // Assets
assets *assetserver.AssetServer assets *assetserver.AssetServer
@ -151,12 +152,18 @@ func (f *Frontend) WindowSetDarkTheme() {
func (f *Frontend) Run(ctx context.Context) error { func (f *Frontend) Run(ctx context.Context) error {
f.ctx = ctx f.ctx = ctx
var _debug = ctx.Value("debug") var _debug = ctx.Value("debug")
var _devtools = ctx.Value("devtools")
if _debug != nil { if _debug != nil {
f.debug = _debug.(bool) 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 = mainWindow
f.mainWindow.Center() f.mainWindow.Center()

View File

@ -215,10 +215,10 @@ int main(int argc, const char * argv[]) {
int hideWindowOnClose = 0; int hideWindowOnClose = 0;
const char* appearance = "NSAppearanceNameDarkAqua"; const char* appearance = "NSAppearanceNameDarkAqua";
int windowIsTranslucent = 1; int windowIsTranslucent = 1;
int debug = 1; int devtools = 1;
int windowStartState = 0; int windowStartState = 0;
int startsHidden = 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); startsHidden, 400, 400, 600, 600, false);
SetBackgroundColour(result, 255, 0, 0, 255); SetBackgroundColour(result, 255, 0, 0, 255);
void *m = NewMenu(""); void *m = NewMenu("");

View File

@ -40,7 +40,7 @@ func bool2Cint(value bool) C.int {
return C.int(0) return C.int(0)
} }
func NewWindow(frontendOptions *options.App, debugMode bool) *Window { func NewWindow(frontendOptions *options.App, debug bool, devtools bool) *Window {
c := NewCalloc() c := NewCalloc()
defer c.Free() defer c.Free()
@ -51,7 +51,7 @@ func NewWindow(frontendOptions *options.App, debugMode bool) *Window {
alwaysOnTop := bool2Cint(frontendOptions.AlwaysOnTop) alwaysOnTop := bool2Cint(frontendOptions.AlwaysOnTop)
hideWindowOnClose := bool2Cint(frontendOptions.HideWindowOnClose) hideWindowOnClose := bool2Cint(frontendOptions.HideWindowOnClose)
startsHidden := bool2Cint(frontendOptions.StartHidden) startsHidden := bool2Cint(frontendOptions.StartHidden)
debug := bool2Cint(debugMode) devtoolsEnabled := bool2Cint(devtools)
var fullSizeContent, hideTitleBar, hideTitle, useToolbar, webviewIsTransparent C.int var fullSizeContent, hideTitleBar, hideTitle, useToolbar, webviewIsTransparent C.int
var titlebarAppearsTransparent, hideToolbarSeparator, windowIsTranslucent 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, var context *C.WailsContext = C.Create(title, width, height, frameless, resizable, fullscreen, fullSizeContent,
hideTitleBar, titlebarAppearsTransparent, hideTitle, useToolbar, hideToolbarSeparator, webviewIsTransparent, 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) minWidth, minHeight, maxWidth, maxHeight, enableFraudulentWebsiteWarnings)
// Create menu // Create menu
@ -114,7 +114,7 @@ func NewWindow(frontendOptions *options.App, debugMode bool) *Window {
result.SetApplicationMenu(frontendOptions.Menu) result.SetApplicationMenu(frontendOptions.Menu)
} }
if debugMode && frontendOptions.Debug.OpenInspectorOnStartup { if debug && frontendOptions.Debug.OpenInspectorOnStartup {
showInspector(result.context) showInspector(result.context)
} }
return result return result

View File

@ -106,6 +106,7 @@ type Frontend struct {
frontendOptions *options.App frontendOptions *options.App
logger *logger.Logger logger *logger.Logger
debug bool debug bool
devtools bool
// Assets // Assets
assets *assetserver.AssetServer assets *assetserver.AssetServer
@ -176,10 +177,16 @@ func NewFrontend(ctx context.Context, appoptions *options.App, myLogger *logger.
go result.startMessageProcessor() go result.startMessageProcessor()
var _debug = ctx.Value("debug") var _debug = ctx.Value("debug")
var _devtools = ctx.Value("devtools")
if _debug != nil { if _debug != nil {
result.debug = _debug.(bool) 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() C.install_signal_handlers()

View File

@ -37,6 +37,7 @@ func gtkBool(input bool) C.gboolean {
type Window struct { type Window struct {
appoptions *options.App appoptions *options.App
debug bool debug bool
devtools bool
gtkWindow unsafe.Pointer gtkWindow unsafe.Pointer
contentManager unsafe.Pointer contentManager unsafe.Pointer
webview unsafe.Pointer webview unsafe.Pointer
@ -54,12 +55,13 @@ func bool2Cint(value bool) C.int {
return C.int(0) return C.int(0)
} }
func NewWindow(appoptions *options.App, debug bool) *Window { func NewWindow(appoptions *options.App, debug bool, devtools bool) *Window {
validateWebKit2Version(appoptions) validateWebKit2Version(appoptions)
result := &Window{ result := &Window{
appoptions: appoptions, appoptions: appoptions,
debug: debug, debug: debug,
devtools: devtools,
minHeight: appoptions.MinHeight, minHeight: appoptions.MinHeight,
minWidth: appoptions.MinWidth, minWidth: appoptions.MinWidth,
maxHeight: appoptions.MaxHeight, maxHeight: appoptions.MaxHeight,
@ -95,8 +97,8 @@ func NewWindow(appoptions *options.App, debug bool) *Window {
defer C.free(unsafe.Pointer(buttonPressedName)) defer C.free(unsafe.Pointer(buttonPressedName))
C.ConnectButtons(unsafe.Pointer(webview)) C.ConnectButtons(unsafe.Pointer(webview))
if debug { if devtools {
C.DevtoolsEnabled(unsafe.Pointer(webview), C.int(1), C.bool(appoptions.Debug.OpenInspectorOnStartup)) C.DevtoolsEnabled(unsafe.Pointer(webview), C.int(1), C.bool(debug && appoptions.Debug.OpenInspectorOnStartup))
} else { } else {
C.DisableContextMenu(unsafe.Pointer(webview)) C.DisableContextMenu(unsafe.Pointer(webview))
} }

View File

@ -48,6 +48,7 @@ type Frontend struct {
logger *logger.Logger logger *logger.Logger
chromium *edge.Chromium chromium *edge.Chromium
debug bool debug bool
devtools bool
// Assets // Assets
assets *assetserver.AssetServer assets *assetserver.AssetServer
@ -142,9 +143,14 @@ func (f *Frontend) Run(ctx context.Context) error {
f.mainWindow = mainWindow f.mainWindow = mainWindow
var _debug = ctx.Value("debug") var _debug = ctx.Value("debug")
var _devtools = ctx.Value("devtools")
if _debug != nil { if _debug != nil {
f.debug = _debug.(bool) f.debug = _debug.(bool)
} }
if _devtools != nil {
f.devtools = _devtools.(bool)
}
f.WindowCenter() f.WindowCenter()
f.setupChromium() f.setupChromium()
@ -489,11 +495,11 @@ func (f *Frontend) setupChromium() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = settings.PutAreDefaultContextMenusEnabled(f.debug) err = settings.PutAreDefaultContextMenusEnabled(f.devtools)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = settings.PutAreDevToolsEnabled(f.debug) err = settings.PutAreDevToolsEnabled(f.devtools)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -234,6 +234,11 @@ func (b *BaseBuilder) CompileProject(options *Options) error {
tags.Add("debug") 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 { if options.Obfuscated {
tags.Add("obfuscated") tags.Add("obfuscated")
} }

View File

@ -40,6 +40,7 @@ type Options struct {
Logger *clilogger.CLILogger // All output to the logger Logger *clilogger.CLILogger // All output to the logger
OutputType string // EG: desktop, server.... OutputType string // EG: desktop, server....
Mode Mode // release or dev Mode Mode // release or dev
Devtools bool // Enable devtools in production
ProjectData *project.Project // The project data ProjectData *project.Project // The project data
Pack bool // Create a package for the app after building Pack bool // Create a package for the app after building
Platform string // The platform to build for Platform string // The platform to build for

View File

@ -56,7 +56,8 @@ If you are unsure about a template, inspect `package.json` and `wails.json` for
|:---------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------| |:---------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------|
| -clean | Cleans the `build/bin` directory | | | -clean | Cleans the `build/bin` directory | |
| -compiler "compiler" | Use a different go compiler to build, eg go1.15beta1 | go | | -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 | | | -dryrun | Prints the build command without executing it | |
| -f | Force build application | | | -f | Force build application | |
| -garbleargs | Arguments to pass to garble | `-literals -tiny -seed=random` | | -garbleargs | Arguments to pass to garble | `-literals -tiny -seed=random` |

View File

@ -73,6 +73,7 @@ App Type: desktop
Platforms: windows/amd64 Platforms: windows/amd64
Compiler: C:\Users\leaan\go\go1.18.3\bin\go.exe Compiler: C:\Users\leaan\go\go1.18.3\bin\go.exe
Build Mode: Production Build Mode: Production
Devtools: false
Skip Frontend: false Skip Frontend: false
Compress: false Compress: false
Package: true Package: true

View File

@ -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) - 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 ### Changed
- Now uses new `go-webview2` module. Added by @leaanthony in [PR](https://github.com/wailsapp/wails/pull/2687). - Now uses new `go-webview2` module. Added by @leaanthony in [PR](https://github.com/wailsapp/wails/pull/2687).