forked from TarsCloud/TarsGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
124 lines (116 loc) · 3.12 KB
/
repo.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package base
import (
"context"
"fmt"
"os"
"os/exec"
"path"
"strings"
"github.com/jslyzt/tarsgo/tars/tools/tarsgo/internal/consts"
)
// Repo is git repository manager.
type Repo struct {
url string
home string
branch string
demoDir string
}
// NewRepo new a repository manager.
func NewRepo(url string, branch string, demoDir string) *Repo {
var start int
start = strings.Index(url, "//")
if start == -1 {
start = strings.Index(url, ":") + 1
} else {
start += 2
}
end := strings.LastIndex(url, "/")
return &Repo{
url: url,
home: TarsGoHomeWithDir("repo/" + url[start:end]),
branch: branch,
demoDir: demoDir,
}
}
// Path returns the repository cache path.
func (r *Repo) Path() string {
start := strings.LastIndex(r.url, "/")
end := strings.LastIndex(r.url, ".git")
if end == -1 {
end = len(r.url)
}
var branch string
if r.branch == "" {
branch = "@main"
} else {
branch = "@" + r.branch
}
return path.Join(r.home, r.url[start+1:end]+branch)
}
// Pull fetch the repository from remote url.
func (r *Repo) Pull(ctx context.Context) error {
cmd := exec.CommandContext(ctx, "git", "symbolic-ref", "HEAD")
cmd.Dir = r.Path()
_, err := cmd.CombinedOutput()
if err != nil {
return nil
}
cmd = exec.CommandContext(ctx, "git", "pull")
cmd.Dir = r.Path()
out, err := cmd.CombinedOutput()
if err != nil {
return err
}
fmt.Println(string(out))
return err
}
// Clone clones the repository to cache path.
func (r *Repo) Clone(ctx context.Context) error {
if _, err := os.Stat(r.Path()); !os.IsNotExist(err) {
return r.Pull(ctx)
}
var cmd *exec.Cmd
if r.branch == "" {
cmd = exec.CommandContext(ctx, "git", "clone", r.url, r.Path())
} else {
cmd = exec.CommandContext(ctx, "git", "clone", "-b", r.branch, r.url, r.Path())
}
out, err := cmd.CombinedOutput()
if err != nil {
return err
}
fmt.Println(string(out))
return nil
}
// CopyTo copies the repository to project path.
func (r *Repo) CopyTo(ctx context.Context, p *Project, to string, demoDir string, ignores []string) error {
if err := r.Clone(ctx); err != nil {
return err
}
replaces := []string{
"_APP_", p.App,
"_SERVER_", p.Server,
"_SERVANT_", p.Servant,
"_MODULE_", p.GoModuleName,
// makefile
"$(foreach path,$(libpath),$(eval -include $(path)/src/github.com/jslyzt/tarsgo/tars/makefile.tars.gomod))", "$(eval -include scripts/makefile.tars.gomod)",
// CMakeLists.txt
"${GOPATH}/src/github.com/jslyzt/tarsgo/", "${CMAKE_CURRENT_SOURCE_DIR}/",
}
err := copyDir(path.Join(r.Path(), "tars", "tools", r.demoDir), to, replaces, ignores)
if err != nil {
return err
}
err = copyDir(path.Join(r.Path(), "tars", "tools", "debugtool"), path.Join(to, "debugtool"), replaces, ignores)
if demoDir == consts.MakeDemoDir {
_ = os.MkdirAll(path.Join(to, "scripts"), 0755)
_ = copyFile(path.Join(r.Path(), "tars", "makefile.tars.gomod"), path.Join(to, "scripts", "makefile.tars.gomod"), replaces)
} else {
replaces = []string{
// cmake
"${GOPATH}/src/github.com/jslyzt/tarsgo/", "${CMAKE_CURRENT_SOURCE_DIR}/",
}
_ = copyDir(path.Join(r.Path(), "cmake"), path.Join(to, "cmake"), replaces, ignores)
}
return err
}