Skip to content

Commit

Permalink
Extend triple target to everything distributed by forc (#667)
Browse files Browse the repository at this point in the history
There are 2 types of downloads:

1. forc-binaries, which is of the format "{os}_{arch}"
2. Everything else, which is the standard "{arch}-{vendor}-{os}"

Currently, we only use the "{os}_{arch}" URL if the component is forc,
however the forc-binaries tarball contains plugins and executables not
defined in the forc component.

This PR extends the "{os}_{arch}" result to everything distributed from
forc.
  • Loading branch information
alfiedotwtf authored Oct 29, 2024
1 parent 1b801e6 commit 003b485
Showing 1 changed file with 54 additions and 38 deletions.
92 changes: 54 additions & 38 deletions src/target_triple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{bail, Result};
use component::{self, Component};
use component::{self, Components};
use std::fmt;

#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
Expand Down Expand Up @@ -52,41 +52,59 @@ impl TargetTriple {
Ok(Self(target_triple))
}

pub fn from_component(component: &str) -> Result<Self> {
match Component::from_name(component).map(|c| c.name)?.as_str() {
component::FORC => {
let os = match std::env::consts::OS {
"macos" => "darwin",
"linux" => "linux",
unsupported_os => bail!("Unsupported os: {}", unsupported_os),
};
let architecture = match std::env::consts::ARCH {
"aarch64" => "arm64",
"x86_64" => "amd64",
unsupported_arch => bail!("Unsupported architecture: {}", unsupported_arch),
};

Ok(Self(format!("{os}_{architecture}")))
}
_ => {
let architecture = match std::env::consts::ARCH {
"aarch64" | "x86_64" => std::env::consts::ARCH,
unsupported_arch => bail!("Unsupported architecture: {}", unsupported_arch),
};

let vendor = match std::env::consts::OS {
"macos" => "apple",
_ => "unknown",
};

let os = match std::env::consts::OS {
"macos" => "darwin",
"linux" => "linux-gnu",
unsupported_os => bail!("Unsupported os: {}", unsupported_os),
};

Ok(Self(format!("{architecture}-{vendor}-{os}")))
}
/// Returns a target triple from the supplied `Component` name, plugin, or executable
///
/// Target triples come in two forms:
///
/// 1. Components distributed by `forc` have the value "[darwin|linux]_[arm64|amd64]"
/// 2. All other components have the value "[aarch64|x86_64]-[apple|unknown]-[darwin|linux-gnu]"
///
/// # Arguments
///
/// * `name` - The name of the component, plugin, or executable.
///
/// # Examples
///
/// ```rust
/// use component::Component;
/// use fuelup::target_triple::TargetTriple;
///
/// let component = Component::from_name("forc").unwrap();
/// let target_triple = TargetTriple::from_component(&component.name);
/// println!("Target triple for 'forc' is: {}", target_triple.unwrap());
/// ```
pub fn from_component(name: &str) -> Result<Self> {
if Components::is_distributed_by_forc(name) {
let os = match std::env::consts::OS {
"macos" => "darwin",
"linux" => "linux",
unsupported_os => bail!("Unsupported os: {}", unsupported_os),
};
let architecture = match std::env::consts::ARCH {
"aarch64" => "arm64",
"x86_64" => "amd64",
unsupported_arch => bail!("Unsupported architecture: {}", unsupported_arch),
};

Ok(Self(format!("{os}_{architecture}")))
} else {
let architecture = match std::env::consts::ARCH {
"aarch64" | "x86_64" => std::env::consts::ARCH,
unsupported_arch => bail!("Unsupported architecture: {}", unsupported_arch),
};

let vendor = match std::env::consts::OS {
"macos" => "apple",
_ => "unknown",
};

let os = match std::env::consts::OS {
"macos" => "darwin",
"linux" => "linux-gnu",
unsupported_os => bail!("Unsupported os: {}", unsupported_os),
};

Ok(Self(format!("{architecture}-{vendor}-{os}")))
}
}
}
Expand Down Expand Up @@ -114,7 +132,6 @@ mod test_from_component {
}

#[test]
#[should_panic] // TODO: #654 will fix this
fn plugins() {
for plugin in Components::collect_plugins().unwrap() {
let component = Component::from_name(&plugin.name).unwrap();
Expand All @@ -124,7 +141,6 @@ mod test_from_component {
}

#[test]
#[should_panic] // TODO: #654 will fix this
fn executables() {
for executable in Components::collect_plugin_executables().unwrap() {
let components = Components::collect().unwrap();
Expand Down

0 comments on commit 003b485

Please sign in to comment.