Skip to content

Commit 7f88fc3

Browse files
yurugAltGr
authored andcommitted
fix: Expose prepare.ml file
Signed-off-by: Yann Regis-Gianas <[email protected]>
1 parent 7285e86 commit 7f88fc3

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/repo/learnocaml_exercise.ml

+32-21
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type compiled = {
2222
type t =
2323
{ id : id ;
2424
prelude_ml : string ;
25+
prepare_ml : string ;
26+
(* absent from the json, empty except when building the exercises *)
2527
template : string ;
2628
solution : string ;
2729
(* absent from the json, empty except when building the exercises *)
@@ -64,10 +66,10 @@ let encoding =
6466
(req "test_lib" compiled_lib_encoding))
6567
in
6668
conv
67-
(fun { id ; prelude_ml ; template ; descr ; compiled ; max_score ; depend ; dependencies ; solution = _} ->
69+
(fun { id ; prelude_ml ; prepare_ml = _; template ; descr ; compiled ; max_score ; depend ; dependencies ; solution = _} ->
6870
(id, prelude_ml, template, descr, compiled, max_score, depend, dependencies))
6971
(fun ((id, prelude_ml, template, descr, compiled, max_score, depend, dependencies)) ->
70-
{ id ; prelude_ml ; template ; descr ; compiled ; max_score ; depend ; dependencies; solution = ""})
72+
{ id ; prelude_ml ; prepare_ml = ""; template ; descr ; compiled ; max_score ; depend ; dependencies; solution = ""})
7173
(obj8
7274
(req "id" string)
7375
(req "prelude_ml" string)
@@ -81,7 +83,7 @@ let encoding =
8183
(* let meta_from_string m =
8284
* Ezjsonm.from_string m
8385
* |> Json_encoding.destruct Learnocaml_meta.encoding
84-
*
86+
*
8587
* let meta_to_string m =
8688
* Json_encoding.construct Learnocaml_meta.encoding m
8789
* |> (function
@@ -138,9 +140,9 @@ module File = struct
138140
with Not_found -> raise (Missing_file ("get " ^ key))
139141

140142
let get_opt file ex =
141-
try (* a missing file here is necessarily [file] *)
142-
get file ex
143-
with Missing_file _ -> None
143+
try (* a missing file here is necessarily [file] *)
144+
get file ex
145+
with Missing_file _ -> None
144146

145147
let has { key ; _ } ex =
146148
StringMap.mem key ex
@@ -186,6 +188,12 @@ module File = struct
186188
field = (fun ex -> ex.prelude_ml) ;
187189
update = (fun prelude_ml ex -> { ex with prelude_ml })
188190
}
191+
let prepare_ml =
192+
{ key = "prepare.ml" ;
193+
decode = (fun v -> v) ; encode = (fun v -> v) ;
194+
field = (fun ex -> ex.prepare_ml) ;
195+
update = (fun prepare_ml ex -> { ex with prepare_ml })
196+
}
189197
let template =
190198
{ key = "template.ml" ;
191199
decode = (fun v -> v) ; encode = (fun v -> v) ;
@@ -242,8 +250,8 @@ module File = struct
242250
(fun test_lib c -> { c with test_lib })
243251
let depend =
244252
{ key = "depend.txt" ;
245-
decode = (fun v -> Some v) ;
246-
encode = (function
253+
decode = (fun v -> Some v) ;
254+
encode = (function
247255
| None -> "" (* no `depend` ~ empty `depend` *)
248256
| Some txt -> txt) ;
249257
field = (fun ex -> ex.depend) ;
@@ -252,7 +260,7 @@ module File = struct
252260

253261
(* [parse_dependencies txt] extracts dependencies from the string [txt].
254262
Dependencies are file names separated by at least one line break.
255-
[txt] may contain comments starting with characters ';' or '#'
263+
[txt] may contain comments starting with characters ';' or '#'
256264
and ending by a line break. *)
257265
let parse_dependencies txt =
258266
let remove_comment ~start:c line =
@@ -267,17 +275,17 @@ module File = struct
267275
| None -> []
268276
| Some txt ->
269277
let filenames = parse_dependencies txt in
270-
List.mapi
278+
List.mapi
271279
(fun pos filename ->
272280
{ key = filename ;
273281
decode = (fun v -> v) ; encode = (fun v -> v) ;
274282
field = (fun ex -> List.nth ex.dependencies pos) ;
275-
update = (fun v ex ->
276-
let dependencies =
283+
update = (fun v ex ->
284+
let dependencies =
277285
List.mapi (fun i v' -> if i = pos then v else v')
278286
ex.dependencies in { ex with dependencies }) })
279287
filenames
280-
288+
281289
module MakeReader (Concur : Concur) = struct
282290
let read ~read_field ?id: ex_id () =
283291
let open Concur in
@@ -396,6 +404,7 @@ module File = struct
396404
join
397405
[ (* read_title () ; *)
398406
read_file prelude_ml ;
407+
read_file prepare_ml ;
399408
read_file template ;
400409
read_file solution ;
401410
read_descrs () ;
@@ -447,7 +456,7 @@ let strip need_js ex =
447456

448457

449458
module MakeReaderAnddWriter (Concur : Concur) = struct
450-
459+
451460
module FileReader = File.MakeReader(Concur)
452461

453462
let read ~read_field ?id () =
@@ -459,6 +468,7 @@ module MakeReaderAnddWriter (Concur : Concur) = struct
459468
{ id = field_from_file File.id ex;
460469
(* meta = field_from_file File.meta ex; *)
461470
prelude_ml = field_from_file File.prelude_ml ex ;
471+
prepare_ml = field_from_file File.prepare_ml ex ;
462472
template = field_from_file File.template ex ;
463473
solution = field_from_file File.solution ex ;
464474
descr = field_from_file File.descr ex ;
@@ -478,14 +488,14 @@ module MakeReaderAnddWriter (Concur : Concur) = struct
478488
};
479489
max_score = 0 ;
480490
depend ;
481-
dependencies =
491+
dependencies =
482492
let field_from_dependency file =
483493
try field_from_file file ex
484-
with File.Missing_file msg
485-
-> let msg' = msg ^ ": dependency declared in "
494+
with File.Missing_file msg
495+
-> let msg' = msg ^ ": dependency declared in "
486496
^ File.(key depend) ^ ", but not found" in
487-
raise (File.Missing_file msg')
488-
in
497+
raise (File.Missing_file msg')
498+
in
489499
List.map field_from_dependency (File.dependencies depend)
490500
}
491501
with File.Missing_file _ as e -> fail e
@@ -505,7 +515,8 @@ module MakeReaderAnddWriter (Concur : Concur) = struct
505515
([ write_field id ;
506516
(* write_field meta ;
507517
* write_field title ; *)
508-
write_field prelude_ml ;
518+
write_field prelude_ml ;
519+
(* prepare not written on purpose *)
509520
write_field template ;
510521
(* solution not written on purpose *)
511522
write_field descr ;
@@ -517,7 +528,7 @@ module MakeReaderAnddWriter (Concur : Concur) = struct
517528
write_field test_cma ;
518529
write_field test_js ;
519530
write_field depend ;
520-
(* write_field max_score *) ]
531+
(* write_field max_score *) ]
521532
@ (List.map write_field (dependencies (access depend ex))) )
522533
>>= fun () ->
523534
return !acc

src/repo/learnocaml_exercise.mli

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ module File : sig
5858
(** Returns the (public) [prelude.ml] *)
5959
val prelude_ml: string file
6060

61+
(** Returns the (private) [prepare.ml] *)
62+
val prepare_ml: string file
63+
6164
(** Returns the (public) [template.ml] *)
6265
val template: string file
6366

@@ -87,7 +90,7 @@ module File : sig
8790
(** Returns the (public) depend file *)
8891
val depend: string option file
8992

90-
(** [dependencies txt] create the (private, already deciphered) dependencies
93+
(** [dependencies txt] create the (private, already deciphered) dependencies
9194
declared in [txt] *)
9295
val dependencies: string option -> string file list
9396
end

0 commit comments

Comments
 (0)