Skip to content

Commit 3083ce7

Browse files
authored
Rollup merge of #72453 - dtolnay:open, r=Mark-Simulacrum
Add flag to open docs: x.py doc --open This aligns with Cargo's flag `cargo doc --open`. Tested with: ```bash # opens doc/index.html x.py doc --stage 0 --open x.py doc --stage 0 --open src/doc # opens doc/book/index.html x.py doc --stage 0 --open src/doc/book # opens doc/std/index.html x.py doc --stage 0 --open src/libstd # opens doc/proc_macro/index.html x.py doc --stage 0 --open src/libproc_macro # opens both x.py doc --stage 0 --open src/libstd src/libproc_macro ```
2 parents 01adfe1 + 07b1de4 commit 3083ce7

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ dependencies = [
213213
"lazy_static 1.4.0",
214214
"libc",
215215
"num_cpus",
216+
"opener",
216217
"pretty_assertions",
217218
"serde",
218219
"serde_json",

src/bootstrap/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ toml = "0.5"
4848
lazy_static = "1.3.0"
4949
time = "0.1"
5050
ignore = "0.4.10"
51+
opener = "0.4"
5152

5253
[target.'cfg(windows)'.dependencies.winapi]
5354
version = "0.3"

src/bootstrap/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ impl<'a> Builder<'a> {
503503
Subcommand::Check { ref paths } => (Kind::Check, &paths[..]),
504504
Subcommand::Clippy { ref paths } => (Kind::Clippy, &paths[..]),
505505
Subcommand::Fix { ref paths } => (Kind::Fix, &paths[..]),
506-
Subcommand::Doc { ref paths } => (Kind::Doc, &paths[..]),
506+
Subcommand::Doc { ref paths, .. } => (Kind::Doc, &paths[..]),
507507
Subcommand::Test { ref paths, .. } => (Kind::Test, &paths[..]),
508508
Subcommand::Bench { ref paths, .. } => (Kind::Bench, &paths[..]),
509509
Subcommand::Dist { ref paths } => (Kind::Dist, &paths[..]),

src/bootstrap/doc.rs

+58-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,35 @@ book!(
7070
RustdocBook, "src/doc/rustdoc", "rustdoc";
7171
);
7272

73+
fn open(builder: &Builder<'_>, path: impl AsRef<Path>) {
74+
if builder.config.dry_run || !builder.config.cmd.open() {
75+
return;
76+
}
77+
78+
let path = path.as_ref();
79+
builder.info(&format!("Opening doc {}", path.display()));
80+
if let Err(err) = opener::open(path) {
81+
builder.info(&format!("{}\n", err));
82+
}
83+
}
84+
85+
// "src/libstd" -> ["src", "libstd"]
86+
//
87+
// Used for deciding whether a particular step is one requested by the user on
88+
// the `x.py doc` command line, which determines whether `--open` will open that
89+
// page.
90+
fn components_simplified(path: &PathBuf) -> Vec<&str> {
91+
path.iter().map(|component| component.to_str().unwrap_or("???")).collect()
92+
}
93+
94+
fn is_explicit_request(builder: &Builder<'_>, path: &str) -> bool {
95+
builder
96+
.paths
97+
.iter()
98+
.map(components_simplified)
99+
.any(|requested| requested.iter().copied().eq(path.split("/")))
100+
}
101+
73102
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
74103
pub struct UnstableBook {
75104
target: Interned<String>,
@@ -200,6 +229,12 @@ impl Step for TheBook {
200229

201230
invoke_rustdoc(builder, compiler, target, path);
202231
}
232+
233+
if is_explicit_request(builder, "src/doc/book") {
234+
let out = builder.doc_out(target);
235+
let index = out.join("book").join("index.html");
236+
open(builder, &index);
237+
}
203238
}
204239
}
205240

@@ -338,6 +373,13 @@ impl Step for Standalone {
338373
}
339374
builder.run(&mut cmd);
340375
}
376+
377+
// We open doc/index.html as the default if invoked as `x.py doc --open`
378+
// with no particular explicit doc requested (e.g. src/libcore).
379+
if builder.paths.is_empty() || is_explicit_request(builder, "src/doc") {
380+
let index = out.join("index.html");
381+
open(builder, &index);
382+
}
341383
}
342384
}
343385

@@ -418,10 +460,25 @@ impl Step for Std {
418460

419461
builder.run(&mut cargo.into());
420462
};
421-
for krate in &["alloc", "core", "std", "proc_macro", "test"] {
463+
let krates = ["alloc", "core", "std", "proc_macro", "test"];
464+
for krate in &krates {
422465
run_cargo_rustdoc_for(krate);
423466
}
424467
builder.cp_r(&my_out, &out);
468+
469+
// Look for src/libstd, src/libcore etc in the `x.py doc` arguments and
470+
// open the corresponding rendered docs.
471+
for path in builder.paths.iter().map(components_simplified) {
472+
if path.get(0) == Some(&"src")
473+
&& path.get(1).map_or(false, |dir| dir.starts_with("lib"))
474+
{
475+
let requested_crate = &path[1][3..];
476+
if krates.contains(&requested_crate) {
477+
let index = out.join(requested_crate).join("index.html");
478+
open(builder, &index);
479+
}
480+
}
481+
}
425482
}
426483
}
427484

src/bootstrap/flags.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub enum Subcommand {
6161
},
6262
Doc {
6363
paths: Vec<PathBuf>,
64+
open: bool,
6465
},
6566
Test {
6667
paths: Vec<PathBuf>,
@@ -248,6 +249,9 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`",
248249
"bench" => {
249250
opts.optmulti("", "test-args", "extra arguments", "ARGS");
250251
}
252+
"doc" => {
253+
opts.optflag("", "open", "open the docs in a browser");
254+
}
251255
"clean" => {
252256
opts.optflag("", "all", "clean all build artifacts");
253257
}
@@ -404,6 +408,7 @@ Arguments:
404408
./x.py doc src/doc/book
405409
./x.py doc src/doc/nomicon
406410
./x.py doc src/doc/book src/libstd
411+
./x.py doc src/libstd --open
407412
408413
If no arguments are passed then everything is documented:
409414
@@ -479,7 +484,7 @@ Arguments:
479484
},
480485
},
481486
"bench" => Subcommand::Bench { paths, test_args: matches.opt_strs("test-args") },
482-
"doc" => Subcommand::Doc { paths },
487+
"doc" => Subcommand::Doc { paths, open: matches.opt_present("open") },
483488
"clean" => {
484489
if !paths.is_empty() {
485490
println!("\nclean does not take a path argument\n");
@@ -613,6 +618,13 @@ impl Subcommand {
613618
_ => None,
614619
}
615620
}
621+
622+
pub fn open(&self) -> bool {
623+
match *self {
624+
Subcommand::Doc { open, .. } => open,
625+
_ => false,
626+
}
627+
}
616628
}
617629

618630
fn split(s: &[String]) -> Vec<String> {

0 commit comments

Comments
 (0)