Skip to content

Commit 08bf964

Browse files
committed
Move remove_component to DistributableToolchain
1 parent b63d211 commit 08bf964

File tree

2 files changed

+62
-57
lines changed

2 files changed

+62
-57
lines changed

src/cli/rustup_mode.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,8 +1145,9 @@ fn target_remove(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
11451145
Some(TargetTriple::new(target)),
11461146
false,
11471147
);
1148-
1149-
toolchain.remove_component(new_component)?;
1148+
let distributable = DistributableToolchain::new(&toolchain)
1149+
.chain_err(|| rustup::ErrorKind::ComponentsUnsupported(toolchain.name().to_string()))?;
1150+
distributable.remove_component(new_component)?;
11501151
}
11511152

11521153
Ok(())
@@ -1196,7 +1197,9 @@ fn component_remove(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
11961197
let new_component = Component::new_with_target(component, false)
11971198
.unwrap_or_else(|| Component::new(component.to_string(), target.clone(), true));
11981199

1199-
toolchain.remove_component(new_component)?;
1200+
let distributable = DistributableToolchain::new(&toolchain)
1201+
.chain_err(|| rustup::ErrorKind::ComponentsUnsupported(toolchain.name().to_string()))?;
1202+
distributable.remove_component(new_component)?;
12001203
}
12011204

12021205
Ok(())

src/toolchain.rs

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -722,60 +722,6 @@ impl<'a> Toolchain<'a> {
722722
Err(ErrorKind::ComponentsUnsupported(self.name.to_string()).into())
723723
}
724724
}
725-
// Distributable only. Installed only.
726-
pub fn remove_component(&self, mut component: Component) -> Result<()> {
727-
if !self.exists() {
728-
return Err(ErrorKind::ToolchainNotInstalled(self.name.to_owned()).into());
729-
}
730-
731-
let toolchain = &self.name;
732-
let toolchain = ToolchainDesc::from_str(toolchain)
733-
.chain_err(|| ErrorKind::ComponentsUnsupported(self.name.to_string()))?;
734-
735-
let prefix = InstallPrefix::from(self.path.to_owned());
736-
let manifestation = Manifestation::open(prefix, toolchain.target.clone())?;
737-
738-
if let Some(manifest) = manifestation.load_manifest()? {
739-
// Rename the component if necessary.
740-
if let Some(c) = manifest.rename_component(&component) {
741-
component = c;
742-
}
743-
744-
let dist_config = manifestation.read_config()?.unwrap();
745-
if !dist_config.components.contains(&component) {
746-
let wildcard_component = component.wildcard();
747-
if dist_config.components.contains(&wildcard_component) {
748-
component = wildcard_component;
749-
} else {
750-
return Err(ErrorKind::UnknownComponent(
751-
self.name.to_string(),
752-
component.description(&manifest),
753-
self.get_component_suggestion(&component, &manifest, true),
754-
)
755-
.into());
756-
}
757-
}
758-
759-
let changes = Changes {
760-
explicit_add_components: vec![],
761-
remove_components: vec![component],
762-
};
763-
764-
manifestation.update(
765-
&manifest,
766-
changes,
767-
false,
768-
&self.download_cfg(),
769-
&self.download_cfg().notify_handler,
770-
&toolchain.manifest_name(),
771-
false,
772-
)?;
773-
774-
Ok(())
775-
} else {
776-
Err(ErrorKind::ComponentsUnsupported(self.name.to_string()).into())
777-
}
778-
}
779725
// Distributable and Custom. Installed only.
780726
pub fn binary_file(&self, name: &str) -> PathBuf {
781727
let mut path = self.path.clone();
@@ -890,6 +836,62 @@ impl<'a> DistributableToolchain<'a> {
890836
))
891837
}
892838

839+
// Installed only.
840+
pub fn remove_component(&self, mut component: Component) -> Result<()> {
841+
// Overlapping code with get_manifest :/.
842+
if !self.0.exists() {
843+
return Err(ErrorKind::ToolchainNotInstalled(self.0.name.to_owned()).into());
844+
}
845+
846+
let toolchain = &self.0.name;
847+
let toolchain = ToolchainDesc::from_str(toolchain)
848+
.chain_err(|| ErrorKind::ComponentsUnsupported(self.0.name.to_string()))?;
849+
850+
let prefix = InstallPrefix::from(self.0.path.to_owned());
851+
let manifestation = Manifestation::open(prefix, toolchain.target.clone())?;
852+
853+
if let Some(manifest) = manifestation.load_manifest()? {
854+
// Rename the component if necessary.
855+
if let Some(c) = manifest.rename_component(&component) {
856+
component = c;
857+
}
858+
859+
let dist_config = manifestation.read_config()?.unwrap();
860+
if !dist_config.components.contains(&component) {
861+
let wildcard_component = component.wildcard();
862+
if dist_config.components.contains(&wildcard_component) {
863+
component = wildcard_component;
864+
} else {
865+
return Err(ErrorKind::UnknownComponent(
866+
self.0.name.to_string(),
867+
component.description(&manifest),
868+
self.0.get_component_suggestion(&component, &manifest, true),
869+
)
870+
.into());
871+
}
872+
}
873+
874+
let changes = Changes {
875+
explicit_add_components: vec![],
876+
remove_components: vec![component],
877+
};
878+
879+
manifestation.update(
880+
&manifest,
881+
changes,
882+
false,
883+
&self.0.download_cfg(),
884+
&self.0.download_cfg().notify_handler,
885+
&toolchain.manifest_name(),
886+
false,
887+
)?;
888+
889+
Ok(())
890+
} else {
891+
Err(ErrorKind::ComponentsUnsupported(self.0.name.to_string()).into())
892+
}
893+
}
894+
893895
// Installed only.
894896
pub fn show_version(&self) -> Result<Option<String>> {
895897
match self.get_manifest()? {

0 commit comments

Comments
 (0)