forked from tcnksm/gcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (105 loc) · 2.88 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
package skeleton
import (
"io"
"path/filepath"
"strings"
"sync"
)
// Skeleton stores meta data of skeleton
type Skeleton struct {
// Path is where skeleton is generated.
Path string
// If WithTest is true, also generate test code.
SkipTest bool
Framework *Framework
Executable *Executable
// ArtifactCh is channel for info output
ArtifactCh chan string
// ErrCh is channel for error output
ErrCh chan error
// 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
doneBaseCh := s.generateBaseFiles()
// Start generating command files
doneCmdCh := s.generateCommandFiles()
<-doneBaseCh
<-doneCmdCh
doneCh <- struct{}{}
}()
return doneCh
}
func (s *Skeleton) generateBaseFiles() <-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) generateCommandFiles() <-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
}