5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 18:42:23 +08:00
wails/v2/pkg/commands/bindings/bindings.go
Oleg Gulevskyy ca8a1fab36
Ability to define prefix / suffix for TS module (#2101)
* 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 <lea.anthony@gmail.com>

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

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>

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

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>

* update changelog

* Minor tweaks

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
2022-11-24 21:33:58 +11:00

76 lines
1.9 KiB
Go

package bindings
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"github.com/samber/lo"
"github.com/wailsapp/wails/v2/internal/colour"
"github.com/wailsapp/wails/v2/internal/shell"
"github.com/wailsapp/wails/v2/pkg/commands/buildtags"
)
// Options for generating bindings
type Options struct {
Filename string
Tags []string
ProjectDirectory string
GoModTidy bool
TsPrefix string
TsSuffix string
}
// GenerateBindings generates bindings for the Wails project in the given ProjectDirectory.
// If no project directory is given then the current working directory is used.
func GenerateBindings(options Options) (string, error) {
filename, _ := lo.Coalesce(options.Filename, "wailsbindings")
if runtime.GOOS == "windows" {
filename += ".exe"
}
// go build -tags bindings -o bindings.exe
tempDir := os.TempDir()
filename = filepath.Join(tempDir, filename)
workingDirectory, _ := lo.Coalesce(options.ProjectDirectory, lo.Must(os.Getwd()))
var stdout, stderr string
var err error
tags := append(options.Tags, "bindings")
genModuleTags := lo.Without(tags, "desktop", "production", "debug", "dev")
tagString := buildtags.Stringify(genModuleTags)
if options.GoModTidy {
stdout, stderr, err = shell.RunCommand(workingDirectory, "go", "mod", "tidy")
if err != nil {
return stdout, fmt.Errorf("%s\n%s\n%s", stdout, stderr, err)
}
}
stdout, stderr, err = shell.RunCommand(workingDirectory, "go", "build", "-tags", tagString, "-o", filename)
if err != nil {
return stdout, fmt.Errorf("%s\n%s\n%s", stdout, stderr, err)
}
defer func() {
// Best effort removal of temp file
_ = os.Remove(filename)
}()
stdout, stderr, err = shell.RunCommand(workingDirectory, filename, "-tsprefix", options.TsPrefix, "-tssuffix", options.TsSuffix)
if err != nil {
return stdout, fmt.Errorf("%s\n%s\n%s", stdout, stderr, err)
}
if stderr != "" {
log.Println(colour.DarkYellow(stderr))
}
return stdout, nil
}