diff --git a/README.md b/README.md index 4b57bfe..2a5ac05 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,16 @@ Usage: ag [short command] [...] Commands: - new [+] [name] create a new project, will create in the current directory if no name is specified - new plugin [+p] [name] create a new plugin project, will create in the current directory if no name is specified - run [r] [...more gowatch args] run project use gowatch, if project files changed will restart auto, gowatch args help see: https://github.com/ssgo/tool - test [t] [...more gowatch args] test project use gowatch, if project files changed will restart auto, gowatch args help see: https://github.com/ssgo/tool - export plugins [ep] export typescript code for used plugins into "plugins/" + init [i] init a new project for empty dir + init plugin [i p] init a new plugin project for empty dir + run [r] will exec `go run .` + watch run [rr] [...] run project use gowatch, if project files changed will restart auto, ... args see gowatch help https://github.com/ssgo/tool + test [t] will exec `go test -v .`, will exec into tests if exists tests dir + watch test [tt] [...] test project use gowatch, if project files changed will restart auto, ... args see gowatch help https://github.com/ssgo/tool + tidy [td] tidy project, find imported plugins from .js files add import code to jsImports.go, export typescript code for used plugins into "plugins/" + tags [] show git tags + 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 diff --git a/go.mod b/go.mod index b9ba630..3ff71af 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,14 @@ module apigo.cc/apigo/ag go 1.18 require ( - github.com/ssgo/httpclient v1.7.5 - github.com/ssgo/u v1.7.5 + github.com/ssgo/httpclient v1.7.6 + github.com/ssgo/u v1.7.6 ) require ( - github.com/ssgo/config v1.7.5 // indirect - github.com/ssgo/log v1.7.5 // indirect - github.com/ssgo/standard v1.7.5 // indirect + github.com/ssgo/config v1.7.6 // indirect + github.com/ssgo/log v1.7.6 // indirect + github.com/ssgo/standard v1.7.6 // indirect golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 // indirect golang.org/x/text v0.3.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/main.go b/main.go index 96dfa76..d77bdb4 100644 --- a/main.go +++ b/main.go @@ -47,12 +47,14 @@ var gitignoreTPL string //go:embed templates/_gitignore_server var gitignoreServerTPL string +var goPath = "go" + type Command struct { Name string ShortName string Args string Comment string - Func func([]string) + Func func([]string) bool } var commands = []Command{ @@ -71,7 +73,8 @@ var commands = []Command{ {"tidy", "td", "", "tidy project, find imported plugins from .js files add import code to jsImports.go, export typescript code for used plugins into \"plugins/\"", tidy}, {"tags", "", "", "show git tags", showGitTags}, {"commit", "co", "comment", "commit git repo and push, comment is need", commitGitRepo}, - {"tag+", "t+", "[version]", "add git tag push, if no new tag specified will use last tag +1", addGitTag}, + {"commit and +tag", "co+", "comment", "commit git repo and push, than update tag, comment is need", commitAndTagGitRepo}, + {"update tag", "t+", "[version]", "add git tag push, if no new tag specified will use last tag +1", addGitTag}, //{"build", "b", "[-m]", "build for current os, output to build/, -m will mix js files into exec file"}, //{"build mac", "bm", "", "build"}, //{"build macarm", "bma", "", "build"}, @@ -96,15 +99,24 @@ func init() { } } -func showGitTags(args []string) { - _ = runCommand("git", "tag", "-l", "v*", "--sort=-taggerdate", "--format=%(refname:short) %(taggerdate:short) %(*objectname:short)") +func showGitTags(args []string) bool { + return nil == runCommand("git", "tag", "-l", "v*", "--sort=-taggerdate", "--format=%(refname:short) %(taggerdate:short) %(*objectname:short)") } -func commitGitRepo(args []string) { +func commitAndTagGitRepo(args []string) bool { + if commitGitRepo(args) { + return addGitTag(args) + } + return false +} + +func commitGitRepo(args []string) bool { comment := strings.Join(args, " ") if comment != "" { if err := runCommand("git", "commit", "-a", "-m", comment); err == nil { - if err := runCommand("git", "push"); err != nil { + if err := runCommand("git", "push"); err == nil { + return true + } else { fmt.Println("git push failed:", err.Error()) } } else { @@ -113,9 +125,10 @@ func commitGitRepo(args []string) { } else { fmt.Println("commit message is empty") } + return false } -func addGitTag(args []string) { +func addGitTag(args []string) bool { newVer := "" if len(args) > 0 { newVer = args[0] @@ -155,6 +168,7 @@ func addGitTag(args []string) { fmt.Println("git add tag failed:", err.Error()) } } + return false } func findTool() (goWatchPath string, logVPath string) { @@ -166,20 +180,17 @@ func findTool() (goWatchPath string, logVPath string) { if binPath, err := exec.LookPath("logv"); err == nil && binPath != "" { logVPath = binPath } - return goWatchPath, logVPath -} -func checkSSGOTool() { - goWatchPath, logVPath := findTool() if goWatchPath == "gowatch" || logVPath == "logv" { - _ = runCommand("go", "get", "-u", "github.com/ssgo/tool") + _ = runCommand(goPath, "get", "-u", "github.com/ssgo/tool") if goWatchPath == "gowatch" { - _ = runCommand("go", "install", "github.com/ssgo/tool/gowatch") + _ = runCommand(goPath, "install", "github.com/ssgo/tool/gowatch") } if logVPath == "logv" { - _ = runCommand("go", "install", "github.com/ssgo/tool/logv") + _ = runCommand(goPath, "install", "github.com/ssgo/tool/logv") } } + return goWatchPath, logVPath } func checkProjectPath() string { @@ -322,7 +333,7 @@ func fetchRepo(name string) (repoPath string) { } } -func initProject(args []string) { +func initProject(args []string) bool { if name := checkProjectPath(); name != "" { projectOK := false if len(args) > 0 { @@ -344,16 +355,18 @@ func initProject(args []string) { writeFile("main.js", mainJSCodeTPL, map[string]any{"name": name}) } - _ = runCommand("go", "mod", "init", name) - _ = runCommand("go", "mod", "edit", "-go=1.18") - _ = runCommand("go", "get", "-u", "apigo.cc/apigo/gojs") - _ = runCommand("go", "get", "-u", "apigo.cc/apigo/plugins") + _ = runCommand(goPath, "mod", "init", name) + _ = runCommand(goPath, "mod", "edit", "-go=1.18") + _ = runCommand(goPath, "get", "-u", "apigo.cc/apigo/gojs") + _ = runCommand(goPath, "get", "-u", "apigo.cc/apigo/plugins") writeFile(".gitignore", gitignoreTPL, map[string]any{"name": name}) - _ = runCommand("go", "mod", "tidy") - checkSSGOTool() + _ = runCommand(goPath, "mod", "tidy") + findTool() fmt.Println(u.BGreen("new project " + name + " created")) + return true } + return false } func CopyFile(from, to string) error { @@ -389,59 +402,61 @@ func CopyFile(from, to string) error { } } -func initPluginProject(args []string) { +func initPluginProject(args []string) bool { if name := checkProjectPath(); name != "" { - _ = runCommand("go", "mod", "init", name) - _ = runCommand("go", "mod", "edit", "-go=1.18") - _ = runCommand("go", "get", "-u", "apigo.cc/apigo/plugin") + _ = runCommand(goPath, "mod", "init", name) + _ = runCommand(goPath, "mod", "edit", "-go=1.18") + _ = runCommand(goPath, "get", "-u", "apigo.cc/apigo/plugin") writeFile("plugin.go", pluginCodeTPL, map[string]any{"name": name}) writeFile(".gitignore", gitignoreTPL, map[string]any{"name": name}) - _ = runCommand("go", "mod", "tidy") + _ = runCommand(goPath, "mod", "tidy") _ = os.Mkdir("tests", 0755) _ = os.Chdir("tests") - _ = runCommand("go", "mod", "init", "tests") - _ = runCommand("go", "mod", "edit", "-go=1.18") - _ = runCommand("go", "mod", "edit", "-require=current-plugin@v0.0.0") - _ = runCommand("go", "mod", "edit", "-replace=current-plugin@v0.0.0=../") - _ = runCommand("go", "get", "-u", "apigo.cc/apigo/plugin") - _ = runCommand("go", "get", "-u", "apigo.cc/apigo/gojs") + _ = runCommand(goPath, "mod", "init", "tests") + _ = runCommand(goPath, "mod", "edit", "-go=1.18") + _ = runCommand(goPath, "mod", "edit", "-require=current-plugin@v0.0.0") + _ = runCommand(goPath, "mod", "edit", "-replace=current-plugin@v0.0.0=../") + _ = runCommand(goPath, "get", "-u", "apigo.cc/apigo/plugin") + _ = runCommand(goPath, "get", "-u", "apigo.cc/apigo/gojs") writeFile("plugin_test.go", pluginTestCodeTPL, map[string]any{"name": name}) writeFile("plugin_test.js", pluginTestJSCodeTPL, map[string]any{"name": name}) - _ = runCommand("go", "mod", "tidy") - checkSSGOTool() + _ = runCommand(goPath, "mod", "tidy") + findTool() fmt.Println(u.BGreen("new plugin " + name + " created")) + return true } + return false } -func _runProject(args []string, isWatch bool) { - _ = runCommand("go", "mod", "tidy") +func _runProject(args []string, isWatch bool) bool { + _ = runCommand(goPath, "mod", "tidy") goBinPath, logVPath := findTool() if isWatch { args = append(args, "-pt", ".go,.js,.yml", "run", ".") } else { - goBinPath = "go" + goBinPath = goPath args = append(args, "run", ".") } - _ = runCommandPipe(logVPath, goBinPath, args...) + return nil == runCommandPipe(logVPath, goBinPath, args...) } -func runProject(args []string) { - _runProject(args, false) +func runProject(args []string) bool { + return _runProject(args, false) } -func devProject(args []string) { - _runProject(args, true) +func devProject(args []string) bool { + return _runProject(args, true) } -func testProject(args []string) { - _testProject(args, false) +func testProject(args []string) bool { + return _testProject(args, false) } -func devTestProject(args []string) { - _testProject(args, true) +func devTestProject(args []string) bool { + return _testProject(args, true) } -func _testProject(args []string, isWatch bool) { +func _testProject(args []string, isWatch bool) bool { if u.FileExists("tests") { if u.FileExists(filepath.Join("tests", "go.mod")) { _ = os.Chdir("tests") @@ -469,14 +484,15 @@ func _testProject(args []string, isWatch bool) { } else { args = append(args, "test", "-v", ".") } - _ = runCommand("go", "mod", "tidy") + _ = runCommand(goPath, "mod", "tidy") goWatchPath, logVPath := findTool() if isWatch { args2 := append([]string{"-pt", ".go,.js,.yml"}, args...) _ = runCommandPipe(logVPath, goWatchPath, args2...) } else { - _ = runCommandPipe(logVPath, "go", args...) + _ = runCommandPipe(logVPath, goPath, args...) } + return true } var pkgMatcher = regexp.MustCompile(`(?im)^\s*(import)?\s*_\s+"([\w-_/.]+)"`) @@ -579,7 +595,7 @@ var replaceMatcher = regexp.MustCompile(`([a-zA-Z0-9._\-/]+)\s+(v[0-9.]+)\s+=>\s var modNameMatcher = regexp.MustCompile(`(?m)^module\s+([a-zA-Z0-9._\-/]+)$`) var pkgNameMatcher = regexp.MustCompile(`(?m)^package\s+([a-zA-Z0-9._\-/]+)$`) -func tidy(args []string) { +func tidy(args []string) bool { // 判断是否可执行项目(不创建指向项目本身的 import _) isMainProject := false isEmptyProject := true @@ -630,10 +646,11 @@ func tidy(args []string) { _ = os.Chdir("..") _ = os.RemoveAll("_makePluginCode") }() - _ = runCommand("go", "mod", "tidy") - if err := runCommand("go", "run", "."); err != nil { + _ = runCommand(goPath, "mod", "tidy") + if err := runCommand(goPath, "run", "."); err != nil { fmt.Println(u.Red(err.Error())) } + return true } func runCommand(name string, args ...string) error { @@ -641,7 +658,11 @@ func runCommand(name string, args ...string) error { cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - return cmd.Run() + err := cmd.Run() + if err != nil { + fmt.Println(u.Red(err.Error())) + } + return err } func runCommandPipe(pipeCommandName, commandName string, args ...string) error { @@ -661,25 +682,31 @@ func runCommandPipe(pipeCommandName, commandName string, args ...string) error { cmd2.Stdin = r cmd2.Stdout = os.Stdout cmd2.Stderr = os.Stderr - if err := cmd2.Start(); err != nil { - return err + var err error + if err = cmd2.Start(); err == nil { + if err = cmd1.Start(); err == nil { + if err = cmd1.Wait(); err == nil { + w.Close() + wClosed = true + // 等待第二个命令完成 + if err = cmd2.Wait(); err == nil { + return nil + } + } + } } - if err := cmd1.Start(); err != nil { - return err - } - if err := cmd1.Wait(); err != nil { - return err - } - w.Close() - wClosed = true - // 等待第二个命令完成 - if err := cmd2.Wait(); err != nil { - return err - } - return nil + fmt.Println(u.Red(err.Error())) + return err } func main() { + var err error + if goPath, err = exec.LookPath("go"); err != nil || goPath == "" { + fmt.Println(u.Red("Please install Go SDK first")) + fmt.Println(u.Cyan("https://go.dev/")) + return + } + if len(os.Args) > 1 { cmd1 := os.Args[1] cmd2 := cmd1 @@ -697,8 +724,8 @@ func main() { } } - fmt.Println("tools for apigo.cloud") - fmt.Println("version ", version) + fmt.Println("tools for apigo.cloud", version) + fmt.Println("go sdk", goPath) fmt.Println() fmt.Println("Usage:") fmt.Println(" ", u.Cyan("ag [command] [...]"))