-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathzeus.go
552 lines (448 loc) · 12 KB
/
zeus.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
* 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 (
"flag"
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
rice "github.com/GeertJohan/go.rice"
"github.com/dreadl0ck/readline"
"github.com/mgutz/ansi"
"github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)
var (
// Log instance for internal logs
Log = newAtomicLogger()
// logging instance for terminal UI
l = log.New(os.Stdout, "", 0)
// all available build target commands
// does not include the built-ins!
// command names mapped to command structs
cmdMap = newCommandMap()
// readline auto completion
completer = newAtomicCompleter()
// assets folder
assetBox *rice.Box
// configuration
conf *config
// project data
projectData *data
// shell formatter
f = newFormatter("path/to/your/formatter", bashLanguage())
g = &globals{
Vars: make(map[string]string, 0),
}
debug bool
asciiArt string
asciiArtYAML string
workingDir string
// status info
s = &status{
recursionMap: make(map[string]int, 0),
}
// running a test?
testingMode bool
promptFilePath = "zeus/.zeus_prompt"
)
type atomicLogger struct {
*logrus.Logger
sync.RWMutex
}
func newAtomicLogger() *atomicLogger {
return &atomicLogger{
logrus.New(),
sync.RWMutex{},
}
}
func initZeus() {
var (
err error
flagCompletions = flag.String("completions", "", "get available command completions")
flagWorkDir = flag.String("C", "", "set work directory to start from")
flagHelp = flag.Bool("h", false, "print zeus help and exit")
)
// set up formatter
Log.Formatter = &prefixed.TextFormatter{}
if runtime.GOOS == "windows" {
Log.Fatal("windows is not (yet) supported.")
}
assetBox = rice.MustFindBox("assets")
asciiArt, err = assetBox.String("ascii_art.txt")
if err != nil {
Log.WithError(err).Fatal("failed to get ascii_art.txt from rice box")
}
asciiArtYAML, err = assetBox.String("ascii_art.yml")
if err != nil {
Log.WithError(err).Fatal("failed to get ascii_art.yml from rice box")
}
// add version number
asciiArtYAML += version + "\n#\n\n"
if len(os.Args) > 1 {
if os.Args[1] == bootstrapCommand {
runBootstrapCommand()
// remove bootstrap arg
os.Args = []string{os.Args[0]}
}
}
if len(os.Args) > 2 {
if os.Args[1] == "makefile" && os.Args[2] == "migrate" {
migrateMakefile(zeusDir)
os.Exit(0)
}
}
flag.Parse()
if *flagWorkDir != "" {
if strings.HasPrefix(*flagWorkDir, "~") {
usr, err := user.Current()
if err != nil {
log.Fatal("unable to get current user for expanding the ~ character: ", err)
}
*flagWorkDir = filepath.Join(usr.HomeDir, strings.TrimPrefix(*flagWorkDir, "~"))
fmt.Println("expanded ~:", *flagWorkDir)
}
if strings.HasPrefix(*flagWorkDir, "$HOME") {
usr, err := user.Current()
if err != nil {
log.Fatal("unable to get current user for expanding $HOME: ", err)
}
*flagWorkDir = filepath.Join(usr.HomeDir, strings.TrimPrefix(*flagWorkDir, "$HOME"))
fmt.Println("expanded $HOME:", *flagWorkDir)
}
err := os.Chdir(*flagWorkDir)
if err != nil {
log.Fatal("failed to change dir: ", err)
}
fullPath, err := os.Getwd()
if err != nil {
log.Fatal("failed to obtain full path of current working directory: ", err)
}
projectDir = fullPath
} else {
projectDir, err = os.Getwd()
if err != nil {
log.Fatal("failed to set project directory on startup: ", err)
}
}
if *flagCompletions != "" {
printCompletions(*flagCompletions)
os.Exit(0)
}
if *flagHelp {
printHelp()
}
stat, err := os.Stat(scriptDir)
if err != nil {
if stat, err = os.Stat(commandsFilePath); err != nil {
Log.WithError(err).Error("no " + scriptDir + " directory or CommandsFile found.")
Log.Info("run 'zeus bootstrap' to create a default setup, or 'zeus makefile migrate' if you want to migrate from a GNU Makefile.")
os.Exit(1)
} else {
// make sure its a file
if stat.IsDir() {
Log.Fatal(commandsFilePath + " is not a file")
}
}
} else {
// make sure its a directory
if !stat.IsDir() {
Log.Fatal("zeus/ is not a directory")
}
}
}
func main() {
initZeus()
var (
cLog = Log.WithField("prefix", "main")
err error
configWarnings []string
)
// look for project data
projectData, err = parseProjectData()
if err != nil {
cLog.WithError(err).Debug("error looking for project data")
projectData = newData()
}
// look for project config
conf, configWarnings, err = parseProjectConfig()
if err != nil {
cLog.WithError(err).Debug("failed to parse project config")
cLog.Info("initializing default configuration")
conf = newConfig()
conf.update()
}
initColorProfile()
// load persisted events from project data
loadEvents()
projectData.Lock()
// validate aliases
for name := range projectData.fields.Aliases {
err = validateAlias(name)
if err != nil {
Log.WithError(err).Fatal("failed to validate alias: ", name)
}
// add to completer
completer.Children = append(completer.Children, readline.PcItem(name))
}
projectData.Unlock()
// get debug value from config
debug = conf.fields.Debug
// handle debug mode for logger
if debug {
Log.Level = logrus.DebugLevel
}
if conf.fields.DisableTimestamps {
formatter := new(prefixed.TextFormatter)
formatter.DisableTimestamp = true
Log.Formatter = formatter
}
// disable colors
if !conf.fields.Colors {
print(cp.Reset)
// lock once
cp.Lock()
cp = colorsOffProfile().parse()
Log.Formatter = &prefixed.TextFormatter{
DisableColors: true,
DisableTimestamp: conf.fields.DisableTimestamps,
}
ansi.DisableColors(true)
} else {
// load colored ascii art
asciiArt, err = assetBox.String("ascii_art_color.txt")
if err != nil {
Log.WithError(err).Fatal("failed to get ascii_art_color.txt from rice box")
}
}
// set working directory
workingDir, err = os.Getwd()
if err != nil {
cLog.WithError(err).Fatal("failed to get current directory name")
}
// only execute when using the interactive shell
if len(os.Args) == 1 {
printProjectHeader()
}
// print config warnings
for _, w := range configWarnings {
Log.Warn(w)
}
// start watchers when running in interactive mode
if conf.fields.Interactive {
// watch config for changes
go conf.watch("")
if conf.fields.AutoFormat {
// watch zeus directory for changes
go f.watchScriptDir("")
}
}
// print makefile command overview
if conf.fields.MakefileOverview {
printMakefileCommandOverview()
}
if conf.fields.ProjectNamePrompt {
// set shell prompt to project name
zeusPrompt = filepath.Base(workingDir)
}
// check if a CommandsFile for the project exists
cmdFile, err := parseCommandsFile(commandsFilePath, false)
if err != nil {
Log.Error("failed to parse commandsFile: ", err, "\n")
os.Exit(1)
}
// handle commandsFile extension
cmdFile.handleExtension()
// handle commandsFile inclusion
cmdFile.handleInclusion()
// watch commandsFile for changes in interactive mode
if conf.fields.Interactive {
go watchCommandsFile(commandsFilePath, "")
}
// handle commandline arguments
handleArgs(os.Args, cmdFile)
// check if interactive mode is enabled in the config
if conf.fields.Interactive {
if conf.fields.WebInterface {
go StartWebListener(true)
}
// handle OS Signals
// all child processes need to be killed when theres an error
handleSignals(cmdFile)
// start interactive mode and start reading from stdin
err = readlineLoop(cmdFile)
if err != nil {
cLog.WithError(err).Fatal("failed to read user input")
}
} else {
printProjectHeader()
if conf.fields.PrintBuiltins {
printBuiltins()
}
printCommands()
}
}
func printHelp() {
l.Println("ZEUS - An Electrifying Build System")
l.Println("author: dreadl0ck@protonmail.ch")
l.Println("for documentation see: https://github.com/dreadl0ck/zeus")
l.Println("run 'zeus bootstrap' to create a default ZEUS setup")
l.Println("or 'zeus makefile migrate' if you want to migrate from a GNU Makefile.")
l.Println("to get an overview over an existing ZEUS project, run 'zeus help' or 'zeus' for the interactive shell.")
os.Exit(0)
}
// print the project ascii art and project infos
func printProjectHeader() {
// print ascii art
clearScreen()
l.Println(cp.Text + asciiArt + "v" + version)
if !conf.fields.Quiet {
if conf.fields.Debug {
l.Println(cp.Text + pad("Project Name", 14) + cp.Prompt + filepath.Base(workingDir) + cp.Text + "\n")
}
printAuthor()
}
printTodoCount()
if projectData.fields.BuildNumber > 0 {
l.Println(cp.Text + pad("BuildNumber", 14) + cp.Prompt + strconv.Itoa(projectData.fields.BuildNumber) + cp.Text)
}
if projectData.fields.Deadline != "" {
l.Println(pad("Deadline", 14) + cp.Prompt + projectData.fields.Deadline + cp.Text)
}
// project infos
listMilestones()
}
// handle commandline arguments
func handleArgs(args []string, cmdFile *CommandsFile) {
// strip commandline flags
for i, elem := range args {
if strings.HasPrefix(elem, "-C=") {
// delete i
args = append(args[:i], args[i+1:]...)
break
}
if elem == "-C" {
// delete i and i+1
args = append(args[:i], args[i+2:]...)
break
}
}
var cLog = Log.WithField("prefix", "handleArgs")
if len(args) > 1 {
var validCommand bool
switch args[1] {
case helpCommand:
if conf.fields.PrintBuiltins {
printBuiltins()
}
printCommands()
case formatCommand:
f.formatCommand()
case dataCommand:
printProjectData()
case aliasCommand:
if len(args) == 2 {
printAliases()
return
}
handleAliasCommand(args[2:])
case configCommand:
handleConfigCommand(args[2:])
case versionCommand:
l.Println(version)
case updateCommand:
updateZeus()
case infoCommand:
printProjectInfo()
case colorsCommand:
if len(args) == 3 {
handleColorsCommand(args[1:])
} else {
printColorsUsageErr()
}
case authorCommand:
handleAuthorCommand(args[1:])
case builtinsCommand:
printBuiltins()
case makefileCommand:
handleMakefileCommand(args[1:])
case gitFilterCommand:
handleGitFilterCommand(args[1:])
case createCommand:
handleCreateCommand(args[1:])
os.Exit(0)
default:
handleSignals(cmdFile)
cmdMap.Lock()
// check if the command exists
if cmd, ok := cmdMap.items[args[1]]; ok {
cmdMap.Unlock()
validCommand = true
count, err := getTotalDependencyCount(cmd)
if err != nil {
l.Println(err)
return
}
s.Lock()
s.numCommands = count
s.Unlock()
shellBusy = true
err = cmd.Run(args[2:], cmd.async)
if err != nil {
cLog.WithError(err).Error("failed to execute " + cmd.name)
cleanup(cmdFile)
os.Exit(1)
}
shellBusy = false
} else {
cmdMap.Unlock()
}
// check if its a commandChain supplied with "" or ''
if strings.Contains(args[1], commandChainSeparator) {
fields := strings.Split(args[1], commandChainSeparator)
if cmdChain, ok := validCommandChain(fields, false); ok {
shellBusy = true
cmdChain.exec(fields)
shellBusy = false
} else {
l.Println("invalid commandChain")
}
return
}
// check if its an alias
if command, ok := projectData.fields.Aliases[args[1]]; ok {
handleLine(command)
os.Exit(0)
}
if !validCommand {
if !testingMode {
cLog.Fatal("unknown command: ", args[1])
}
}
}
if !testingMode {
os.Exit(0)
}
}
}