mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-04 03:40:45 +08:00
refactor clilogger
This commit is contained in:
parent
2a59272b86
commit
c9bf4e3d48
@ -2,19 +2,19 @@ package build
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"io"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/leaanthony/clir"
|
"github.com/leaanthony/clir"
|
||||||
"github.com/leaanthony/slicer"
|
"github.com/leaanthony/slicer"
|
||||||
"github.com/wailsapp/wails/v2/internal/logger"
|
"github.com/wailsapp/wails/v2/pkg/clilogger"
|
||||||
"github.com/wailsapp/wails/v2/pkg/commands/build"
|
"github.com/wailsapp/wails/v2/pkg/commands/build"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddBuildSubcommand adds the `build` command for the Wails application
|
// AddBuildSubcommand adds the `build` command for the Wails application
|
||||||
func AddBuildSubcommand(app *clir.Cli) {
|
func AddBuildSubcommand(app *clir.Cli, w io.Writer) {
|
||||||
|
|
||||||
outputType := "desktop"
|
outputType := "desktop"
|
||||||
|
|
||||||
@ -56,11 +56,8 @@ func AddBuildSubcommand(app *clir.Cli) {
|
|||||||
command.Action(func() error {
|
command.Action(func() error {
|
||||||
|
|
||||||
// Create logger
|
// Create logger
|
||||||
logger := logger.New()
|
logger := clilogger.New(w)
|
||||||
|
logger.Mute(quiet)
|
||||||
if !quiet {
|
|
||||||
logger.AddOutput(os.Stdout)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate output type
|
// Validate output type
|
||||||
if !validTargetTypes.Contains(outputType) {
|
if !validTargetTypes.Contains(outputType) {
|
||||||
@ -72,8 +69,8 @@ func AddBuildSubcommand(app *clir.Cli) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
task := fmt.Sprintf("Building %s Application", strings.Title(outputType))
|
task := fmt.Sprintf("Building %s Application", strings.Title(outputType))
|
||||||
logger.Writeln(task)
|
logger.Println(task)
|
||||||
logger.Writeln(strings.Repeat("-", len(task)))
|
logger.Println(strings.Repeat("-", len(task)))
|
||||||
|
|
||||||
// Setup mode
|
// Setup mode
|
||||||
mode := build.Debug
|
mode := build.Debug
|
||||||
@ -108,9 +105,9 @@ func doBuild(buildOptions *build.Options) error {
|
|||||||
}
|
}
|
||||||
// Output stats
|
// Output stats
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
buildOptions.Logger.Writeln("")
|
buildOptions.Logger.Println("")
|
||||||
buildOptions.Logger.Writeln(fmt.Sprintf("Built '%s' in %s.", outputFilename, elapsed.Round(time.Millisecond).String()))
|
buildOptions.Logger.Println(fmt.Sprintf("Built '%s' in %s.", outputFilename, elapsed.Round(time.Millisecond).String()))
|
||||||
buildOptions.Logger.Writeln("")
|
buildOptions.Logger.Println("")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package dev
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -13,13 +14,13 @@ import (
|
|||||||
"github.com/leaanthony/clir"
|
"github.com/leaanthony/clir"
|
||||||
"github.com/leaanthony/slicer"
|
"github.com/leaanthony/slicer"
|
||||||
"github.com/wailsapp/wails/v2/internal/fs"
|
"github.com/wailsapp/wails/v2/internal/fs"
|
||||||
"github.com/wailsapp/wails/v2/internal/logger"
|
|
||||||
"github.com/wailsapp/wails/v2/internal/process"
|
"github.com/wailsapp/wails/v2/internal/process"
|
||||||
|
"github.com/wailsapp/wails/v2/pkg/clilogger"
|
||||||
"github.com/wailsapp/wails/v2/pkg/commands/build"
|
"github.com/wailsapp/wails/v2/pkg/commands/build"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddSubcommand adds the `dev` command for the Wails application
|
// AddSubcommand adds the `dev` command for the Wails application
|
||||||
func AddSubcommand(app *clir.Cli) error {
|
func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
||||||
|
|
||||||
command := app.NewSubCommand("dev", "Development mode")
|
command := app.NewSubCommand("dev", "Development mode")
|
||||||
|
|
||||||
@ -51,8 +52,7 @@ func AddSubcommand(app *clir.Cli) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create logger
|
// Create logger
|
||||||
logger := logger.New()
|
logger := clilogger.New(w)
|
||||||
logger.AddOutput(os.Stdout)
|
|
||||||
app.PrintBanner()
|
app.PrintBanner()
|
||||||
|
|
||||||
// TODO: Check you are in a project directory
|
// TODO: Check you are in a project directory
|
||||||
@ -74,11 +74,11 @@ func AddSubcommand(app *clir.Cli) error {
|
|||||||
debounceQuit := make(chan bool, 1)
|
debounceQuit := make(chan bool, 1)
|
||||||
|
|
||||||
// Do initial build
|
// Do initial build
|
||||||
logger.Info("Building application for development...")
|
logger.Println("Building application for development...")
|
||||||
debugBinaryProcess = restartApp(logger, outputType, ldflags, compilerCommand, buildFrontend, debugBinaryProcess)
|
debugBinaryProcess = restartApp(logger, outputType, ldflags, compilerCommand, buildFrontend, debugBinaryProcess)
|
||||||
|
|
||||||
go debounce(100*time.Millisecond, watcher.Events, debounceQuit, func(event fsnotify.Event) {
|
go debounce(100*time.Millisecond, watcher.Events, debounceQuit, func(event fsnotify.Event) {
|
||||||
// logger.Info("event: %+v", event)
|
// logger.Println("event: %+v", event)
|
||||||
|
|
||||||
// Check for new directories
|
// Check for new directories
|
||||||
if event.Op&fsnotify.Create == fsnotify.Create {
|
if event.Op&fsnotify.Create == fsnotify.Create {
|
||||||
@ -86,7 +86,7 @@ func AddSubcommand(app *clir.Cli) error {
|
|||||||
if fs.DirExists(event.Name) {
|
if fs.DirExists(event.Name) {
|
||||||
if !strings.Contains(event.Name, "node_modules") {
|
if !strings.Contains(event.Name, "node_modules") {
|
||||||
watcher.Add(event.Name)
|
watcher.Add(event.Name)
|
||||||
logger.Info("Watching directory: %s", event.Name)
|
logger.Println("Watching directory: %s", event.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -95,7 +95,7 @@ func AddSubcommand(app *clir.Cli) error {
|
|||||||
// Check for file writes
|
// Check for file writes
|
||||||
if event.Op&fsnotify.Write == fsnotify.Write {
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
||||||
|
|
||||||
// logger.Info("modified file: %s", event.Name)
|
// logger.Println("modified file: %s", event.Name)
|
||||||
var rebuild bool = false
|
var rebuild bool = false
|
||||||
|
|
||||||
// Iterate all file patterns
|
// Iterate all file patterns
|
||||||
@ -112,14 +112,14 @@ func AddSubcommand(app *clir.Cli) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !rebuild {
|
if !rebuild {
|
||||||
logger.Info("Filename change: %s did not match extension list %s", event.Name, extensions)
|
logger.Println("Filename change: %s did not match extension list %s", event.Name, extensions)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if buildFrontend {
|
if buildFrontend {
|
||||||
logger.Info("Full rebuild triggered: %s updated", event.Name)
|
logger.Println("Full rebuild triggered: %s updated", event.Name)
|
||||||
} else {
|
} else {
|
||||||
logger.Info("Partial build triggered: %s updated", event.Name)
|
logger.Println("Partial build triggered: %s updated", event.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do a rebuild
|
// Do a rebuild
|
||||||
@ -152,7 +152,7 @@ func AddSubcommand(app *clir.Cli) error {
|
|||||||
if strings.Contains(dir, "node_modules") {
|
if strings.Contains(dir, "node_modules") {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.Info("Watching directory: %s", dir)
|
logger.Println("Watching directory: %s", dir)
|
||||||
err = watcher.Add(dir)
|
err = watcher.Add(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err.Error())
|
logger.Fatal(err.Error())
|
||||||
@ -176,7 +176,7 @@ func AddSubcommand(app *clir.Cli) error {
|
|||||||
debugBinaryProcess.Kill()
|
debugBinaryProcess.Kill()
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info("Development mode exited")
|
logger.Println("Development mode exited")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -203,15 +203,15 @@ exit:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func restartApp(logger *logger.Logger, outputType string, ldflags string, compilerCommand string, buildFrontend bool, debugBinaryProcess *process.Process) *process.Process {
|
func restartApp(logger *clilogger.CLILogger, outputType string, ldflags string, compilerCommand string, buildFrontend bool, debugBinaryProcess *process.Process) *process.Process {
|
||||||
|
|
||||||
appBinary, err := buildApp(logger, outputType, ldflags, compilerCommand, buildFrontend)
|
appBinary, err := buildApp(logger, outputType, ldflags, compilerCommand, buildFrontend)
|
||||||
println()
|
println()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Build Failed: %s", err.Error())
|
logger.Println("[ERROR] Build Failed: %s", err.Error())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
logger.Info("Build new binary: %s", appBinary)
|
logger.Println("Build new binary: %s", appBinary)
|
||||||
|
|
||||||
// Kill existing binary if need be
|
// Kill existing binary if need be
|
||||||
if debugBinaryProcess != nil {
|
if debugBinaryProcess != nil {
|
||||||
@ -238,7 +238,7 @@ func restartApp(logger *logger.Logger, outputType string, ldflags string, compil
|
|||||||
return newProcess
|
return newProcess
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildApp(logger *logger.Logger, outputType string, ldflags string, compilerCommand string, buildFrontend bool) (string, error) {
|
func buildApp(logger *clilogger.CLILogger, outputType string, ldflags string, compilerCommand string, buildFrontend bool) (string, error) {
|
||||||
|
|
||||||
// Create random output file
|
// Create random output file
|
||||||
outputFile := fmt.Sprintf("debug-%d", time.Now().Unix())
|
outputFile := fmt.Sprintf("debug-%d", time.Now().Unix())
|
||||||
|
@ -2,37 +2,37 @@ package doctor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
|
|
||||||
"github.com/leaanthony/clir"
|
"github.com/leaanthony/clir"
|
||||||
"github.com/wailsapp/wails/v2/internal/logger"
|
|
||||||
"github.com/wailsapp/wails/v2/internal/system"
|
"github.com/wailsapp/wails/v2/internal/system"
|
||||||
"github.com/wailsapp/wails/v2/internal/system/packagemanager"
|
"github.com/wailsapp/wails/v2/internal/system/packagemanager"
|
||||||
|
"github.com/wailsapp/wails/v2/pkg/clilogger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddSubcommand adds the `doctor` command for the Wails application
|
// AddSubcommand adds the `doctor` command for the Wails application
|
||||||
func AddSubcommand(app *clir.Cli) error {
|
func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
||||||
|
|
||||||
command := app.NewSubCommand("doctor", "Diagnose your environment")
|
command := app.NewSubCommand("doctor", "Diagnose your environment")
|
||||||
|
|
||||||
command.Action(func() error {
|
command.Action(func() error {
|
||||||
|
|
||||||
// Create logger
|
logger := clilogger.New(w)
|
||||||
logger := logger.New()
|
|
||||||
logger.AddOutput(os.Stdout)
|
|
||||||
|
|
||||||
app.PrintBanner()
|
app.PrintBanner()
|
||||||
print("Scanning system - please wait...")
|
logger.Print("Scanning system - please wait...")
|
||||||
|
|
||||||
// Get system info
|
// Get system info
|
||||||
info, err := system.GetInfo()
|
info, err := system.GetInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
println("Done.")
|
logger.Println("Done.")
|
||||||
|
|
||||||
// Start a new tabwriter
|
// Start a new tabwriter
|
||||||
w := new(tabwriter.Writer)
|
w := new(tabwriter.Writer)
|
||||||
@ -112,22 +112,22 @@ func AddSubcommand(app *clir.Cli) error {
|
|||||||
fmt.Fprintf(w, "\n")
|
fmt.Fprintf(w, "\n")
|
||||||
fmt.Fprintf(w, "* - Optional Dependency\n")
|
fmt.Fprintf(w, "* - Optional Dependency\n")
|
||||||
w.Flush()
|
w.Flush()
|
||||||
println()
|
logger.Println("")
|
||||||
println("Diagnosis")
|
logger.Println("Diagnosis")
|
||||||
println("---------\n")
|
logger.Println("---------\n")
|
||||||
|
|
||||||
// Generate an appropriate diagnosis
|
// Generate an appropriate diagnosis
|
||||||
|
|
||||||
if len(dependenciesMissing) == 0 && dependenciesAvailableRequired == 0 {
|
if len(dependenciesMissing) == 0 && dependenciesAvailableRequired == 0 {
|
||||||
println("Your system is ready for Wails development!")
|
logger.Println("Your system is ready for Wails development!")
|
||||||
}
|
}
|
||||||
|
|
||||||
if dependenciesAvailableRequired != 0 {
|
if dependenciesAvailableRequired != 0 {
|
||||||
println("Install required packages using: " + info.Dependencies.InstallAllRequiredCommand())
|
log.Println("Install required packages using: " + info.Dependencies.InstallAllRequiredCommand())
|
||||||
}
|
}
|
||||||
|
|
||||||
if dependenciesAvailableOptional != 0 {
|
if dependenciesAvailableOptional != 0 {
|
||||||
println("Install optional packages using: " + info.Dependencies.InstallAllOptionalCommand())
|
log.Println("Install optional packages using: " + info.Dependencies.InstallAllOptionalCommand())
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(externalPackages) > 0 {
|
if len(externalPackages) > 0 {
|
||||||
@ -135,18 +135,18 @@ func AddSubcommand(app *clir.Cli) error {
|
|||||||
if p.Optional {
|
if p.Optional {
|
||||||
print("[Optional] ")
|
print("[Optional] ")
|
||||||
}
|
}
|
||||||
println("Install " + p.Name + ": " + p.InstallCommand)
|
log.Println("Install " + p.Name + ": " + p.InstallCommand)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(dependenciesMissing) != 0 {
|
if len(dependenciesMissing) != 0 {
|
||||||
// TODO: Check if apps are available locally and if so, adjust the diagnosis
|
// TODO: Check if apps are available locally and if so, adjust the diagnosis
|
||||||
println("Fatal:")
|
log.Println("Fatal:")
|
||||||
println("Required dependencies missing: " + strings.Join(dependenciesMissing, " "))
|
log.Println("Required dependencies missing: " + strings.Join(dependenciesMissing, " "))
|
||||||
println("Please read this article on how to resolve this: https://wails.app/guides/resolving-missing-packages")
|
log.Println("Please read this article on how to resolve this: https://wails.app/guides/resolving-missing-packages")
|
||||||
}
|
}
|
||||||
|
|
||||||
println()
|
log.Println("")
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -2,17 +2,17 @@ package initialise
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/leaanthony/clir"
|
"github.com/leaanthony/clir"
|
||||||
"github.com/wailsapp/wails/v2/internal/logger"
|
|
||||||
"github.com/wailsapp/wails/v2/internal/templates"
|
"github.com/wailsapp/wails/v2/internal/templates"
|
||||||
|
"github.com/wailsapp/wails/v2/pkg/clilogger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddSubcommand adds the `init` command for the Wails application
|
// AddSubcommand adds the `init` command for the Wails application
|
||||||
func AddSubcommand(app *clir.Cli) error {
|
func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
||||||
|
|
||||||
// Load the template shortnames
|
// Load the template shortnames
|
||||||
validShortNames, err := templates.TemplateShortNames()
|
validShortNames, err := templates.TemplateShortNames()
|
||||||
@ -46,32 +46,29 @@ func AddSubcommand(app *clir.Cli) error {
|
|||||||
command.Action(func() error {
|
command.Action(func() error {
|
||||||
|
|
||||||
// Create logger
|
// Create logger
|
||||||
logger := logger.New()
|
logger := clilogger.New(w)
|
||||||
|
logger.Mute(quiet)
|
||||||
if !quiet {
|
|
||||||
logger.AddOutput(os.Stdout)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Are we listing templates?
|
// Are we listing templates?
|
||||||
if list {
|
if list {
|
||||||
app.PrintBanner()
|
app.PrintBanner()
|
||||||
err := templates.OutputList(logger)
|
err := templates.OutputList(logger)
|
||||||
logger.Writeln("")
|
logger.Println("")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate output type
|
// Validate output type
|
||||||
if !validShortNames.Contains(templateName) {
|
if !validShortNames.Contains(templateName) {
|
||||||
logger.Write(fmt.Sprintf("ERROR: Template '%s' is not valid", templateName))
|
logger.Print(fmt.Sprintf("[ERROR] Template '%s' is not valid", templateName))
|
||||||
logger.Writeln("")
|
logger.Println("")
|
||||||
command.PrintHelp()
|
command.PrintHelp()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate name
|
// Validate name
|
||||||
if len(projectName) == 0 {
|
if len(projectName) == 0 {
|
||||||
logger.Writeln("ERROR: Project name required")
|
logger.Println("ERROR: Project name required")
|
||||||
logger.Writeln("")
|
logger.Println("")
|
||||||
command.PrintHelp()
|
command.PrintHelp()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -81,8 +78,8 @@ func AddSubcommand(app *clir.Cli) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
task := fmt.Sprintf("Initialising Project %s", strings.Title(projectName))
|
task := fmt.Sprintf("Initialising Project %s", strings.Title(projectName))
|
||||||
logger.Writeln(task)
|
logger.Println(task)
|
||||||
logger.Writeln(strings.Repeat("-", len(task)))
|
logger.Println(strings.Repeat("-", len(task)))
|
||||||
|
|
||||||
// Create Template Options
|
// Create Template Options
|
||||||
options := &templates.Options{
|
options := &templates.Options{
|
||||||
@ -112,9 +109,9 @@ func initProject(options *templates.Options) error {
|
|||||||
|
|
||||||
// Output stats
|
// Output stats
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
options.Logger.Writeln("")
|
options.Logger.Println("")
|
||||||
options.Logger.Writeln(fmt.Sprintf("Initialised project '%s' in %s.", options.ProjectName, elapsed.Round(time.Millisecond).String()))
|
options.Logger.Println(fmt.Sprintf("Initialised project '%s' in %s.", options.ProjectName, elapsed.Round(time.Millisecond).String()))
|
||||||
options.Logger.Writeln("")
|
options.Logger.Println("")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -22,17 +22,17 @@ func main() {
|
|||||||
|
|
||||||
app := clir.NewCli("Wails", "Go/HTML Application Framework", version)
|
app := clir.NewCli("Wails", "Go/HTML Application Framework", version)
|
||||||
|
|
||||||
build.AddBuildSubcommand(app)
|
build.AddBuildSubcommand(app, os.Stdout)
|
||||||
err = initialise.AddSubcommand(app)
|
err = initialise.AddSubcommand(app, os.Stdout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatal(err.Error())
|
fatal(err.Error())
|
||||||
}
|
}
|
||||||
err = doctor.AddSubcommand(app)
|
err = doctor.AddSubcommand(app, os.Stdout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatal(err.Error())
|
fatal(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
err = dev.AddSubcommand(app)
|
err = dev.AddSubcommand(app, os.Stdout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatal(err.Error())
|
fatal(err.Error())
|
||||||
}
|
}
|
||||||
|
@ -3,19 +3,19 @@ package process
|
|||||||
import (
|
import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
"github.com/wailsapp/wails/v2/internal/logger"
|
"github.com/wailsapp/wails/v2/pkg/clilogger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Process defines a process that can be executed
|
// Process defines a process that can be executed
|
||||||
type Process struct {
|
type Process struct {
|
||||||
logger *logger.Logger
|
logger *clilogger.CLILogger
|
||||||
cmd *exec.Cmd
|
cmd *exec.Cmd
|
||||||
exitChannel chan bool
|
exitChannel chan bool
|
||||||
Running bool
|
Running bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewProcess creates a new process struct
|
// NewProcess creates a new process struct
|
||||||
func NewProcess(logger *logger.Logger, cmd string, args ...string) *Process {
|
func NewProcess(logger *clilogger.CLILogger, cmd string, args ...string) *Process {
|
||||||
return &Process{
|
return &Process{
|
||||||
logger: logger,
|
logger: logger,
|
||||||
cmd: exec.Command(cmd, args...),
|
cmd: exec.Command(cmd, args...),
|
||||||
@ -33,10 +33,10 @@ func (p *Process) Start() error {
|
|||||||
|
|
||||||
p.Running = true
|
p.Running = true
|
||||||
|
|
||||||
go func(cmd *exec.Cmd, running *bool, logger *logger.Logger, exitChannel chan bool) {
|
go func(cmd *exec.Cmd, running *bool, logger *clilogger.CLILogger, exitChannel chan bool) {
|
||||||
logger.Info("Starting process (PID: %d)", cmd.Process.Pid)
|
logger.Println("Starting process (PID: %d)", cmd.Process.Pid)
|
||||||
cmd.Wait()
|
cmd.Wait()
|
||||||
logger.Info("Exiting process (PID: %d)", cmd.Process.Pid)
|
logger.Println("Exiting process (PID: %d)", cmd.Process.Pid)
|
||||||
*running = false
|
*running = false
|
||||||
exitChannel <- true
|
exitChannel <- true
|
||||||
}(p.cmd, &p.Running, p.logger, p.exitChannel)
|
}(p.cmd, &p.Running, p.logger, p.exitChannel)
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/leaanthony/slicer"
|
"github.com/leaanthony/slicer"
|
||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
"github.com/wailsapp/wails/v2/internal/fs"
|
"github.com/wailsapp/wails/v2/internal/fs"
|
||||||
"github.com/wailsapp/wails/v2/internal/logger"
|
"github.com/wailsapp/wails/v2/pkg/clilogger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Cahce for the templates
|
// Cahce for the templates
|
||||||
@ -35,7 +35,7 @@ type Options struct {
|
|||||||
TemplateName string
|
TemplateName string
|
||||||
BinaryName string
|
BinaryName string
|
||||||
TargetDir string
|
TargetDir string
|
||||||
Logger *logger.Logger
|
Logger *clilogger.CLILogger
|
||||||
}
|
}
|
||||||
|
|
||||||
// Template holds data relating to a template
|
// Template holds data relating to a template
|
||||||
@ -218,14 +218,13 @@ func Install(options *Options) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// OutputList prints the list of available tempaltes to the given logger
|
// OutputList prints the list of available tempaltes to the given logger
|
||||||
func OutputList(logger *logger.Logger) error {
|
func OutputList(logger *clilogger.CLILogger) error {
|
||||||
templates, err := List()
|
templates, err := List()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, writer := range logger.Writers() {
|
table := tablewriter.NewWriter(logger.Writer)
|
||||||
table := tablewriter.NewWriter(writer)
|
|
||||||
table.SetHeader([]string{"Template", "Short Name", "Description"})
|
table.SetHeader([]string{"Template", "Short Name", "Description"})
|
||||||
table.SetAutoWrapText(false)
|
table.SetAutoWrapText(false)
|
||||||
table.SetAutoFormatHeaders(true)
|
table.SetAutoFormatHeaders(true)
|
||||||
@ -242,6 +241,5 @@ func OutputList(logger *logger.Logger) error {
|
|||||||
table.Append([]string{template.Name, template.ShortName, template.Description})
|
table.Append([]string{template.Name, template.ShortName, template.Description})
|
||||||
}
|
}
|
||||||
table.Render()
|
table.Render()
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
59
v2/pkg/clilogger/clilogger.go
Normal file
59
v2/pkg/clilogger/clilogger.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package clilogger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CLILogger is used by the cli
|
||||||
|
type CLILogger struct {
|
||||||
|
Writer io.Writer
|
||||||
|
mute bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// New cli logger
|
||||||
|
func New(writer io.Writer) *CLILogger {
|
||||||
|
return &CLILogger{
|
||||||
|
Writer: writer,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mute sets whether the logger should be muted
|
||||||
|
func (c *CLILogger) Mute(value bool) {
|
||||||
|
c.mute = value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print works like Printf
|
||||||
|
func (c *CLILogger) Print(message string, args ...interface{}) {
|
||||||
|
if c.mute {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := fmt.Fprintf(c.Writer, message, args...)
|
||||||
|
if err != nil {
|
||||||
|
c.Fatal("Fatal: ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Println works like Printf but with a line ending
|
||||||
|
func (c *CLILogger) Println(message string, args ...interface{}) {
|
||||||
|
if c.mute {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
temp := fmt.Sprintf(message, args...)
|
||||||
|
_, err := fmt.Fprintln(c.Writer, temp)
|
||||||
|
if err != nil {
|
||||||
|
c.Fatal("Fatal: ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fatal prints the given message then aborts
|
||||||
|
func (c *CLILogger) Fatal(message string, args ...interface{}) {
|
||||||
|
temp := fmt.Sprintf(message, args...)
|
||||||
|
_, err := fmt.Fprintln(c.Writer, "FATAL: "+temp)
|
||||||
|
if err != nil {
|
||||||
|
println("FATAL: ", err)
|
||||||
|
}
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
@ -14,9 +14,9 @@ import (
|
|||||||
"github.com/wailsapp/wails/v2/internal/assetdb"
|
"github.com/wailsapp/wails/v2/internal/assetdb"
|
||||||
"github.com/wailsapp/wails/v2/internal/fs"
|
"github.com/wailsapp/wails/v2/internal/fs"
|
||||||
"github.com/wailsapp/wails/v2/internal/html"
|
"github.com/wailsapp/wails/v2/internal/html"
|
||||||
"github.com/wailsapp/wails/v2/internal/logger"
|
|
||||||
"github.com/wailsapp/wails/v2/internal/project"
|
"github.com/wailsapp/wails/v2/internal/project"
|
||||||
"github.com/wailsapp/wails/v2/internal/shell"
|
"github.com/wailsapp/wails/v2/internal/shell"
|
||||||
|
"github.com/wailsapp/wails/v2/pkg/clilogger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BaseBuilder is the common builder struct
|
// BaseBuilder is the common builder struct
|
||||||
@ -305,7 +305,7 @@ func (b *BaseBuilder) NpmRunWithEnvironment(projectDir, buildTarget string, verb
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BuildFrontend executes the `npm build` command for the frontend directory
|
// BuildFrontend executes the `npm build` command for the frontend directory
|
||||||
func (b *BaseBuilder) BuildFrontend(outputLogger *logger.Logger) error {
|
func (b *BaseBuilder) BuildFrontend(outputLogger *clilogger.CLILogger) error {
|
||||||
verbose := false
|
verbose := false
|
||||||
|
|
||||||
frontendDir := filepath.Join(b.projectData.Path, "frontend")
|
frontendDir := filepath.Join(b.projectData.Path, "frontend")
|
||||||
@ -313,10 +313,10 @@ func (b *BaseBuilder) BuildFrontend(outputLogger *logger.Logger) error {
|
|||||||
// Check there is an 'InstallCommand' provided in wails.json
|
// Check there is an 'InstallCommand' provided in wails.json
|
||||||
if b.projectData.InstallCommand == "" {
|
if b.projectData.InstallCommand == "" {
|
||||||
// No - don't install
|
// No - don't install
|
||||||
outputLogger.Writeln(" - No Install command. Skipping.")
|
outputLogger.Println(" - No Install command. Skipping.")
|
||||||
} else {
|
} else {
|
||||||
// Do install if needed
|
// Do install if needed
|
||||||
outputLogger.Writeln(" - Installing dependencies...")
|
outputLogger.Println(" - Installing dependencies...")
|
||||||
if err := b.NpmInstallUsingCommand(frontendDir, b.projectData.InstallCommand); err != nil {
|
if err := b.NpmInstallUsingCommand(frontendDir, b.projectData.InstallCommand); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -324,12 +324,12 @@ func (b *BaseBuilder) BuildFrontend(outputLogger *logger.Logger) error {
|
|||||||
|
|
||||||
// Check if there is a build command
|
// Check if there is a build command
|
||||||
if b.projectData.BuildCommand == "" {
|
if b.projectData.BuildCommand == "" {
|
||||||
outputLogger.Writeln(" - No Build command. Skipping.")
|
outputLogger.Println(" - No Build command. Skipping.")
|
||||||
// No - ignore
|
// No - ignore
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
outputLogger.Writeln(" - Compiling Frontend Project")
|
outputLogger.Println(" - Compiling Frontend Project")
|
||||||
cmd := strings.Split(b.projectData.BuildCommand, " ")
|
cmd := strings.Split(b.projectData.BuildCommand, " ")
|
||||||
stdout, stderr, err := shell.RunCommand(frontendDir, cmd[0], cmd[1:]...)
|
stdout, stderr, err := shell.RunCommand(frontendDir, cmd[0], cmd[1:]...)
|
||||||
if verbose || err != nil {
|
if verbose || err != nil {
|
||||||
|
@ -6,8 +6,8 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/leaanthony/slicer"
|
"github.com/leaanthony/slicer"
|
||||||
"github.com/wailsapp/wails/v2/internal/logger"
|
|
||||||
"github.com/wailsapp/wails/v2/internal/project"
|
"github.com/wailsapp/wails/v2/internal/project"
|
||||||
|
"github.com/wailsapp/wails/v2/pkg/clilogger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Mode is the type used to indicate the build modes
|
// Mode is the type used to indicate the build modes
|
||||||
@ -25,7 +25,7 @@ var modeMap = []string{"Debug", "Production"}
|
|||||||
// Options contains all the build options as well as the project data
|
// Options contains all the build options as well as the project data
|
||||||
type Options struct {
|
type Options struct {
|
||||||
LDFlags string // Optional flags to pass to linker
|
LDFlags string // Optional flags to pass to linker
|
||||||
Logger *logger.Logger // All output to the logger
|
Logger *clilogger.CLILogger // All output to the logger
|
||||||
OutputType string // EG: desktop, server....
|
OutputType string // EG: desktop, server....
|
||||||
Mode Mode // release or debug
|
Mode Mode // release or debug
|
||||||
ProjectData *project.Project // The project data
|
ProjectData *project.Project // The project data
|
||||||
@ -47,11 +47,6 @@ func Build(options *Options) (string, error) {
|
|||||||
// Extract logger
|
// Extract logger
|
||||||
outputLogger := options.Logger
|
outputLogger := options.Logger
|
||||||
|
|
||||||
// Create a default logger if it doesn't exist
|
|
||||||
if outputLogger == nil {
|
|
||||||
outputLogger = logger.New()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get working directory
|
// Get working directory
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -95,7 +90,7 @@ func Build(options *Options) (string, error) {
|
|||||||
builder.SetProjectData(projectData)
|
builder.SetProjectData(projectData)
|
||||||
|
|
||||||
if !options.IgnoreFrontend {
|
if !options.IgnoreFrontend {
|
||||||
outputLogger.Writeln(" - Building Wails Frontend")
|
outputLogger.Println(" - Building Wails Frontend")
|
||||||
err = builder.BuildFrontend(outputLogger)
|
err = builder.BuildFrontend(outputLogger)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@ -103,7 +98,7 @@ func Build(options *Options) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Build the base assets
|
// Build the base assets
|
||||||
outputLogger.Writeln(" - Compiling Assets")
|
outputLogger.Println(" - Compiling Assets")
|
||||||
err = builder.BuildRuntime(options)
|
err = builder.BuildRuntime(options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@ -114,16 +109,16 @@ func Build(options *Options) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Compile the application
|
// Compile the application
|
||||||
outputLogger.Write(" - Compiling Application in " + GetModeAsString(options.Mode) + " mode...")
|
outputLogger.Print(" - Compiling Application in " + GetModeAsString(options.Mode) + " mode...")
|
||||||
err = builder.CompileProject(options)
|
err = builder.CompileProject(options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
outputLogger.Writeln("done.")
|
outputLogger.Println("done.")
|
||||||
// Do we need to pack the app?
|
// Do we need to pack the app?
|
||||||
if options.Pack {
|
if options.Pack {
|
||||||
|
|
||||||
outputLogger.Writeln(" - Packaging Application")
|
outputLogger.Println(" - Packaging Application")
|
||||||
|
|
||||||
// TODO: Allow cross platform build
|
// TODO: Allow cross platform build
|
||||||
err = packageProject(options, runtime.GOOS)
|
err = packageProject(options, runtime.GOOS)
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
package build
|
package build
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/wailsapp/wails/v2/internal/logger"
|
|
||||||
"github.com/wailsapp/wails/v2/internal/project"
|
"github.com/wailsapp/wails/v2/internal/project"
|
||||||
|
"github.com/wailsapp/wails/v2/pkg/clilogger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Builder defines a builder that can build Wails applications
|
// Builder defines a builder that can build Wails applications
|
||||||
type Builder interface {
|
type Builder interface {
|
||||||
SetProjectData(projectData *project.Project)
|
SetProjectData(projectData *project.Project)
|
||||||
BuildAssets(*Options) error
|
BuildAssets(*Options) error
|
||||||
BuildFrontend(*logger.Logger) error
|
BuildFrontend(*clilogger.CLILogger) error
|
||||||
BuildRuntime(*Options) error
|
BuildRuntime(*Options) error
|
||||||
CompileProject(*Options) error
|
CompileProject(*Options) error
|
||||||
CleanUp()
|
CleanUp()
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/wailsapp/wails/v2/internal/fs"
|
"github.com/wailsapp/wails/v2/internal/fs"
|
||||||
"github.com/wailsapp/wails/v2/internal/html"
|
"github.com/wailsapp/wails/v2/internal/html"
|
||||||
"github.com/wailsapp/wails/v2/internal/logger"
|
"github.com/wailsapp/wails/v2/pkg/clilogger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DesktopBuilder builds applications for the desktop
|
// DesktopBuilder builds applications for the desktop
|
||||||
@ -47,10 +47,10 @@ func (d *DesktopBuilder) BuildAssets(options *Options) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BuildBaseAssets builds the assets for the desktop application
|
// BuildBaseAssets builds the assets for the desktop application
|
||||||
func (d *DesktopBuilder) BuildBaseAssets(assets *html.AssetBundle, outputLogger *logger.Logger) error {
|
func (d *DesktopBuilder) BuildBaseAssets(assets *html.AssetBundle, outputLogger *clilogger.CLILogger) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
outputLogger.Write(" - Embedding Assets...")
|
outputLogger.Print(" - Embedding Assets...")
|
||||||
|
|
||||||
// Get target asset directory
|
// Get target asset directory
|
||||||
assetDir := fs.RelativePath("../../../internal/ffenestri")
|
assetDir := fs.RelativePath("../../../internal/ffenestri")
|
||||||
@ -68,7 +68,7 @@ func (d *DesktopBuilder) BuildBaseAssets(assets *html.AssetBundle, outputLogger
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
outputLogger.Writeln("done.")
|
outputLogger.Println("done.")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -101,7 +101,7 @@ func (d *DesktopBuilder) BuildRuntime(options *Options) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
outputLogger.Write(" - Embedding Runtime...")
|
outputLogger.Print(" - Embedding Runtime...")
|
||||||
envvars := []string{"WAILSPLATFORM=" + options.Platform}
|
envvars := []string{"WAILSPLATFORM=" + options.Platform}
|
||||||
if err := d.NpmRunWithEnvironment(sourceDir, "build:desktop", false, envvars); err != nil {
|
if err := d.NpmRunWithEnvironment(sourceDir, "build:desktop", false, envvars); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -112,7 +112,7 @@ func (d *DesktopBuilder) BuildRuntime(options *Options) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
outputLogger.Writeln("done.")
|
outputLogger.Println("done.")
|
||||||
|
|
||||||
// Convert to C structure
|
// Convert to C structure
|
||||||
runtimeC := `
|
runtimeC := `
|
||||||
|
@ -99,13 +99,13 @@ func (s *ServerBuilder) BuildRuntime(options *Options) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
options.Logger.Write(" - Embedding Runtime...")
|
options.Logger.Print(" - Embedding Runtime...")
|
||||||
envvars := []string{"WAILSPLATFORM=" + options.Platform}
|
envvars := []string{"WAILSPLATFORM=" + options.Platform}
|
||||||
var err error
|
var err error
|
||||||
if err = s.NpmRunWithEnvironment(sourceDir, "build:server", false, envvars); err != nil {
|
if err = s.NpmRunWithEnvironment(sourceDir, "build:server", false, envvars); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
options.Logger.Writeln("done.")
|
options.Logger.Println("done.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user