From 6bc36d18ce5bcbf542718a177f179f169bfd756b Mon Sep 17 00:00:00 2001 From: Pietro Albini Date: Thu, 3 Nov 2022 11:42:51 +0100 Subject: [PATCH 1/2] move browser opening logic in Builder This allows open() to be called from other places in bootstrap (I need this for Ferrocene), and it simplifies the callers by moving the "was_invoked_explicitly" check into the function. --- src/bootstrap/builder.rs | 18 ++++++++++++++++++ src/bootstrap/doc.rs | 40 +++++++++++----------------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 6de3746363337..406bae02d84da 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -2207,6 +2207,24 @@ impl<'a> Builder<'a> { false } + + pub(crate) fn maybe_open_in_browser(&self, path: impl AsRef) { + if self.was_invoked_explicitly::(Kind::Doc) { + self.open_in_browser(path); + } + } + + pub(crate) fn open_in_browser(&self, path: impl AsRef) { + if self.config.dry_run || !self.config.cmd.open() { + return; + } + + let path = path.as_ref(); + self.info(&format!("Opening doc {}", path.display())); + if let Err(err) = opener::open(path) { + self.info(&format!("{}\n", err)); + } + } } #[cfg(test)] diff --git a/src/bootstrap/doc.rs b/src/bootstrap/doc.rs index 5d265b9ad0c19..0045856e2c957 100644 --- a/src/bootstrap/doc.rs +++ b/src/bootstrap/doc.rs @@ -12,7 +12,7 @@ use std::fs; use std::io; use std::path::{Path, PathBuf}; -use crate::builder::{Builder, Compiler, Kind, RunConfig, ShouldRun, Step}; +use crate::builder::{Builder, Compiler, RunConfig, ShouldRun, Step}; use crate::cache::{Interned, INTERNER}; use crate::compile; use crate::config::{Config, TargetSelection}; @@ -85,18 +85,6 @@ book!( StyleGuide, "src/doc/style-guide", "style-guide"; ); -fn open(builder: &Builder<'_>, path: impl AsRef) { - if builder.config.dry_run || !builder.config.cmd.open() { - return; - } - - let path = path.as_ref(); - builder.info(&format!("Opening doc {}", path.display())); - if let Err(err) = opener::open(path) { - builder.info(&format!("{}\n", err)); - } -} - // "library/std" -> ["library", "std"] // // Used for deciding whether a particular step is one requested by the user on @@ -240,11 +228,9 @@ impl Step for TheBook { invoke_rustdoc(builder, compiler, &shared_assets, target, path); } - if builder.was_invoked_explicitly::(Kind::Doc) { - let out = builder.doc_out(target); - let index = out.join("book").join("index.html"); - open(builder, &index); - } + let out = builder.doc_out(target); + let index = out.join("book").join("index.html"); + builder.maybe_open_in_browser::(index); } } @@ -384,10 +370,7 @@ impl Step for Standalone { // We open doc/index.html as the default if invoked as `x.py doc --open` // with no particular explicit doc requested (e.g. library/core). - if builder.paths.is_empty() || builder.was_invoked_explicitly::(Kind::Doc) { - let index = out.join("index.html"); - open(builder, &index); - } + builder.maybe_open_in_browser::(out.join("index.html")); } } @@ -507,7 +490,7 @@ impl Step for Std { for requested_crate in requested_crates { if STD_PUBLIC_CRATES.iter().any(|k| *k == requested_crate.as_str()) { let index = out.join(requested_crate).join("index.html"); - open(builder, &index); + builder.open_in_browser(index); } } } @@ -759,7 +742,7 @@ impl Step for Rustc { // Let's open the first crate documentation page: if let Some(krate) = to_open { let index = out.join(krate).join("index.html"); - open(builder, &index); + builder.open_in_browser(index); } } } @@ -1019,10 +1002,9 @@ impl Step for RustcBook { name: INTERNER.intern_str("rustc"), src: INTERNER.intern_path(out_base), }); - if builder.was_invoked_explicitly::(Kind::Doc) { - let out = builder.doc_out(self.target); - let index = out.join("rustc").join("index.html"); - open(builder, &index); - } + + let out = builder.doc_out(self.target); + let index = out.join("rustc").join("index.html"); + builder.maybe_open_in_browser::(index); } } From 5e4618ff1433e92009c7478933874f5f23b34e29 Mon Sep 17 00:00:00 2001 From: Pietro Albini Date: Thu, 3 Nov 2022 16:42:28 +0100 Subject: [PATCH 2/2] address review comment --- src/bootstrap/doc.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/doc.rs b/src/bootstrap/doc.rs index 0045856e2c957..1357718b84e35 100644 --- a/src/bootstrap/doc.rs +++ b/src/bootstrap/doc.rs @@ -12,7 +12,7 @@ use std::fs; use std::io; use std::path::{Path, PathBuf}; -use crate::builder::{Builder, Compiler, RunConfig, ShouldRun, Step}; +use crate::builder::{Builder, Compiler, Kind, RunConfig, ShouldRun, Step}; use crate::cache::{Interned, INTERNER}; use crate::compile; use crate::config::{Config, TargetSelection}; @@ -370,7 +370,10 @@ impl Step for Standalone { // We open doc/index.html as the default if invoked as `x.py doc --open` // with no particular explicit doc requested (e.g. library/core). - builder.maybe_open_in_browser::(out.join("index.html")); + if builder.paths.is_empty() || builder.was_invoked_explicitly::(Kind::Doc) { + let index = out.join("index.html"); + builder.open_in_browser(&index); + } } }