From b5f68e24d63c219965eef994d18a915850f41c1f Mon Sep 17 00:00:00 2001 From: Lukas Crepaz Date: Mon, 1 Nov 2021 08:57:10 +0100 Subject: [PATCH] added appargs for application arguments in dev mode --- v2/cmd/wails/internal/commands/dev/dev.go | 18 +++++++++++++++--- v2/go.mod | 1 + v2/go.sum | 2 ++ v2/internal/project/project.go | 3 +++ website/docs/reference/cli.mdx | 1 + website/docs/reference/project-config.mdx | 4 +++- 6 files changed, 25 insertions(+), 4 deletions(-) diff --git a/v2/cmd/wails/internal/commands/dev/dev.go b/v2/cmd/wails/internal/commands/dev/dev.go index d809f3d50..0b760ccae 100644 --- a/v2/cmd/wails/internal/commands/dev/dev.go +++ b/v2/cmd/wails/internal/commands/dev/dev.go @@ -16,10 +16,10 @@ import ( "syscall" "time" + "github.com/google/shlex" "github.com/wailsapp/wails/v2/cmd/wails/internal" "github.com/wailsapp/wails/v2/internal/gomod" - "github.com/leaanthony/slicer" "github.com/wailsapp/wails/v2/internal/project" "github.com/pkg/browser" @@ -72,6 +72,7 @@ type devFlags struct { forceBuild bool debounceMS int devServerURL string + appargs string } // AddSubcommand adds the `dev` command for the Wails application @@ -93,6 +94,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error { command.BoolFlag("f", "Force build application", &flags.forceBuild) command.IntFlag("debounce", "The amount of time to wait to trigger a reload on change", &flags.debounceMS) command.StringFlag("devserverurl", "The url of the dev server to use", &flags.devServerURL) + command.StringFlag("appargs", "arguments to pass to the underlying app (quoted and space searated)", &flags.appargs) command.Action(func() error { // Create logger @@ -353,6 +355,10 @@ func loadAndMergeProjectConfig(cwd string, flags *devFlags) (*project.Project, e shouldSaveConfig = true } + if flags.appargs == "" && projectConfig.AppArgs != "" { + flags.appargs = projectConfig.AppArgs + } + if shouldSaveConfig { err = projectConfig.Save() if err != nil { @@ -462,7 +468,13 @@ func restartApp(buildOptions *build.Options, debugBinaryProcess *process.Process debugBinaryProcess = nil } - args := slicer.StringSlicer{} + + // parse appargs if any + args, err := shlex.Split(flags.appargs) + + if err != nil { + buildOptions.Logger.Fatal("Unable to parse appargs: %s", err.Error()) + } // Set environment variables accordingly os.Setenv("loglevel", flags.loglevel) @@ -470,7 +482,7 @@ func restartApp(buildOptions *build.Options, debugBinaryProcess *process.Process os.Setenv("devserverurl", flags.devServerURL) // Start up new binary with correct args - newProcess := process.NewProcess(appBinary, args.AsSlice()...) + newProcess := process.NewProcess(appBinary, args...) err = newProcess.Start(exitCodeChannel) if err != nil { // Remove binary diff --git a/v2/go.mod b/v2/go.mod index d0058db4b..8c122d6a6 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -56,6 +56,7 @@ require ( github.com/go-git/gcfg v1.5.0 // indirect github.com/go-ole/go-ole v1.2.5 // indirect github.com/google/go-cmp v0.5.5 // indirect + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect diff --git a/v2/go.sum b/v2/go.sum index 592545177..0a548f1c8 100644 --- a/v2/go.sum +++ b/v2/go.sum @@ -75,6 +75,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/v2/internal/project/project.go b/v2/internal/project/project.go index 148f4eda6..99063d0f4 100644 --- a/v2/internal/project/project.go +++ b/v2/internal/project/project.go @@ -53,6 +53,9 @@ type Project struct { // The url to use to server assets. Default "https://localhost:34115" DevServerURL string `json:"devserverurl"` + + // Arguments that are forwared to the application in dev mode + AppArgs string `json:"appargs"` } func (p *Project) Save() error { diff --git a/website/docs/reference/cli.mdx b/website/docs/reference/cli.mdx index 644d8dec6..8b2996281 100644 --- a/website/docs/reference/cli.mdx +++ b/website/docs/reference/cli.mdx @@ -134,6 +134,7 @@ Your system is ready for Wails development! | -wailsjsdir | The directory to generate the generated Wails JS modules | Value in `wails.json` | | -debounce | The time to wait for reload after an asset change is detected | 100 (milliseconds) | | -devserverurl "url" | Use 3rd party dev server url, EG Vite | "http://localhost:34115" | +| -appargs "args" | Arguments passed to the application in shell style | | If the `assetdir`, `wailsjsdir`, `debounce` or `devserverurl` flags are provided on the command line, they are saved in `wails.json`, and become the defaults for subsequent invocations. diff --git a/website/docs/reference/project-config.mdx b/website/docs/reference/project-config.mdx index ccf9cd9b2..dad072cbe 100644 --- a/website/docs/reference/project-config.mdx +++ b/website/docs/reference/project-config.mdx @@ -17,7 +17,9 @@ The project config resides in the `wails.json` file in the project directory. Th "version": "[Project config version]", "outputfilename": "[The name of the binary]", "debounceMS": 100, // The default time the dev server waits to reload when it detects a vhange in assets - "devserverurl": "[URL to the dev server serving local assets. Default: http://localhost:34115]" + "devserverurl": "[URL to the dev server serving local assets. Default: http://localhost:34115]", + "appargs": "[Arguments passed to the application in shell style when in dev mode]" + } ```