mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 22:13:36 +08:00
[v2] Add -devserverurl flag
This commit is contained in:
parent
f6b83b0933
commit
7572b64bec
@ -27,6 +27,8 @@ import (
|
||||
"github.com/wailsapp/wails/v2/pkg/commands/build"
|
||||
)
|
||||
|
||||
const defaultDevServerURL = "http://localhost:34115"
|
||||
|
||||
func LogGreen(message string, args ...interface{}) {
|
||||
text := fmt.Sprintf(message, args...)
|
||||
println(colour.Green(text))
|
||||
@ -96,6 +98,9 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
||||
debounceMS := 100
|
||||
command.IntFlag("debounce", "The amount of time to wait to trigger a reload on change", &debounceMS)
|
||||
|
||||
devServerURL := defaultDevServerURL
|
||||
command.StringFlag("devserverurl", "The url of the dev server to use", &devServerURL)
|
||||
|
||||
command.Action(func() error {
|
||||
|
||||
// Create logger
|
||||
@ -132,6 +137,18 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if devServerURL == defaultDevServerURL && projectConfig.DevServerURL != defaultDevServerURL && projectConfig.DevServerURL != "" {
|
||||
devServerURL = projectConfig.DevServerURL
|
||||
}
|
||||
|
||||
if devServerURL != projectConfig.DevServerURL {
|
||||
projectConfig.DevServerURL = devServerURL
|
||||
err := projectConfig.Save()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if wailsjsdir == "" && projectConfig.WailsJSDir != "" {
|
||||
wailsjsdir = projectConfig.WailsJSDir
|
||||
}
|
||||
@ -198,7 +215,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
||||
|
||||
// Do initial build
|
||||
logger.Println("Building application for development...")
|
||||
newProcess, appBinary, err := restartApp(logger, buildOptions, debugBinaryProcess, loglevel, passthruArgs, assetDir, false, exitCodeChannel)
|
||||
newProcess, appBinary, err := restartApp(logger, buildOptions, debugBinaryProcess, loglevel, passthruArgs, assetDir, false, exitCodeChannel, devServerURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -232,6 +249,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
||||
}
|
||||
|
||||
LogGreen("Watching (sub)/directory: %s", projectDir)
|
||||
LogGreen("Using Dev Server URL: %s", devServerURL)
|
||||
|
||||
// Setup a watcher for non-node_modules directories
|
||||
dirs.Each(func(dir string) {
|
||||
@ -325,7 +343,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
||||
rebuild = false
|
||||
LogGreen("[Rebuild triggered] files updated")
|
||||
// Try and build the app
|
||||
newBinaryProcess, _, err = restartApp(logger, buildOptions, debugBinaryProcess, loglevel, passthruArgs, assetDir, false, exitCodeChannel)
|
||||
newBinaryProcess, _, err = restartApp(logger, buildOptions, debugBinaryProcess, loglevel, passthruArgs, assetDir, false, exitCodeChannel, devServerURL)
|
||||
if err != nil {
|
||||
LogRed("Error during build: %s", err.Error())
|
||||
continue
|
||||
@ -370,7 +388,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func restartApp(logger *clilogger.CLILogger, buildOptions *build.Options, debugBinaryProcess *process.Process, loglevel string, passthruArgs []string, assetDir string, firstRun bool, exitCodeChannel chan int) (*process.Process, string, error) {
|
||||
func restartApp(logger *clilogger.CLILogger, buildOptions *build.Options, debugBinaryProcess *process.Process, loglevel string, passthruArgs []string, assetDir string, firstRun bool, exitCodeChannel chan int, devServerURL string) (*process.Process, string, error) {
|
||||
|
||||
appBinary, err := build.Build(buildOptions)
|
||||
println()
|
||||
@ -400,7 +418,9 @@ func restartApp(logger *clilogger.CLILogger, buildOptions *build.Options, debugB
|
||||
if assetDir != "" {
|
||||
args.Add("-assetdir", assetDir)
|
||||
}
|
||||
|
||||
if devServerURL != "" {
|
||||
args.Add("-devserverurl", devServerURL)
|
||||
}
|
||||
if len(passthruArgs) > 0 {
|
||||
args.AddSlice(passthruArgs)
|
||||
}
|
||||
|
@ -61,11 +61,16 @@ func CreateApp(appoptions *options.App) (*App, error) {
|
||||
|
||||
// Check for CLI Flags
|
||||
assetdir := flag.String("assetdir", "", "Directory to serve assets")
|
||||
devServerURL := flag.String("devserverurl", "http://localhost:34115", "URL of development server")
|
||||
loglevel := flag.String("loglevel", "debug", "Loglevel to use - Trace, Debug, Info, Warning, Error")
|
||||
flag.Parse()
|
||||
if devServerURL != nil && *devServerURL != "" {
|
||||
ctx = context.WithValue(ctx, "devserverurl", *devServerURL)
|
||||
}
|
||||
if assetdir != nil && *assetdir != "" {
|
||||
ctx = context.WithValue(ctx, "assetdir", *assetdir)
|
||||
}
|
||||
|
||||
if loglevel != nil && *loglevel != "" {
|
||||
level, err := pkglogger.StringToLogLevel(*loglevel)
|
||||
if err != nil {
|
||||
|
@ -36,6 +36,7 @@ type Frontend struct {
|
||||
|
||||
// Assets
|
||||
assets *assetserver.DesktopAssetServer
|
||||
devServerURL string
|
||||
|
||||
// main window handle
|
||||
mainWindow *Window
|
||||
@ -59,19 +60,32 @@ func NewFrontend(ctx context.Context, appoptions *options.App, myLogger *logger.
|
||||
maxWidth: appoptions.MaxWidth,
|
||||
}
|
||||
|
||||
bindingsJSON, err := appBindings.ToJSON()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
_devServerURL := ctx.Value("devserverurl")
|
||||
if _devServerURL != nil {
|
||||
result.devServerURL = _devServerURL.(string)
|
||||
if result.devServerURL == "" {
|
||||
result.devServerURL = "http://localhost:34115"
|
||||
}
|
||||
if result.devServerURL != "http://localhost:34115" {
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we have been given a directory to serve assets from.
|
||||
// If so, this means we are in dev mode and are serving assets off disk.
|
||||
// We indicate this through the `servingFromDisk` flag to ensure requests
|
||||
// aren't cached by WebView2 in dev mode
|
||||
|
||||
_assetdir := ctx.Value("assetdir")
|
||||
if _assetdir != nil {
|
||||
result.servingFromDisk = true
|
||||
}
|
||||
|
||||
bindingsJSON, err := appBindings.ToJSON()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
assets, err := assetserver.NewDesktopAssetServer(ctx, appoptions.Assets, bindingsJSON)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@ -306,7 +320,11 @@ func (f *Frontend) setupChromium() {
|
||||
f.WindowSetRGBA(f.frontendOptions.RGBA)
|
||||
|
||||
chromium.AddWebResourceRequestedFilter("*", edge.COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL)
|
||||
if f.devServerURL == "http://localhost:34115" {
|
||||
chromium.Navigate("file://wails/")
|
||||
} else {
|
||||
chromium.Navigate(f.devServerURL)
|
||||
}
|
||||
}
|
||||
|
||||
type EventNotify struct {
|
||||
|
@ -93,6 +93,9 @@ func (d *DevWebServer) Run(ctx context.Context) error {
|
||||
}
|
||||
}))
|
||||
|
||||
_devServerURL := ctx.Value("devserverurl")
|
||||
if _devServerURL == "http://localhost:34115" {
|
||||
// Setup internal dev server
|
||||
_assetdir := ctx.Value("assetdir")
|
||||
if _assetdir == nil {
|
||||
return fmt.Errorf("no assetdir provided")
|
||||
@ -127,13 +130,17 @@ func (d *DevWebServer) Run(ctx context.Context) error {
|
||||
|
||||
d.LogDebug("Serving application at http://localhost:34115")
|
||||
|
||||
defer func() {
|
||||
err := d.server.Shutdown()
|
||||
if err != nil {
|
||||
d.logger.Error(err.Error())
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Launch desktop app
|
||||
err := d.desktopFrontend.Run(ctx)
|
||||
d.LogDebug("Starting shutdown")
|
||||
err2 := d.server.Shutdown()
|
||||
if err2 != nil {
|
||||
d.logger.Error(err.Error())
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
@ -47,8 +47,11 @@ type Project struct {
|
||||
// Fully qualified filename
|
||||
filename string
|
||||
|
||||
// The debounce time for hot-reload of the built-in dev server
|
||||
// The debounce time for hot-reload of the built-in dev server. Default 100
|
||||
DebounceMS int `json:"debounceMS"`
|
||||
|
||||
// The url to use to server assets. Default "https://localhost:34115"
|
||||
DevServerURL string `json:"devserverurl"`
|
||||
}
|
||||
|
||||
func (p *Project) Save() error {
|
||||
|
Loading…
Reference in New Issue
Block a user