Add ProductionOverrides option so any option can be overridden in production builds

This commit is contained in:
Lea Anthony 2023-08-10 20:36:40 +10:00
commit 5671f3527b
No known key found for this signature in database
GPG key ID: 33DAF7BB90A58405
4 changed files with 151 additions and 10 deletions

View file

@ -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:

View file

@ -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{

View file

@ -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.

View file

@ -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)
}
})
}
}