/
generate.go
268 lines (223 loc) · 6.74 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* ZEUS - An Electrifying Build System
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
// Generate a standalone version of a single command or commandChain.
// If all commands are of the same language, generate a single script,
// if there are multiple scripting languages involved, generate a directory with all required scripts.
func handleGenerateCommand(args []string) {
if len(args) < 3 {
printGenerateCommandUsageErr()
return
}
var (
chain commandChain
ok bool
mixed bool
language string
arguments []string
outputName = zeusDir + "/generated/" + args[1]
)
// check if it's a valid command chain
if chain, ok = validCommandChain(args[2:], false); !ok {
l.Println("invalid command chain")
return
}
// make sure generated dir exists
err := os.Mkdir(zeusDir+"/generated", 0744)
if err != nil && err != os.ErrExist {
l.Println("failed to create generated directory:", err)
}
// check if it contains multiple languages
for _, cmd := range chain {
if language == "" {
language = cmd.language
}
if language != cmd.language {
mixed = true
break
}
}
if mixed {
// create a directory for multiple scripts
err := os.Mkdir(outputName, 0744)
if err != nil {
Log.WithError(err).Error("failed to create directory for mixed commandChain")
return
}
f, err := os.Create(outputName + "/run.sh")
if err != nil {
Log.WithError(err).Error("failed to create run script")
return
}
err = f.Chmod(0700)
if err != nil {
Log.WithError(err).Error("failed to make run script executable")
return
}
f.WriteString("#!/bin/bash\n\n")
f.WriteString("# generated by ZEUS v" + version + " @ " + time.Now().String() + "\n")
f.WriteString("# commandChain: " + strings.Join(args[2:], " ") + "\n")
f.WriteString("\n./" + filepath.Base(chain[0].path) + "\n")
f.Close()
l.Println("generated " + outputName + "/run.sh")
}
chainSlice := strings.Split(strings.Join(args[3:], " "), commandChainSeparator)
if len(chainSlice) > 0 {
arguments = strings.Fields(chainSlice[0])
}
// iterate over the chain and generate scripts
for i, cmd := range chain {
f, lang, err := generateScript(outputName, mixed, cmd, arguments)
if err != nil {
l.Println(err)
return
}
// inject call to next script
if i < len(chain)-1 {
nextCmd := chain[i+1]
nextLang, err := nextCmd.getLanguage()
if err != nil {
l.Println(nextCmd.name + ": " + err.Error() + ": " + nextCmd.language)
return
}
f.WriteString("\n" + lang.Comment + " execute next script: " + filepath.Base(nextCmd.path) + "\n")
f.WriteString(lang.ExecOpPrefix + nextLang.Interpreter + " " + filepath.Base(nextCmd.path) + lang.ExecOpSuffix + "\n")
}
f.Close()
}
}
// generate a single script
// if mixed each script will be put into the outputName directory
// if not, a single script file will be generated
// returns the fileDescriptor, language and an error
func generateScript(outputName string, mixed bool, cmd *command, args []string) (*os.File, *Language, error) {
var arguments string
// get language for current command
lang, err := cmd.getLanguage()
if err != nil {
cmd.dump()
return nil, nil, errors.New(cmd.name + ": " + err.Error() + ": " + cmd.language)
}
// handle arguments
if len(cmd.args) > 0 {
arguments, _, err = cmd.parseArguments(args)
if err != nil {
return nil, nil, err
}
}
if mixed {
// create new file in output directory
outputName = outputName + "/" + cmd.name + lang.FileExtension
}
f, err := os.Create(outputName)
if err != nil {
return nil, nil, err
}
header := lang.Comment + " generated by ZEUS v" + version + "\n"
header += lang.Comment + " Timestamp: " + time.Now().Format(timestampFormat) + "\n"
// insert bang and args
f.WriteString(lang.Bang + "\n" + header + "\n" + generateGlobals(lang) + "\n" + arguments + "\n")
// add language specific global code
code, err := ioutil.ReadFile(zeusDir + "/globals/globals" + lang.FileExtension)
if err == nil {
f.WriteString("\n")
f.Write(code)
f.WriteString("\n")
}
// add dependencies
err = addDependencies(cmd, f, lang, outputName, mixed)
if err != nil {
return nil, nil, err
}
// add script of current command
if cmd.exec == "" {
c, err := ioutil.ReadFile(cmd.path)
if err != nil {
l.Println("failed to read: " + cmd.path)
return nil, nil, err
}
f.Write(c)
} else {
f.WriteString(cmd.exec)
}
// make script executable
err = os.Chmod(outputName, 0700)
if err != nil {
Log.Error("failed to make script executable")
return nil, nil, err
}
l.Println("generated " + outputName)
return f, lang, nil
}
func addDependencies(cmd *command, f *os.File, lang *Language, outputName string, mixed bool) error {
for i, d := range cmd.dependencies {
fields := strings.Fields(d)
if len(fields) == 0 {
return ErrEmptyDependency
}
// lookup
dep, err := cmdMap.getCommand(fields[0])
if err != nil {
return errors.New("invalid dependency: " + err.Error())
}
// get language for current command
depLang, err := dep.getLanguage()
if err != nil {
l.Println(dep.name+":", err, ", language:", dep.language)
return err
}
if lang.Name == depLang.Name {
// add to current script
if dep.exec == "" {
c, err := ioutil.ReadFile(dep.path)
if err != nil {
l.Println("failed to read: " + dep.path)
return err
}
f.Write(c)
} else {
f.WriteString(dep.exec)
}
} else {
// dependency script is in another language
// generate a new script and inject a call to it
generateScript(outputName, mixed, dep, []string{})
fields := strings.Fields(cmd.dependencies[i+1])
if len(fields) == 0 {
return ErrEmptyDependency
}
// lookup
nextDep, err := cmdMap.getCommand(fields[0])
if err != nil {
return err
}
f.WriteString("\n" + lang.Comment + " execute next script: " + filepath.Base(nextDep.path) + "\n")
f.WriteString(lang.ExecOpPrefix + lang.Interpreter + " " + filepath.Base(nextDep.path) + lang.ExecOpSuffix + "\n")
}
f.WriteString("\n")
}
return nil
}