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
open Dec_template
type source =
| Git of string
| Local_dir of string
| Official of (module Spin_template.Template)
type example_command =
{ name : string
; description : string
}
type t =
{ name : string
; description : string
; raw_files : string list
; parse_binaries : bool
; files : (string, string) Hashtbl.t
; context : (string, string) Hashtbl.t
; pre_gen_actions : Template_actions.t list
; post_gen_actions : Template_actions.t list
; example_commands : example_command list
; source : source
}
let ignore_files files ~context ~(dec : Dec_template.t) =
let open Result.Syntax in
let+ ignores =
Template_expr.filter_map
(fun x -> x)
dec.ignore_file_rules
~context
~condition:(fun el -> el.Ignore_rule.enabled_if)
in
let ignores =
List.map (fun (ignore : Ignore_rule.t) -> ignore.files) ignores
|> List.concat
in
files
|> Hashtbl.to_list
|> List.filter_map (fun (path, content) ->
if
List.exists
(fun glob ->
let normalized_path =
Str.global_replace (Str.regexp "\\\\") "/" path
in
Glob.matches_glob normalized_path ~glob)
ignores
then
None
else
Some (path, content))
|> Hashtbl.of_list
let populate_template_files files =
files
|> Hashtbl.to_list
|> List.filter_map (fun (path, content) ->
let fpath = Fpath.v path in
let root = Fpath.v "template" in
match Fpath.rem_prefix root fpath with
| Some fpath ->
let path = Fpath.to_string fpath in
Some (path, content)
| None ->
None)
|> Hashtbl.of_list
let populate_example_commands ~context (dec : Dec_template.t) =
Template_expr.filter_map
(fun el -> { name = el.name; description = el.description })
dec.example_commands
~context
~condition:(fun el -> el.Example_command.enabled_if)
let source_of_dec = function
| Dec_common.Source.Git s ->
Ok (Git s)
| Dec_common.Source.Local_dir s ->
Ok (Local_dir s)
| Dec_common.Source.Official s ->
(match Official_template.of_name s with
| Some (module T) ->
Ok (Official (module T))
| None ->
Error (Printf.sprintf "The official template does not exist: %s" s))
let source_to_dec = function
| Git s ->
Dec_common.Source.Git s
| Local_dir s ->
Dec_common.Source.Local_dir s
| Official (module T) ->
Dec_common.Source.Official T.name
let source_of_string s =
match Official_template.of_name s with
| Some v ->
Some (Official v)
| None ->
(match Dec_common.Git_repo.decode (Sexplib.Sexp.Atom s) with
| Ok s ->
Some (Git s)
| Error _ ->
if Sys.is_directory s then
Some (Local_dir s)
else
None)
let read_source_spin_file ?(download_git = false) source =
let open Result.Syntax in
match source with
| Official (module T) ->
Official_template.read_spin_file (module T)
| Local_dir dir ->
Local_template.read_spin_file dir
| Git repo ->
let* template_dir =
if download_git then
Git_template.donwload_git_repo repo
else
Git_template.cache_dir_of_repo repo
in
Local_template.read_spin_file template_dir
let read_source_template_files ?(download_git = false) source =
let open Result.Syntax in
match source with
| Official (module T) ->
Ok (Official_template.files_with_content (module T) |> Hashtbl.of_list)
| Local_dir dir ->
let files = Local_template.files_with_content dir in
Ok (Hashtbl.of_list files)
| Git repo ->
let+ template_dir =
if download_git then
Git_template.donwload_git_repo repo
else
Git_template.cache_dir_of_repo repo
in
let files = Local_template.files_with_content template_dir in
Hashtbl.of_list files
let rec of_dec
?(use_defaults = false)
?(files = Hashtbl.create 256)
?(ignore_configs = false)
?(ignore_actions = false)
?(ignore_example_commands = false)
~source
~context
(dec : Dec_template.t)
=
let open Result.Syntax in
let* base =
match dec.base_template with
| Some base ->
let* source =
source_of_dec base.source
|> Result.map_error (fun reason ->
Spin_error.invalid_template ~msg:reason dec.name)
in
read
source
~use_defaults
~context
~ignore_configs:base.ignore_configs
~ignore_actions:base.ignore_actions
~ignore_example_commands:base.ignore_example_commands
| None ->
Result.ok
{ name = ""
; description = ""
; raw_files = []
; parse_binaries = false
; context
; files = Hashtbl.create 256
; pre_gen_actions = []
; post_gen_actions = []
; example_commands = []
; source
}
in
let* () =
if ignore_configs then
Result.ok ()
else
Template_configuration.populate_context
~use_defaults
~context
dec.configurations
in
let* pre_gen_actions =
if ignore_actions then
Result.ok []
else
Template_actions.of_decs_with_condition ~context dec.pre_gen_actions
in
let* post_gen_actions =
if ignore_actions then
Result.ok []
else
Template_actions.of_decs_with_condition ~context dec.post_gen_actions
in
let* example_commands =
if ignore_example_commands then
Result.ok []
else
populate_example_commands ~context dec
in
let parse_binaries =
match dec.parse_binaries with Some v -> v | None -> base.parse_binaries
in
let raw_files =
match dec.raw_files with
| Some v ->
base.raw_files @ v
| None ->
base.raw_files
in
let files = populate_template_files files in
let merged_files = Hashtbl.copy base.files in
Hashtbl.merge files ~into:merged_files;
let+ merged_files = ignore_files merged_files ~dec ~context in
{ name = dec.name
; description = dec.description
; parse_binaries
; raw_files
; context
; files = merged_files
; pre_gen_actions = base.pre_gen_actions @ pre_gen_actions
; post_gen_actions = base.post_gen_actions @ post_gen_actions
; example_commands = base.example_commands @ example_commands
; source
}
and read
?(use_defaults = false)
?(ignore_configs = false)
?(ignore_actions = false)
?(ignore_example_commands = false)
?context
source
=
let open Result.Syntax in
let context = Option.value context ~default:(Hashtbl.create 256) in
let* spin_file = read_source_spin_file source ~download_git:true in
let* files = read_source_template_files source ~download_git:false in
of_dec
spin_file
~ignore_configs
~ignore_actions
~ignore_example_commands
~files
~context
~source
~use_defaults
let run_actions ~path actions =
let open Result.Syntax in
let+ _ =
Result.List.fold_left
(fun acc el ->
let+ action = Template_actions.run ~path el in
action :: acc)
[]
actions
in
()
let generate ~path:generation_root template =
let open Result.Syntax in
let* _ = run_actions ~path:generation_root template.pre_gen_actions in
Logs.app (fun m ->
m
"\nšļø Creating a new project from %a in %s"
Pp.pp_blue
template.name
generation_root);
let normalized_raw_files =
template.raw_files |> List.map File_generator.normalize_path
in
let files_to_copy, files_to_generate =
List.fold_left
(fun (files_to_copy, files_to_generate) file ->
let file_path, file_content = file in
let normalized_file_path = File_generator.normalize_path file_path in
let file = normalized_file_path, file_content in
match
( template.parse_binaries
, File_generator.is_binary_file file_path
, Glob.matches_globs normalized_file_path ~globs:normalized_raw_files
)
with
| _, _, true ->
file :: files_to_copy, files_to_generate
| false, true, _ ->
file :: files_to_copy, files_to_generate
| _, _, _ ->
files_to_copy, file :: files_to_generate)
([], [])
(Hashtbl.to_list template.files)
in
Sys.mkdir_p generation_root;
let* _ =
files_to_copy
|> Result.List.iter_left (fun (path, content) ->
let path = Filename.concat generation_root path in
File_generator.copy ~context:template.context ~content path)
in
let* _ =
files_to_generate
|> Result.List.iter_left (fun (path, content) ->
let path = Filename.concat generation_root path in
File_generator.generate ~context:template.context ~content path)
in
Logs.app (fun m -> m "%a" Pp.pp_bright_green "Done!\n");
let* _ = run_actions ~path:generation_root template.post_gen_actions in
Logs.app (fun m ->
m "š Success! Your project is ready at %s" generation_root);
let () =
match template.example_commands with
| [] ->
()
| l ->
Logs.app (fun m ->
m
"\n\
Here are some example commands that you can run inside this \
directory:");
List.iter
(fun (el : example_command) ->
Logs.app (fun m -> m "");
Logs.app (fun m -> m " %a" Pp.pp_blue el.name);
Logs.app (fun m -> m " %s" el.description))
l
in
Logs.app (fun m -> m "\nHappy hacking!");
Result.ok ()