-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathmain.go
221 lines (171 loc) · 4.29 KB
/
main.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
216
217
218
219
220
221
package skeleton
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
)
// Skeleton stores meta data of skeleton
type Skeleton struct {
// Path is where skeleton is generated.
Path string
Framework *Framework
Executable *Executable
// If WithTest is true, also generate test code.
SkipTest bool
// ArtifactCh is channel for info output
ArtifactCh chan string
// ErrCh is channel for error output
ErrCh chan error
// StaticDir contains static contents which will
// be copied final output directory
StaticDir string
// Verbose enables logging output below INFO
Verbose bool
// LogWriter
LogWriter io.Writer
}
// Generate generates code files from tempalte files.
func (s *Skeleton) Generate() <-chan struct{} {
s.Debugf("Start generating")
// doneCh is used to tell task it done to parent function
doneCh := make(chan struct{})
go func() {
// Start generating base files
doneBase := s.genBase()
// Start generating command files
doneTmpl := s.genTmpl()
// Read static files and copy it on output directory.
// Same name file which is generaed by gcli
// will be overwrite by this
doneStatic := s.copyStatic()
<-doneStatic
<-doneBase
<-doneTmpl
doneCh <- struct{}{}
}()
return doneCh
}
func (s *Skeleton) copyStatic() <-chan struct{} {
s.Debugf("Start reading static files")
doneCh := make(chan struct{})
go func() {
defer func() {
doneCh <- struct{}{}
}()
// Ignore when staticDir is not provided.
if s.StaticDir == "" {
return
}
// Ignore when staticDir is not exist.
if _, err := os.Stat(s.StaticDir); os.IsNotExist(err) {
return
}
fis, err := ioutil.ReadDir(s.StaticDir)
if err != nil {
s.ErrCh <- err
return
}
for _, fi := range fis {
if fi.IsDir() {
continue
}
srcFile := filepath.Join(s.StaticDir, fi.Name())
src, err := os.Open(srcFile)
if err != nil {
s.ErrCh <- err
return
}
defer src.Close()
dstFile := filepath.Join(s.Path, fi.Name())
// Create directory if necessary
dir, _ := filepath.Split(dstFile)
if dir != "" {
if err := mkdir(dir); err != nil {
s.ErrCh <- err
return
}
}
dst, err := os.Create(dstFile)
if err != nil {
s.ErrCh <- err
return
}
defer dst.Close()
if _, err := io.Copy(dst, src); err != nil {
s.Debugf(err.Error())
s.ErrCh <- err
return
}
s.ArtifactCh <- dstFile
}
}()
return doneCh
}
func (s *Skeleton) genBase() <-chan struct{} {
s.Debugf("Start generating base files")
// doneCh is used to tell task it done
doneCh := make(chan struct{})
go func() {
var wg sync.WaitGroup
baseTmpls := CommonTemplates
baseTmpls = append(baseTmpls, s.Framework.BaseTemplates...)
for _, tmpl := range baseTmpls {
s.Debugf("Use tempalte file: %s, output path tempalte string: %s",
tmpl.Path, tmpl.OutputPathTmpl)
if s.SkipTest && strings.HasSuffix(tmpl.Path, "_test.go.tmpl") {
s.Debugf("Skip test tempalte file: %s", filepath.Base(tmpl.Path))
continue
}
wg.Add(1)
go func(tmpl Template) {
defer wg.Done()
tmpl.OutputPathTmpl = filepath.Join(s.Path, tmpl.OutputPathTmpl)
outputPath, err := tmpl.Exec(s.Executable)
if err != nil {
s.ErrCh <- err
}
s.ArtifactCh <- outputPath
}(tmpl)
}
// Wait until all task is done
wg.Wait()
// Tell doneCh about finishing generating
doneCh <- struct{}{}
}()
return doneCh
}
func (s *Skeleton) genTmpl() <-chan struct{} {
s.Debugf("Start generating command files")
// doneCh is used to tell task it done
doneCh := make(chan struct{})
go func() {
var wg sync.WaitGroup
for _, cmd := range s.Executable.Commands {
wg.Add(1)
go func(cmd *Command) {
defer wg.Done()
for _, tmpl := range s.Framework.CommandTemplates {
s.Debugf("Use tempalte file: %s, output path tempalte string: %s",
tmpl.Path, tmpl.OutputPathTmpl)
if s.SkipTest && strings.HasSuffix(tmpl.Path, "_test.go.tmpl") {
s.Debugf("Skip test tempalte file: %s", tmpl.Path)
continue
}
tmpl.OutputPathTmpl = filepath.Join(s.Path, tmpl.OutputPathTmpl)
outputPath, err := tmpl.Exec(cmd)
if err != nil {
s.ErrCh <- err
}
s.ArtifactCh <- outputPath
}
}(cmd)
}
// Wait until all task is done.
wg.Wait()
doneCh <- struct{}{}
}()
return doneCh
}