Skip to content

Commit

Permalink
fix: update component remove (#211)
Browse files Browse the repository at this point in the history
* add published as a key to components.toml

* support removing component executables that don't share the name with the component

* use expect over unwrap

* fix flow within 'component add' when checking if component exist

* clippy
  • Loading branch information
eightfilms authored Sep 27, 2022
1 parent fad26bf commit cbfd87e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 23 deletions.
25 changes: 25 additions & 0 deletions component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct Component {
pub executables: Vec<String>,
pub repository_url: String,
pub targets: Vec<String>,
pub publish: Option<bool>,
}

#[derive(Debug)]
Expand All @@ -44,6 +45,30 @@ impl Components {
Ok(components)
}

pub fn collect() -> Result<Components> {
let components = Self::from_toml(COMPONENTS_TOML)?;
Ok(components)
}

pub fn collect_publishables() -> Result<Vec<Component>> {
let components = Self::from_toml(COMPONENTS_TOML)?;

let mut publishables: Vec<Component> = components
.component
.keys()
.map(|c| {
components
.component
.get(c)
.expect("Failed to parse components.toml")
})
.filter_map(|c| c.publish.and_then(|_| Some(c.clone())))
.collect();

publishables.sort_by_key(|c| c.name.clone());
Ok(publishables)
}

pub fn collect_exclude_plugins() -> Result<Vec<Component>> {
let components = Self::from_toml(COMPONENTS_TOML)?;

Expand Down
5 changes: 4 additions & 1 deletion components.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[component.forc]
name = "forc"
tarball_prefix = "forc-binaries"
executables = ["forc"]
executables = ["forc", "forc-fmt", "forc-lsp", "forc-explore"]
repository_url = "https://github.com/FuelLabs/sway"
targets = ["linux_amd64", "linux_arm64", "darwin_amd64", "darwin_arm64"]
publish = true

[component.forc-fmt]
name = "forc-fmt"
Expand Down Expand Up @@ -36,10 +37,12 @@ is_plugin = true
executables = ["forc-deploy", "forc-run"]
repository_url = "https://github.com/FuelLabs/sway"
targets = ["linux_amd64", "linux_arm64", "darwin_amd64", "darwin_arm64"]
publish = true

[component.fuel-core]
name = "fuel-core"
tarball_prefix = "fuel-core"
executables = ["fuel-core"]
repository_url = "https://github.com/FuelLabs/fuel-core"
targets = [ "aarch64-unknown-linux-gnu", "x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "x86_64-apple-darwin" ]
publish = true
14 changes: 7 additions & 7 deletions src/ops/fuelup_component/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ You may create a custom toolchain using 'fuelup toolchain new <toolchain>'.",
)
};

if toolchain.has_component(&maybe_versioned_component) {
info!(
"{} already exists in toolchain '{}'; replacing existing version with `latest` version",
&maybe_versioned_component, toolchain.name
);
}

let (component, version): (&str, Option<Version>) =
match maybe_versioned_component.split_once('@') {
Some(t) => {
Expand All @@ -48,6 +41,13 @@ You may create a custom toolchain using 'fuelup toolchain new <toolchain>'.",
None => (&maybe_versioned_component, None),
};

if toolchain.has_component(component) {
info!(
"{} already exists in toolchain '{}'; replacing existing version with `latest` version",
&maybe_versioned_component, toolchain.name
);
}

let download_cfg =
DownloadCfg::new(component, TargetTriple::from_component(component)?, version)?;
toolchain.add_component(download_cfg)?;
Expand Down
53 changes: 38 additions & 15 deletions src/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,22 @@ impl Toolchain {
}

pub fn has_component(&self, component: &str) -> bool {
self.bin_path.join(component).exists()
let executables = &Components::collect()
.expect("Failed to collect components")
.component[component]
.executables;

executables.iter().all(|e| self.bin_path.join(e).is_file())
}

fn can_remove(&self, component: &str) -> bool {
// Published components are the ones downloadable, and hence removable.
Components::collect_publishables()
.expect("Failed to collect publishable components")
.iter()
.map(|c| c.name.clone())
.collect::<String>()
.contains(component)
}

pub fn add_component(&self, download_cfg: DownloadCfg) -> Result<DownloadCfg> {
Expand Down Expand Up @@ -237,23 +252,31 @@ impl Toolchain {
Ok(download_cfg)
}

fn remove_executables(&self, component: &str) -> Result<()> {
let executables = &Components::collect().unwrap().component[component].executables;
for executable in executables {
remove_file(self.bin_path.join(executable))
.with_context(|| format!("failed to remove executable '{}'", executable))?;
}
Ok(())
}

pub fn remove_component(&self, component: &str) -> Result<()> {
if self.has_component(component) {
info!("Removing '{}' from toolchain '{}'", component, self.name);
let component_path = self.bin_path.join(component);
remove_file(component_path)
.with_context(|| format!("failed to remove component '{}'", component))?;
// If component to remove is 'forc', silently remove forc plugins
if component == component::FORC {
for plugin in Components::collect_plugin_executables()? {
let plugin_path = self.bin_path.join(plugin);
remove_file(plugin_path)
.with_context(|| format!("failed to remove component '{}'", component))?;
}
if self.can_remove(component) {
if self.has_component(component) {
info!("Removing '{}' from toolchain '{}'", component, self.name);
match self.remove_executables(component) {
Ok(_) => info!("'{}' removed from toolchain '{}'", component, self.name),
Err(e) => error!(
"Failed to remove '{}' from toolchain '{}': {}",
component, self.name, e
),
};
} else {
info!("'{}' not found in toolchain '{}'", component, self.name);
}
info!("'{}' removed from toolchain '{}'", component, self.name);
} else {
info!("'{}' not found in toolchain '{}'", component, self.name);
info!("'{}' is not a removable component", component);
}

Ok(())
Expand Down

0 comments on commit cbfd87e

Please sign in to comment.