Skip to content

Commit

Permalink
Ability to define prefix / suffix for TS module (#2101)
Browse files Browse the repository at this point in the history
* add tspostfix and tsprefix flags + organise under struct

* postifx -> suffix

* tsPrefix options on bindings struct

* pass prefix and suffix to the executable

* add support for CLI flags for generating module

* method to set TSpref/suff to bindings

* use passed ts prefix for typescriptify

* add brief Readme udpate to include new flags

* create reusable common flags

* use common flags instead of hardcoded text

* support tsprefix & suffix for dev

* add tsPrefix & tsSuffix for build cmd

* take pref & suff in account when gen d.ts

* export colorsful log functions into utils for reuse

* detect and warn the user about usage of reserved keyword

* fmt

* add TrimSpace on fn input

* refactor utils -> logutils

* add bindings -> ts_generation options to wailsjson parse

* use wailsjson for ts generation

* update warning message + extract to func

* remove suff/pref info from readme

* update json schema

* add tests for prefix and suffix case

* rename suffix method

* Update v2/internal/typescriptify/typescriptify.go

Co-authored-by: Lea Anthony <[email protected]>

* Update website/static/schemas/config.v2.json

Co-authored-by: Lea Anthony <[email protected]>

* Update website/static/schemas/config.v2.json

Co-authored-by: Lea Anthony <[email protected]>

* update changelog

* Minor tweaks

Co-authored-by: Lea Anthony <[email protected]>
  • Loading branch information
OlegGulevskyy and leaanthony authored Nov 24, 2022
1 parent c1c1bff commit ca8a1fa
Show file tree
Hide file tree
Showing 20 changed files with 352 additions and 67 deletions.
3 changes: 2 additions & 1 deletion v2/cmd/wails/internal/commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package build

import (
"fmt"
"github.com/wailsapp/wails/v2/pkg/commands/buildtags"
"io"
"os"
"os/exec"
Expand All @@ -11,6 +10,8 @@ import (
"text/tabwriter"
"time"

"github.com/wailsapp/wails/v2/pkg/commands/buildtags"

"github.com/wailsapp/wails/v2/internal/colour"
"github.com/wailsapp/wails/v2/internal/project"
"github.com/wailsapp/wails/v2/internal/system"
Expand Down
77 changes: 28 additions & 49 deletions v2/cmd/wails/internal/commands/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,13 @@ import (

"github.com/fsnotify/fsnotify"
"github.com/leaanthony/clir"
"github.com/wailsapp/wails/v2/cmd/wails/internal/logutils"
"github.com/wailsapp/wails/v2/internal/fs"
"github.com/wailsapp/wails/v2/internal/process"
"github.com/wailsapp/wails/v2/pkg/clilogger"
"github.com/wailsapp/wails/v2/pkg/commands/build"
)

func LogGreen(message string, args ...interface{}) {
if len(message) == 0 {
return
}
text := fmt.Sprintf(message, args...)
println(colour.Green(text))
}

func LogRed(message string, args ...interface{}) {
if len(message) == 0 {
return
}
text := fmt.Sprintf(message, args...)
println(colour.Red(text))
}

func LogDarkYellow(message string, args ...interface{}) {
if len(message) == 0 {
return
}
text := fmt.Sprintf(message, args...)
println(colour.DarkYellow(text))
}

func sliceToMap(input []string) map[string]struct{} {
result := map[string]struct{}{}
for _, value := range input {
Expand Down Expand Up @@ -184,16 +161,18 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {

if !buildOptions.SkipBindings {
if flags.verbosity == build.VERBOSE {
LogGreen("Generating Bindings...")
logutils.LogGreen("Generating Bindings...")
}
stdout, err := bindings.GenerateBindings(bindings.Options{
Tags: buildOptions.UserTags,
Tags: buildOptions.UserTags,
TsPrefix: projectConfig.Bindings.TsGeneration.Prefix,
TsSuffix: projectConfig.Bindings.TsGeneration.Suffix,
})
if err != nil {
return err
}
if flags.verbosity == build.VERBOSE {
LogGreen(stdout)
logutils.LogGreen(stdout)
}
}

Expand Down Expand Up @@ -238,7 +217,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
}
defer func() {
if err := killProcessAndCleanupBinary(debugBinaryProcess, appBinary); err != nil {
LogDarkYellow("Unable to kill process and cleanup binary: %s", err)
logutils.LogDarkYellow("Unable to kill process and cleanup binary: %s", err)
}
}()

Expand All @@ -263,17 +242,17 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
}
}(watcher)

LogGreen("Watching (sub)/directory: %s", cwd)
LogGreen("Using DevServer URL: %s", devServerURL)
logutils.LogGreen("Watching (sub)/directory: %s", cwd)
logutils.LogGreen("Using DevServer URL: %s", devServerURL)
if flags.frontendDevServerURL != "" {
LogGreen("Using Frontend DevServer URL: %s", flags.frontendDevServerURL)
logutils.LogGreen("Using Frontend DevServer URL: %s", flags.frontendDevServerURL)
}
LogGreen("Using reload debounce setting of %d milliseconds", flags.debounceMS)
logutils.LogGreen("Using reload debounce setting of %d milliseconds", flags.debounceMS)

// Show dev server URL in terminal after 3 seconds
go func() {
time.Sleep(3 * time.Second)
LogGreen("\n\nTo develop in the browser and call your bound Go methods from Javascript, navigate to: %s", devServerURL)
logutils.LogGreen("\n\nTo develop in the browser and call your bound Go methods from Javascript, navigate to: %s", devServerURL)
}()

// Watch for changes and trigger restartApp()
Expand All @@ -288,7 +267,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
debugBinaryProcess = nil
appBinary = ""

LogGreen("Development mode exited")
logutils.LogGreen("Development mode exited")

return nil
})
Expand All @@ -312,7 +291,7 @@ func killProcessAndCleanupBinary(process *process.Process, binary string) error
}

func runCommand(dir string, exitOnError bool, command string, args ...string) error {
LogGreen("Executing: " + command + " " + strings.Join(args, " "))
logutils.LogGreen("Executing: " + command + " " + strings.Join(args, " "))
cmd := exec.Command(command, args...)
cmd.Dir = dir
output, err := cmd.CombinedOutput()
Expand Down Expand Up @@ -460,7 +439,7 @@ func runFrontendDevWatcherCommand(frontendDirectory string, devCommand string, d
}
}

LogGreen("Running frontend DevWatcher command: '%s'", devCommand)
logutils.LogGreen("Running frontend DevWatcher command: '%s'", devCommand)
var wg sync.WaitGroup
wg.Add(1)

Expand All @@ -474,7 +453,7 @@ func runFrontendDevWatcherCommand(frontendDirectory string, devCommand string, d
if err := cmd.Wait(); err != nil {
wasRunning := atomic.CompareAndSwapInt32(&state, stateRunning, stateStopped)
if err.Error() != "exit status 1" && wasRunning {
LogRed("Error from DevWatcher '%s': %s", devCommand, err.Error())
logutils.LogRed("Error from DevWatcher '%s': %s", devCommand, err.Error())
}
}
atomic.StoreInt32(&state, stateStopped)
Expand All @@ -496,13 +475,13 @@ func restartApp(buildOptions *build.Options, debugBinaryProcess *process.Process
appBinary, err := build.Build(buildOptions)
println()
if err != nil {
LogRed("Build error - " + err.Error())
logutils.LogRed("Build error - " + err.Error())

msg := "Continuing to run current version"
if debugBinaryProcess == nil {
msg = "No version running, build will be retriggered as soon as changes have been detected"
}
LogDarkYellow(msg)
logutils.LogDarkYellow(msg)
return nil, "", nil
}

Expand Down Expand Up @@ -558,7 +537,7 @@ func doWatcherLoop(buildOptions *build.Options, debugBinaryProcess *process.Proc
}
thePath, err := filepath.Abs(dir)
if err != nil {
LogRed("Unable to expand reloadDir '%s': %s", dir, err)
logutils.LogRed("Unable to expand reloadDir '%s': %s", dir, err)
continue
}
dirsThatTriggerAReload = append(dirsThatTriggerAReload, thePath)
Expand All @@ -585,7 +564,7 @@ func doWatcherLoop(buildOptions *build.Options, debugBinaryProcess *process.Proc
quit = true
}
case err := <-watcher.Errors:
LogDarkYellow(err.Error())
logutils.LogDarkYellow(err.Error())
case item := <-watcher.Events:
isEligibleFile := func(fileName string) bool {
// Iterate all file patterns
Expand Down Expand Up @@ -637,7 +616,7 @@ func doWatcherLoop(buildOptions *build.Options, debugBinaryProcess *process.Proc
if err != nil {
buildOptions.Logger.Fatal("%s", err.Error())
}
LogGreen("Added new directory to watcher: %s", item.Name)
logutils.LogGreen("Added new directory to watcher: %s", item.Name)
}
} else if isEligibleFile(item.Name) {
// Handle creation of new file.
Expand All @@ -652,11 +631,11 @@ func doWatcherLoop(buildOptions *build.Options, debugBinaryProcess *process.Proc
case <-timer.C:
if rebuild {
rebuild = false
LogGreen("[Rebuild triggered] files updated")
logutils.LogGreen("[Rebuild triggered] files updated")
// Try and build the app
newBinaryProcess, _, err := restartApp(buildOptions, debugBinaryProcess, flags, exitCodeChannel)
if err != nil {
LogRed("Error during build: %s", err.Error())
logutils.LogRed("Error during build: %s", err.Error())
continue
}
// If we have a new process, saveConfig it
Expand All @@ -669,11 +648,11 @@ func doWatcherLoop(buildOptions *build.Options, debugBinaryProcess *process.Proc
if assetDir == "" {
resp, err := http.Get(assetDirURL)
if err != nil {
LogRed("Error during retrieving assetdir: %s", err.Error())
logutils.LogRed("Error during retrieving assetdir: %s", err.Error())
} else {
content, err := io.ReadAll(resp.Body)
if err != nil {
LogRed("Error reading assetdir from devserver: %s", err.Error())
logutils.LogRed("Error reading assetdir from devserver: %s", err.Error())
} else {
assetDir = string(content)
}
Expand All @@ -689,19 +668,19 @@ func doWatcherLoop(buildOptions *build.Options, debugBinaryProcess *process.Proc
}
}
} else if len(dirsThatTriggerAReload) == 0 {
LogRed("Reloading couldn't be triggered: Please specify -assetdir or -reloaddirs")
logutils.LogRed("Reloading couldn't be triggered: Please specify -assetdir or -reloaddirs")
}
}
if reload {
reload = false
_, err := http.Get(reloadURL)
if err != nil {
LogRed("Error during refresh: %s", err.Error())
logutils.LogRed("Error during refresh: %s", err.Error())
}
}
changedPaths = map[string]struct{}{}
case <-quitChannel:
LogGreen("\nCaught quit")
logutils.LogGreen("\nCaught quit")
quit = true
}
}
Expand Down
3 changes: 2 additions & 1 deletion v2/cmd/wails/internal/commands/dev/dev_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"syscall"

"github.com/wailsapp/wails/v2/cmd/wails/internal/logutils"
"golang.org/x/sys/unix"
)

Expand All @@ -30,7 +31,7 @@ func killProc(cmd *exec.Cmd, devCommand string) {
if err == nil {
err := syscall.Kill(-pgid, unix.SIGTERM) // note the minus sign
if err != nil {
LogRed("Error from '%s' when attempting to kill the process: %s", devCommand, err.Error())
logutils.LogRed("Error from '%s' when attempting to kill the process: %s", devCommand, err.Error())
}
}
}
5 changes: 3 additions & 2 deletions v2/cmd/wails/internal/commands/dev/stdout_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/acarl005/stripansi"
"github.com/wailsapp/wails/v2/cmd/wails/internal/logutils"
)

// stdoutScanner acts as a stdout target that will scan the incoming
Expand Down Expand Up @@ -35,10 +36,10 @@ func (s *stdoutScanner) Write(data []byte) (n int, err error) {
continue
}
viteServerURL := strings.TrimSpace(line[index+6:])
LogGreen("Vite Server URL: %s", viteServerURL)
logutils.LogGreen("Vite Server URL: %s", viteServerURL)
_, err := url.Parse(viteServerURL)
if err != nil {
LogRed(err.Error())
logutils.LogRed(err.Error())
} else {
s.ViteServerURLChan <- viteServerURL
}
Expand Down
7 changes: 7 additions & 0 deletions v2/cmd/wails/internal/commands/generate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ Generate a starter template for you to customise.
| :------------- | :----------- |
| -frontend | Copies all the files from the current directory into the template's `frontend` directory. Useful for converting frontend projects created by boilerplate generators. |
| -q | Suppress output |


## Module

`wails generate module [-h]`

Generate TS module for your frontend
27 changes: 22 additions & 5 deletions v2/cmd/wails/internal/commands/generate/module.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
package generate

import (
"io"
"os"

"github.com/leaanthony/clir"
"github.com/wailsapp/wails/v2/internal/project"
"github.com/wailsapp/wails/v2/pkg/commands/bindings"
"github.com/wailsapp/wails/v2/pkg/commands/buildtags"
"io"
)

type generateFlags struct {
tags string
}

// AddModuleCommand adds the `module` subcommand for the `generate` command
func AddModuleCommand(app *clir.Cli, parent *clir.Command, w io.Writer) error {

command := parent.NewSubCommand("module", "Generate wailsjs modules")
var tags string
command.StringFlag("tags", "tags to pass to Go compiler (quoted and space separated)", &tags)
genFlags := generateFlags{}
command.StringFlag("tags", "tags to pass to Go compiler (quoted and space separated)", &genFlags.tags)

command.Action(func() error {
cwd, err := os.Getwd()
if err != nil {
return err
}
projectConfig, err := project.Load(cwd)
if err != nil {
return err
}

buildTags, err := buildtags.Parse(tags)
buildTags, err := buildtags.Parse(genFlags.tags)
if err != nil {
return err
}

_, err = bindings.GenerateBindings(bindings.Options{
Tags: buildTags,
Tags: buildTags,
TsPrefix: projectConfig.Bindings.TsGeneration.Prefix,
TsSuffix: projectConfig.Bindings.TsGeneration.Suffix,
})
if err != nil {
return err
Expand Down
31 changes: 31 additions & 0 deletions v2/cmd/wails/internal/logutils/color-logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package logutils

import (
"fmt"

"github.com/wailsapp/wails/v2/internal/colour"
)

func LogGreen(message string, args ...interface{}) {
if len(message) == 0 {
return
}
text := fmt.Sprintf(message, args...)
println(colour.Green(text))
}

func LogRed(message string, args ...interface{}) {
if len(message) == 0 {
return
}
text := fmt.Sprintf(message, args...)
println(colour.Red(text))
}

func LogDarkYellow(message string, args ...interface{}) {
if len(message) == 0 {
return
}
text := fmt.Sprintf(message, args...)
println(colour.DarkYellow(text))
}
Loading

0 comments on commit ca8a1fa

Please sign in to comment.