From 2ea2fafca90e98f42557f8c47f3391fda44c34e8 Mon Sep 17 00:00:00 2001 From: DarkDrek Date: Tue, 1 May 2018 17:33:10 +0200 Subject: [PATCH] Add cargo clean --doc --- src/bin/command_prelude.rs | 4 ++++ src/bin/commands/clean.rs | 2 ++ src/cargo/ops/cargo_clean.rs | 9 ++++++++ tests/testsuite/clean.rs | 40 ++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) diff --git a/src/bin/command_prelude.rs b/src/bin/command_prelude.rs index 9875b6b7c..2f6a04624 100644 --- a/src/bin/command_prelude.rs +++ b/src/bin/command_prelude.rs @@ -105,6 +105,10 @@ pub trait AppExt: Sized { self._arg(opt("release", release)) } + fn arg_doc(self, doc: &'static str) -> Self { + self._arg(opt("doc", doc)) + } + fn arg_target_triple(self, target: &'static str) -> Self { self._arg(opt("target", target).value_name("TRIPLE")) } diff --git a/src/bin/commands/clean.rs b/src/bin/commands/clean.rs index 1661091d8..a7606a644 100644 --- a/src/bin/commands/clean.rs +++ b/src/bin/commands/clean.rs @@ -10,6 +10,7 @@ pub fn cli() -> App { .arg_target_triple("Target triple to clean output for (default all)") .arg_target_dir() .arg_release("Whether or not to clean release artifacts") + .arg_doc("Whether or not to clean just the documentation directory") .after_help( "\ If the --package argument is given, then SPEC is a package id specification @@ -27,6 +28,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { spec: values(args, "package"), target: args.target(), release: args.is_present("release"), + doc: args.is_present("doc"), }; ops::clean(&ws, &opts)?; Ok(()) diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index 72b196699..e6b72f843 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -17,6 +17,8 @@ pub struct CleanOptions<'a> { pub target: Option, /// Whether to clean the release directory pub release: bool, + /// Whether to just clean the doc directory + pub doc: bool, } /// Cleans the project from build artifacts. @@ -24,6 +26,13 @@ pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> { let target_dir = ws.target_dir(); let config = ws.config(); + // If the doc option is set, we just want to delete the doc directory. + if opts.doc { + let target_dir = target_dir.join("doc"); + let target_dir = target_dir.into_path_unlocked(); + return rm_rf(&target_dir, config); + } + // If we have a spec, then we need to delete some packages, otherwise, just // remove the whole target directory and be done with it! // diff --git a/tests/testsuite/clean.rs b/tests/testsuite/clean.rs index f5a409f67..661ea532e 100644 --- a/tests/testsuite/clean.rs +++ b/tests/testsuite/clean.rs @@ -161,6 +161,46 @@ fn clean_release() { ); } +#[test] +fn clean_doc() { + let p = project("foo") + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + + [dependencies] + a = { path = "a" } + "#, + ) + .file("src/main.rs", "fn main() {}") + .file( + "a/Cargo.toml", + r#" + [package] + name = "a" + version = "0.0.1" + authors = [] + "#, + ) + .file("a/src/lib.rs", "") + .build(); + + assert_that(p.cargo("doc"), execs().with_status(0)); + + let doc_path = &p.build_dir().join("doc"); + + assert_that(doc_path, existing_dir()); + + assert_that(p.cargo("clean").arg("--doc"), execs().with_status(0)); + + assert_that(doc_path, is_not(existing_dir())); + assert_that(p.build_dir(), existing_dir()); +} + #[test] fn build_script() { let p = project("foo")