Skip to content

Commit

Permalink
Merge pull request #2088 from kinnison/kinnison/component-with-target
Browse files Browse the repository at this point in the history
component: Support installation/removal of `component-target`
  • Loading branch information
kinnison authored Oct 29, 2019
2 parents 37d52a9 + 72f9880 commit 9c7f313
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,8 @@ fn component_add(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
});

for component in m.values_of("component").expect("") {
let new_component = Component::new(component.to_string(), target.clone(), true);
let new_component = Component::new_with_target(component, false)
.unwrap_or_else(|| Component::new(component.to_string(), target.clone(), true));

toolchain.add_component(new_component)?;
}
Expand All @@ -1138,7 +1139,8 @@ fn component_remove(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
});

for component in m.values_of("component").expect("") {
let new_component = Component::new(component.to_string(), target.clone(), true);
let new_component = Component::new_with_target(component, false)
.unwrap_or_else(|| Component::new(component.to_string(), target.clone(), true));

toolchain.remove_component(new_component)?;
}
Expand Down
16 changes: 16 additions & 0 deletions src/dist/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ impl TargetTriple {
}
}

impl std::convert::TryFrom<PartialTargetTriple> for TargetTriple {
type Error = &'static str;
fn try_from(value: PartialTargetTriple) -> std::result::Result<Self, Self::Error> {
if value.arch.is_some() && value.os.is_some() && value.env.is_some() {
Ok(Self(format!(
"{}-{}-{}",
value.arch.unwrap(),
value.os.unwrap(),
value.env.unwrap()
)))
} else {
Err("Incomplete / bad target triple")
}
}
}

impl PartialTargetTriple {
pub fn new(name: &str) -> Option<Self> {
if name.is_empty() {
Expand Down
21 changes: 20 additions & 1 deletion src/dist/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use crate::errors::*;
use crate::utils::toml_utils::*;

use crate::dist::dist::{Profile, TargetTriple};
use crate::dist::dist::{PartialTargetTriple, Profile, TargetTriple};
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::str::FromStr;
Expand Down Expand Up @@ -496,6 +496,25 @@ impl Component {
is_extension,
}
}

pub fn new_with_target(pkg_with_target: &str, is_extension: bool) -> Option<Self> {
use std::convert::TryFrom;
for (pos, _) in pkg_with_target.match_indices('-') {
let pkg = &pkg_with_target[0..pos];
let target = &pkg_with_target[pos + 1..];
if let Some(partial) = PartialTargetTriple::new(target) {
if let Ok(triple) = TargetTriple::try_from(partial) {
return Some(Self {
pkg: pkg.to_string(),
target: Some(triple),
is_extension,
});
}
}
}
None
}

pub fn wildcard(&self) -> Self {
Self {
pkg: self.pkg.clone(),
Expand Down
53 changes: 53 additions & 0 deletions tests/cli-v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,59 @@ fn add_all_targets_fail() {
});
}

#[test]
fn add_target_by_component_add() {
setup(&|config| {
expect_ok(config, &["rustup", "default", "nightly"]);
expect_not_stdout_ok(
config,
&["rustup", "target", "list"],
&format!("{} (installed)", clitools::CROSS_ARCH1),
);
expect_ok(
config,
&[
"rustup",
"component",
"add",
&format!("rust-std-{}", clitools::CROSS_ARCH1),
],
);
expect_stdout_ok(
config,
&["rustup", "target", "list"],
&format!("{} (installed)", clitools::CROSS_ARCH1),
);
})
}

#[test]
fn remove_target_by_component_remove() {
setup(&|config| {
expect_ok(config, &["rustup", "default", "nightly"]);
expect_ok(config, &["rustup", "target", "add", clitools::CROSS_ARCH1]);
expect_stdout_ok(
config,
&["rustup", "target", "list"],
&format!("{} (installed)", clitools::CROSS_ARCH1),
);
expect_ok(
config,
&[
"rustup",
"component",
"remove",
&format!("rust-std-{}", clitools::CROSS_ARCH1),
],
);
expect_not_stdout_ok(
config,
&["rustup", "target", "list"],
&format!("{} (installed)", clitools::CROSS_ARCH1),
);
})
}

#[test]
fn add_target_no_toolchain() {
setup(&|config| {
Expand Down

0 comments on commit 9c7f313

Please sign in to comment.