Add cargo clean --doc
authorDarkDrek <wackerei@gmail.com>
Tue, 1 May 2018 15:33:10 +0000 (17:33 +0200)
committerDarkDrek <wackerei@gmail.com>
Tue, 1 May 2018 15:38:22 +0000 (17:38 +0200)
src/bin/command_prelude.rs
src/bin/commands/clean.rs
src/cargo/ops/cargo_clean.rs
tests/testsuite/clean.rs

index 9875b6b7c22f1c1982962d9ae9f1593b86e4d4d8..2f6a046243074e720376b9b318a3a0e0858e8ef6 100644 (file)
@@ -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"))
     }
index 1661091d8c7729615e6533490d1d8ad82c3b713c..a7606a6444c96719bf98eb00d24d70145889a0c0 100644 (file)
@@ -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(())
index 72b1966990b19521208a1c8169ec3b32940cecd5..e6b72f843600812a003cbbf1df0381f7a781d3ce 100644 (file)
@@ -17,6 +17,8 @@ pub struct CleanOptions<'a> {
     pub target: Option<String>,
     /// 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!
     //
index f5a409f6728d316f64c655bf2588855719c1c93d..661ea532e14ad63c8499600fa5487991c0060686 100644 (file)
@@ -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")