Skip to content

Commit

Permalink
Fix mirage clean
Browse files Browse the repository at this point in the history
  • Loading branch information
samoht committed Jun 30, 2020
1 parent 5159d4a commit 8c12e3b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 21 deletions.
47 changes: 30 additions & 17 deletions lib/functoria/filegen.ml
Expand Up @@ -43,33 +43,46 @@ module Make (P : PROJECT) = struct
| `Opam | `Make -> Fmt.str "# %s" line
| `OCaml -> Fmt.str "(* %s *)" line

let short_headers lang =
match lang with
| `Sexp -> Fmt.str ";; Generated by"
| `Opam | `Make -> "# Generated by"
| `OCaml -> "(* Generated by"

let has_headers file contents =
match Fpath.basename file with
| "dune-project" | "dune-workspace" -> (
let lines = String.cuts ~sep:"\n" ~empty:true (String.trim contents) in
match List.rev lines with
| x :: _ -> String.is_infix ~affix:(short_headers `Sexp) x
| _ -> false )
| _ -> (
match lang file with
| None -> false
| Some lang ->
let affix = short_headers lang in
String.is_infix ~affix contents )

let can_overwrite file =
Action.is_file file >>= function
| false -> Action.ok true
| true -> (
if Fpath.basename file = "dune-project" then
Action.read_file file >|= fun x ->
let x = String.cuts ~sep:"\n" ~empty:true x in
match List.rev x with x :: _ -> x = headers `Sexp | _ -> false
else
match lang file with
| None -> Action.ok false
| Some lang ->
let affix = headers lang in
Action.read_file file >|= fun x -> String.is_infix ~affix x )
| true -> Action.read_file file >|= has_headers file

let rm file =
can_overwrite file >>= function
| false -> Action.ok ()
| true -> Action.rm file

let with_headers file contents =
match Fpath.basename file with
| "dune-project" -> Fmt.str "%s\n\n%s" contents (headers `Sexp)
| _ -> (
match lang file with
| None -> Fmt.invalid_arg "%a: invalide lang" Fpath.pp file
| Some lang -> Fmt.str "%s\n\n%s" (headers lang) contents )
if has_headers file contents then contents
else
match Fpath.basename file with
| "dune-project" | "dune-workspace" ->
Fmt.str "%s\n%s\n" contents (headers `Sexp)
| _ -> (
match lang file with
| None -> Fmt.invalid_arg "%a: invalide lang" Fpath.pp file
| Some lang -> Fmt.str "%s\n\n%s" (headers lang) contents )

let write file contents =
can_overwrite file >>= function
Expand Down
2 changes: 2 additions & 0 deletions lib/functoria/filegen.mli
Expand Up @@ -27,5 +27,7 @@ end
module Make (P : PROJECT) : sig
val write : Fpath.t -> string -> unit Action.t

val headers : [ `OCaml | `Sexp | `Make | `Opam ] -> string

val rm : Fpath.t -> unit Action.t
end
4 changes: 2 additions & 2 deletions lib/functoria/tool.ml
Expand Up @@ -219,13 +219,13 @@ module Make (P : S) = struct

let clean_files args =
Action.ls (Fpath.v ".") (fun file ->
Fpath.parent file = Fpath.v "."
Fpath.parent file = Fpath.v "./"
&&
let base, ext = Fpath.split_ext file in
let base = Fpath.basename base in
match (base, ext) with
| _, (".opam" | ".install") -> true
| "Makefile", "" -> true
| ("Makefile" | "dune-project" | "dune-workspace"), "" -> true
| _ -> false)
>>= fun files ->
Action.List.iter ~f:Filegen.rm files >>= fun () ->
Expand Down
2 changes: 1 addition & 1 deletion test/functoria/e2e/test.ml
Expand Up @@ -217,7 +217,7 @@ let test_cache () =
test "configure --file %a --vote=%s" Fpath.pp config_ml str;
test "build --file %a" Fpath.pp config_ml;
Alcotest.(check string) "cache is valid" str (read_file Fpath.(root / "vote"));
clean ()
test "clean --file %a" Fpath.pp config_ml

let test_help () =
let help_ppf = Fmt.with_buffer (Buffer.create 10) in
Expand Down
5 changes: 4 additions & 1 deletion test/functoria/tool/clean.err.expected
Expand Up @@ -10,7 +10,10 @@
* Run_cmd 'dune build ./config.exe' (ok)
* Is_file? test.context -> false
* Run_cmd 'dune exec --root . -- ./config.exe clean a b c --dry-run' (ok)
* Ls ./ (0 entry)
* Ls ./ (1 entry)
* Is_file? dune-project -> true
* Read dune-project (47 bytes)
* Rm dune-project (removed)
* Rm test.context (no-op)
* Get_var INSIDE_FUNCTORIA_TESTS -> <not set>
* Rmdir _build (no-op)

0 comments on commit 8c12e3b

Please sign in to comment.