mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 19:50:15 +08:00
[bindings] Pass ts_generation flags as env variable (#2194)
Otherwise this will clash if an app also uses flags internally
This commit is contained in:
parent
7a22e0f885
commit
eb2d929824
40
v2/internal/shell/env.go
Normal file
40
v2/internal/shell/env.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package shell
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func UpsertEnv(env []string, key string, update func(v string) string) []string {
|
||||||
|
newEnv := make([]string, len(env), len(env)+1)
|
||||||
|
found := false
|
||||||
|
for i := range env {
|
||||||
|
if strings.HasPrefix(env[i], key+"=") {
|
||||||
|
eqIndex := strings.Index(env[i], "=")
|
||||||
|
val := env[i][eqIndex+1:]
|
||||||
|
newEnv[i] = fmt.Sprintf("%s=%v", key, update(val))
|
||||||
|
found = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newEnv[i] = env[i]
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
newEnv = append(newEnv, fmt.Sprintf("%s=%v", key, update("")))
|
||||||
|
}
|
||||||
|
return newEnv
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveEnv(env []string, key string) []string {
|
||||||
|
newEnv := make([]string, 0, len(env))
|
||||||
|
for _, e := range env {
|
||||||
|
if strings.HasPrefix(e, key+"=") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newEnv = append(newEnv, e)
|
||||||
|
}
|
||||||
|
return newEnv
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetEnv(env []string, key string, value string) []string {
|
||||||
|
return UpsertEnv(env, key, func(_ string) string { return value })
|
||||||
|
}
|
67
v2/internal/shell/env_test.go
Normal file
67
v2/internal/shell/env_test.go
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package shell
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestUpdateEnv(t *testing.T) {
|
||||||
|
|
||||||
|
env := []string{"one=1", "two=a=b", "three="}
|
||||||
|
newEnv := UpsertEnv(env, "two", func(v string) string {
|
||||||
|
return v + "+added"
|
||||||
|
})
|
||||||
|
newEnv = UpsertEnv(newEnv, "newVar", func(v string) string {
|
||||||
|
return "added"
|
||||||
|
})
|
||||||
|
newEnv = UpsertEnv(newEnv, "three", func(v string) string {
|
||||||
|
return "3"
|
||||||
|
})
|
||||||
|
newEnv = UpsertEnv(newEnv, "GOARCH", func(v string) string {
|
||||||
|
return "amd64"
|
||||||
|
})
|
||||||
|
|
||||||
|
if len(newEnv) != 5 {
|
||||||
|
t.Errorf("expected: 5, got: %d", len(newEnv))
|
||||||
|
}
|
||||||
|
if newEnv[1] != "two=a=b+added" {
|
||||||
|
t.Errorf("expected: \"two=a=b+added\", got: %q", newEnv[1])
|
||||||
|
}
|
||||||
|
if newEnv[2] != "three=3" {
|
||||||
|
t.Errorf("expected: \"three=3\", got: %q", newEnv[2])
|
||||||
|
}
|
||||||
|
if newEnv[3] != "newVar=added" {
|
||||||
|
t.Errorf("expected: \"newVar=added\", got: %q", newEnv[3])
|
||||||
|
}
|
||||||
|
if newEnv[4] != "GOARCH=amd64" {
|
||||||
|
t.Errorf("expected: \"newVar=added\", got: %q", newEnv[4])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetEnv(t *testing.T) {
|
||||||
|
env := []string{"one=1", "two=a=b", "three="}
|
||||||
|
newEnv := SetEnv(env, "two", "set")
|
||||||
|
newEnv = SetEnv(newEnv, "newVar", "added")
|
||||||
|
|
||||||
|
if len(newEnv) != 4 {
|
||||||
|
t.Errorf("expected: 4, got: %d", len(newEnv))
|
||||||
|
}
|
||||||
|
if newEnv[1] != "two=set" {
|
||||||
|
t.Errorf("expected: \"two=set\", got: %q", newEnv[1])
|
||||||
|
}
|
||||||
|
if newEnv[3] != "newVar=added" {
|
||||||
|
t.Errorf("expected: \"newVar=added\", got: %q", newEnv[3])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveEnv(t *testing.T) {
|
||||||
|
env := []string{"one=1", "two=a=b", "three=3"}
|
||||||
|
newEnv := RemoveEnv(env, "two")
|
||||||
|
|
||||||
|
if len(newEnv) != 2 {
|
||||||
|
t.Errorf("expected: 2, got: %d", len(newEnv))
|
||||||
|
}
|
||||||
|
if newEnv[0] != "one=1" {
|
||||||
|
t.Errorf("expected: \"one=1\", got: %q", newEnv[1])
|
||||||
|
}
|
||||||
|
if newEnv[1] != "three=3" {
|
||||||
|
t.Errorf("expected: \"three=3\", got: %q", newEnv[3])
|
||||||
|
}
|
||||||
|
}
|
@ -62,7 +62,19 @@ func CreateCommand(directory string, command string, args ...string) *exec.Cmd {
|
|||||||
// RunCommand will run the given command + args in the given directory
|
// RunCommand will run the given command + args in the given directory
|
||||||
// Will return stdout, stderr and error
|
// Will return stdout, stderr and error
|
||||||
func RunCommand(directory string, command string, args ...string) (string, string, error) {
|
func RunCommand(directory string, command string, args ...string) (string, string, error) {
|
||||||
|
return RunCommandWithEnv(nil, directory, command, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunCommandWithEnv will run the given command + args in the given directory and using the specified env.
|
||||||
|
//
|
||||||
|
// Env specifies the environment of the process. Each entry is of the form "key=value".
|
||||||
|
// If Env is nil, the new process uses the current process's environment.
|
||||||
|
//
|
||||||
|
// Will return stdout, stderr and error
|
||||||
|
func RunCommandWithEnv(env []string, directory string, command string, args ...string) (string, string, error) {
|
||||||
cmd := CreateCommand(directory, command, args...)
|
cmd := CreateCommand(directory, command, args...)
|
||||||
|
cmd.Env = env
|
||||||
|
|
||||||
var stdo, stde bytes.Buffer
|
var stdo, stde bytes.Buffer
|
||||||
cmd.Stdout = &stdo
|
cmd.Stdout = &stdo
|
||||||
cmd.Stderr = &stde
|
cmd.Stderr = &stde
|
||||||
|
@ -62,7 +62,12 @@ func GenerateBindings(options Options) (string, error) {
|
|||||||
_ = os.Remove(filename)
|
_ = os.Remove(filename)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
stdout, stderr, err = shell.RunCommand(workingDirectory, filename, "-tsprefix", options.TsPrefix, "-tssuffix", options.TsSuffix)
|
// Set environment variables accordingly
|
||||||
|
env := os.Environ()
|
||||||
|
env = shell.SetEnv(env, "tsprefix", options.TsPrefix)
|
||||||
|
env = shell.SetEnv(env, "tssuffix", options.TsSuffix)
|
||||||
|
|
||||||
|
stdout, stderr, err = shell.RunCommandWithEnv(env, workingDirectory, filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return stdout, fmt.Errorf("%s\n%s\n%s", stdout, stderr, err)
|
return stdout, fmt.Errorf("%s\n%s\n%s", stdout, stderr, err)
|
||||||
}
|
}
|
||||||
|
@ -302,8 +302,8 @@ func (b *BaseBuilder) CompileProject(options *Options) error {
|
|||||||
cmd.Env = os.Environ() // inherit env
|
cmd.Env = os.Environ() // inherit env
|
||||||
|
|
||||||
if options.Platform != "windows" {
|
if options.Platform != "windows" {
|
||||||
// Use upsertEnv so we don't overwrite user's CGO_CFLAGS
|
// Use shell.UpsertEnv so we don't overwrite user's CGO_CFLAGS
|
||||||
cmd.Env = upsertEnv(cmd.Env, "CGO_CFLAGS", func(v string) string {
|
cmd.Env = shell.UpsertEnv(cmd.Env, "CGO_CFLAGS", func(v string) string {
|
||||||
if options.Platform == "darwin" {
|
if options.Platform == "darwin" {
|
||||||
if v != "" {
|
if v != "" {
|
||||||
v += " "
|
v += " "
|
||||||
@ -312,8 +312,8 @@ func (b *BaseBuilder) CompileProject(options *Options) error {
|
|||||||
}
|
}
|
||||||
return v
|
return v
|
||||||
})
|
})
|
||||||
// Use upsertEnv so we don't overwrite user's CGO_CXXFLAGS
|
// Use shell.UpsertEnv so we don't overwrite user's CGO_CXXFLAGS
|
||||||
cmd.Env = upsertEnv(cmd.Env, "CGO_CXXFLAGS", func(v string) string {
|
cmd.Env = shell.UpsertEnv(cmd.Env, "CGO_CXXFLAGS", func(v string) string {
|
||||||
if v != "" {
|
if v != "" {
|
||||||
v += " "
|
v += " "
|
||||||
}
|
}
|
||||||
@ -321,7 +321,7 @@ func (b *BaseBuilder) CompileProject(options *Options) error {
|
|||||||
return v
|
return v
|
||||||
})
|
})
|
||||||
|
|
||||||
cmd.Env = upsertEnv(cmd.Env, "CGO_ENABLED", func(v string) string {
|
cmd.Env = shell.UpsertEnv(cmd.Env, "CGO_ENABLED", func(v string) string {
|
||||||
return "1"
|
return "1"
|
||||||
})
|
})
|
||||||
if options.Platform == "darwin" {
|
if options.Platform == "darwin" {
|
||||||
@ -338,7 +338,7 @@ func (b *BaseBuilder) CompileProject(options *Options) error {
|
|||||||
}
|
}
|
||||||
addUTIFramework := majorVersion >= 11
|
addUTIFramework := majorVersion >= 11
|
||||||
// Set the minimum Mac SDK to 10.13
|
// Set the minimum Mac SDK to 10.13
|
||||||
cmd.Env = upsertEnv(cmd.Env, "CGO_LDFLAGS", func(v string) string {
|
cmd.Env = shell.UpsertEnv(cmd.Env, "CGO_LDFLAGS", func(v string) string {
|
||||||
if v != "" {
|
if v != "" {
|
||||||
v += " "
|
v += " "
|
||||||
}
|
}
|
||||||
@ -352,11 +352,11 @@ func (b *BaseBuilder) CompileProject(options *Options) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Env = upsertEnv(cmd.Env, "GOOS", func(v string) string {
|
cmd.Env = shell.UpsertEnv(cmd.Env, "GOOS", func(v string) string {
|
||||||
return options.Platform
|
return options.Platform
|
||||||
})
|
})
|
||||||
|
|
||||||
cmd.Env = upsertEnv(cmd.Env, "GOARCH", func(v string) string {
|
cmd.Env = shell.UpsertEnv(cmd.Env, "GOARCH", func(v string) string {
|
||||||
return options.Arch
|
return options.Arch
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -608,22 +608,3 @@ func (b *BaseBuilder) BuildFrontend(outputLogger *clilogger.CLILogger) error {
|
|||||||
pterm.Println("Done.")
|
pterm.Println("Done.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func upsertEnv(env []string, key string, update func(v string) string) []string {
|
|
||||||
newEnv := make([]string, len(env), len(env)+1)
|
|
||||||
found := false
|
|
||||||
for i := range env {
|
|
||||||
if strings.HasPrefix(env[i], key+"=") {
|
|
||||||
eqIndex := strings.Index(env[i], "=")
|
|
||||||
val := env[i][eqIndex+1:]
|
|
||||||
newEnv[i] = fmt.Sprintf("%s=%v", key, update(val))
|
|
||||||
found = true
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
newEnv[i] = env[i]
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
newEnv = append(newEnv, fmt.Sprintf("%s=%v", key, update("")))
|
|
||||||
}
|
|
||||||
return newEnv
|
|
||||||
}
|
|
||||||
|
@ -2,40 +2,6 @@ package build
|
|||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestUpdateEnv(t *testing.T) {
|
|
||||||
|
|
||||||
env := []string{"one=1", "two=a=b", "three="}
|
|
||||||
newEnv := upsertEnv(env, "two", func(v string) string {
|
|
||||||
return v + "+added"
|
|
||||||
})
|
|
||||||
newEnv = upsertEnv(newEnv, "newVar", func(v string) string {
|
|
||||||
return "added"
|
|
||||||
})
|
|
||||||
newEnv = upsertEnv(newEnv, "three", func(v string) string {
|
|
||||||
return "3"
|
|
||||||
})
|
|
||||||
newEnv = upsertEnv(newEnv, "GOARCH", func(v string) string {
|
|
||||||
return "amd64"
|
|
||||||
})
|
|
||||||
|
|
||||||
if len(newEnv) != 5 {
|
|
||||||
t.Errorf("expected: 5, got: %d", len(newEnv))
|
|
||||||
}
|
|
||||||
if newEnv[1] != "two=a=b+added" {
|
|
||||||
t.Errorf("expected: \"two=a=b+added\", got: %q", newEnv[1])
|
|
||||||
}
|
|
||||||
if newEnv[2] != "three=3" {
|
|
||||||
t.Errorf("expected: \"three=3\", got: %q", newEnv[2])
|
|
||||||
}
|
|
||||||
if newEnv[3] != "newVar=added" {
|
|
||||||
t.Errorf("expected: \"newVar=added\", got: %q", newEnv[3])
|
|
||||||
}
|
|
||||||
if newEnv[4] != "GOARCH=amd64" {
|
|
||||||
t.Errorf("expected: \"newVar=added\", got: %q", newEnv[4])
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_commandPrettifier(t *testing.T) {
|
func Test_commandPrettifier(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
Loading…
Reference in New Issue
Block a user