/
new.go
215 lines (185 loc) · 5.39 KB
/
new.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
203
204
205
206
207
208
209
210
211
212
213
214
215
package command
import (
"bytes"
"context"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/google/go-github/v39/github"
"github.com/jader1992/gocore/framework/cobra"
"github.com/jader1992/gocore/framework/util"
"github.com/spf13/cast"
"io/fs"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"time"
)
// new相关的名称
func initNewCommand() *cobra.Command {
return newCommand
}
var newCommand = &cobra.Command{
Use: "new",
Aliases: []string{"create", "init"},
Short: "创建一个新的应用",
RunE: func(c *cobra.Command, args []string) error {
currentPath := util.GetExecDirectory()
var name string
var folder string
var mod string
var version string
var release *github.RepositoryRelease
{
prompt := &survey.Input{
Message: "请输入目录名称:",
}
err := survey.AskOne(prompt, &name)
if err != nil {
return err
}
folder = filepath.Join(currentPath, name)
if util.Exists(folder) {
isForce := false
prompt2 := &survey.Confirm{
Message: "目录" + folder + "已经存在,是否删除重新创建?(确认后立刻执行删除操作!)",
Default: false,
}
err := survey.AskOne(prompt2, &isForce)
if err != nil {
return err
}
if isForce {
if err := os.RemoveAll(folder); err != nil {
return err
}
} else {
fmt.Println("目录已存在,创建应用失败")
return nil
}
}
}
{
prompt := &survey.Input{
Message: "请输入模块名称(go.mod中的module, 默认为文件夹名称):",
}
err := survey.AskOne(prompt, &mod)
if err != nil {
return err
}
if mod == "" {
mod = name
}
}
{
// 获取core版本
client := github.NewClient(nil)
prompt := &survey.Input{
Message: "请输入版本名称(参考 https://github.com/jader1992/gocore/releases,默认为最新版本):",
}
err := survey.AskOne(prompt, &version)
if err != nil {
return err
}
if version != "" {
// 确认版本是否正确
release, _, err := client.Repositories.GetReleaseByTag(context.Background(), "jader1992", "gocore", version)
if err != nil || release == nil {
fmt.Println("版本不存在,创建应用失败, 请参考:https://github.com/jader1992/gocore/releases")
return nil
}
}
if version == "" {
release, _, err = client.Repositories.GetLatestRelease(context.Background(), "jader1992", "gocore")
version = release.GetTagName()
}
}
fmt.Println(version)
fmt.Println("====================================================")
fmt.Println("开始进行创建应用操作")
fmt.Println("创建目录:", folder)
fmt.Println("应用名称:", mod)
fmt.Println("gocore框架版本:", release.GetTagName())
// 创建临时目录
templateFolder := filepath.Join(currentPath, "template-gocore-"+version+"-"+cast.ToString(time.Now().Unix()))
_ = os.Mkdir(templateFolder, os.ModePerm)
fmt.Println("创建临时目录", templateFolder)
// 下载项目
url := release.GetZipballURL()
err := util.DownloadFile(filepath.Join(templateFolder, "template.zip"), url) // 下载压缩包
if err != nil {
return err
}
fmt.Println("下载zip包到template.zip")
// 拷贝template项目
_, err = util.Unzip(filepath.Join(templateFolder, "template.zip"), templateFolder)
if err != nil {
return err
}
// 获取folder下的相关的解压目录
fInfos, err := ioutil.ReadDir(templateFolder)
if err != nil {
return err
}
for _, fInfo := range fInfos {
if fInfo.IsDir() && strings.Contains(fInfo.Name(), "jader1992-gocore-") {
if err := os.Rename(filepath.Join(templateFolder, fInfo.Name()), folder); err != nil {
return err
}
}
}
fmt.Println("解压zip包")
if err := os.RemoveAll(templateFolder); err != nil {
return err
}
fmt.Println("删除临时文件夹:", templateFolder)
_ = os.RemoveAll(path.Join(folder, ".git"))
fmt.Println("删除.git目录")
// 删除framework目录
_ = os.RemoveAll(path.Join(folder, "framework"))
fmt.Println("删除framework目录")
_ = filepath.Walk(folder, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
c, err := ioutil.ReadFile(path)
if err != nil {
return err
}
if path == filepath.Join(folder, "go.mod") {
fmt.Println("更新文件:" + path)
c = bytes.ReplaceAll(c, []byte("module github.com/jader1992/gocore"), []byte("module "+mod)) // 创建应用名
c = bytes.ReplaceAll(c, []byte("require ("), []byte("require (\n\tgithub.com/jader1992/gocore "+version))
err := ioutil.WriteFile(path, c, 0644)
if err != nil {
return err
}
return nil
}
isContain := bytes.Contains(c, []byte("github.com/jader1992/gocore/app"))
if isContain {
fmt.Println("更新文件:", path)
c = bytes.ReplaceAll(c, []byte("github.com/jader1992/gocore/app"), []byte(mod+"/app"))
err = ioutil.WriteFile(path, c, 0644)
if err != nil {
return err
}
}
isContainCmd := bytes.Contains(c, []byte("./gocore"))
if isContainCmd {
fmt.Println("更新文件:", path)
c = bytes.ReplaceAll(c, []byte("./gocore"), []byte("./"+name))
err = ioutil.WriteFile(path, c, 0644)
if err != nil {
return err
}
}
return nil
})
fmt.Println("创建应用结束")
fmt.Println("目录:", folder)
fmt.Println("====================================================")
return nil
},
}