From 5671f3527b6aeb986a0572da7f412300a0dace69 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Thu, 10 Aug 2023 20:36:40 +1000 Subject: [PATCH] Add `ProductionOverrides` option so any option can be overridden in production builds --- v3/V3 Changes.md | 8 ++ v3/pkg/application/application.go | 16 +-- v3/pkg/application/options_application.go | 14 +- .../application/options_application_test.go | 123 ++++++++++++++++++ 4 files changed, 151 insertions(+), 10 deletions(-) create mode 100644 v3/pkg/application/options_application_test.go diff --git a/v3/V3 Changes.md b/v3/V3 Changes.md index 239a0b651..b66156d0d 100644 --- a/v3/V3 Changes.md +++ b/v3/V3 Changes.md @@ -1,5 +1,13 @@ # Changes for v3 +## Options + +The application options have been revised since v2. + +### Production Overrides + +Any application option can now be overridden using the `ProductionOverrides` field. When building in production mode, the values in this field will be used instead of the values in the main options struct. This allows you to have different options for development and production. + ## Events In v3, there are 3 types of events: diff --git a/v3/pkg/application/application.go b/v3/pkg/application/application.go index a75f00011..0c4ca969b 100644 --- a/v3/pkg/application/application.go +++ b/v3/pkg/application/application.go @@ -46,10 +46,17 @@ func New(appOptions Options) *App { return globalApplication } + // Patch isDebug if we aren't in prod mode + if isDebugMode == nil { + isDebugMode = func() bool { + return true + } + } + mergeApplicationDefaults(&appOptions) result := &App{ - options: appOptions, + options: appOptions.getOptions(isDebugMode()), applicationEventListeners: make(map[uint][]*EventListener), windows: make(map[uint]*WebviewWindow), systemTrays: make(map[uint]*SystemTray), @@ -63,13 +70,6 @@ func New(appOptions Options) *App { result.log.AddOutput(&logger.Console{}) } - // Patch isDebug if we aren't in prod mode - if isDebugMode == nil { - isDebugMode = func() bool { - return true - } - } - result.Events = NewWailsEventProcessor(result.dispatchEventToWindows) opts := assetserveroptions.Options{ diff --git a/v3/pkg/application/options_application.go b/v3/pkg/application/options_application.go index 7892822d0..56c5b6252 100644 --- a/v3/pkg/application/options_application.go +++ b/v3/pkg/application/options_application.go @@ -1,10 +1,9 @@ package application import ( + "github.com/wailsapp/wails/v3/pkg/logger" "io/fs" "net/http" - - "github.com/wailsapp/wails/v3/pkg/logger" ) type Options struct { @@ -24,6 +23,17 @@ type Options struct { // PanicHandler is a way to register a custom panic handler PanicHandler func(any) + + // ProductionOverrides allows you to override any option in production builds + ProductionOverrides *Options +} + +func (o Options) getOptions(debugMode bool) Options { + if o.ProductionOverrides == nil || debugMode { + o.ProductionOverrides = nil + return o + } + return *o.ProductionOverrides } // AssetOptions defines the configuration of the AssetServer. diff --git a/v3/pkg/application/options_application_test.go b/v3/pkg/application/options_application_test.go new file mode 100644 index 000000000..2e274e2f7 --- /dev/null +++ b/v3/pkg/application/options_application_test.go @@ -0,0 +1,123 @@ +package application + +import ( + "reflect" + "testing" +) + +func TestOptions_getOptions(t *testing.T) { + + tests := []struct { + name string + input Options + debugMode bool + want Options + }{ + { + name: "Override Icon in Production", + input: Options{ + Icon: []byte("debug-icon"), + ProductionOverrides: &Options{ + Icon: []byte("prod-icon"), + }, + }, + want: Options{ + Icon: []byte("prod-icon"), + }, + }, + { + name: "Don't override Icon in debug", + input: Options{ + Icon: []byte("debug-icon"), + ProductionOverrides: &Options{ + Icon: []byte("prod-icon"), + }, + }, + debugMode: true, + want: Options{ + Icon: []byte("debug-icon"), + }, + }, + { + name: "Override Mac in Production", + input: Options{ + Mac: MacOptions{ + ApplicationShouldTerminateAfterLastWindowClosed: false, + }, + ProductionOverrides: &Options{ + Mac: MacOptions{ + ApplicationShouldTerminateAfterLastWindowClosed: true, + }, + }, + }, + want: Options{ + Mac: MacOptions{ + ApplicationShouldTerminateAfterLastWindowClosed: true, + }, + }, + }, + { + name: "Don't override Mac in debug", + input: Options{ + Mac: MacOptions{ + ApplicationShouldTerminateAfterLastWindowClosed: false, + }, + ProductionOverrides: &Options{ + Mac: MacOptions{ + ApplicationShouldTerminateAfterLastWindowClosed: true, + }, + }, + }, + debugMode: true, + want: Options{ + Mac: MacOptions{ + ApplicationShouldTerminateAfterLastWindowClosed: false, + }, + }, + }, + { + name: "Override Flags in Production", + input: Options{ + Flags: map[string]interface{}{ + "environment": "debug", + }, + ProductionOverrides: &Options{ + Flags: map[string]interface{}{ + "environment": "prod", + }, + }, + }, + want: Options{ + Flags: map[string]interface{}{ + "environment": "prod", + }, + }, + }, + { + name: "Do not override Flags in debug", + input: Options{ + Flags: map[string]interface{}{ + "environment": "debug", + }, + ProductionOverrides: &Options{ + Flags: map[string]interface{}{ + "environment": "prod", + }, + }, + }, + debugMode: true, + want: Options{ + Flags: map[string]interface{}{ + "environment": "debug", + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.input.getOptions(tt.debugMode); !reflect.DeepEqual(got, tt.want) { + t.Errorf("getOptions() = %v, want %v", got, tt.want) + } + }) + } +}