From 91d03de2216dd66c17b4e66899bebf04ccbfa867 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Fri, 26 May 2017 06:32:32 -0400 Subject: [PATCH] Add clean subcommand (#89) --- Makefile | 3 ++- bin/main.ml | 19 +++++++++++++++++++ doc/jbuild | 1 + src/build_system.ml | 7 +++++++ src/build_system.mli | 4 ++++ src/path.mli | 2 +- 6 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 4c32ded7..82f46532 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,8 @@ all-supported-ocaml-versions: $(BIN) build @install @runtest --workspace jbuild-workspace.dev --root . clean: - rm -rf _build + -$(BIN) clean + -rm -f ./boot.exe doc: cd doc && sphinx-build . _build diff --git a/bin/main.ml b/bin/main.ml index 79e61aac..64df5880 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -426,6 +426,24 @@ let runtest = $ Arg.(value & pos_all string ["."] name_)) , Term.info "runtest" ~doc ~man) +let clean = + let doc = "Clean the project." in + let man = + [ `S "DESCRIPTION" + ; `P {|Removes files added by jbuilder such as _build, .install, and .merlin|} + ; `Blocks help_secs + ] + in + let go common = + begin + set_common common ~targets:[]; + Build_system.all_targets_ever_built () |> List.iter ~f:Path.unlink_no_err; + Path.(rm_rf (append root (of_string "_build"))) + end + in + ( Term.(const go $ common) + , Term.info "clean" ~doc ~man) + let format_external_libs libs = String_map.bindings libs |> List.map ~f:(fun (name, kind) -> @@ -776,6 +794,7 @@ let all = ; external_lib_deps ; build_targets ; runtest + ; clean ; install ; uninstall ; exec diff --git a/doc/jbuild b/doc/jbuild index 6f1be958..e956e5d1 100644 --- a/doc/jbuild +++ b/doc/jbuild @@ -8,6 +8,7 @@ let commands = ; "install" ; "installed-libraries" ; "runtest" + ; "clean" ; "uninstall" ; "exec" ; "subst" diff --git a/src/build_system.ml b/src/build_system.ml index 43215126..143aafda 100644 --- a/src/build_system.ml +++ b/src/build_system.ml @@ -561,6 +561,13 @@ module Trace = struct trace end +let all_targets_ever_built () = + if Sys.file_exists Trace.file then + let trace = Trace.load () in + Hashtbl.fold trace ~init:[] ~f:(fun ~key ~data:_ acc -> key :: acc) + else + [] + let create ~contexts ~file_tree ~rules = let all_source_files = File_tree.fold file_tree ~init:Pset.empty ~f:(fun dir acc -> diff --git a/src/build_system.mli b/src/build_system.mli index f8b4a225..ff6a6ce7 100644 --- a/src/build_system.mli +++ b/src/build_system.mli @@ -61,3 +61,7 @@ val build_rules -> ?recursive:bool (* default false *) -> Path.t list -> Rule.t list Future.t + +val all_targets_ever_built + : unit + -> Path.t list diff --git a/src/path.mli b/src/path.mli index f16b182e..08997397 100644 --- a/src/path.mli +++ b/src/path.mli @@ -1,6 +1,6 @@ open Import -(** In the current worksapce (anything under the current project root) *) +(** In the current workspace (anything under the current project root) *) module Local : sig type t