Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend triple target to everything distributed by forc #667

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading