Skip to content

Commit 2ea2faf

Browse files
committed
Add cargo clean --doc
1 parent 181de52 commit 2ea2faf

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

src/bin/command_prelude.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ pub trait AppExt: Sized {
105105
self._arg(opt("release", release))
106106
}
107107

108+
fn arg_doc(self, doc: &'static str) -> Self {
109+
self._arg(opt("doc", doc))
110+
}
111+
108112
fn arg_target_triple(self, target: &'static str) -> Self {
109113
self._arg(opt("target", target).value_name("TRIPLE"))
110114
}

src/bin/commands/clean.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub fn cli() -> App {
1010
.arg_target_triple("Target triple to clean output for (default all)")
1111
.arg_target_dir()
1212
.arg_release("Whether or not to clean release artifacts")
13+
.arg_doc("Whether or not to clean just the documentation directory")
1314
.after_help(
1415
"\
1516
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 {
2728
spec: values(args, "package"),
2829
target: args.target(),
2930
release: args.is_present("release"),
31+
doc: args.is_present("doc"),
3032
};
3133
ops::clean(&ws, &opts)?;
3234
Ok(())

src/cargo/ops/cargo_clean.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,22 @@ pub struct CleanOptions<'a> {
1717
pub target: Option<String>,
1818
/// Whether to clean the release directory
1919
pub release: bool,
20+
/// Whether to just clean the doc directory
21+
pub doc: bool,
2022
}
2123

2224
/// Cleans the project from build artifacts.
2325
pub fn clean(ws: &Workspace, opts: &CleanOptions) -> CargoResult<()> {
2426
let target_dir = ws.target_dir();
2527
let config = ws.config();
2628

29+
// If the doc option is set, we just want to delete the doc directory.
30+
if opts.doc {
31+
let target_dir = target_dir.join("doc");
32+
let target_dir = target_dir.into_path_unlocked();
33+
return rm_rf(&target_dir, config);
34+
}
35+
2736
// If we have a spec, then we need to delete some packages, otherwise, just
2837
// remove the whole target directory and be done with it!
2938
//

tests/testsuite/clean.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,46 @@ fn clean_release() {
161161
);
162162
}
163163

164+
#[test]
165+
fn clean_doc() {
166+
let p = project("foo")
167+
.file(
168+
"Cargo.toml",
169+
r#"
170+
[package]
171+
name = "foo"
172+
version = "0.0.1"
173+
authors = []
174+
175+
[dependencies]
176+
a = { path = "a" }
177+
"#,
178+
)
179+
.file("src/main.rs", "fn main() {}")
180+
.file(
181+
"a/Cargo.toml",
182+
r#"
183+
[package]
184+
name = "a"
185+
version = "0.0.1"
186+
authors = []
187+
"#,
188+
)
189+
.file("a/src/lib.rs", "")
190+
.build();
191+
192+
assert_that(p.cargo("doc"), execs().with_status(0));
193+
194+
let doc_path = &p.build_dir().join("doc");
195+
196+
assert_that(doc_path, existing_dir());
197+
198+
assert_that(p.cargo("clean").arg("--doc"), execs().with_status(0));
199+
200+
assert_that(doc_path, is_not(existing_dir()));
201+
assert_that(p.build_dir(), existing_dir());
202+
}
203+
164204
#[test]
165205
fn build_script() {
166206
let p = project("foo")

0 commit comments

Comments
 (0)