This repository was archived by the owner on Jul 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_common.go
206 lines (183 loc) · 3.87 KB
/
project_common.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
package main
import (
"fmt"
"os"
"path/filepath"
"text/template"
"time"
)
func CreateDir(pr *ParsingResult) {
_, err := os.Stat(pr.Path())
if !os.IsNotExist(err) {
if pr.IsForced() {
err = os.RemoveAll(pr.Path())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
} else {
fmt.Fprintln(os.Stderr,
fmt.Sprintf("File or directory %s exists", pr.Path()))
os.Exit(1)
}
}
err = os.MkdirAll(pr.Path(), os.ModePerm)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func createLicense(pr IProject) {
if pr.License() == LICENSE_NONE {
return
}
path := filepath.Join(pr.Path(), "LICENSE")
file, err := os.Create(path)
defer file.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
template := getTemplate(pr.License())
now := time.Now()
if isNoAuthor(pr.License()) {
_, err = file.WriteString(template)
} else {
_, err = file.WriteString(
fmt.Sprintf(template, fmt.Sprint(now.Year()), pr.Author()))
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func isNoAuthor(license License) bool {
return license == LICENSE_AGPL3 || license == LICENSE_GPL2 ||
license == LICENSE_GPL3 || license == LICENSE_MPL2
}
func createREADME(pr IProject) {
path := filepath.Join(pr.Path(), "README.md")
file, err := os.Create(path)
defer file.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
tpl := fmt.Sprintf(templateREADME)
tmpl, err := template.New("readmd").Parse(tpl)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
now := time.Now()
if pr.License() == LICENSE_NONE {
err = tmpl.Execute(file, struct {
Prog string
Author string
Brief string
Year int
License string
}{
pr.Prog(),
pr.Author(),
pr.Brief(),
now.Year(),
"",
})
} else {
err = tmpl.Execute(file, struct {
Prog string
Author string
Brief string
Year int
License string
}{
pr.Prog(),
pr.Author(),
pr.Brief(),
now.Year(),
licenseToString(pr.License()),
})
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func createProjStruct(pr IProject) {
// Create source dir
path := filepath.Join(pr.Path(), pr.Src())
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Create .gitkeep in source dir
path = filepath.Join(path, ".gitkeep")
file, err := os.Create(path)
defer file.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Create include dir
path = filepath.Join(pr.Path(), pr.Include())
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Create .gitkeep in include dir
path = filepath.Join(path, ".gitkeep")
file, err = os.Create(path)
defer file.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Create dist dir
path = filepath.Join(pr.Path(), pr.Dist())
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Create .gitkeep in dist dir
path = filepath.Join(path, ".gitkeep")
file, err = os.Create(path)
defer file.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Create test dir
path = filepath.Join(pr.Path(), pr.Test())
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Create .gitkeep in test dir
path = filepath.Join(path, ".gitkeep")
file, err = os.Create(path)
defer file.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Create example dir
path = filepath.Join(pr.Path(), pr.Example())
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Create .gitkeep in example dir
path = filepath.Join(path, ".gitkeep")
file, err = os.Create(path)
defer file.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}