-
Notifications
You must be signed in to change notification settings - Fork 1
/
poli
276 lines (212 loc) · 6.19 KB
/
poli
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
#! /usr/bin/env arturo
;; Script that empowers `poliglota`
;; please, read readme or type `./poli --help` to get aid
version: 1.0.0
usage: {:
USAGE: poli COMMAND [OPTIONS] [ARGS]...
Options:
-h, --help Show this message and exit
-v, --version Show current version
Commands:
new Create a new project
add Add an implementation to a project
fill Fill a project with missing implementations
--------- --------------------
Usage: poli new [OPTIONS] <project>
poli new [OPTIONS] {-l|--last}
Options:
-e, --empty project is empty
-l, --last uses the last interacted project
--------- --------------------
Usage: poli add [OPTIONS] <template> <project>
poli add [OPTIONS] <template> {-l|--last}
Options:
--as <name> Create with a new name
-e, --empty Implementation is empty
-l, --last uses the last interacted project
--------- --------------------
Usage: poli fill <project>
--------- --------------------
✨ Powered by Arturo Programming Language ✨
:}
saveLast: $[project :string][
;; description: « save the last interacted repository to "info.json"
;; example: {
;; saveLast "my-project"
;; }
file: read.json "info.json"
file\last: project
write.json "info.json" file
]
; -- COMMANDS:
newProject: $[name :string][
;; description: « creates a new project
;; options: [
;; empty: :bool « new project is empty
;; ]
;; sideEffects: [
;; 'exit
;; 'saveLast
;; ]
if exists? .directory ~"repository/|name|" [
panic ~"\"|name|\" already exists"
]
if attr? 'empty [
try? -> execute ~"mkdir repository/|name|/"
else -> panic ~"Can't create \"|name|\""
saveLast name
exit
]
try? -> copy.directory ~"templates/" ~"repository/|name|/"
else -> panic ~"Can't create \"|name|\""
saveLast name
exit
]
addImplementation: $[
template :string
project :string
implementation :string
][
;; description: « add an implementation to a project
;; options: [
;; empty: :bool « new implementation is empty
;; ]
;; sideEffects: [
;; 'exit
;; 'saveLast
;; ]
if not? exists? .directory ~"repository/|project|" [
panic ~{
"|project|" doesn't exist. Please, use `new` command. See:
|usage|
}
]
if exists? .directory ~"repository/|project|/|implementation|" [
panic ~"\"|implementation|\" already exists within \"|project|\"."
]
if attr? 'empty [
try? -> execute ~"mkdir repository/|project|/|implementation|"
else -> panic ~"Can't create \"|implementation|\" within \"|project|\"."
saveLast project
exit
]
try? -> copy.directory ~"templates/|template|/"
~"repository/|project|/|implementation|"
else -> panic ~"Can't create \"|implementation|\" inside \"|project|\"."
saveLast project
exit
]
fillProject: $[project :string][
;; description: « fill project with missing implementations
;; sideEffects: [
;; 'exit
;; 'saveLast
;; ]
if not? exists? .directory ~"repository/|project|" [
panic ~{
"|project|" doesn't exist. Please, use `new` command. See:
|usage|
}
]
try? -> execute
~"cp templates/** repository/|project| --recursive --no-preserve=timestamp --no-clobber"
else -> panic ~"Can't fill \"|project|\"."
saveLast project
exit
]
; -- PARSER
minimal: [args number][
;; description: « raises error if args were insufficient
;; sideEffects: [
;; ensure
;; ]
ensure -> args\[number]
]
parser: $[args][
;; description: « parse command-line arguments
;; sideEffects: [
;; 'ensure
;; 'exit
;; 'panic
;; 'saveLast
;; ]
if some? ["--help" "-h"] 'op -> in? op args [
print usage
exit
]
if some? ["--version" "-v"] 'op -> in? op args [
print version
exit
]
f: first args
case [f]
when? [= "new"] [
drop 'args 1
if some? ["--empty" "-e"] 'op -> in? op args [
remove 'args ["--empty" "-e"]
ensure -> minimal args 1
newProject.empty (first args)
]
minimal args 1
newProject (first args)
]
when? [= "add"] [
drop 'args 1
minimal args 2
newName: false
if in? "--as" args [
minimal args 4
i: index args "--as"
remove 'args "--as"
newName: (get args i)
remove 'args newName
]
emptyFlag: false
if some? ["--empty" "-e"] 'op -> in? op args [
remove 'args ["--empty" "-e"]
ensure -> minimal args 2
emptyFlag: true
]
lastFlag: false
if some? ["--last" "-l"] 'op -> in? op args [
remove 'args ["--last" "-l"]
ensure -> minimal args 1
information: read.json "info.json"
if information\last <> ""
-> lastFlag: information\last
]
if and? string? newName true? emptyFlag
-> panic ~{
You can't use `--as` and `--empty` at the same time.
Your arguments: |arg|
|usage|
}
if logical? lastFlag [
try? -> minimal args 2
else -> panic ~"You didn't interact with any project yet."
]
template: (get args 0)
project: (string? lastFlag)?
-> lastFlag
->(get args 1)
name: (string? newName)?
-> newName
-> template
try? [
ensure -> template != "--as"
ensure -> project != "--as"
ensure -> name != "--as"
]
else -> ~"Your arguments: |arg|\n|usage|"
(true? emptyFlag)?
-> addImplementation.empty template project name
-> addImplementation template project name
]
when? [= "fill"] [
drop 'args 1
minimal args 1
fillProject (first args)
]
else -> panic ~"Your arguments: |arg|\n|usage|"
]
parser arg