-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
202 lines (167 loc) · 4.3 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package projectcmd
import (
"bytes"
"context"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/fatih/color"
"github.com/mangohow/mangokit/cmd/mangokit/parallel"
)
type ProjectGenerator struct {
DirName string
ProjectName string
BaseDir string
RepoUrl string
Branch string
}
func (p *ProjectGenerator) Generate(ctx context.Context) error {
// 克隆项目
if err := p.CloneRepo(ctx); err != nil {
return err
}
// 修改文件
if err := p.ModifyFiles(); err != nil {
// 清理文件夹
os.RemoveAll(p.BaseDir)
return err
}
// git init
if err := p.GitInit(); err != nil {
return err
}
color.Green("create project success!\n")
color.Cyan("next step:\n")
color.Cyan("cd %s && go mod tidy \n", p.DirName)
return nil
}
func (p *ProjectGenerator) CloneRepo(ctx context.Context) error {
// 克隆模板项目
var cmd *exec.Cmd
if p.Branch == "" {
cmd = exec.CommandContext(ctx, "git", "clone", p.RepoUrl)
} else {
cmd = exec.CommandContext(context.Background(), "git", "clone", "-b", p.Branch, p.RepoUrl)
}
if output, err := cmd.CombinedOutput(); err != nil {
color.Red(string(output))
color.Red("clone repo failed, %v\n", err)
return err
}
words := strings.Split(p.RepoUrl, "/")
oldDirName := words[len(words)-1]
// 修改目录名
if err := os.Rename(oldDirName, p.DirName); err != nil {
color.Red("modify dir name failed, %v\n", err)
os.RemoveAll(oldDirName)
return err
}
return nil
}
func (p *ProjectGenerator) ModifyFiles() error {
// 1.删除.git文件夹
gitDir := filepath.Join(p.BaseDir, ".git")
if err := os.RemoveAll(gitDir); err != nil {
color.Red("remove .git failed, %v\n", err)
return err
}
// 2.从 go mod中获取原项目名
module, err := p.GetGoModName()
if err != nil {
return err
}
// 3. 删除go.mod
if err := os.Remove(filepath.Join(p.BaseDir, "go.mod")); err != nil {
color.Red("remove go.mod failed, %v\n", err)
return err
}
// 4. 修改go文件中的import
if err := p.ModifyGoImports(module); err != nil {
return err
}
// 5. 重新初始化go mod
return p.GoModInit()
}
func (p *ProjectGenerator) GetGoModName() (string, error) {
goModPath := filepath.Join(p.BaseDir, "go.mod")
content, err := os.ReadFile(goModPath)
if err != nil {
color.Red("read go.mod failed, %v\n", err)
return "", err
}
index := bytes.Index(content, []byte("module")) + len("module")
lineEndIndex := bytes.Index(content, []byte("\n"))
moduleStr := string(content[index:lineEndIndex])
module := strings.Trim(moduleStr, " \r\n")
return module, nil
}
func (p *ProjectGenerator) ModifyGoImports(oldModule string) error {
goFiles := make([]string, 0)
// 获取项目目录下所有go文件
err := filepath.WalkDir(p.BaseDir, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
if strings.HasSuffix(path, ".go") {
goFiles = append(goFiles, path)
}
return nil
})
if err != nil {
color.Red("walk dir failed, %v\n", err)
return err
}
// 修改go imports
oldMod := []byte(oldModule)
newMod := []byte(p.ProjectName)
// 启动多个goroutine来修改go文件中的import
err = parallel.Parallel(context.Background(), 8, goFiles, func(file string) error {
return p.modifyGoImports(file, oldMod, newMod)
})
if err != nil {
color.Red("modify go imports error, %v\n", err)
return err
}
return nil
}
func (p *ProjectGenerator) modifyGoImports(path string, oldModule, newModule []byte) error {
content, err := os.ReadFile(path)
if err != nil {
return err
}
replaced := bytes.ReplaceAll(content, oldModule, newModule)
if err = os.WriteFile(path, replaced, 0644); err != nil {
return err
}
return nil
}
func (p *ProjectGenerator) GoModInit() error {
cmd := exec.Command("go", "mod", "init", p.ProjectName)
// 进入base dir
if err := os.Chdir(p.BaseDir); err != nil {
color.Red("change dir failed, %v\n", err)
return err
}
if err := cmd.Run(); err != nil {
color.Red("go mod init failed, %v\n", err)
return err
}
os.Chdir("..")
return nil
}
func (p *ProjectGenerator) GitInit() error {
cmd := exec.Command("git", "init")
// 进入base dir
if err := os.Chdir(p.BaseDir); err != nil {
color.Red("change dir failed, %v\n", err)
return err
}
if err := cmd.Run(); err != nil {
color.Red("git init failed, %v\n", err)
return err
}
os.Chdir("..")
return nil
}