Skip to content

Commit d3d3397

Browse files
committed
Use Profile enum for x.py setup
1 parent 6e06388 commit d3d3397

File tree

3 files changed

+75
-38
lines changed

3 files changed

+75
-38
lines changed

src/bootstrap/flags.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use getopts::Options;
1212

1313
use crate::builder::Builder;
1414
use crate::config::{Config, TargetSelection};
15+
use crate::setup::Profile;
1516
use crate::{Build, DocTests};
1617

1718
/// Deserialized version of all flags for this compile.
@@ -94,7 +95,7 @@ pub enum Subcommand {
9495
paths: Vec<PathBuf>,
9596
},
9697
Setup {
97-
path: String,
98+
profile: Profile,
9899
},
99100
}
100101

@@ -533,18 +534,19 @@ Arguments:
533534
Subcommand::Run { paths }
534535
}
535536
"setup" => {
536-
let path = if paths.len() > 1 {
537+
let profile = if paths.len() > 1 {
537538
println!("\nat most one profile can be passed to setup\n");
538539
usage(1, &opts, verbose, &subcommand_help)
539540
} else if let Some(path) = paths.pop() {
540-
t!(path.into_os_string().into_string().map_err(|path| format!(
541-
"{} is not a valid UTF8 string",
542-
path.to_string_lossy()
543-
)))
541+
let profile_string = t!(path.into_os_string().into_string().map_err(
542+
|path| format!("{} is not a valid UTF8 string", path.to_string_lossy())
543+
));
544+
545+
profile_string.parse().expect("unknown profile")
544546
} else {
545547
t!(crate::setup::interactive_path())
546548
};
547-
Subcommand::Setup { path }
549+
Subcommand::Setup { profile }
548550
}
549551
_ => {
550552
usage(1, &opts, verbose, &subcommand_help);

src/bootstrap/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,8 @@ impl Build {
471471
return clean::clean(self, all);
472472
}
473473

474-
if let Subcommand::Setup { path: include_name } = &self.config.cmd {
475-
return setup::setup(&self.config.src, include_name);
474+
if let Subcommand::Setup { profile } = &self.config.cmd {
475+
return setup::setup(&self.config.src, *profile);
476476
}
477477

478478
{

src/bootstrap/setup.rs

+64-29
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,56 @@
11
use crate::t;
22
use std::path::{Path, PathBuf};
3+
use std::str::FromStr;
34
use std::{
4-
env, fs,
5+
env, fmt, fs,
56
io::{self, Write},
67
};
78

8-
pub fn setup(src_path: &Path, include_name: &str) {
9+
#[derive(Clone, Copy, Eq, PartialEq)]
10+
pub enum Profile {
11+
Compiler,
12+
Codegen,
13+
Library,
14+
User,
15+
}
16+
17+
impl Profile {
18+
fn include_path(&self, src_path: &Path) -> PathBuf {
19+
PathBuf::from(format!("{}/src/bootstrap/defaults/config.{}.toml", src_path.display(), self))
20+
}
21+
}
22+
23+
#[derive(Debug)]
24+
pub struct ProfileErr {
25+
pub name: String,
26+
}
27+
28+
impl FromStr for Profile {
29+
type Err = ProfileErr;
30+
31+
fn from_str(s: &str) -> Result<Self, Self::Err> {
32+
match s {
33+
"a" | "lib" | "library" => Ok(Profile::Library),
34+
"b" | "compiler" => Ok(Profile::Compiler),
35+
"c" | "llvm" | "codegen" => Ok(Profile::Codegen),
36+
"d" | "maintainer" | "user" => Ok(Profile::User),
37+
_ => Err(ProfileErr { name: s.to_string() }),
38+
}
39+
}
40+
}
41+
42+
impl fmt::Display for Profile {
43+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44+
match self {
45+
Profile::Compiler => write!(f, "compiler"),
46+
Profile::Codegen => write!(f, "codegen"),
47+
Profile::Library => write!(f, "library"),
48+
Profile::User => write!(f, "user"),
49+
}
50+
}
51+
}
52+
53+
pub fn setup(src_path: &Path, profile: Profile) {
954
let cfg_file = env::var_os("BOOTSTRAP_CONFIG").map(PathBuf::from);
1055

1156
if cfg_file.as_ref().map_or(false, |f| f.exists()) {
@@ -14,15 +59,10 @@ pub fn setup(src_path: &Path, include_name: &str) {
1459
"error: you asked `x.py` to setup a new config file, but one already exists at `{}`",
1560
file.display()
1661
);
62+
println!("help: try adding `profile = \"{}\"` at the top of {}", profile, file.display());
1763
println!(
18-
"help: try adding `profile = \"{}\"` at the top of {}",
19-
include_name,
20-
file.display()
21-
);
22-
println!(
23-
"note: this will use the configuration in {}/src/bootstrap/defaults/config.{}.toml",
24-
src_path.display(),
25-
include_name
64+
"note: this will use the configuration in {}",
65+
profile.include_path(src_path).display()
2666
);
2767
std::process::exit(1);
2868
}
@@ -31,35 +71,33 @@ pub fn setup(src_path: &Path, include_name: &str) {
3171
let settings = format!(
3272
"# Includes one of the default files in src/bootstrap/defaults\n\
3373
profile = \"{}\"\n",
34-
include_name
74+
profile
3575
);
3676
t!(fs::write(path, settings));
3777

38-
let include_path =
39-
format!("{}/src/bootstrap/defaults/config.{}.toml", src_path.display(), include_name);
40-
println!("`x.py` will now use the configuration at {}", include_path);
78+
let include_path = profile.include_path(src_path);
79+
println!("`x.py` will now use the configuration at {}", include_path.display());
4180

42-
let suggestions = match include_name {
43-
"llvm" | "codegen" | "compiler" => &["check", "build", "test"][..],
44-
"library" => &["check", "build", "test library/std", "doc"],
45-
"maintainer" | "user" => &["dist", "build"],
46-
_ => return,
81+
let suggestions = match profile {
82+
Profile::Codegen | Profile::Compiler => &["check", "build", "test"][..],
83+
Profile::Library => &["check", "build", "test library/std", "doc"],
84+
Profile::User => &["dist", "build"],
4785
};
4886

4987
println!("To get started, try one of the following commands:");
5088
for cmd in suggestions {
5189
println!("- `x.py {}`", cmd);
5290
}
5391

54-
if include_name != "user" {
92+
if profile != Profile::User {
5593
println!(
5694
"For more suggestions, see https://rustc-dev-guide.rust-lang.org/building/suggested.html"
5795
);
5896
}
5997
}
6098

6199
// Used to get the path for `Subcommand::Setup`
62-
pub fn interactive_path() -> io::Result<String> {
100+
pub fn interactive_path() -> io::Result<Profile> {
63101
let mut input = String::new();
64102
println!(
65103
"Welcome to the Rust project! What do you want to do with x.py?
@@ -72,17 +110,14 @@ d) Install Rust from source"
72110
print!("Please choose one (a/b/c/d): ");
73111
io::stdout().flush()?;
74112
io::stdin().read_line(&mut input)?;
75-
break match input.trim().to_lowercase().as_str() {
76-
"a" | "lib" | "library" => "library",
77-
"b" | "compiler" => "compiler",
78-
"c" | "llvm" => "llvm",
79-
"d" | "user" | "maintainer" => "maintainer",
80-
_ => {
81-
println!("error: unrecognized option '{}'", input.trim());
113+
break match input.trim().to_lowercase().parse() {
114+
Ok(profile) => profile,
115+
Err(ProfileErr { name }) => {
116+
println!("error: unrecognized option '{}'", name);
82117
println!("note: press Ctrl+C to exit");
83118
continue;
84119
}
85120
};
86121
};
87-
Ok(template.to_owned())
122+
Ok(template)
88123
}

0 commit comments

Comments
 (0)