update run、dev、test、.... for ag tool

This commit is contained in:
Star 2024-03-24 12:40:01 +08:00
parent e5f5626d46
commit a3ebcbb695
9 changed files with 93 additions and 54 deletions

7
go.mod
View File

@ -2,9 +2,6 @@ module apigo.cloud/git/apigo/ag
go 1.18 go 1.18
require github.com/ssgo/u v1.7.2 require github.com/ssgo/u v1.7.3
require ( require gopkg.in/yaml.v3 v3.0.1 // indirect
apigo.cloud/git/apigo/plugin v1.0.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

74
main.go
View File

@ -56,8 +56,10 @@ var commands = []Command{
//{"new api", "+a", "[path] [method]", "create a new api for server project, will use restful api if specified http method", newServerAPI}, //{"new api", "+a", "[path] [method]", "create a new api for server project, will use restful api if specified http method", newServerAPI},
//{"new game", "+g", "[name]", "create a new game project, will create in the current directory if no name is specified", newServerProject}, //{"new game", "+g", "[name]", "create a new game project, will create in the current directory if no name is specified", newServerProject},
//{"new webkit", "+w", "[name]", "create a new webkit project, will create in the current directory if no name is specified", newServerProject}, //{"new webkit", "+w", "[name]", "create a new webkit project, will create in the current directory if no name is specified", newServerProject},
{"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", runProject}, //{"download", "dl", "[plugin name]", "will fetch plugin into project", downloadPlugin},
{"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", testProject}, {"run", "r", "", "will exec `go run .`", runProject},
{"test", "t", "", "will exec `go test -v .`, will exec into tests if exists tests dir", testProject},
{"dev", "d", "[...more gowatch args]", "run project use gowatch, if project files changed will restart auto, gowatch args help see: https://github.com/ssgo/tool", devProject},
{"export plugins", "ep", "", "export typescript code for used plugins into \"plugins/\"", makePluginCode}, {"export plugins", "ep", "", "export typescript code for used plugins into \"plugins/\"", makePluginCode},
//{"git login", "/lg", "", "login to apigo.cloud/git", }, //{"git login", "/lg", "", "login to apigo.cloud/git", },
//{"git new", "/+", "name", "create new public repository from apigo.cloud/git", }, //{"git new", "/+", "name", "create new public repository from apigo.cloud/git", },
@ -74,7 +76,9 @@ var commands = []Command{
//{"build linux", "bl", "", "build", }, //{"build linux", "bl", "", "build", },
//{"build linuxarm", "bla", "", "build", }, //{"build linuxarm", "bla", "", "build", },
//{"build all", "ba", "", "build", }, //{"build all", "ba", "", "build", },
//{"deploy", "d", "", "deploy", }, //{"publish", "p", "", "publish project to target server", },
// TODO 从 apigo.cloud/git 中读取信息增加命令例如server
// TODO 从 plugins 中读取信息增加命令例如dao
} }
func findTool() (gowatchPath string, logvPath string) { func findTool() (gowatchPath string, logvPath string) {
@ -170,21 +174,32 @@ func newServerAPI(args []string) {
} }
func runProject(args []string) { func runProject(args []string) {
gowatchPath, logvPath := findTool()
_ = runCommand("go", "mod", "tidy") _ = runCommand("go", "mod", "tidy")
_ = runCommandPipe(logvPath, gowatchPath, "-pt", ".go,.yml,.js", "run", ".") _, logvPath := findTool()
//_ = runCommandPipe(logvPath, gowatchPath, "-pt", ".go,.yml,.js", "-ig", "api", "run", ".") _ = runCommandPipe(logvPath, "go", "run", ".")
} }
func testProject(args []string) { func testProject(args []string) {
gowatchPath, logvPath := findTool() cmdArgs := make([]string, 0)
if u.FileExists("tests") { if u.FileExists("tests") {
_ = os.Chdir("tests") if u.FileExists(filepath.Join("tests", "go.mod")) {
_ = runCommand("go", "mod", "tidy") _ = os.Chdir("tests")
_ = runCommandPipe(logvPath, gowatchPath, "-p", ".,..", "-pt", ".go,.yml,.js", "test", "-v", ".") cmdArgs = append(cmdArgs, "test", "-v", ".")
} else {
cmdArgs = append(cmdArgs, "test", "-v", "tests")
}
} else { } else {
_ = runCommandPipe(logvPath, gowatchPath, "-pt", ".go,.yml,.js", "test", "-v", ".") cmdArgs = append(cmdArgs, "test", "-v", ".")
} }
_ = runCommand("go", "mod", "tidy")
_, logvPath := findTool()
_ = runCommandPipe(logvPath, "go", cmdArgs...)
}
func devProject(args []string) {
gowatchPath, logvPath := findTool()
_ = runCommand("go", "mod", "tidy")
_ = runCommandPipe(logvPath, gowatchPath, "-p", ".,./main.js", "run", ".")
} }
var pkgMatcher = regexp.MustCompile(`(?m)^\s*_\s+"([\w-_/.]+)"`) var pkgMatcher = regexp.MustCompile(`(?m)^\s*_\s+"([\w-_/.]+)"`)
@ -194,11 +209,11 @@ func makePluginCodeImports(from string, imports *[]string) {
for _, f := range files { for _, f := range files {
if f.IsDir() { if f.IsDir() {
if !strings.HasPrefix(f.Name(), ".") { if !strings.HasPrefix(f.Name(), ".") {
makePluginCodeImports(path.Join(from, f.Name()), imports) makePluginCodeImports(filepath.Join(from, f.Name()), imports)
} }
} else { } else {
if strings.HasSuffix(f.Name(), ".go") && !strings.HasPrefix(f.Name(), ".") && f.Name() != "makePluginCode.go" { if strings.HasSuffix(f.Name(), ".go") && !strings.HasPrefix(f.Name(), ".") && f.Name() != "makePluginCode.go" {
if code, err := u.ReadFile(path.Join(from, f.Name())); err == nil { if code, err := u.ReadFile(filepath.Join(from, f.Name())); err == nil {
for _, m := range pkgMatcher.FindAllStringSubmatch(code, 1024) { for _, m := range pkgMatcher.FindAllStringSubmatch(code, 1024) {
if m[1] != "current-plugin" { if m[1] != "current-plugin" {
*imports = u.AppendUniqueString(*imports, m[1]) *imports = u.AppendUniqueString(*imports, m[1])
@ -224,13 +239,16 @@ func writeFile(filename string, fileContent string, data any) {
} }
var replaceMatcher = regexp.MustCompile(`([a-zA-Z0-9._\-/]+)\s+(v[0-9.]+)\s+=>\s+([a-zA-Z0-9._\-/\\]+)`) var replaceMatcher = regexp.MustCompile(`([a-zA-Z0-9._\-/]+)\s+(v[0-9.]+)\s+=>\s+([a-zA-Z0-9._\-/\\]+)`)
var modNameMatcher = regexp.MustCompile(`(?m)^module\s+([a-zA-Z0-9._\-/]+)$`)
func makePluginCode(args []string) { func makePluginCode(args []string) {
// 判断是否可执行项目(不创建指向项目本身的 import _ // 判断是否可执行项目(不创建指向项目本身的 import _
isMainProject := false isMainProject := false
isEmptyProject := true
if files, err := os.ReadDir("."); err == nil { if files, err := os.ReadDir("."); err == nil {
for _, f := range files { for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".go") { if !f.IsDir() && strings.HasSuffix(f.Name(), ".go") {
isEmptyProject = false
code, _ := u.ReadFile(f.Name()) code, _ := u.ReadFile(f.Name())
if strings.Contains(code, "package main") || strings.Contains(code, "func main(") { if strings.Contains(code, "package main") || strings.Contains(code, "func main(") {
isMainProject = true isMainProject = true
@ -244,14 +262,17 @@ func makePluginCode(args []string) {
imports := make([]string, 0) imports := make([]string, 0)
makePluginCodeImports(".", &imports) makePluginCodeImports(".", &imports)
findGoModCode, _ := u.ReadFile("go.mod")
goModCode := "module main\ngo 1.18\n" goModCode := "module main\ngo 1.18\n"
currentModuleName := "current-project"
if !isMainProject { if !isMainProject {
imports = append(imports, "current-project") if m := modNameMatcher.FindStringSubmatch(findGoModCode); m != nil {
goModCode += "require current-project v0.0.0 // indirect\nreplace current-project v0.0.0 => ../\n" currentModuleName = m[1]
}
goModCode += "require " + currentModuleName + " v0.0.0 // indirect\nreplace " + currentModuleName + " v0.0.0 => ../\n"
} }
// 扫描 replace处理路径后加入到 _makePluginCode/go.mod // 扫描 replace处理路径后加入到 _makePluginCode/go.mod
findGoModCode, _ := u.ReadFile("go.mod")
for _, m := range replaceMatcher.FindAllStringSubmatch(findGoModCode, 100) { for _, m := range replaceMatcher.FindAllStringSubmatch(findGoModCode, 100) {
replacePath := m[3] replacePath := m[3]
if absPath, err := filepath.Abs(m[3]); err == nil { if absPath, err := filepath.Abs(m[3]); err == nil {
@ -260,19 +281,26 @@ func makePluginCode(args []string) {
goModCode += fmt.Sprintln("replace", m[1], m[2], "=>", replacePath) goModCode += fmt.Sprintln("replace", m[1], m[2], "=>", replacePath)
} }
if !isMainProject && !isEmptyProject {
imports = append(imports, currentModuleName)
}
_ = u.WriteFile("_makePluginCode/go.mod", goModCode) _ = u.WriteFile("_makePluginCode/go.mod", goModCode)
writeFile("_makePluginCode/main.go", makePluginCodeTPL, map[string]any{"imports": imports}) writeFile("_makePluginCode/main.go", makePluginCodeTPL, map[string]any{"imports": imports})
_ = os.Chdir("_makePluginCode") _ = os.Chdir("_makePluginCode")
defer func() {
_ = os.Chdir("..")
_ = os.RemoveAll("_makePluginCode")
}()
_ = runCommand("go", "mod", "tidy") _ = runCommand("go", "mod", "tidy")
if err := runCommand("go", "run", "."); err != nil { if err := runCommand("go", "run", "."); err != nil {
fmt.Println(u.Red(err.Error())) fmt.Println(u.Red(err.Error()))
} }
_ = os.Chdir("..")
_ = os.RemoveAll("_makePluginCode")
} }
func runCommand(name string, args ...string) error { func runCommand(name string, args ...string) error {
cmd := exec.Command(name, args...) cmd := exec.Command(name, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
return cmd.Run() return cmd.Run()
@ -283,7 +311,13 @@ func runCommandPipe(pipeCommandName, commandName string, args ...string) error {
cmd2 := exec.Command(pipeCommandName) cmd2 := exec.Command(pipeCommandName)
r, w := io.Pipe() r, w := io.Pipe()
defer w.Close() wClosed := false
defer func() {
if !wClosed {
w.Close()
}
}()
cmd1.Stdin = os.Stdin
cmd1.Stdout = w cmd1.Stdout = w
cmd1.Stderr = w cmd1.Stderr = w
cmd2.Stdin = r cmd2.Stdin = r
@ -298,6 +332,8 @@ func runCommandPipe(pipeCommandName, commandName string, args ...string) error {
if err := cmd1.Wait(); err != nil { if err := cmd1.Wait(); err != nil {
return err return err
} }
w.Close()
wClosed = true
// 等待第二个命令完成 // 等待第二个命令完成
if err := cmd2.Wait(); err != nil { if err := cmd2.Wait(); err != nil {
return err return err

View File

@ -1,3 +1,4 @@
.* .*
!.gitignore !.gitignore
/go.sum go.sum
/build

View File

@ -1,7 +1,8 @@
.* .*
!.gitignore !.gitignore
/go.sum go.sum
env.yml
env.json
/www /www
/server /server
/env.yml /build
/env.json

View File

@ -2,19 +2,8 @@ package main
import ( import (
"apigo.cloud/git/apigo/gojs" "apigo.cloud/git/apigo/gojs"
//_ "apigo.cloud/git/apigo/plugins/file"
) )
func main() { func main() {
worldName := "" gojs.RunFile("main.js", nil)
gojs.RunFile("main.js", &gojs.RuntimeOption{
Globals: map[string]interface{}{
"setWorldName": func(name string) {
worldName = name
},
"getWorldName": func() string {
return worldName
},
},
})
} }

View File

@ -1,9 +1,4 @@
import console from '_/console' import console from 'console'
import logger from '_/logger'
import file from '_/apigo.cloud/git/apigo/plguins/file'
// console.info(file.read('test.txt'))
setWorldName('gojs') let name = console.input("please enter your name: ")
logger.info('Hello '+getWorldName()) console.log("hello "+name)
// return 'OK'

View File

@ -7,7 +7,7 @@ import (
) )
func TestPlusNumber(t *testing.T) { func TestPlusNumber(t *testing.T) {
r, err, _ := gojs.Run("return 1+2", nil) r, err := gojs.Run("return 1+2", nil)
if err != nil || u.Int(r) != 3 { if err != nil || u.Int(r) != 3 {
t.Fatal("TestPlusNumber failed", r, err) t.Fatal("TestPlusNumber failed", r, err)
} }

View File

@ -5,7 +5,6 @@ import (
"apigo.cloud/git/apigo/plugin" "apigo.cloud/git/apigo/plugin"
"fmt" "fmt"
"github.com/ssgo/u" "github.com/ssgo/u"
"path"
"strings" "strings"
{{- range.imports}} {{- range.imports}}
_ "{{.}}" _ "{{.}}"
@ -29,12 +28,13 @@ const loggerModuleCode = `export default {
func writeModule(id, name, code string, modules *[]string, imports *[]string) { func writeModule(id, name, code string, modules *[]string, imports *[]string) {
idParts := strings.Split(id, "/") idParts := strings.Split(id, "/")
filename := path.Join("node_modules", path.Join(idParts...)+u.StringIf(len(idParts) == 1, "/index.ts", ".ts")) filename := filepath.Join("node_modules", filepath.Join(idParts...)+u.StringIf(len(idParts) == 1, "/index.ts", ".ts"))
if err := u.WriteFile("../"+filename, code); err != nil { if err := u.WriteFile("../"+filename, code); err != nil {
fmt.Println(u.Cyan(id), "-", name, u.BRed(err.Error())) fmt.Println(u.Cyan(id), "-", name, u.BRed(err.Error()))
} else { } else {
fmt.Println(u.Cyan(id), "-", name, u.BGreen("OK"), u.Dim(">> "+filename)) fmt.Println(u.Cyan(id), "-", name, u.BGreen("OK"), u.Dim(">> "+filename))
(*modules) = u.AppendUniqueString(*modules, fmt.Sprint("\"", idParts[0], "\": \"v0.0.0\"")) //(*modules) = u.AppendUniqueString(*modules, fmt.Sprint("\"", idParts[0], "\": \"v0.0.0\""))
(*modules) = u.AppendUniqueString(*modules, idParts[0])
(*imports) = u.AppendUniqueString(*imports, fmt.Sprint("import ", idParts[len(idParts)-1], " from '"+id+"'")) (*imports) = u.AppendUniqueString(*imports, fmt.Sprint("import ", idParts[len(idParts)-1], " from '"+id+"'"))
} }
} }
@ -49,7 +49,27 @@ func main() {
writeModule(plg.Id, plg.Name, code, &modules, &imports) writeModule(plg.Id, plg.Name, code, &modules, &imports)
} }
} }
_ = u.WriteFile("../package.json", "{\n \"devDependencies\": {\n "+strings.Join(modules, ",\n ")+"\n }\n}") packageJsonFile := filepath.Join("..", "package.json")
oldPackageJson, _ := u.ReadFile(packageJsonFile)
insertModulesPostfix := ""
if oldPackageJson == "" {
oldPackageJson = "{\n \"devDependencies\": {\n\n }\n}"
} else if !strings.Contains(oldPackageJson, "\"devDependencies\": {") {
oldPackageJson = strings.Replace(oldPackageJson, "{", "{\n \"devDependencies\": {\n\n },", 1)
} else {
insertModulesPostfix = ","
}
insertModules := make([]string, 0)
for _, mod := range modules {
if !strings.Contains(oldPackageJson, "\""+mod+"\":") {
insertModules = u.AppendUniqueString(insertModules, fmt.Sprint("\"", mod, "\": \"v0.0.0\""))
}
}
if len(insertModules) > 0 {
newPackageJson := strings.Replace(oldPackageJson, "\"devDependencies\": {", "\"devDependencies\": {\n "+strings.Join(insertModules, ",\n ")+insertModulesPostfix, 1)
//_ = u.WriteFile("../package.json", "{\n \"devDependencies\": {\n "+strings.Join(modules, ",\n ")+"\n }\n}")
_ = u.WriteFile("../package.json", newPackageJson)
}
fmt.Println() fmt.Println()
fmt.Println("Usage:") fmt.Println("Usage:")
fmt.Println(" " + u.Magenta(strings.Join(imports, "\n "))) fmt.Println(" " + u.Magenta(strings.Join(imports, "\n ")))

View File

@ -15,7 +15,7 @@ func TestPlugin(t *testing.T) {
for _, f := range files { for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), "_test.js") { if !f.IsDir() && strings.HasSuffix(f.Name(), "_test.js") {
testName := f.Name()[0 : len(f.Name())-8] testName := f.Name()[0 : len(f.Name())-8]
r, err, _ := gojs.RunFile(f.Name(), nil) r, err := gojs.RunFile(f.Name(), nil)
if err != nil || r != true { if err != nil || r != true {
t.Fatal("test "+testName+" failed", r, err) t.Fatal("test "+testName+" failed", r, err)
} else { } else {