/
generate.go
149 lines (105 loc) · 3.3 KB
/
generate.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
package generate
import (
"bufio"
"fmt"
"os"
"path"
"path/filepath"
)
var allowExit = true
// TemplateConfigWriter creates a skeleton Granitic configuration file in the supplied location
type TemplateConfigWriter func(confDir string, pg *ProjectGenerator)
// TemplateComponentWriter creates a skeleton Granitic component definition file in the supplied location
type TemplateComponentWriter func(confDir string, pg *ProjectGenerator)
// ModFileWriter creates a go.mod file in the supplied location
type ModFileWriter func(baseDir string, moduleName string, pg *ProjectGenerator)
// MainFileContentWriter creates a Go source file with main function that initialises and passes control to Granitic
type MainFileContentWriter func(w *bufio.Writer, pp string)
// ProjectGenerator creates a blank Granitic project that is ready to build and start
type ProjectGenerator struct {
ConfWriterFunc TemplateConfigWriter
CompWriterFunc TemplateComponentWriter
MainFileFunc MainFileContentWriter
ModFileFunc ModFileWriter
ToolName string
}
// Settings contains the arguments for this tool
type Settings struct {
ProjectName string
ModuleName string
BaseFolder string
}
type exitFunc func(message string, a ...interface{})
// SettingsFromArgs uses CLI parameters to populate a Settings object
func SettingsFromArgs(ef exitFunc) Settings {
a := os.Args
if len(a) < 2 {
ef("You must provide a name for your project")
}
project := a[1]
module := project
if len(a) > 2 {
module = a[2]
}
return Settings{
ModuleName: module,
ProjectName: project,
}
}
// Generate creates the folder structure and blank/skeleton files for a new Granitic project that will be ready to build
func (pg *ProjectGenerator) Generate(s Settings) {
name := s.ProjectName
module := s.ModuleName
base := s.BaseFolder
projectDir := filepath.Join(base, name)
confDir := filepath.Join(projectDir, "config")
compDir := filepath.Join(projectDir, "comp-def")
pg.mkDir(projectDir)
pg.mkDir(confDir)
pg.mkDir(compDir)
pg.CompWriterFunc(compDir, pg)
pg.ConfWriterFunc(confDir, pg)
pg.writeMainFile(base, name, module)
pg.writeGitIgnore(base, name)
pg.ModFileFunc(projectDir, module, pg)
}
func (pg *ProjectGenerator) writeMainFile(base, name, module string) {
mainFile := filepath.Join(base, name, "main.go")
f := pg.OpenOutputFile(mainFile)
defer f.Close()
w := bufio.NewWriter(f)
pg.MainFileFunc(w, module)
w.Flush()
}
func (pg *ProjectGenerator) writeGitIgnore(base, name string) {
ignoreFile := filepath.Join(base, name, ".gitignore")
f := pg.OpenOutputFile(ignoreFile)
defer f.Close()
w := bufio.NewWriter(f)
w.WriteString("bindings*\n")
w.WriteString(name + "\n")
w.Flush()
}
// OpenOutputFile opens the supplied file path in create mode. Exits if there is a problem opening the file.
func (pg *ProjectGenerator) OpenOutputFile(p string) *os.File {
os.MkdirAll(path.Dir(p), 0755)
if f, err := os.Create(p); err != nil {
pg.ExitError(err.Error())
} else {
return f
}
return nil
}
func (pg *ProjectGenerator) mkDir(dir string) {
if err := os.Mkdir(dir, 0755); err != nil {
pg.ExitError(err.Error())
}
}
// ExitError writes
func (pg *ProjectGenerator) ExitError(message string, a ...interface{}) {
m := fmt.Sprintf("%s: %s \n", pg.ToolName, message)
fmt.Printf(m, a...)
if allowExit {
os.Exit(1)
}
}