-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakesure
executable file
·567 lines (491 loc) · 15.9 KB
/
makesure
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#!/bin/sh
VERSION="0.9.7.1"
exec awk -v "Version=$VERSION" -v "Prog=$0" '
BEGIN {
Shell = "bash" # default shell
SupportedShells["bash"]
SupportedShells["sh"]
SupportedOptions["tracing"]
SupportedOptions["silent"]
SupportedOptions["timing"]
Tmp = isDir("/dev/shm") ? "/dev/shm" : "/tmp"
split("",Lines)
split("",Args) # parsed CLI args
split("",ArgGoals) # invoked goals
split("",Options)
split("",GoalNames) # list
split("",GoalsByName) # name -> ""
split("",Code) # name -> body
split("",DefineOverrides) # k -> v
DefinesFile=""
split("",Dependencies) # name,i -> dep goal
split("",DependenciesCnt) # name -> dep cnd
split("",Doc) # name,i -> doc str
split("",DocCnt) # name -> doc lines cnt
split("",ReachedIf) # name -> condition line
split("",GlobFiles) # list
Mode = "prelude" # prelude/goal/goal_glob
srand()
prepareArgs()
MyDirScript = "MYDIR=" quoteArg(getMyDir(ARGV[1])) "; export MYDIR; cd \"$MYDIR\""
}
{ Lines[NR]=$0 }
"@options" == $1 { handleOptions(); next }
"@define" == $1 { handleDefine(); next }
"@shell" == $1 { handleShell(); next }
"@goal" == $1 { handleGoal(); next }
"@goal_glob" == $1 { handleGoalGlob(); next }
"@doc" == $1 { handleDoc(); next }
"@depends_on" == $1 { handleDependsOn(); next }
"@reached_if" == $1 { handleReachedIf(); next }
{ handleCodeLine($0); next }
END { if (!Died) doWork() }
function prepareArgs( i,arg) {
for (i = 2; i < ARGC; i++) {
arg = ARGV[i]
#print i " " arg
if (substr(arg,1,1) == "-") {
if (arg == "-f" || arg == "--file") {
delete ARGV[i]
ARGV[1] = ARGV[++i]
} else if (arg == "-D" || arg == "--define") {
delete ARGV[i]
handleOptionDefineOverride(ARGV[++i])
} else
Args[arg]
} else
arrPush(ArgGoals, arg)
delete ARGV[i] # https://unix.stackexchange.com/a/460375
}
if ("-h" in Args || "--help" in Args) {
print "makesure ver. " Version
print "Usage: makesure [options...] [-f buildfile] [goals...]"
print " -f,--file buildfile"
print " set buildfile to use (default Makesurefile)"
print " -l,--list list all available goals"
print " -d,--resolved list resolved dependencies to reach given goals"
print " -D \"var=val\",--define \"var=val\""
print " override @define values"
print " -s,--silent silent mode - only output what goals output"
print " -t,--timing display execution times for goals and total"
print " -x,--tracing enable tracing in bash/sh via `set -x`"
print " -v,--version print version and exit"
print " -h,--help print help and exit"
print " -U,--selfupdate update makesure to latest version"
realExit(0)
} else if ("-v" in Args || "--version" in Args) {
print Version
realExit(0)
} else if ("-U" in Args || "--selfupdate" in Args) {
selfUpdate()
realExit(0)
}
if (!isFile(ARGV[1])) {
if (isFile(ARGV[1] "/Makesurefile"))
ARGV[1] = ARGV[1] "/Makesurefile"
else
dieMsg("makesure file not found: " ARGV[1])
}
if ("-s" in Args || "--silent" in Args)
Options["silent"]
if ("-x" in Args || "--tracing" in Args)
Options["tracing"]
if ("-t" in Args || "--timing" in Args)
Options["timing"]
}
function dbgA(name, arr, i) { print "--- " name ": "; for (i in arr) print i " : " arr[i] }
function splitKV(arg, kv, n) {
n = index(arg, "=")
kv[0] = trim(substr(arg,1,n-1))
kv[1] = trim(substr(arg,n+1))
}
function handleOptionDefineOverride(arg, kv) {
handleDefineLine(arg)
splitKV(arg, kv)
DefineOverrides[kv[0]] = kv[1]
}
function handleOptions() {
checkPreludeOnly()
for (i=2; i<=NF; i++) {
if (!($i in SupportedOptions))
die("Option " $i " is not supported")
Options[$i]
}
}
function handleDefine( line,kv) {
$1 = ""
handleDefineLine($0)
}
function handleDefineLine(line, kv) {
checkPreludeOnly()
if (!DefinesFile)
DefinesFile = executeGetLine("mktemp " Tmp "/makesure.XXXXXXXXXX")
splitKV(line, kv)
if (!(kv[0] in DefineOverrides)) {
handleCodeLine(line)
handleCodeLine("echo " quoteArg(line) " >> " DefinesFile)
}
}
function handleShell() {
checkPreludeOnly()
Shell = trim($2)
if (!(Shell in SupportedShells))
die("Shell '\''" Shell "'\'' is not supported")
}
function adjustOptions() {
if ("silent" in Options)
delete Options["timing"]
}
function started(mode) {
if (isPrelude()) adjustOptions()
Mode = mode
}
function handleGoal() {
started("goal")
registerGoal($2)
}
function registerGoal(goal_name) {
goal_name = trim(goal_name)
if (length(goal_name) == 0) {
die("Goal must have a name")
}
if (goal_name in GoalsByName) {
die("Goal " goal_name " is already defined")
}
arrPush(GoalNames, goal_name)
GoalsByName[goal_name]
}
function calcGlob(pattern, script, file) {
split("",GlobFiles)
script = MyDirScript "; for f in ./" pattern " ; do test -e \"$f\" && echo \"$f\" ; done"
while (script | getline file) {
file = substr(file, 3)
arrPush(GlobFiles,file)
}
close(script)
}
function handleGoalGlob( goal_name,i) {
started("goal_glob")
$1 = ""
calcGlob(trim($0))
for (i=0; i<arrLen(GlobFiles); i++){
registerGoal(GlobFiles[i])
}
}
function handleDoc( i) {
checkGoalOnly()
if ("goal" == Mode)
registerDoc(currentGoalName())
else {
for (i=0; i<arrLen(GlobFiles); i++){
registerDoc(GlobFiles[i])
}
}
}
function registerDoc(goal_name) {
$1 = ""
Doc[goal_name, DocCnt[goal_name]++] = trim($0)
}
function handleDependsOn( i) {
checkGoalOnly()
if ("goal" == Mode)
registerDependsOn(currentGoalName())
else {
for (i=0; i<arrLen(GlobFiles); i++){
registerDependsOn(GlobFiles[i])
}
}
}
function registerDependsOn(goal_name, i) {
for (i=2; i<=NF; i++) {
Dependencies[goal_name, DependenciesCnt[goal_name]++] = $i
}
}
function handleReachedIf( i) {
checkGoalOnly()
if ("goal" == Mode)
registerReachedIf(currentGoalName())
else {
for (i=0; i<arrLen(GlobFiles); i++){
registerReachedIf(GlobFiles[i], makeGlobVarsCode(i))
}
}
}
function makeGlobVarsCode(i) {
return "ITEM=" quoteArg(GlobFiles[i]) "; INDEX=" i "; TOTAL=" arrLen(GlobFiles) "; "
}
function registerReachedIf(goal_name, pre_script) {
if (goal_name in ReachedIf) {
die("Multiple " $1 " not allowed for a goal")
}
$1 = ""
ReachedIf[goal_name] = pre_script trim($0)
}
function doWork(\
i,j,goal_name,dep_cnt,dep,reached_if,reached_goals,empty_goals,defines_line,
body,goal_body,goal_bodies,resolved_goals,exit_code, t0,t1,t2, goal_timed) {
started("end") # end last directive
if ("-l" in Args || "--list" in Args) {
print "Available goals:"
for (i = 0; i < arrLen(GoalNames); i++) {
goal_name = GoalNames[i]
print " " goal_name
if (goal_name in DocCnt) {
for (j = 0; j < DocCnt[goal_name]; j++)
print " " Doc[goal_name, j]
}
}
} else {
if ("timing" in Options)
t0 = currentTimeMillis()
# run prelude first to process all @defines
goal_body[0] = MyDirScript
if ("tracing" in Options)
addLine(goal_body, "set -x")
addLine(goal_body, trim(Code[""]))
exit_code = shellExec(goal_body[0])
if (exit_code != 0)
realExit(exit_code)
addLine(defines_line, MyDirScript)
if (DefinesFile) {
addLine(defines_line, ". " DefinesFile)
}
for (i = 0; i < arrLen(GoalNames); i++) {
goal_name = GoalNames[i]
body = trim(Code[goal_name])
reached_if = ReachedIf[goal_name]
reached_goals[goal_name] = reached_if ? checkConditionReached(defines_line[0], reached_if) : 0
empty_goals[goal_name] = length(body) == 0
# check valid dependencies
dep_cnt = DependenciesCnt[goal_name]
for (j=0; j < dep_cnt; j++) {
dep = Dependencies[goal_name, j]
if (!(dep in GoalsByName))
dieMsg("Goal '\''" goal_name "'\'' has unknown dependency '\''" dep "'\''") # TODO find a way to provide line reference
if (!reached_goals[goal_name]) {
# we only add a dependency to this goal if it'\''s not reached
#print " [not reached] " goal_name " -> " dep
topologicalSortAddConnection(goal_name, dep)
} else {
#print " [ reached] " goal_name " -> " dep
}
}
goal_body[0] = ""
if (!("silent" in Options)) {
addStr(goal_body, "echo \" goal '\''" goal_name "'\'' ")
if (reached_goals[goal_name])
addStr(goal_body, "[already satisfied].")
else if (empty_goals[goal_name])
addStr(goal_body, "[empty].")
else
addStr(goal_body, "...")
addStr(goal_body, "\"")
}
if (reached_goals[goal_name])
addLine(goal_body, "exit 0")
addLine(goal_body, defines_line[0])
if ("tracing" in Options)
addLine(goal_body, "set -x")
addLine(goal_body, body)
goal_bodies[goal_name] = goal_body[0]
}
resolveGoalsToRun(resolved_goals)
if ("-d" in Args || "--resolved" in Args) {
printf("Resolved goals to reach for '\''%s'\'':\n", join(ArgGoals, 0, arrLen(ArgGoals), " "))
for (i = 0; i < arrLen(resolved_goals); i++) {
print " " resolved_goals[i]
}
} else {
for (i = 0; i < arrLen(resolved_goals); i++) {
goal_name = resolved_goals[i]
goal_timed = "timing" in Options && !reached_goals[goal_name] && !empty_goals[goal_name]
if (goal_timed)
t1 = t2 ? t2 : currentTimeMillis()
exit_code = shellExec(goal_bodies[goal_name])
if (exit_code != 0)
print " goal '\''" goal_name "'\'' failed"
if (goal_timed) {
t2 = currentTimeMillis()
print " goal '\''" goal_name "'\'' took " renderDuration(t2 - t1)
}
if (exit_code != 0)
break
}
if ("timing" in Options)
print " total time " renderDuration((t2 ? t2 : currentTimeMillis()) - t0)
if (exit_code != 0)
realExit(exit_code)
}
realExit(0)
}
}
function resolveGoalsToRun(result, i, goal_name, loop) {
if (arrLen(ArgGoals) == 0)
arrPush(ArgGoals, "default")
for (i = 0; i < arrLen(ArgGoals); i++) {
goal_name = ArgGoals[i]
if (!(goal_name in GoalsByName)) {
dieMsg("Goal not found: " goal_name) # TODO can we show line number here?
}
topologicalSortPerform(goal_name, result, loop)
}
if (loop[0] == 1) {
dieMsg("There is a loop in goal dependencies via " loop[1] " -> " loop[2])
}
}
function isPrelude() { return "prelude"==Mode }
function checkPreludeOnly() { if (!isPrelude()) die("Only use " $1 " in prelude") }
function checkGoalOnly() { if ("goal" != Mode && "goal_glob" != Mode) die("Only use " $1 " in goal/goal_glob") }
function currentGoalName() { return isPrelude() ? "" : arrLast(GoalNames) }
function realExit(code, i) {
Died = 1
if (DefinesFile)
system("rm " DefinesFile)
exit code
}
function die(msg, n) { if (!n) n=NR; dieMsg(msg ":\n" ARGV[1] ":" n ": " Lines[n]) }
function dieMsg(msg, out) {
out = "cat 1>&2" # trick to write from awk to stderr
print msg | out
close(out)
realExit(1)
}
function checkConditionReached(defines_line, condition_str, script) {
script = defines_line # need this to initialize variables for check conditions
script = script "\n" condition_str
#print "script: " script
return shellExec(script) == 0
}
function shellExec(script, res) {
script = Shell " -e -c " quoteArg(script)
#print script
res = system(script)
#print "res " res
return res
}
function getMyDir(makesurefilePath) {
return executeGetLine("cd \"$(dirname " quoteArg(makesurefilePath) ")\"; pwd")
}
function handleCodeLine(line, goal_name) {
if ("goal_glob" == Mode) {
for (i=0; i<arrLen(GlobFiles); i++){
if (!Code[goal_name = GlobFiles[i]])
addCodeLine(goal_name, makeGlobVarsCode(i))
addCodeLine(goal_name, line)
}
} else
addCodeLine(currentGoalName(), line)
}
function addCodeLine(name, line) {
#print "Append line for '\''" name "'\'': " line
Code[name] = addL(Code[name], line)
}
function topologicalSortAddConnection(from, to) {
# Slist - list of successors by node
# Scnt - count of successors by node
Slist[from, ++Scnt[from]] = to # add '\''to'\'' to successors of '\''from'\''
}
function topologicalSortPerform(node, result, loop, i, s) {
if (Visited[node] == 2)
return
Visited[node] = 1
for (i = 1; i <= Scnt[node]; i++) {
if (Visited[s = Slist[node, i]] == 0)
topologicalSortPerform(s, result, loop)
else if (Visited[s] == 1) {
loop[0] = 1
loop[1] = s
loop[2] = node
}
}
Visited[node] = 2
arrPush(result, node)
}
function currentTimeMillis( script, res) {
res = executeGetLine("date +%s%3N")
sub(/%3N/, "000", res) # if date doesn'\''t support %N (macos?) just use second-precision
return res + 0
}
function selfUpdate( url, tmp, err, newVer) {
url = "https://raw.githubusercontent.com/xonixx/makesure/main/makesure_stable?" rand()
tmp = executeGetLine("mktemp /tmp/makesure_new.XXXXXXXXXX")
err = dl(url, tmp)
if (!err && !ok("chmod +x " tmp)) err = "can'\''t chmod +x " tmp
if (!err) {
newVer = executeGetLine(tmp " -v")
if (Version != newVer) {
if (!ok("cp " tmp " " quoteArg(Prog)))
err = "can'\''t overwrite " Prog
else print "updated " Version " -> " newVer
} else print "you have latest version " Version " installed"
}
system("rm " tmp)
if (err) dieMsg(err);
}
function renderDuration(deltaMillis,\
deltaSec,deltaMin,deltaHr,deltaDay,dayS,hrS,minS,secS,secSI,res) {
deltaSec = deltaMillis / 1000
deltaMin = 0
deltaHr = 0
deltaDay = 0
if (deltaSec >= 60) {
deltaMin = int(deltaSec / 60)
deltaSec = deltaSec - deltaMin * 60
}
if (deltaMin >= 60) {
deltaHr = int(deltaMin / 60)
deltaMin = deltaMin - deltaHr * 60
}
if (deltaHr >= 24) {
deltaDay = int(deltaHr / 24)
deltaHr = deltaHr - deltaDay * 24
}
dayS = deltaDay > 0 ? deltaDay " d" : ""
hrS = deltaHr > 0 ? deltaHr " h" : ""
minS = deltaMin > 0 ? deltaMin " m" : ""
secS = deltaSec > 0 ? deltaSec " s" : ""
secSI = deltaSec > 0 ? int(deltaSec) " s" : ""
if (dayS != "")
res = dayS " " (hrS == "" ? "0 h" : hrS)
else if (deltaHr > 0)
res = hrS " " (minS == "" ? "0 m" : minS)
else if (deltaMin > 0)
res = minS " " (secSI == "" ? "0 s" : secSI)
else
res = deltaSec > 0 ? secS : "0 s"
return res
}
function executeGetLine(script, res) {
script | getline res
close(script)
return res
}
function dl(url, dest, verbose) {
verbose = "VERBOSE" in ENVIRON
if (commandExists("wget")) {
if (!ok("wget " (verbose ? "" : "-q") " " quoteArg(url) " -O" quoteArg(dest)))
return "error with wget"
} else if (commandExists("curl")) {
if (!ok("curl " (verbose ? "" : "-s") " " quoteArg(url) " -o " quoteArg(dest)))
return "error with curl"
} else return "wget/curl no found"
}
function join(arr, start_incl, end_excl, sep, result, i) {
result = arr[start_incl]
for (i = start_incl + 1; i < end_excl; i++)
result = result sep arr[i]
return result
}
function addStr(target, str) { target[0] = target[0] str }
function addLine(target, line) { target[0] = addL(target[0], line) }
function addL(s, l) { return s ? s "\n" l : l }
function arrPush(arr, elt) { arr[arr[-7]++] = elt }
function arrLen(arr) { return 0 + arr[-7] }
function arrLast(arr) { return arr[arrLen(arr)-1] }
function commandExists(cmd) { return ok("which " cmd " 2>&1 >/dev/null") }
function ok(cmd) { return system(cmd) == 0 }
function isFile(path) { return ok("test -f " quoteArg(path)) }
function isDir(path) { return ok("test -d " quoteArg(path)) }
function quoteArg(a) { gsub("'\''", "'\''\\'\'''\''", a); return "'\''" a "'\''" }
function trim(s) { sub(/^[ \t\r\n]+/, "", s); sub(/[ \t\r\n]+$/, "", s); return s; }
' Makesurefile "$@"