Skip to content

Commit c26f4fc

Browse files
authored
Merge pull request #1549 from nrc/renames
Change handling of renames
2 parents c1470c9 + 06af140 commit c26f4fc

File tree

16 files changed

+439
-354
lines changed

16 files changed

+439
-354
lines changed

src/rustup-cli/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ pub fn rustc_version(toolchain: &Toolchain) -> String {
284284
pub fn list_targets(toolchain: &Toolchain) -> Result<()> {
285285
let mut t = term2::stdout();
286286
for component in toolchain.list_components()? {
287-
if component.component.pkg == "rust-std" {
287+
if component.component.name_in_manifest() == "rust-std" {
288288
let target = component
289289
.component
290290
.target
@@ -310,7 +310,7 @@ pub fn list_targets(toolchain: &Toolchain) -> Result<()> {
310310
pub fn list_components(toolchain: &Toolchain) -> Result<()> {
311311
let mut t = term2::stdout();
312312
for component in toolchain.list_components()? {
313-
let name = component.component.name();
313+
let name = component.name;
314314
if component.required {
315315
let _ = t.attr(term2::Attr::Bold);
316316
let _ = writeln!(t, "{} (default)", name);

src/rustup-cli/rustup_mode.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ fn show(cfg: &Cfg) -> Result<()> {
650650
match t.list_components() {
651651
Ok(cs_vec) => cs_vec
652652
.into_iter()
653-
.filter(|c| c.component.pkg == "rust-std")
653+
.filter(|c| c.component.name_in_manifest() == "rust-std")
654654
.filter(|c| c.installed)
655655
.collect(),
656656
Err(_) => vec![],
@@ -778,10 +778,7 @@ fn target_add(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
778778
let toolchain = explicit_or_dir_toolchain(cfg, m)?;
779779

780780
for target in m.values_of("target").expect("") {
781-
let new_component = Component {
782-
pkg: "rust-std".to_string(),
783-
target: Some(TargetTriple::from_str(target)),
784-
};
781+
let new_component = Component::new("rust-std".to_string(), Some(TargetTriple::from_str(target)));
785782

786783
toolchain.add_component(new_component)?;
787784
}
@@ -793,10 +790,7 @@ fn target_remove(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
793790
let toolchain = explicit_or_dir_toolchain(cfg, m)?;
794791

795792
for target in m.values_of("target").expect("") {
796-
let new_component = Component {
797-
pkg: "rust-std".to_string(),
798-
target: Some(TargetTriple::from_str(target)),
799-
};
793+
let new_component = Component::new("rust-std".to_string(), Some(TargetTriple::from_str(target)));
800794

801795
toolchain.remove_component(new_component)?;
802796
}
@@ -823,10 +817,7 @@ fn component_add(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
823817
});
824818

825819
for component in m.values_of("component").expect("") {
826-
let new_component = Component {
827-
pkg: component.to_string(),
828-
target: target.clone(),
829-
};
820+
let new_component = Component::new(component.to_string(), target.clone());
830821

831822
toolchain.add_component(new_component)?;
832823
}
@@ -847,10 +838,7 @@ fn component_remove(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
847838
});
848839

849840
for component in m.values_of("component").expect("") {
850-
let new_component = Component {
851-
pkg: component.to_string(),
852-
target: target.clone(),
853-
};
841+
let new_component = Component::new(component.to_string(), target.clone());
854842

855843
toolchain.remove_component(new_component)?;
856844
}

src/rustup-cli/self_update.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use common::{self, Confirm};
3434
use errors::*;
3535
use rustup_dist::dist;
3636
use rustup_utils::utils;
37+
use rustup::{TOOLS, DUP_TOOLS};
3738
use same_file::Handle;
3839
use std::env;
3940
use std::env::consts::EXE_SUFFIX;
@@ -192,14 +193,6 @@ doing then it is fine to continue installation without the build
192193
tools, but otherwise, install the C++ build tools before proceeding.
193194
"#;
194195

195-
static TOOLS: &'static [&'static str] =
196-
&["rustc", "rustdoc", "cargo", "rust-lldb", "rust-gdb", "rls", "cargo-clippy"];
197-
198-
// Tools which are commonly installed by Cargo as well as rustup. We take a bit
199-
// more care with these to ensure we don't overwrite the user's previous
200-
// installation.
201-
static DUP_TOOLS: &'static [&'static str] = &["rustfmt", "cargo-fmt"];
202-
203196
static UPDATE_ROOT: &'static str = "https://static.rust-lang.org/rustup";
204197

205198
/// `CARGO_HOME` suitable for display, possibly with $HOME

src/rustup-dist/src/errors.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::{self, Write};
33
use temp;
44
use toml;
55
use rustup_utils;
6-
use manifest::Component;
6+
use manifest::{Component, Manifest};
77

88
error_chain! {
99
links {
@@ -82,15 +82,9 @@ error_chain! {
8282
ComponentFilePermissionsFailed {
8383
description("error setting file permissions during install")
8484
}
85-
ComponentDownloadFailed(c: Component) {
85+
ComponentDownloadFailed(c: String) {
8686
description("component download failed")
87-
display("component download failed for {}{}", c.pkg, {
88-
if let Some(ref t) = c.target {
89-
format!("-{}", t)
90-
} else {
91-
"".to_owned()
92-
}
93-
})
87+
display("component download failed for {}", c)
9488
}
9589
Parsing(e: toml::de::Error) {
9690
description("error parsing manifest")
@@ -99,50 +93,42 @@ error_chain! {
9993
description("unsupported manifest version")
10094
display("manifest version '{}' is not supported", v)
10195
}
102-
MissingPackageForComponent(c: Component) {
96+
MissingPackageForComponent(name: String) {
10397
description("missing package for component")
104-
display("server sent a broken manifest: missing package for component {}", c.name())
98+
display("server sent a broken manifest: missing package for component {}", name)
10599
}
106100
MissingPackageForRename(name: String) {
107101
description("missing package for the target of a rename")
108102
display("server sent a broken manifest: missing package for the target of a rename {}", name)
109103
}
110-
RequestedComponentsUnavailable(c: Vec<Component>) {
104+
RequestedComponentsUnavailable(c: Vec<Component>, manifest: Manifest) {
111105
description("some requested components are unavailable to download")
112-
display("{}", component_unavailable_msg(&c))
106+
display("{}", component_unavailable_msg(&c, &manifest))
113107
}
114108
}
115109
}
116110

117-
fn component_unavailable_msg(cs: &[Component]) -> String {
111+
fn component_unavailable_msg(cs: &[Component], manifest: &Manifest) -> String {
118112
assert!(!cs.is_empty());
119113

120114
let mut buf = vec![];
121115

122-
fn format_component(c: &Component) -> String {
123-
if let Some(ref t) = c.target {
124-
format!("'{}' for '{}'", c.pkg, t)
125-
} else {
126-
format!("'{}'", c.pkg)
127-
}
128-
}
129-
130116
if cs.len() == 1 {
131117
let _ = write!(
132118
buf,
133119
"component {} is unavailable for download",
134-
format_component(&cs[0])
120+
&cs[0].description(manifest)
135121
);
136122
} else {
137123
use itertools::Itertools;
138124
let same_target = cs.iter()
139125
.all(|c| c.target == cs[0].target || c.target.is_none());
140126
if same_target {
141-
let mut cs_strs = cs.iter().map(|c| format!("'{}'", c.pkg));
127+
let mut cs_strs = cs.iter().map(|c| format!("'{}'", c.short_name(manifest)));
142128
let cs_str = cs_strs.join(", ");
143129
let _ = write!(buf, "some components unavailable for download: {}", cs_str);
144130
} else {
145-
let mut cs_strs = cs.iter().map(format_component);
131+
let mut cs_strs = cs.iter().map(|c| c.description(manifest));
146132
let cs_str = cs_strs.join(", ");
147133
let _ = write!(buf, "some components unavailable for download: {}", cs_str);
148134
}

src/rustup-dist/src/manifest.rs

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct Manifest {
2626
pub date: String,
2727
pub packages: HashMap<String, Package>,
2828
pub renames: HashMap<String, String>,
29+
pub reverse_renames: HashMap<String, String>,
2930
}
3031

3132
#[derive(Clone, Debug, PartialEq)]
@@ -57,7 +58,7 @@ pub struct PackageBins {
5758

5859
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
5960
pub struct Component {
60-
pub pkg: String,
61+
pkg: String,
6162
pub target: Option<TargetTriple>,
6263
}
6364

@@ -78,11 +79,13 @@ impl Manifest {
7879
if !SUPPORTED_MANIFEST_VERSIONS.contains(&&*version) {
7980
return Err(ErrorKind::UnsupportedVersion(version).into());
8081
}
82+
let (renames, reverse_renames) = Self::table_to_renames(&mut table, path)?;
8183
Ok(Manifest {
8284
manifest_version: version,
8385
date: get_string(&mut table, "date", path)?,
8486
packages: Self::table_to_packages(&mut table, path)?,
85-
renames: Self::table_to_renames(table, path)?,
87+
renames,
88+
reverse_renames,
8689
})
8790
}
8891
pub fn to_toml(self) -> toml::value::Table {
@@ -127,19 +130,22 @@ impl Manifest {
127130
}
128131

129132
fn table_to_renames(
130-
mut table: toml::value::Table,
133+
table: &mut toml::value::Table,
131134
path: &str,
132-
) -> Result<HashMap<String, String>> {
133-
let mut result = HashMap::new();
134-
let rename_table = get_table(&mut table, "rename", path)?;
135+
) -> Result<(HashMap<String, String>, HashMap<String, String>)> {
136+
let mut renames = HashMap::new();
137+
let mut reverse_renames = HashMap::new();
138+
let rename_table = get_table(table, "rename", path)?;
135139

136140
for (k, v) in rename_table {
137141
if let toml::Value::Table(mut t) = v {
138-
result.insert(k.to_owned(), get_string(&mut t, "to", path)?);
142+
let to = get_string(&mut t, "to", path)?;
143+
renames.insert(k.to_owned(), to.clone());
144+
reverse_renames.insert(to, k.to_owned());
139145
}
140146
}
141147

142-
Ok(result)
148+
Ok((renames, reverse_renames))
143149
}
144150
fn renames_to_table(renames: HashMap<String, String>) -> toml::value::Table {
145151
let mut result = toml::value::Table::new();
@@ -164,9 +170,9 @@ impl Manifest {
164170
fn validate_targeted_package(&self, tpkg: &TargetedPackage) -> Result<()> {
165171
for c in tpkg.components.iter().chain(tpkg.extensions.iter()) {
166172
let cpkg = self.get_package(&c.pkg)
167-
.chain_err(|| ErrorKind::MissingPackageForComponent(c.clone()))?;
173+
.chain_err(|| ErrorKind::MissingPackageForComponent(c.short_name(self)))?;
168174
let _ctpkg = cpkg.get_target(c.target.as_ref())
169-
.chain_err(|| ErrorKind::MissingPackageForComponent(c.clone()))?;
175+
.chain_err(|| ErrorKind::MissingPackageForComponent(c.short_name(self)))?;
170176
}
171177
Ok(())
172178
}
@@ -194,6 +200,16 @@ impl Manifest {
194200

195201
Ok(())
196202
}
203+
204+
// If the component should be renamed by this manifest, then return a new
205+
// component with the new name. If not, return `None`.
206+
pub fn rename_component(&self, component: &Component) -> Option<Component> {
207+
self.renames.get(&component.pkg).map(|r| {
208+
let mut c = component.clone();
209+
c.pkg = r.clone();
210+
c
211+
})
212+
}
197213
}
198214

199215
impl Package {
@@ -359,6 +375,15 @@ impl TargetedPackage {
359375
}
360376

361377
impl Component {
378+
pub fn new(pkg: String, target: Option<TargetTriple>) -> Component {
379+
Component { pkg, target }
380+
}
381+
pub fn wildcard(&self) -> Component {
382+
Component {
383+
pkg: self.pkg.clone(),
384+
target: None,
385+
}
386+
}
362387
pub fn from_toml(mut table: toml::value::Table, path: &str) -> Result<Self> {
363388
Ok(Component {
364389
pkg: get_string(&mut table, "pkg", path)?,
@@ -384,18 +409,30 @@ impl Component {
384409
result.insert("pkg".to_owned(), toml::Value::String(self.pkg));
385410
result
386411
}
387-
pub fn name(&self) -> String {
412+
pub fn name(&self, manifest: &Manifest) -> String {
413+
let pkg = self.short_name(manifest);
388414
if let Some(ref t) = self.target {
389-
format!("{}-{}", self.pkg, t)
415+
format!("{}-{}", pkg, t)
416+
} else {
417+
format!("{}", pkg)
418+
}
419+
}
420+
pub fn short_name(&self, manifest: &Manifest) -> String {
421+
if let Some(from) = manifest.reverse_renames.get(&self.pkg) {
422+
from.to_owned()
390423
} else {
391-
format!("{}", self.pkg)
424+
self.pkg.clone()
392425
}
393426
}
394-
pub fn description(&self) -> String {
427+
pub fn description(&self, manifest: &Manifest) -> String {
428+
let pkg = self.short_name(manifest);
395429
if let Some(ref t) = self.target {
396-
format!("'{}' for target '{}'", self.pkg, t)
430+
format!("'{}' for target '{}'", pkg, t)
397431
} else {
398-
format!("'{}'", self.pkg)
432+
format!("'{}'", pkg)
399433
}
400434
}
435+
pub fn name_in_manifest(&self) -> &String {
436+
&self.pkg
437+
}
401438
}

0 commit comments

Comments
 (0)