From 7347d2caa2eafbd6a8dcc931ed1ee6a1b2ca4122 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Mon, 11 Jan 2021 11:20:25 +1100 Subject: [PATCH] Add `wails debug` command --- v2/cmd/wails/internal/commands/debug/debug.go | 123 ++++++++++++++++++ v2/cmd/wails/internal/commands/dev/dev.go | 15 ++- v2/cmd/wails/main.go | 6 + 3 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 v2/cmd/wails/internal/commands/debug/debug.go diff --git a/v2/cmd/wails/internal/commands/debug/debug.go b/v2/cmd/wails/internal/commands/debug/debug.go new file mode 100644 index 00000000..90e2c43f --- /dev/null +++ b/v2/cmd/wails/internal/commands/debug/debug.go @@ -0,0 +1,123 @@ +package debug + +import ( + "fmt" + "github.com/wailsapp/wails/v2/internal/shell" + "io" + "os" + "runtime" + "strings" + "time" + + "github.com/leaanthony/clir" + "github.com/leaanthony/slicer" + "github.com/wailsapp/wails/v2/pkg/clilogger" + "github.com/wailsapp/wails/v2/pkg/commands/build" +) + +// AddSubcommand adds the `debug` command for the Wails application +func AddSubcommand(app *clir.Cli, w io.Writer) error { + + outputType := "desktop" + + validTargetTypes := slicer.String([]string{"desktop", "hybrid", "server"}) + + command := app.NewSubCommand("debug", "Builds the application then runs delve on the binary") + + // Setup target type flag + description := "Type of application to build. Valid types: " + validTargetTypes.Join(",") + command.StringFlag("t", description, &outputType) + + compilerCommand := "go" + command.StringFlag("compiler", "Use a different go compiler to build, eg go1.15beta1", &compilerCommand) + + quiet := false + command.BoolFlag("q", "Suppress output to console", &quiet) + + // ldflags to pass to `go` + ldflags := "" + command.StringFlag("ldflags", "optional ldflags", &ldflags) + + // Log to file + logFile := "" + command.StringFlag("l", "Log to file", &logFile) + + command.Action(func() error { + + // Create logger + logger := clilogger.New(w) + logger.Mute(quiet) + + // Validate output type + if !validTargetTypes.Contains(outputType) { + return fmt.Errorf("output type '%s' is not valid", outputType) + } + + if !quiet { + app.PrintBanner() + } + + task := fmt.Sprintf("Building %s Application", strings.Title(outputType)) + logger.Println(task) + logger.Println(strings.Repeat("-", len(task))) + + // Setup mode + mode := build.Debug + + // Create BuildOptions + buildOptions := &build.Options{ + Logger: logger, + OutputType: outputType, + Mode: mode, + Pack: false, + Platform: runtime.GOOS, + LDFlags: ldflags, + Compiler: compilerCommand, + KeepAssets: false, + } + + outputFilename, err := doDebugBuild(buildOptions) + if err != nil { + return err + } + + // Check delve exists + delveExists := shell.CommandExists("dlv") + if !delveExists { + return fmt.Errorf("cannot launch delve (Is it installed?)") + } + + // Get cwd + cwd, err := os.Getwd() + if err != nil { + return err + } + + // Launch delve + println("Launching Delve on port 2345...") + command := shell.CreateCommand(cwd, "dlv", "--listen=:2345", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", outputFilename) + return command.Run() + }) + + return nil +} + +// doDebugBuild is our main build command +func doDebugBuild(buildOptions *build.Options) (string, error) { + + // Start Time + start := time.Now() + + outputFilename, err := build.Build(buildOptions) + if err != nil { + return "", err + } + + // Output stats + elapsed := time.Since(start) + buildOptions.Logger.Println("") + buildOptions.Logger.Println(fmt.Sprintf("Built '%s' in %s.", outputFilename, elapsed.Round(time.Millisecond).String())) + buildOptions.Logger.Println("") + + return outputFilename, nil +} diff --git a/v2/cmd/wails/internal/commands/dev/dev.go b/v2/cmd/wails/internal/commands/dev/dev.go index a9fe1789..dabd431d 100644 --- a/v2/cmd/wails/internal/commands/dev/dev.go +++ b/v2/cmd/wails/internal/commands/dev/dev.go @@ -85,7 +85,10 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error { // If this is a folder, add it to our watch list if fs.DirExists(event.Name) { if !strings.Contains(event.Name, "node_modules") { - watcher.Add(event.Name) + err := watcher.Add(event.Name) + if err != nil { + logger.Fatal("%s", err.Error()) + } logger.Println("Watching directory: %s", event.Name) } } @@ -173,7 +176,10 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error { // Kill the current program if running if debugBinaryProcess != nil { - debugBinaryProcess.Kill() + err := debugBinaryProcess.Kill() + if err != nil { + return err + } } logger.Println("Development mode exited") @@ -231,7 +237,10 @@ func restartApp(logger *clilogger.CLILogger, outputType string, ldflags string, err = newProcess.Start() if err != nil { // Remove binary - fs.DeleteFile(appBinary) + deleteError := fs.DeleteFile(appBinary) + if deleteError != nil { + logger.Fatal("Unable to delete app binary: " + appBinary) + } logger.Fatal("Unable to start application: %s", err.Error()) } diff --git a/v2/cmd/wails/main.go b/v2/cmd/wails/main.go index e96fa3e3..b989af21 100644 --- a/v2/cmd/wails/main.go +++ b/v2/cmd/wails/main.go @@ -5,6 +5,7 @@ import ( "github.com/leaanthony/clir" "github.com/wailsapp/wails/v2/cmd/wails/internal/commands/build" + "github.com/wailsapp/wails/v2/cmd/wails/internal/commands/debug" "github.com/wailsapp/wails/v2/cmd/wails/internal/commands/dev" "github.com/wailsapp/wails/v2/cmd/wails/internal/commands/doctor" "github.com/wailsapp/wails/v2/cmd/wails/internal/commands/generate" @@ -27,6 +28,11 @@ func main() { if err != nil { fatal(err.Error()) } + + err = debug.AddSubcommand(app, os.Stdout) + if err != nil { + fatal(err.Error()) + } err = doctor.AddSubcommand(app, os.Stdout) if err != nil { fatal(err.Error())