Skip to content

Commit fd35aae

Browse files
committed
Move add_component to DistributableToolchain
1 parent 08bf964 commit fd35aae

File tree

2 files changed

+69
-66
lines changed

2 files changed

+69
-66
lines changed

src/cli/rustup_mode.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,8 @@ fn target_add(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
11301130
Some(TargetTriple::new(target)),
11311131
false,
11321132
);
1133-
toolchain.add_component(new_component)?;
1133+
let distributable = DistributableToolchain::new(&toolchain)?;
1134+
distributable.add_component(new_component)?;
11341135
}
11351136

11361137
Ok(())
@@ -1176,8 +1177,8 @@ fn component_add(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
11761177
for component in m.values_of("component").unwrap() {
11771178
let new_component = Component::new_with_target(component, false)
11781179
.unwrap_or_else(|| Component::new(component.to_string(), target.clone(), true));
1179-
1180-
toolchain.add_component(new_component)?;
1180+
let distributable = DistributableToolchain::new(&toolchain)?;
1181+
distributable.add_component(new_component)?;
11811182
}
11821183

11831184
Ok(())

src/toolchain.rs

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -659,69 +659,6 @@ impl<'a> Toolchain<'a> {
659659
None
660660
}
661661
}
662-
// Distributable only. Installed only.
663-
pub fn add_component(&self, mut component: Component) -> Result<()> {
664-
if !self.exists() {
665-
return Err(ErrorKind::ToolchainNotInstalled(self.name.to_owned()).into());
666-
}
667-
668-
let toolchain = &self.name;
669-
let toolchain = ToolchainDesc::from_str(toolchain)
670-
.chain_err(|| ErrorKind::ComponentsUnsupported(self.name.to_string()))?;
671-
672-
let prefix = InstallPrefix::from(self.path.to_owned());
673-
let manifestation = Manifestation::open(prefix, toolchain.target.clone())?;
674-
675-
if let Some(manifest) = manifestation.load_manifest()? {
676-
// Rename the component if necessary.
677-
if let Some(c) = manifest.rename_component(&component) {
678-
component = c;
679-
}
680-
681-
// Validate the component name
682-
let rust_pkg = manifest
683-
.packages
684-
.get("rust")
685-
.expect("manifest should contain a rust package");
686-
let targ_pkg = rust_pkg
687-
.targets
688-
.get(&toolchain.target)
689-
.expect("installed manifest should have a known target");
690-
691-
if !targ_pkg.components.contains(&component) {
692-
let wildcard_component = component.wildcard();
693-
if targ_pkg.components.contains(&wildcard_component) {
694-
component = wildcard_component;
695-
} else {
696-
return Err(ErrorKind::UnknownComponent(
697-
self.name.to_string(),
698-
component.description(&manifest),
699-
self.get_component_suggestion(&component, &manifest, false),
700-
)
701-
.into());
702-
}
703-
}
704-
705-
let changes = Changes {
706-
explicit_add_components: vec![component],
707-
remove_components: vec![],
708-
};
709-
710-
manifestation.update(
711-
&manifest,
712-
changes,
713-
false,
714-
&self.download_cfg(),
715-
&self.download_cfg().notify_handler,
716-
&toolchain.manifest_name(),
717-
false,
718-
)?;
719-
720-
Ok(())
721-
} else {
722-
Err(ErrorKind::ComponentsUnsupported(self.name.to_string()).into())
723-
}
724-
}
725662
// Distributable and Custom. Installed only.
726663
pub fn binary_file(&self, name: &str) -> PathBuf {
727664
let mut path = self.path.clone();
@@ -797,6 +734,71 @@ impl<'a> DistributableToolchain<'a> {
797734
}
798735
}
799736

737+
// Installed only.
738+
pub fn add_component(&self, mut component: Component) -> Result<()> {
739+
if !self.0.exists() {
740+
return Err(ErrorKind::ToolchainNotInstalled(self.0.name.to_owned()).into());
741+
}
742+
743+
let toolchain = &self.0.name;
744+
let toolchain = ToolchainDesc::from_str(toolchain)
745+
.chain_err(|| ErrorKind::ComponentsUnsupported(self.0.name.to_string()))?;
746+
747+
let prefix = InstallPrefix::from(self.0.path.to_owned());
748+
let manifestation = Manifestation::open(prefix, toolchain.target.clone())?;
749+
750+
if let Some(manifest) = manifestation.load_manifest()? {
751+
// Rename the component if necessary.
752+
if let Some(c) = manifest.rename_component(&component) {
753+
component = c;
754+
}
755+
756+
// Validate the component name
757+
let rust_pkg = manifest
758+
.packages
759+
.get("rust")
760+
.expect("manifest should contain a rust package");
761+
let targ_pkg = rust_pkg
762+
.targets
763+
.get(&toolchain.target)
764+
.expect("installed manifest should have a known target");
765+
766+
if !targ_pkg.components.contains(&component) {
767+
let wildcard_component = component.wildcard();
768+
if targ_pkg.components.contains(&wildcard_component) {
769+
component = wildcard_component;
770+
} else {
771+
return Err(ErrorKind::UnknownComponent(
772+
self.0.name.to_string(),
773+
component.description(&manifest),
774+
self.0
775+
.get_component_suggestion(&component, &manifest, false),
776+
)
777+
.into());
778+
}
779+
}
780+
781+
let changes = Changes {
782+
explicit_add_components: vec![component],
783+
remove_components: vec![],
784+
};
785+
786+
manifestation.update(
787+
&manifest,
788+
changes,
789+
false,
790+
&self.0.download_cfg(),
791+
&self.0.download_cfg().notify_handler,
792+
&toolchain.manifest_name(),
793+
false,
794+
)?;
795+
796+
Ok(())
797+
} else {
798+
Err(ErrorKind::ComponentsUnsupported(self.0.name.to_string()).into())
799+
}
800+
}
801+
800802
// Installed only.
801803
fn get_manifest(&self) -> Result<Option<Manifest>> {
802804
if !self.0.exists() {

0 commit comments

Comments
 (0)