forked from TarsCloud/TarsGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
87 lines (75 loc) · 2.29 KB
/
project.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package base
import (
"context"
"fmt"
"os"
"os/exec"
"path"
"github.com/AlecAivazis/survey/v2"
"github.com/fatih/color"
)
// Project is a project template.
type Project struct {
App string
Server string
Servant string
GoModuleName string
}
func NewProject(app, server, servant, goModuleName string) *Project {
return &Project{
App: app,
Server: server,
Servant: servant,
GoModuleName: goModuleName,
}
}
// New new a project from remote repo.
func (p *Project) Create(ctx context.Context, dir string, layout string, branch string, demoDir string) error {
to := path.Join(dir, p.Server)
if _, err := os.Stat(to); !os.IsNotExist(err) {
fmt.Printf("🚫 %s already exists\n", p.Server)
override := false
prompt := &survey.Confirm{
Message: "📂 Do you want to override the folder ?",
Help: "Delete the existing folder and create the project.",
}
e := survey.AskOne(prompt, &override)
if e != nil {
return e
}
if !override {
return err
}
os.RemoveAll(to)
}
if err := GoInstall("github.com/jslyzt/tarsgo/tars/tools/tars2go"); err != nil {
return err
}
fmt.Printf("🚀 Creating server %s.%s, layout repo is %s, please wait a moment.\n\n", p.App, p.Server, layout)
repo := NewRepo(layout, branch, demoDir)
if err := repo.CopyTo(ctx, p, to, demoDir, []string{".git"}); err != nil {
return err
}
if err := os.Rename(path.Join(to, "Servant.tars"), path.Join(to, p.Servant+".tars")); err != nil {
return err
}
if err := os.Rename(path.Join(to, "Servant_imp.go"), path.Join(to, p.Servant+"_imp.go")); err != nil {
return err
}
cmd := exec.CommandContext(ctx, "go", "mod", "init", p.GoModuleName)
cmd.Dir = to
out, err := cmd.CombinedOutput()
if err != nil {
return err
}
fmt.Println(string(out))
Tree(to, dir)
fmt.Printf("\n>>> Great!Done! You can jump in %s\n", color.GreenString(p.Server))
fmt.Println(">>> Tips: After editing the Tars file, execute the following cmd to automatically generate golang files.")
fmt.Printf(">>> %s/bin/tars2go *.tars\n", os.Getenv("GOPATH"))
fmt.Println(color.WhiteString("$ cd %s", p.Server))
fmt.Println(color.WhiteString("$ ./start.sh"))
fmt.Println("🤝 Thanks for using TarsGo")
fmt.Println("📚 Tutorial: https://tarscloud.github.io/TarsDocs/")
return nil
}