-
Notifications
You must be signed in to change notification settings - Fork 1
/
ojob.yaml
2305 lines (2126 loc) · 75.5 KB
/
ojob.yaml
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# oJob common jobs
# Copyright 2023 Nuno Aguiar
jobs:
# OJOB PASS
# ----------------
- name : ojob pass
typeArgs:
noLog: true
shortcut:
name : pass
keyArg: __args
args :
debug : __debug
templateArgs: __templateArgs
help :
text : Placeholder/pass job to allow for arguments injection
expects:
- name: __args
desc: The args to inject
- name: __debug
desc: Boolean to indicate that args to be inject should be printed before
- name: __templateArgs
desc: Boolean, true by default, to indicate to apply template to each string in args (use only if typeArgs.noTemplateArgs = false OR job.templateArgs = true)
check:
in:
__args : isMap.default({})
__debug : toBoolean.isBoolean.default(false)
__templateArgs: toBoolean.isBoolean.default(true)
exec : |
var ar = clone(args.__args)
// Process templateArgs
if (args.__templateArgs) {
traverse(ar, (aK, aV, aP, aO) => {
if (isNull(aV)) aO[aK] = __
if (isString(aV) && aV.indexOf("{{") >= 0) {
aO[aK] = templify(aV, args)
}
})
} else {
// Process nulls
traverse(ar, (aK, aV, aP, aO) => {
if (isNull(aV)) aO[aK] = __
})
}
if (args.__debug) cprint( ow.format.withSideLine(printTreeOrS(ar, __, { bgcolor: "BG(235)" }), __, "FG(240)", "BG(235)", ow.format.withSideLineThemes().openBottomCurvedRect) )
delete args.__argsp
delete args.__debug
delete args.__templateArgs
args = merge(args, ar)
# OJOB DEBUG
# ----------------
- name : ojob debug
typeArgs:
noLog : true
shortcut:
name : jobdebug
keyArg: job
args :
lineColor : lineColor
textColor : textColor
theme : theme
emoticons : emoticons
signs : signs
includeTime: includeTime
help :
text: Outputs the current args and res values to help debug an ojob flow.
exec : |
ow.loadFormat()
print("")
cprint( ow.format.withSideLine("ARGS:", __, "FG(240)", "BG(240),BOLD", ow.format.withSideLineThemes().openBottomCurvedRect) )
cprint( ow.format.withSideLine(colorify(args, {bgcolor:"BG(235)"}), __, "FG(240)", "BG(235)", ow.format.withSideLineThemes().openTopCurvedRect) )
cprint( ow.format.withSideLine("RES:", __, "FG(240),BOLD", "BG(240),BOLD", ow.format.withSideLineThemes().openBottomCurvedRect) )
cprint( ow.format.withSideLine(colorify($get("res"), {bgcolor:"BG(235)"}), __, "FG(240)", "BG(235)", ow.format.withSideLineThemes().openTopCurvedRect) )
# --------------------
- name: ojob job debug
typeArgs:
noLog: true
shortcut:
name : jobsdebug
keyArg: jobs
args :
lineColor : lineColor
textColor : textColor
theme : theme
emoticons : emoticons
signs : signs
includeTime: includeTime
help:
text : |
Provides an alternative to print based debug.
Example:
# ----------------
- name: Sample job
exec: |
//@ Declaring array
var ar = [ 0, 1, 2, 3, 4, 5 ]
//@ Start cycle
var ii = 0;
while(ii < ar.length) {
print("II = " + ii)
ii++
//# ii == 3
}
//@ End cycle
//? ii
//?s args
//?y args
expects:
- name: job
desc: The job to change to include debug
- name: jobs
desc: The jobs array to change to include debug
- name: lineColor
desc: The line color around the debug info
- name: textColor
desc: The text color around the debug info
- name: theme
desc: The withSideLineThemes theme to use
- name: emoticons
desc: If emoticons should be used or not
- name: signs
desc: "A custom map of emoticons (keys: checkpoint, assert and print)"
- name: includeTime
desc: A boolean value to indicate if a time indication should be included
check:
in:
job : isString.default(__)
jobs : isArray.default([])
lineColor : isString.default("FG(220)")
textColor : isString.default("BG(230),BLACK")
lineError : isString.default("FG(220)")
textError : isString.default("BG(196),FG(255),BOLD")
theme : isString.default("doubleLineBothSides")
emoticons : toBoolean.isBoolean.default(true)
signs : isMap.default(__)
includeTime: toBoolean.isBoolean.default(false)
exec: |
var OAF_DEBUG_ARGS
if (isMap(OAF_DEBUG_ARGS)) args = merge(OAF_DEBUG_ARGS, args)
args.signs = _$(args.signs, "signs").isMap().default({
checkpoint: 0x1F37A,
assert : 0x1F44D,
print : 0x1F50E,
error : 0x1F621,
time : "🕒"
})
if (isDef(args.job) && args.jobs.length == 0) args.jobs.push(args.job)
if (isUnDef(getEnv("OJOB_DEBUG")) || toBoolean(getEnv("OJOB_DEBUG"))) {
ow.loadFormat()
var ch = ow.oJob.getJobsCh()
if (args.jobs.length == 0) args.jobs = ch.getKeys().filter(j => j.name != "ojob job debug").map(j => j.name)
var owd = false
if (isDef(ow.loadDebug)) {
ow.loadDebug()
ow.debug.register()
owd = true
}
args.jobs.forEach(jj => {
var job = ch.get({ name: jj })
if (owd) {
if (isUnDef(job.lang) || (isDef(job.lang) && (job.lang == "oaf" || job.lang == "js") ) ) {
job.exec = ow.debug.debug(job.exec, args, true)
}
} else {
var _m = (s, c) => {
var _s = ";try{"
if (isDef(c)) _s += "if(" + c + ") {"
var _t = (args.includeTime ? "(new Date()).toISOString() +\" | \" + " : "")
_s += "cprint(ow.format.withSideLine(" + _t + s + ", __, \"" + args.lineColor + "\", \"" + args.textColor + "\", ow.format.withSideLineThemes()." + args.theme + ")) "
if (isDef(c)) _s += "}"
_s += "}catch(__e_debug){cprint(ow.format.withSideLine(" + _t + "' " + sign.error + " ' + String(__e_debug), __, \"" + args.lineError + "\", \"" + args.textError + "\", ow.format.withSideLineThemes()." + args.theme + "))};"
return _s
}
var sign
if (args.emoticons) {
sign = args.signs
if (isNumber(sign.checkpoint)) sign.checkpoint = ow.format.string.unicode(sign.checkpoint)
if (isNumber(sign.assert)) sign.assert = ow.format.string.unicode(sign.assert)
if (isNumber(sign.print)) sign.print = ow.format.string.unicode(sign.print)
if (isNumber(sign.error)) sign.error = ow.format.string.unicode(sign.error)
}
sign.checkpoint = _$(sign.checkpoint).default("@")
sign.assert = _$(sign.assert).default("#")
sign.print = _$(sign.print).default("?")
sign.error = _$(sign.error).default("!")
job.exec = job.exec.split("\n").map(line => {
var l
// checkpoint equivalent
l = line.trim().match(/\/\/\@ (.+)$/)
if (isArray(l)) {
var s = l[1]
line = line.replace(/\/\/\@ (.+)$/, _m("\" " + sign.checkpoint + " " + s.replace(/\"/g, "\\\"") + "\""))
}
// assert equivalent
l = line.trim().match(/\/\/\# (.+)$/)
if (isArray(l)) {
var s = l[1]
line = line.replace(/\/\/\# (.+)$/, _m("\" " + sign.assert + " " + s + "\"", s))
}
// print equivalent
l = line.trim().match(/\/\/\? (.+)$/)
if (isArray(l)) {
var s = l[1]
line = line.replace(/\/\/\? (.+)$/, _m("\" "+ sign.print + " " + s + " = \" + stringify(" + s + ") + \"\""))
}
// slon print equivalent
l = line.trim().match(/\/\/\?s (.+)$/)
if (isArray(l)) {
var s = l[1]
line = line.replace(/\/\/\?s (.+)$/, _m("\" " + sign.print + " " + s + " = \" + af.toSLON(" + s + ") + \"\""))
}
// yaml print equivalent
l = line.trim().match(/\/\/\?y (.+)$/)
if (isArray(l)) {
var s = l[1]
line = line.replace(/\/\/\?y (.+)$/, _m("\" " + sign.print + " " + s + " = \" + af.toYAML(" + s + ") + \"\""))
}
return line
}).join("\n")
}
ch.set({ name: jj }, job)
})
}
# OJOB GET PM
# ------------------
- name : ojob get pm
args :
__key: args
check:
in:
__key: isString.default("res")
exec : |
var out = __pm
if (isDef(__pm._map)) out = __pm._map
if (isDef(__pm._list)) out = __pm._list
if (args.__key == "args") {
args = merge(args, out)
} else {
$set(args.__key, out)
}
// Delete all __ args
Object.keys(args).filter(k => k.startsWith("__")).forEach(k => delete args[k])
# OJOB GET
# ---------------
- name : ojob get
typeArgs:
shortcut:
name : get
keyArg: __key
args :
path: __path
help :
text : Retrieves a specific map key (or path) using $get
expects:
- name: __key
desc: Map key to retrieve (key is also checked for compatibility)
- name: __path
desc: The path to consider from the __key
check:
in:
__key : isString.default("res")
__path: isString.default("@")
exec : |
if (isUnDef(args.__key) && isDef(args.key)) args.__key = args.key
var res = args.__key == "args" ? $path(args, args.__path) : $path($get(args.__key), args.__path)
if (isArray(res)) res = { _list: res }
args = merge(args, res)
// Delete all __ args
Object.keys(args).filter(k => k.startsWith("__")).forEach(k => delete args[k])
# OJOB SET
# ---------------
- name : ojob set
typeArgs:
shortcut:
name : set
keyArg: __key
args :
path: __path
help :
text : Sets a "key" with the current value on a "path" using $set
expects:
- name: __key
desc: Map key
- name: __path
desc: A key or path to a value from the current args
check:
in:
__key : isString.default("res")
__path: isString
exec : |
var r = $path(args, args.__path)
if (isDef(r) && args.__key != "args") $set(args.__key, r)
// Delete all __ args
Object.keys(args).filter(k => k.startsWith("__")).forEach(k => delete args[k])
# OJOB UNSET
# -----------------
- name : ojob unset
typeArgs:
shortcut:
name : unset
keyArg: __key
help :
text : Unsets a "key" using $unset
expects:
- name: __key
desc: Map key
check :
in:
__key: isString.default("res")
exec : |
$unset(args.__key)
// Delete all __ args
Object.keys(args).filter(k => k.startsWith("__")).forEach(k => delete args[k])
# OJOB FILE GET
# --------------------
- name : ojob file get
typeArgs:
noLog : true
shortcut:
name : fileget
keyArg: __file
args :
path : __path
cache: __cache
ttl : __ttl
out : __out
key : __key
help :
text : "Retrieves a specific map key (or path) from an YAML or JSON file provided."
expects:
- name : __file
desc : The file path to an YAML or JSON file
required: true
- name: __path
desc: Path of the file contents
- name: __cache
desc: "Boolean value that if false it won't cache the file contents (default: true)"
- name: __ttl
desc: If cache is enabled lets you define a numeric ttl
- name: __out
desc: The path on args to set the map key/path contents
- name: __key
desc: If __out is not defined will set the content into the provided key
check:
in:
__file : isString
__path : isString.default("@")
__cache: toBoolean.isBoolean.default(true)
__ttl : isNumber.default(__)
__out : isString.default(__)
__key : isString.default("res")
exec : |
var res;
if (args.__cache) {
var c = $cache("__ojob_file_get");
if (isDef(args.__ttl)) c= c.ttl(args.__ttl);
c = c.fn(aK => {
if (isDef(aK.file)) {
if (aK.file.endsWith(".json")) {
return io.readFileJSON(aK.file);
} else {
if (/\.ya?ml$/.test(aK.file)) {
return io.readFileYAML(aK.file);
} else {
return { error: "Not an yaml, yml or json file."};
}
}
} else {
return {}
}
})
.create();
res = c.get({ file: args.__file });
} else {
if (args.__file.endsWith(".json")) {
res = io.readFileJSON(args.__file);
} else {
if (/\.ya?ml$/.test(args.__file)) {
res = io.readFileYAML(args.__file);
} else {
res = { error: "Not an yaml, yml or json file."};
}
}
}
res = $path(res, args.__path)
if (isDef(args.__out)) {
$$(args).set(args.__out, res)
} else {
if (isDef(args.__key)) {
$set(args.__key, res)
} else {
Object.keys(res).forEach(r => $$(args).set(r, res[r]))
}
}
// Delete all __ args
Object.keys(args).filter(k => k.startsWith("__")).forEach(k => delete args[k])
# OJOB TEMPLATE
# --------------------
- name : ojob template
typeArgs:
shortcut:
name : template
keyArg: template
args :
templateFile: templateFile
data : data
dataFile : dataFile
outputFile : outputFile
key : __key
tpath : __tpath
dpath : __dpath
outPath : __outPath
out : __out
help :
text: Applies the OpenAF template over the provided data producing an output.
expects:
- name: __key
desc: The key that holds template and/or data (default to 'res'). If 'args' it will use the current arguments.
- name: __tpath
desc: The path in __key where a string with the template can be found.
- name: __dpath
desc: The path in __key where a map/array data to use can be found.
- name: __out
desc: The output will be stored into the provided key (defaults to 'res')
- name: __outPath
desc: If defined the $set path where __out will be set in the provided key.
- name: template
desc: If defined, will be used as template
- name: templateFile
desc: If defined, it will use the provided template file.
- name: data
desc: If defined, will be used as data
- name: dataFile
desc: If defined, it will use the provided data file (either yaml or json).
- name: outputFile
desc: If defined, the output will be written to the provided file path.
returns:
- name: output
desc: If no outputFile is provided this will hold the output of applying the template with the provided data
check:
in:
__key : isString.default("res")
__tpath : isString.default("@")
__dpath : isString.default("@")
__out : isString.default("res")
__outPath : isString.default("")
templateFile: isString.default(__)
dataFile : isString.default(__)
outputFile : isString.default(__)
exec : |
ow.loadTemplate()
ow.template.addConditionalHelpers()
ow.template.addFormatHelpers()
ow.template.addOpenAFHelpers()
var data, template
if (isDef(args.data)) {
data = args.data
} else {
data = args.__key == "args" ? $path(args, args.__dpath) : $path($get(args.__key), args.__dpath)
}
if (isDef(args.dataFile)) {
if (args.dataFile.match(/\.ya?ml$/i)) {
data = io.readFileYAML(args.dataFile)
} else {
data = io.readFileJSON(args.dataFile)
}
}
if (isDef(args.template)) {
template = args.template
} else {
template = args.__key == "args" ? $path(args, args.__tpath) : $path($get(args.__key), args.__tpath)
}
if (isDef(args.templateFile)) {
template = io.readFileString(args.templateFile)
}
if (isDef(args.logJob)) $job(args.logJob, args)
var _r = templify(template, data)
if (isDef(args.outputFile)) {
io.writeFileString(args.outputFile, _r)
} else {
if (args.__out == "args") {
if (isDef(args.__outPath) && args.__outPath.length > 0) $$(args).set(args.__outPath, _r); else args.output = _r
} else {
var __r = {}
if (isDef(args.__outPath) && args.__outPath.length > 0) $$(__r).set(args.__outPath, _r); __r.output = _r
$set(args.__out, __r)
}
}
# ---------------------------
- name : ojob template folder
typeArgs:
shortcut:
name : templateFolder
keyArg: templateFolder
args :
templatePath: __templatePath
data : data
dataFile : dataFile
outputFolder: outputFolder
key : __key
dpath : __dpath
logJob : logJob
metaTemplate: metaTemplate
help :
text: >
Given a templateFolder it will execute 'ojob template' for each (recursively), with the provided data, to output to outputFolder.
Optionally metaTemplate can be use where each json/yaml file in templateFolder all or part of the arguments for 'ojob template'.
expects:
- name: templateFolder
desc: The original folder where the templates are located.
required: true
- name: __templatePath
desc: If defined, will apply a $path string over the recursive list of files in templateFolder.
- name: outputFolder
desc: The path where the output should be stored.
required: true
- name: data
desc: If defined, will be used as data
- name: dataFile
desc: If defined, it will use the provided data file (either yaml or json).
- name: __key
desc: The key that holds template and/or data (default to 'res'). If 'args' it will use the current arguments.
- name: __dpath
desc: The path in __key where a map/array data to use can be found.
- name: logJob
desc: A ojob job to log the 'ojob template' activity (receives the same arguments as 'ojob template')
- name: metaTemplate
desc: Boolean that if 'true' will interpret any json/yaml file, in the templateFolder, as a map/array of arguments to use when calling 'ojob template' overriden the defaults.
check:
in:
templateFolder: isString
__templatePath: isString.default("@")
outputFolder : isString
dataFile : isString.default(__)
__key : isString.default("res")
__dpath : isString.default("@")
logJob : isString.default(__)
metaTemplate : toBoolean.isBoolean.default(false)
exec : |
var data
if (isDef(args.data)) {
data = args.data
} else {
data = args.__key == "args" ? $path(args, args.__dpath) : $path($get(args.__key), args.__dpath)
}
if (isDef(args.dataFile)) {
if (args.dataFile.match(/\.ya?ml$/i)) {
data = io.readFileYAML(args.dataFile)
} else {
data = io.readFileJSON(args.dataFile)
}
}
var lst = []
$path(listFilesRecursive(args.templateFolder), args.__templatePath).forEach(f => {
var _d = {
templateFile : f.filepath,
outputFile : f.filepath.replace(new RegExp("^" + args.templateFolder), args.outputFolder),
data : data,
logJob : args.logJob
}
if (args.metaTemplate && f.filename.match(/\.(ya?ml|json)$/i)) {
var _t
if (f.filename.match(/\.ya?ml$/i)) {
_t = io.readFileYAML(f.filepath)
} else {
_t = io.readFileJSON(f.filepath)
}
delete _d.templateFile
if (isMap(_t)) {
if (isDef(_t.outputFile) && !_t.outputFile.startsWith("/")) _t.outputFile = args.outputFolder + "/" + _t.outputFile
lst.push(merge(_d, _t))
}
if (isArray(_t)) {
lst = lst.concat(_t.map(_r => {
if (isDef(_r.outputFile) && !_r.outputFile.startsWith("/")) _r.outputFile = args.outputFolder + "/" + _r.outputFile
return merge(_d, _r)
}))
}
} else {
lst.push(_d)
}
})
io.mkdir(args.outputFolder)
$job("ojob template", lst)
# ------------------------
- name : ojob find/replace
typeArgs:
noLog : true
shortcut:
name : findReplace
keyArg: __key
args :
path : __path
inputKey : inputKey
inputPath : inputPath
inputFile : inputFile
outputFile : outputFile
useRegExp : useRegExp
flagsRegExp: flagsRegExp
logJob : logJob
help :
text : Performs an in-memory find/replace on a provided string or file and outputs to args.output or, optionally, to a file.
expects:
- name: __key
desc: The key that holds template and/or data (default to 'res'). If 'args' it will use the current arguments.
- name: __path
desc: The path in __key where a map of replacements ([text/regexp]:[replace text]) can be found.
- name: inputKey
desc: If defined, indicates the key that holds the string of data to replace.
- name: inputPath
desc: If defined with inputKey, indicates the path to use to select the string of data to replace.
- name: inputFile
desc: If defined the contents to be replaced will be read from the inputFile.
- name: outputFile
desc: If defined will output of the content replacement to the defined file.
- name: useRegExp
desc: Boolean value to determine if the map of replacements will be interpreted as a regexp or text.
- name: flagsRegExp
desc: If useRegExp=true the JavaScript reg exp flags to use (defaults to 'g')
- name: logJob
desc: Optionally provide a logging job with the current args and __op with 'read' or 'write'
returns:
- name: output
desc: If outputFile is not defined the output will contain the content replacement
check:
in:
__key : default("res")
__path : isString.default("@")
inputKey : isString.default(__)
inputPath : isString.default(__)
inputFile : isString.default(__)
outputFile : isString.default(__)
useRegExp : toBoolean.isBoolean.default(false)
flagsRegExp: isString.default("g")
logJob : default(__)
exec : |
var data
if (isMap(args.__key)) {
data = args.__key
} else {
data = $path(args.__key == "args" ? args : $get(args.__key), args.__path)
}
_$(data, "__key data").isMap().$_()
if (isUnDef(args.inputKey) && isUnDef(args.inputFile)) throw "Need args inputKey or inputFile"
var escapeRegExp = aString => aString.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
args.__op = "read"
if (isDef(args.logJob)) $job(args.logJob, args)
var content
if (isDef(args.inputKey)) {
content = $path(args.inputKey == "args" ? args : $get(args.inputKey), args.inputPath)
} else {
content = io.readFileString(args.inputFile)
}
Object.keys(data).forEach(k => {
content = content.replace(new RegExp(args.useRegExp ? k : escapeRegExp(k), (args.useRegExp ? args.flagsRegExp : "g")), data[k])
})
args.__op = "write"
if (isDef(args.logJob)) $job(args.logJob, args)
if (isDef(args.outputFile)) {
io.writeFileString(args.outputFile, content)
} else {
args.output = content
}
# OJOB CHANNEL
# ------------
- name : ojob channel
typeArgs:
shortcut:
name : ch
keyArg: __name
args :
op : __op
key : __key
kpath: __kpath
k : key
ks : keys
v : value
vs : values
vpath: __vpath
extra: extra
help:
text: Provides a set of operations over an OpenAF channel
expects:
- name: __name
desc: The name of the OpenAF channel to use
required: true
- name: __op
desc: The operation to perform (e.g. setall, set, get, unset, unsetall, getkeys, getall and size)
required: true
- name: __key
desc: The key from where to retrieve the operation arguments (args to retrive from arguments)
- name: __kpath
desc: If defined, the path over the values retrieved from __key where the key or keys of the operation are defined
- name: key
desc: If defined, the key to use with the operations set, get and unset
- name: keys
desc: If defined, the set of fields to use with the operations setall and unsetall
- name: value
desc: If defined, the value to use with the operations set, get and unset
- name: values
desc: If defined, an array of values to use with the operations setall and unsetall
- name: __vpath
desc: If defined, the path over the values retrieved from __key where the value or values of the operation are defined
- name: extra
desc: If defined, will provide an extra argument (usually a map), depending on channel type, for the getall and getkeys operations.
returns:
- name: _list
desc: If __key == 'args' will return the results of getall and getkeys
- name: _map
desc: If __key == 'args' will return the results of get
- name: size
desc: If __key == 'args' will return the size of the channel
check:
in:
__name : isString
__op : isString
key : default(__)
keys : default(__)
value : default(__)
values : default(__)
__kpath : isString.default(__)
__vpath : isString.default(__)
extra : default(__)
__key : default("res")
exec : |
var getKV = () => {
var r = { k: __, v: __ }
if (isDef(args.key)) r.k = args.key
if (isDef(args.keys)) r.k = args.keys
if (isDef(args.value)) r.v = args.value
if (isDef(args.values)) r.v = args.values
if (args.__key == "args") {
if (isDef(args.__kpath)) r.k = $path(args, args.__kpath)
if (isDef(args.__vpath)) r.v = $path(args, args.__vpath)
} else {
if (isDef(args.__kpath)) r.k = $path($get(args.__key), args.__kpath)
if (isDef(args.__vpath)) r.v = $path($get(args.__key), args.__vpath)
}
return r
}
switch (args.__op.toLowerCase()) {
case "getall":
var _res = $ch(args.__name).getAll(args.extra)
if (isUnDef(_res)) _res = []
if (args.__key != "args") $set(args.__key, _res); else args._list = _res
break
case "setall":
var _r = getKV()
$ch(args.__name).setAll(_r.k, _r.v)
break
case "set":
var _r = getKV()
$ch(args.__name).set(_r.k, _r.v)
break
case "get":
var _r = getKV()
var _res = $ch(args.__name).get(_r.k)
if (isUnDef(_res)) _res = {}
if (args.__key != "args") $set(args.__key, _res); else args._map = _res
break
case "unset":
var _r = getKV()
$ch(args.__name).unset(_r.k)
break
case "unsetall":
var _r = getKV()
$ch(args.__name).unsetAll(_r.k, _r.v)
break
case "getkeys":
var _res = $ch(args.__name).getKeys(args.extra)
if (isUnDef(_res)) _res = []
if (args.__key != "args") $set(args.__key, _res); else args._list = _res
break
case "size":
var _res = $ch(args.__name).size()
if (isUnDef(_res)) _res = -1
if (args.__key != "args") $set(args.__key, _res); else args.size = _res
}
# -----------------
- name : ojob print
typeArgs:
noLog : true
shortcut:
name : print
keyArg: msg
args :
key : __key
path : __path
level: level
help :
text: Prints a message line given an OpenAF template
expects:
- name: __key
desc: Map key to retrieve ('args' for arguments)
- name: __path
desc: The path to consider from the __key
- name: msg
desc: The message template to use
required: true
- name: level
desc: The level of the message (info (default) or error)
check:
in:
__key : isString.default("res")
__path : isString.default("@")
msg : isString
level : isString.oneOf(["info","error"]).default("info")
exec : |
var data = args.__key == "args" ? $path(args, args.__path) : $path($get(args.__key), args.__path)
switch(args.level) {
case "error": tprintErr(args.msg, data); break
case "info" : tprint(args.msg, data) ; break
}
# -----------------------
- name : ojob print md
typeArgs:
noLog: true
shortcut:
name : printmd
keyArg: __text
args :
outputMD: __outputMD
help :
text : Parses an input text as simple ascii markdown
expects:
- name: __text
desc: The text template to parse.
- name: __outputMD
desc: A boolean flag that if true will no parse the input text
check :
in:
__text : isString
__outputMD: toBoolean.isBoolean.default(false)
exec : |
var cargs = clone(args)
delete cargs.__text
if (args.__outputMD)
printnl($t(args.__text, cargs))
else
printnl(ow.format.withMD($t(args.__text, cargs)))
// Delete all __ args
Object.keys(args).filter(k => k.startsWith("__")).forEach(k => delete args[k])
# ---------------
- name : ojob log
typeArgs:
noLog: true
shortcut:
name : log
keyArg: msg
args :
key : __key
path : __path
level : level
options: options
help :
text: Logs a message line given an OpenAF template
expects:
- name: __key
desc: Map key to retrieve ('args' for arguments)
- name: __path
desc: The path to consider from the __key
- name: msg
desc: The message template to use
required: true
- name: level
desc: The level of the message (info (default), warn or error)
- name: options
desc: Extra options to provide to the tlog* functions. See more in the help of tlog, tlogErr and tlogWarn.
check:
in:
__key : isString.default("res")
__path : isString.default("@")
msg : isString
level : isString.oneOf(["info","warn","error"]).default("info")
options: isMap.default({})
exec : |
var data = args.__key == "args" ? $path(args, args.__path) : $path($get(args.__key), args.__path)
switch(args.level) {
case "warn" : tlogWarn(args.msg, data, args.options); break
case "error": tlogErr(args.msg, data, args.options) ; break
case "info" : tlog(args.msg, data, args.options) ; break
}
# OJOB FUNCTION
# --------------------
- name : ojob function
typeArgs:
noLog : true
shortcut:
name : fn
keyArg: __fn
args :
key : __key
path : __path
fnPath: __fnPath
help :
text : |
Executes the provided function mapping any args to the function arguments using the odoc help available for the provided function.
Note: accessing odoc might be slow on a first execution.
expects:
- name: __key
desc: The key string to retrieve previous results (defaults to 'res')
required: true
- name: __fn
desc: The function to execute
- name: __path
desc: If defined the args path for the function arguments to consider
- name: __fnPath
desc: If defined the args path where to set the function result
check:
in:
__fn : isString
__key: isString.default("res")
exec: |
if (args.__fn.indexOf(".") < 0) args.__fn = "global." + args.__fn;
var res = $fnM(args.__fn, (isDef(args.__path) ? $path(args, args.__path) : args));
if (isString(args.__fnPath)) res = $$({}).set(args.__fnPath, res);
if (isString(args.__key)) $set(args.__key, res); else args = merge(args, res)
// Delete all __ args
Object.keys(args).filter(k => k.startsWith("__")).forEach(k => delete args[k])
# OJOB OUTPUT
# -----------------
- name: ojob output
typeArgs:
noLog : true
shortcut:
name : output
keyArg: __key
args :