From 1ad0207498b2fed8216e7a01d5b9db3700ab2415 Mon Sep 17 00:00:00 2001 From: "STARAI\\Star" Date: Mon, 9 Sep 2024 00:00:29 +0800 Subject: [PATCH] fix for windows --- README.md | 3 --- main.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2a5ac05..e798a80 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,5 @@ Commands: commit [co] comment commit git repo and push, comment is need tag+ [t+] [version] add git tag push, if no new tag specified will use last tag +1 -Examples: - ag + create a new simple project with Hello World - ag +p ali create a new plugin project named ali for use aliyun services ``` diff --git a/main.go b/main.go index a944a6f..09ff1d8 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "os/exec" "path/filepath" "regexp" + "runtime" "strconv" "strings" "text/template" @@ -48,6 +49,8 @@ var gitignoreServerTPL string var goPath = "go" +var goRunEnv = []string{"CGO_ENABLED=1"} + type Command struct { Name string ShortName string @@ -96,6 +99,25 @@ func init() { if homePath, err := os.UserHomeDir(); err == nil { cachePath = filepath.Join(homePath, ".ag", "cache") } + if runtime.GOOS == "windows" { + if u.FileExists("D:\\TDM-GCC-64\\bin\\gcc.exe") { + goRunEnv = append(goRunEnv, "CC=D:\\TDM-GCC-64\\bin\\gcc.exe") + goRunEnv = append(goRunEnv, "CXX=D:\\TDM-GCC-64\\bin\\g++.exe") + } else if u.FileExists("C:\\TDM-GCC-64\\bin\\gcc.exe") { + goRunEnv = append(goRunEnv, "CC=C:\\TDM-GCC-64\\bin\\gcc.exe") + goRunEnv = append(goRunEnv, "CXX=C:\\TDM-GCC-64\\bin\\g++.exe") + } else if binPath, err := exec.LookPath("gcc"); err != nil || binPath == "" { + fmt.Println("gcc is not found, please install TMD-GCC by https://jmeubank.github.io/tdm-gcc/") + fmt.Println("run \"pacman -S mingw-w64-ucrt-x86_64-gcc\" on msys2") + fmt.Println("if not installed at D:\\msys64 or D:\\msys64, please add \"***\\TDM-GCC-64\\bin\" to Environment Variables") + } + envs, _ := u.RunCommand("go", "env") + for _, line := range envs { + if strings.HasPrefix(line, "set GO") && !strings.HasPrefix(line, "set GOTMPDIR") && !strings.HasPrefix(line, "set GOWORK") { + goRunEnv = append(goRunEnv, line[4:]) + } + } + } } func showGitTags(args []string) bool { @@ -436,7 +458,7 @@ func _runProject(args []string, isWatch bool) bool { goBinPath = goPath args = append(args, "run", ".") } - return nil == runCommandPipe(logVPath, goBinPath, args...) + return nil == runCommandPipeWithEnv(logVPath, goBinPath, goRunEnv, args...) } func runProject(args []string) bool { @@ -487,9 +509,9 @@ func _testProject(args []string, isWatch bool) bool { goWatchPath, logVPath := findTool() if isWatch { args2 := append([]string{"-pt", ".go,.js,.yml"}, args...) - _ = runCommandPipe(logVPath, goWatchPath, args2...) + _ = runCommandPipeWithEnv(logVPath, goWatchPath, goRunEnv, args2...) } else { - _ = runCommandPipe(logVPath, goPath, args...) + _ = runCommandPipeWithEnv(logVPath, goPath, goRunEnv, args...) } return true } @@ -646,22 +668,35 @@ func tidy(args []string) bool { imports = append(imports, currentModuleName) } - _ = u.WriteFile("_makePluginCode/go.mod", goModCode) - writeFile("_makePluginCode/main.go", makePluginCodeTPL, map[string]any{"imports": imports}) + _ = u.WriteFile(filepath.Join("_makePluginCode", "go.mod"), goModCode) + writeFile(filepath.Join("_makePluginCode", "main.go"), makePluginCodeTPL, map[string]any{"imports": imports}) _ = os.Chdir("_makePluginCode") defer func() { _ = os.Chdir("..") - _ = os.RemoveAll("_makePluginCode") + //_ = os.RemoveAll("_makePluginCode") }() _ = runCommand(goPath, "mod", "tidy") - if err := runCommand(goPath, "run", "."); err != nil { + if err := runCommandWithEnv(goPath, goRunEnv, "run", "."); err != nil { fmt.Println(u.Red(err.Error())) } return true } func runCommand(name string, args ...string) error { + return runCommandWithEnv(name, nil, args...) +} + +func runCommandWithEnv(name string, env []string, args ...string) error { + pathname, _ := os.Getwd() + fmt.Println(u.BMagenta(pathname), u.BCyan(name), u.Cyan(strings.Join(args, " "))) cmd := exec.Command(name, args...) + if env != nil { + if runtime.GOOS == "windows" { + env = append(env, "GOTMPDIR="+pathname, "GOWORK="+pathname) + } + cmd.Env = append(cmd.Env, env...) + } + cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr @@ -673,8 +708,21 @@ func runCommand(name string, args ...string) error { } func runCommandPipe(pipeCommandName, commandName string, args ...string) error { + return runCommandPipeWithEnv(pipeCommandName, commandName, nil, args...) +} + +func runCommandPipeWithEnv(pipeCommandName, commandName string, env []string, args ...string) error { + pathname, _ := os.Getwd() + fmt.Println(u.BMagenta(pathname), u.BCyan(commandName), u.Cyan(strings.Join(args, " ")), u.BMagenta(pipeCommandName)) cmd1 := exec.Command(commandName, args...) cmd2 := exec.Command(pipeCommandName) + if env != nil { + if runtime.GOOS == "windows" { + env = append(env, "GOTMPDIR="+pathname, "GOWORK="+pathname) + } + cmd1.Env = append(cmd1.Env, env...) + cmd2.Env = append(cmd2.Env, env...) + } r, w := io.Pipe() wClosed := false @@ -722,7 +770,8 @@ func main() { } for i := len(commands) - 1; i >= 0; i-- { cmdInfo := commands[i] - if cmd2 == cmdInfo.Name || cmd2 == cmdInfo.ShortName { + //fmt.Println(">>>>>>", cmd1, cmdInfo.Name, "|", cmd2, cmdInfo.ShortName) + if len(os.Args) > 2 && (cmd2 == cmdInfo.Name || cmd2 == cmdInfo.ShortName) { cmdInfo.Func(os.Args[3:]) return } else if cmd1 == cmdInfo.Name || cmd1 == cmdInfo.ShortName {