fix install logv bug

This commit is contained in:
Star 2024-07-04 17:58:16 +08:00
parent 462e891a61
commit b32865f984
3 changed files with 112 additions and 80 deletions

View File

@ -16,11 +16,16 @@ Usage:
ag [short command] [...] ag [short command] [...]
Commands: Commands:
new [+] [name] create a new project, will create in the current directory if no name is specified init [i] init a new project for empty dir
new plugin [+p] [name] create a new plugin project, will create in the current directory if no name is specified init plugin [i p] init a new plugin project for empty dir
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 run [r] will exec `go run .`
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 watch run [rr] [...] run project use gowatch, if project files changed will restart auto, ... args see gowatch help https://github.com/ssgo/tool
export plugins [ep] export typescript code for used plugins into "plugins/" 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: Examples:
ag + create a new simple project with Hello World ag + create a new simple project with Hello World

10
go.mod
View File

@ -3,14 +3,14 @@ module apigo.cc/apigo/ag
go 1.18 go 1.18
require ( require (
github.com/ssgo/httpclient v1.7.5 github.com/ssgo/httpclient v1.7.6
github.com/ssgo/u v1.7.5 github.com/ssgo/u v1.7.6
) )
require ( require (
github.com/ssgo/config v1.7.5 // indirect github.com/ssgo/config v1.7.6 // indirect
github.com/ssgo/log v1.7.5 // indirect github.com/ssgo/log v1.7.6 // indirect
github.com/ssgo/standard v1.7.5 // indirect github.com/ssgo/standard v1.7.6 // indirect
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 // indirect golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 // indirect
golang.org/x/text v0.3.0 // indirect golang.org/x/text v0.3.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect

161
main.go
View File

@ -47,12 +47,14 @@ var gitignoreTPL string
//go:embed templates/_gitignore_server //go:embed templates/_gitignore_server
var gitignoreServerTPL string var gitignoreServerTPL string
var goPath = "go"
type Command struct { type Command struct {
Name string Name string
ShortName string ShortName string
Args string Args string
Comment string Comment string
Func func([]string) Func func([]string) bool
} }
var commands = []Command{ 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}, {"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}, {"tags", "", "", "show git tags", showGitTags},
{"commit", "co", "comment", "commit git repo and push, comment is need", commitGitRepo}, {"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", "b", "[-m]", "build for current os, output to build/, -m will mix js files into exec file"},
//{"build mac", "bm", "", "build"}, //{"build mac", "bm", "", "build"},
//{"build macarm", "bma", "", "build"}, //{"build macarm", "bma", "", "build"},
@ -96,15 +99,24 @@ func init() {
} }
} }
func showGitTags(args []string) { func showGitTags(args []string) bool {
_ = runCommand("git", "tag", "-l", "v*", "--sort=-taggerdate", "--format=%(refname:short) %(taggerdate:short) %(*objectname:short)") 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, " ") comment := strings.Join(args, " ")
if comment != "" { if comment != "" {
if err := runCommand("git", "commit", "-a", "-m", comment); err == nil { 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()) fmt.Println("git push failed:", err.Error())
} }
} else { } else {
@ -113,9 +125,10 @@ func commitGitRepo(args []string) {
} else { } else {
fmt.Println("commit message is empty") fmt.Println("commit message is empty")
} }
return false
} }
func addGitTag(args []string) { func addGitTag(args []string) bool {
newVer := "" newVer := ""
if len(args) > 0 { if len(args) > 0 {
newVer = args[0] newVer = args[0]
@ -155,6 +168,7 @@ func addGitTag(args []string) {
fmt.Println("git add tag failed:", err.Error()) fmt.Println("git add tag failed:", err.Error())
} }
} }
return false
} }
func findTool() (goWatchPath string, logVPath string) { 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 != "" { if binPath, err := exec.LookPath("logv"); err == nil && binPath != "" {
logVPath = binPath logVPath = binPath
} }
return goWatchPath, logVPath
}
func checkSSGOTool() {
goWatchPath, logVPath := findTool()
if goWatchPath == "gowatch" || logVPath == "logv" { if goWatchPath == "gowatch" || logVPath == "logv" {
_ = runCommand("go", "get", "-u", "github.com/ssgo/tool") _ = runCommand(goPath, "get", "-u", "github.com/ssgo/tool")
if goWatchPath == "gowatch" { if goWatchPath == "gowatch" {
_ = runCommand("go", "install", "github.com/ssgo/tool/gowatch") _ = runCommand(goPath, "install", "github.com/ssgo/tool/gowatch")
} }
if logVPath == "logv" { 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 { 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 != "" { if name := checkProjectPath(); name != "" {
projectOK := false projectOK := false
if len(args) > 0 { if len(args) > 0 {
@ -344,16 +355,18 @@ func initProject(args []string) {
writeFile("main.js", mainJSCodeTPL, map[string]any{"name": name}) writeFile("main.js", mainJSCodeTPL, map[string]any{"name": name})
} }
_ = runCommand("go", "mod", "init", name) _ = runCommand(goPath, "mod", "init", name)
_ = runCommand("go", "mod", "edit", "-go=1.18") _ = runCommand(goPath, "mod", "edit", "-go=1.18")
_ = runCommand("go", "get", "-u", "apigo.cc/apigo/gojs") _ = runCommand(goPath, "get", "-u", "apigo.cc/apigo/gojs")
_ = runCommand("go", "get", "-u", "apigo.cc/apigo/plugins") _ = runCommand(goPath, "get", "-u", "apigo.cc/apigo/plugins")
writeFile(".gitignore", gitignoreTPL, map[string]any{"name": name}) writeFile(".gitignore", gitignoreTPL, map[string]any{"name": name})
_ = runCommand("go", "mod", "tidy") _ = runCommand(goPath, "mod", "tidy")
checkSSGOTool() findTool()
fmt.Println(u.BGreen("new project " + name + " created")) fmt.Println(u.BGreen("new project " + name + " created"))
return true
} }
return false
} }
func CopyFile(from, to string) error { 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 != "" { if name := checkProjectPath(); name != "" {
_ = runCommand("go", "mod", "init", name) _ = runCommand(goPath, "mod", "init", name)
_ = runCommand("go", "mod", "edit", "-go=1.18") _ = runCommand(goPath, "mod", "edit", "-go=1.18")
_ = runCommand("go", "get", "-u", "apigo.cc/apigo/plugin") _ = runCommand(goPath, "get", "-u", "apigo.cc/apigo/plugin")
writeFile("plugin.go", pluginCodeTPL, map[string]any{"name": name}) writeFile("plugin.go", pluginCodeTPL, map[string]any{"name": name})
writeFile(".gitignore", gitignoreTPL, 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.Mkdir("tests", 0755)
_ = os.Chdir("tests") _ = os.Chdir("tests")
_ = runCommand("go", "mod", "init", "tests") _ = runCommand(goPath, "mod", "init", "tests")
_ = runCommand("go", "mod", "edit", "-go=1.18") _ = runCommand(goPath, "mod", "edit", "-go=1.18")
_ = runCommand("go", "mod", "edit", "-require=current-plugin@v0.0.0") _ = runCommand(goPath, "mod", "edit", "-require=current-plugin@v0.0.0")
_ = runCommand("go", "mod", "edit", "-replace=current-plugin@v0.0.0=../") _ = runCommand(goPath, "mod", "edit", "-replace=current-plugin@v0.0.0=../")
_ = runCommand("go", "get", "-u", "apigo.cc/apigo/plugin") _ = runCommand(goPath, "get", "-u", "apigo.cc/apigo/plugin")
_ = runCommand("go", "get", "-u", "apigo.cc/apigo/gojs") _ = runCommand(goPath, "get", "-u", "apigo.cc/apigo/gojs")
writeFile("plugin_test.go", pluginTestCodeTPL, map[string]any{"name": name}) writeFile("plugin_test.go", pluginTestCodeTPL, map[string]any{"name": name})
writeFile("plugin_test.js", pluginTestJSCodeTPL, map[string]any{"name": name}) writeFile("plugin_test.js", pluginTestJSCodeTPL, map[string]any{"name": name})
_ = runCommand("go", "mod", "tidy") _ = runCommand(goPath, "mod", "tidy")
checkSSGOTool() findTool()
fmt.Println(u.BGreen("new plugin " + name + " created")) fmt.Println(u.BGreen("new plugin " + name + " created"))
return true
} }
return false
} }
func _runProject(args []string, isWatch bool) { func _runProject(args []string, isWatch bool) bool {
_ = runCommand("go", "mod", "tidy") _ = runCommand(goPath, "mod", "tidy")
goBinPath, logVPath := findTool() goBinPath, logVPath := findTool()
if isWatch { if isWatch {
args = append(args, "-pt", ".go,.js,.yml", "run", ".") args = append(args, "-pt", ".go,.js,.yml", "run", ".")
} else { } else {
goBinPath = "go" goBinPath = goPath
args = append(args, "run", ".") args = append(args, "run", ".")
} }
_ = runCommandPipe(logVPath, goBinPath, args...) return nil == runCommandPipe(logVPath, goBinPath, args...)
} }
func runProject(args []string) { func runProject(args []string) bool {
_runProject(args, false) return _runProject(args, false)
} }
func devProject(args []string) { func devProject(args []string) bool {
_runProject(args, true) return _runProject(args, true)
} }
func testProject(args []string) { func testProject(args []string) bool {
_testProject(args, false) return _testProject(args, false)
} }
func devTestProject(args []string) { func devTestProject(args []string) bool {
_testProject(args, true) return _testProject(args, true)
} }
func _testProject(args []string, isWatch bool) { func _testProject(args []string, isWatch bool) bool {
if u.FileExists("tests") { if u.FileExists("tests") {
if u.FileExists(filepath.Join("tests", "go.mod")) { if u.FileExists(filepath.Join("tests", "go.mod")) {
_ = os.Chdir("tests") _ = os.Chdir("tests")
@ -469,14 +484,15 @@ func _testProject(args []string, isWatch bool) {
} else { } else {
args = append(args, "test", "-v", ".") args = append(args, "test", "-v", ".")
} }
_ = runCommand("go", "mod", "tidy") _ = runCommand(goPath, "mod", "tidy")
goWatchPath, logVPath := findTool() goWatchPath, logVPath := findTool()
if isWatch { if isWatch {
args2 := append([]string{"-pt", ".go,.js,.yml"}, args...) args2 := append([]string{"-pt", ".go,.js,.yml"}, args...)
_ = runCommandPipe(logVPath, goWatchPath, args2...) _ = runCommandPipe(logVPath, goWatchPath, args2...)
} else { } else {
_ = runCommandPipe(logVPath, "go", args...) _ = runCommandPipe(logVPath, goPath, args...)
} }
return true
} }
var pkgMatcher = regexp.MustCompile(`(?im)^\s*(import)?\s*_\s+"([\w-_/.]+)"`) 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 modNameMatcher = regexp.MustCompile(`(?m)^module\s+([a-zA-Z0-9._\-/]+)$`)
var pkgNameMatcher = regexp.MustCompile(`(?m)^package\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 _ // 判断是否可执行项目(不创建指向项目本身的 import _
isMainProject := false isMainProject := false
isEmptyProject := true isEmptyProject := true
@ -630,10 +646,11 @@ func tidy(args []string) {
_ = os.Chdir("..") _ = os.Chdir("..")
_ = os.RemoveAll("_makePluginCode") _ = os.RemoveAll("_makePluginCode")
}() }()
_ = runCommand("go", "mod", "tidy") _ = runCommand(goPath, "mod", "tidy")
if err := runCommand("go", "run", "."); err != nil { if err := runCommand(goPath, "run", "."); err != nil {
fmt.Println(u.Red(err.Error())) fmt.Println(u.Red(err.Error()))
} }
return true
} }
func runCommand(name string, args ...string) error { func runCommand(name string, args ...string) error {
@ -641,7 +658,11 @@ func runCommand(name string, args ...string) error {
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr 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 { func runCommandPipe(pipeCommandName, commandName string, args ...string) error {
@ -661,25 +682,31 @@ func runCommandPipe(pipeCommandName, commandName string, args ...string) error {
cmd2.Stdin = r cmd2.Stdin = r
cmd2.Stdout = os.Stdout cmd2.Stdout = os.Stdout
cmd2.Stderr = os.Stderr cmd2.Stderr = os.Stderr
if err := cmd2.Start(); err != nil { var err error
return err if err = cmd2.Start(); err == nil {
} if err = cmd1.Start(); err == nil {
if err := cmd1.Start(); err != nil { if err = cmd1.Wait(); err == nil {
return err
}
if err := cmd1.Wait(); err != nil {
return err
}
w.Close() w.Close()
wClosed = true wClosed = true
// 等待第二个命令完成 // 等待第二个命令完成
if err := cmd2.Wait(); err != nil { if err = cmd2.Wait(); err == nil {
return err
}
return nil return nil
}
}
}
}
fmt.Println(u.Red(err.Error()))
return err
} }
func main() { 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 { if len(os.Args) > 1 {
cmd1 := os.Args[1] cmd1 := os.Args[1]
cmd2 := cmd1 cmd2 := cmd1
@ -697,8 +724,8 @@ func main() {
} }
} }
fmt.Println("tools for apigo.cloud") fmt.Println("tools for apigo.cloud", version)
fmt.Println("version ", version) fmt.Println("go sdk", goPath)
fmt.Println() fmt.Println()
fmt.Println("Usage:") fmt.Println("Usage:")
fmt.Println(" ", u.Cyan("ag [command] [...]")) fmt.Println(" ", u.Cyan("ag [command] [...]"))