-
Notifications
You must be signed in to change notification settings - Fork 1
/
new.go
150 lines (133 loc) 路 3.02 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
package new
import (
"errors"
"go/parser"
"go/token"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/samber/lo"
"github.com/spf13/cobra"
"golang.org/x/mod/modfile"
"github.com/rickywei/sparrow/project"
)
var CmdNew = &cobra.Command{
Use: "new",
Short: "Create a new project",
Long: "Create a new project",
Run: run,
}
func run(_ *cobra.Command, args []string) {
workDir, err := os.Getwd()
if err != nil {
panic(err)
}
project := ""
if len(args) == 0 {
prompt := &survey.Input{
Message: "What is the project name?",
Help: "Please input the project name. eg. github.com/rickywei/sparrow or sparrow.",
}
if err = survey.AskOne(prompt, &project); err != nil || project == "" {
return
}
} else {
project = args[0]
}
p := &newProject{
project: project,
name: filepath.Base(project),
dir: workDir,
}
if err = p.New(); err != nil {
log.Printf("failed to new project %v\n", err)
}
}
type newProject struct {
project string
name string
dir string
}
func (p *newProject) New() (err error) {
dst := filepath.Join(p.dir, p.name)
_, err = os.Stat(dst)
if !os.IsNotExist(err) {
log.Printf("%s already exists\n", p.name)
prompt := &survey.Confirm{
Message: "Do you want to override the folder?",
Help: "Delete the existing folder and create the project.",
}
confirm := false
if err = survey.AskOne(prompt, &confirm); err != nil {
return
}
if !confirm {
return
}
// if err = os.RemoveAll(dst); err != nil {
// return
// }
}
err = fs.WalkDir(project.EmbedFS, ".", func(path string, d fs.DirEntry, e error) (err error) {
if e != nil {
return e
}
to := filepath.Join(dst, strings.Replace(path, "project", "", 1))
log.Println(to)
if d.IsDir() {
// return os.Mkdir(to, 0755)
err = os.Mkdir(to, 0755)
if errors.Is(err, os.ErrExist) {
err = nil
}
return
}
data, err := fs.ReadFile(project.EmbedFS, path)
if err != nil {
return
}
if filepath.Ext(path) == ".go" {
if filepath.Base(path) == "template.go" {
return
}
if data, err = p.replaceImport(string(data)); err != nil {
return
}
} else if filepath.Base(path) == "go.mod" {
var f *modfile.File
f, err = modfile.Parse(path, data, nil)
if err != nil {
return
}
if err = f.AddModuleStmt(p.project); err != nil {
return
}
if data, err = f.Format(); err != nil {
return
}
}
err = os.WriteFile(to, data, 0644)
return
})
return
}
func (p *newProject) replaceImport(src string) (data []byte, err error) {
f, err := parser.ParseFile(token.NewFileSet(), "", src, parser.ImportsOnly)
if err != nil {
return
}
last, err := lo.Last(f.Imports)
if err != nil {
data, err = []byte(src), nil
return
}
packagePart := src[:f.Name.End()]
importPart := src[f.Name.End():last.End()]
importPart = strings.ReplaceAll(importPart, "github.com/rickywei/sparrow/project", p.project)
otherPart := src[last.End():]
data = []byte(packagePart + importPart + otherPart)
return
}