Source file update.ml
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
open Types
open Ezcmd.V2
open EZCMD.TYPES
open Ez_file.V1
open EzFile.OP
open EzCompat
exception Skip
type update_args =
{ mutable arg_upgrade : bool;
mutable arg_force : bool;
mutable arg_diff : bool;
mutable arg_skip : (bool * string) list;
mutable arg_promote_skip : bool;
mutable arg_edition : string option;
mutable arg_min_edition : string option
}
let default_args () =
{ arg_upgrade = false;
arg_force = false;
arg_diff = false;
arg_skip = [];
arg_promote_skip = false;
arg_edition = None;
arg_min_edition = None
}
let update_args () =
let args = default_args () in
let specs =
[ ( [ "f"; "force" ],
Arg.Unit (fun () -> args.arg_force <- true),
EZCMD.info
"Force overwriting modified files (otherwise, they would be skipped)"
);
( [ "skip" ],
Arg.String
(fun s ->
args.arg_skip <- (true, s) :: args.arg_skip;
args.arg_upgrade <- true ),
EZCMD.info ~docv:"FILE" "Add $(docv) to skip list" );
( [ "unskip" ],
Arg.String
(fun s ->
args.arg_skip <- (false, s) :: args.arg_skip;
args.arg_upgrade <- true ),
EZCMD.info ~docv:"FILE" "Remove $(docv) from skip list" );
( [ "diff" ],
Arg.Unit (fun () -> args.arg_diff <- true),
EZCMD.info "Print a diff of user-modified files that are being skipped"
);
( [ "promote-skip" ],
Arg.Unit (fun () -> args.arg_promote_skip <- true),
EZCMD.info "Promote user-modified files to skip field" );
( [ "edition" ],
Arg.String (fun s -> args.arg_edition <- Some s),
EZCMD.info ~docv:"OCAMLVERSION" "Set project default OCaml version" );
( [ "min-edition" ],
Arg.String (fun s -> args.arg_min_edition <- Some s),
EZCMD.info ~docv:"OCAMLVERSION" "Set project minimal OCaml version" )
]
in
(args, specs)
let compute_config_hash files =
let files = List.sort compare files in
let files =
List.map
(fun (file, content) -> (file, Hashes.digest_content ~file content))
files
in
let to_hash =
String.concat "?"
(List.map (fun (file, hash) -> Printf.sprintf "%s^%s" file hash) files)
in
Hashes.digest_content ~file:"" to_hash
let update_files ?args ?(git = false) ?(create = false) p =
let args =
match args with
| None -> default_args ()
| Some args -> args
in
let changed = false in
let p, changed =
match args.arg_skip with
| [] -> (p, changed)
| skip ->
let skip =
List.fold_left
(fun skips (bool, elem) ->
if bool then
elem :: skips
else
EzList.remove elem skips )
p.skip skip
in
let p = { p with skip } in
(p, true)
in
let p, changed =
match args.arg_edition with
| None -> (p, changed)
| Some edition -> ({ p with edition }, true)
in
let p, changed =
match args.arg_min_edition with
| None -> (p, changed)
| Some min_edition -> ({ p with min_edition }, true)
in
let can_skip = ref [] in
let skip_set = ref StringSet.empty in
List.iter (fun skip -> skip_set := StringSet.add skip !skip_set) p.skip;
List.iter
(fun pk ->
match pk.p_skip with
| None -> ()
| Some list ->
List.iter
(fun skip ->
skip_set := StringSet.add (Filename.concat pk.dir skip) !skip_set )
list )
p.packages;
let skip_set = !skip_set in
let not_skipped s =
can_skip := s :: !can_skip;
not (StringSet.mem s skip_set)
in
let skipped = ref [] in
let write_file ?(record = true) ~perm hashes filename content =
Hashes.write hashes ~record ~perm filename content
in
let can_update ~filename ~perm hashes content =
let old_content = EzFile.read_file filename in
let old_perm = (Unix.lstat filename).Unix.st_perm in
if content = old_content && Hashes.perm_equal perm old_perm then begin
begin
match Hashes.get hashes filename with
| exception Not_found ->
Printf.eprintf "Warning: .drom: missing hash for %S\n%!" filename;
let hash =
Hashes.digest_content ~file:filename ~perm:old_perm old_content
in
Hashes.update ~git:false hashes filename hash
| _ -> ()
end;
false
end else
args.arg_force
||
match Hashes.get hashes filename with
| exception Not_found ->
skipped := filename :: !skipped;
Printf.eprintf "Skipping existing file %s\n%!" filename;
false
| former_hash ->
let hash =
Hashes.digest_content ~file:filename ~perm:old_perm old_content
in
let modified =
former_hash <> hash
&&
former_hash <> Digest.string old_content
in
if modified then (
skipped := filename :: !skipped;
Printf.eprintf "Skipping modified file %s\n%!" filename;
if args.arg_diff then begin
let basename = Filename.basename filename in
let dirname = Globals.drom_dir // "temp" in
let dirname_a = dirname // "a" in
let dirname_b = dirname // "b" in
EzFile.make_dir ~p:true dirname_a;
EzFile.make_dir ~p:true dirname_b;
let file_a = dirname_a // basename in
let file_b = dirname_b // basename in
EzFile.write_file file_a old_content;
EzFile.write_file file_b content;
( try Misc.call [| "diff"; "-u"; file_a; file_b |] with
| _ -> () );
Sys.remove file_a;
Sys.remove file_b
end
);
not modified
in
let write_file ?( record = true)
?( create = false)
?( skip = false) ?( force = false)
?( skips = []) ?(perm = 0o644) hashes filename
content =
try
if skip then raise Skip;
if force then (
Printf.eprintf "Forced Update of file %s\n%!" filename;
write_file hashes filename content ~perm
) else if
not_skipped filename
&&
List.for_all not_skipped skips
then
if not record then
write_file ~record:false hashes filename content ~perm
else if not (Sys.file_exists filename) then (
if Globals.verbose 2 then
Printf.eprintf "Creating file %s\n%!" filename;
write_file hashes filename content ~perm
) else if create then
raise Skip
else if can_update ~filename ~perm hashes content then (
Printf.eprintf "Updating file %s\n%!" filename;
write_file hashes filename content ~perm
) else
raise Skip
else
raise Skip
with
| Skip ->
let filename = "_drom" // "skipped" // filename in
EzFile.make_dir ~p:true (Filename.dirname filename);
EzFile.write_file filename content
in
let config = Config.config () in
let p, changed =
if args.arg_upgrade then
let p, changed =
match (p.github_organization, config.config_github_organization) with
| None, Some s -> ({ p with github_organization = Some s }, true)
| _ -> (p, changed)
in
let p, changed =
match (p.authors, config.config_author) with
| [], Some s -> ({ p with authors = [ s ] }, true)
| _ -> (p, changed)
in
let p, changed =
match (p.copyright, config.config_copyright) with
| None, Some s -> ({ p with copyright = Some s }, true)
| _ -> (p, changed)
in
(p, changed)
else
(p, changed)
in
List.iter (fun package -> package.project <- p) p.packages;
Hashes.with_ctxt ~git (fun hashes ->
if create then
if git && not (Sys.file_exists ".git") then (
Git.call [ "init"; "-q" ];
match config.config_github_organization with
| None -> ()
| Some organization ->
Git.call
[ "remote";
"add";
"origin";
Printf.sprintf "git@github.com:%s/%s" organization
p.package.name
];
if Sys.file_exists "README.md" then Git.call [ "add"; "README.md" ];
Git.call [ "commit"; "--allow-empty"; "-m"; "Initial commit" ]
);
List.iter
(fun package ->
let gen_opam_file =
match package.kind with
| Virtual -> (
match StringMap.find "gen-opam" package.p_fields with
| exception _ -> false
| s -> (
match String.lowercase s with
| "all"
| "some" ->
true
| _ -> false ) )
| _ -> true
in
if gen_opam_file then (
let opam_filename = package.name ^ ".opam" in
( match package.p_gen_version with
| None -> ()
| Some file ->
let version_file = package.dir // file in
if Sys.file_exists version_file then Sys.remove version_file;
write_file hashes (version_file ^ "t")
(GenVersion.file package file) );
EzFile.make_dir ~p:true "opam";
let full_filename = "opam" // opam_filename in
write_file hashes full_filename
(Opam.opam_of_project Single package);
if Sys.file_exists opam_filename then begin
Printf.eprintf "Removing deprecated %s (moved to %s)\n%!"
opam_filename full_filename;
Sys.remove opam_filename
end
) )
p.packages;
EzFile.make_dir ~p:true Globals.drom_dir;
EzFile.write_file
(Globals.drom_dir // "known-licences.txt")
(License.known_licenses ());
EzFile.write_file
(Globals.drom_dir // "known-skeletons.txt")
(Skeleton.known_skeletons ());
EzFile.write_file (Globals.drom_dir // "header.ml") (License.header_ml p);
EzFile.write_file
(Globals.drom_dir // "header.mll")
(License.header_mll p);
EzFile.write_file
(Globals.drom_dir // "header.mly")
(License.header_mly p);
EzFile.write_file
(Globals.drom_dir // "maximum-skip-field.txt")
(Printf.sprintf "skip = \"%s\"\n" (String.concat " " !can_skip));
Skeleton.write_files
(fun file ~create ~skips ~content ~record ~skip ~perm ->
write_file hashes ~perm file ~create ~skips ~record ~skip content )
p;
let p, changed =
if args.arg_promote_skip && !skipped <> [] then (
let skip = p.skip @ !skipped in
Printf.eprintf "skip field promotion: %s\n%!"
(String.concat " " !skipped);
({ p with skip }, true)
) else
(p, changed)
in
let upgrade = args.arg_upgrade || changed in
let skip = not (upgrade || not (Sys.file_exists "drom.toml")) in
let files = Project.to_files p in
let files =
List.map
(fun (file, content) ->
let content =
if upgrade then begin
write_file ~skip ~force:upgrade hashes file content;
content
end else
try EzFile.read_file file with
| Sys_error _ -> ""
in
(file, content) )
files
in
let hash = compute_config_hash files in
Hashes.update ~git:false hashes "." hash );
()
let update_files ~twice ?args ?(git = false) ?(create = false) p =
update_files ?args ~git ~create p;
if twice then begin
Printf.eprintf "Re-iterate file generation for consistency...\n%!";
update_files ?args ~git p
end