-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
autom8.art
executable file
·176 lines (134 loc) · 5 KB
/
autom8.art
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
#!/usr/bin/env arturo
;========================================================
; Autom8
;
; Fast templating for any type of project
; in Arturo
;--------------------------------------------------------
;; name: autom8
;; version: 0.1.0
;; author: drkameleon
;; website: « https://github.com/arturo-lang/autom8
;; category: [automation productivity]
;========================================================
;=======================================
; Entry function
;=======================================
autom8: function [cfg, src, tgt][
;-----------------------------
; Globals
;-----------------------------
InfoPadding: 13
InfoLineSize: 75
Debug: false
Verbose: false
DataSubfolder: "data"
;-----------------------------
; Private Helpers
;-----------------------------
showHeader: function [message][
if Verbose [
colorize: function[X][color #magenta X]
if attr 'success -> colorize: function[X][color #green X]
print ""
print colorize repeat "=" InfoLineSize
print [pad "" InfoPadding colorize upper message]
print colorize repeat "=" InfoLineSize
]
]
showProgress: function [current,total,file][
if Verbose ->
print [
color #cyan pad ~"[|current+1|/|total|]" InfoPadding
join.with: " ▪︎ " select split.path file -- extract.extension file => [not? empty?]
]
]
;-----------------------------
; Main
;-----------------------------
try -> Debug: cfg\debug
try -> Verbose: cfg\verbose
try -> DataSubfolder: cfg\data
dataSrc: join.path @[src DataSubfolder]
if not? exists? .directory dataSrc -> panic ~"data folder: |DataSubfolder| doesn't exist"
files: list.recursive dataSrc
loop @ cfg\rules [rule, settings][
try -> showHeader settings\title
fileSet: select files => [not? empty? match & rule]
fileCount: size fileSet
loop.with:'c fileSet 'srcFile [
;-----------------------------
; resolve paths
;-----------------------------
relSrc: normalize srcFile -- dataSrc ++ "/"
showProgress c fileCount relSrc
relTgt: ""
try? -> relTgt: call settings\target @[relSrc]
else -> relTgt: relSrc
tgtFile: join.path @[tgt relTgt]
tgtDir: extract.directory tgtFile
tmpl: ""
try [
tmpl: (function? settings\template)? -> call settings\template @[relSrc]
-> settings\template
]
tmpl: join.path @[src normalize tmpl]
route: split.path relSrc -- extract.extension relSrc
if Debug ->
print [ "\n"
"srcFile:" srcFile "\n"
"relSrc:" relSrc "\n"
"tgtFile:" tgtFile "\n"
"tgtDir:" tgtDir "\n"
"relTgt:" relTgt "\n"
"tmpl:" tmpl "\n"
"route:" route "\n"
]
;-----------------------------
; process file
;-----------------------------
; read data
W: #.data srcFile
; preprocess data - if necessary
try -> W: call settings\pre @[relSrc W]
; process template
content: render.template read tmpl
; postprocess content - if necessary
try -> content: call settings\post @[relSrc content]
; create target directory recursively - if not exists
if not? exists? .directory tgtDir [
write.directory tgtDir ø
]
; write file to target directory
write tgtFile content
]
]
showHeader.success "Project complete :)"
]
;=======================================
; Standalone tool
;=======================================
if standalone? ::
if 1 < size arg -> panic "too many arguments - try --help for usage info"
defaultOutputDir: "/_out"
defaultConfig: "config.art"
print color #white.bold {:
.--. .---.
--.--..--..--.--' '--.----. .--.--.--.|- O -|
' .-. || || |'-. .-'| .-. || |.' '.
| '-' |' '' ' | | ' '-' '| | | ||- O -|
`--`--' `----' '--' `---' `--`--`--' `---'
:}
src: normalize "."
try -> src: arg\0
tgt: normalize join.path @[src defaultOutputDir]
cfg: join.path @[src defaultConfig]
try -> tgt: normalize args\at
try -> cfg: args\config
if not? exists? .directory src -> panic "given source directory doesn't exist"
if not? exists? cfg -> panic "valid configuration file not found"
autom8 #cfg src tgt
;========================================================
; This is the end,
; my only friend, the end...
;========================================================