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

feat: support toolchain export #526

Open
wants to merge 59 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
5d36096
feat: support toolchain export
Halimao Nov 23, 2023
e28f87b
add exported success print
Halimao Nov 23, 2023
0ec6b72
chore: code fmt
Halimao Dec 4, 2023
7d2a2bc
Merge branch 'FuelLabs:master' into feat/export-toolchain
Halimao Dec 4, 2023
8d0b943
Merge branch 'feat/export-toolchain' of github.com:Halimao/fuelup int…
Halimao Dec 4, 2023
be6ccdb
refactor: add some suggestion change
Halimao Dec 4, 2023
bf083af
chore: code fmt
Halimao Dec 4, 2023
374fc8d
refactor: check channel validation
Halimao Dec 5, 2023
323a41e
add tests
Halimao Dec 5, 2023
1ebd698
valid channel by Channel::from_str to
Halimao Dec 5, 2023
16a7bb1
fmt code
Halimao Dec 5, 2023
3ebf111
Update src/commands/toolchain.rs
Halimao Dec 9, 2023
d37b2f8
Update src/commands/toolchain.rs
Halimao Dec 9, 2023
022f904
refactor: move valid channel to a const
Halimao Dec 9, 2023
b915d8e
add --channel option
Halimao Dec 9, 2023
735e821
fix: fix --channel option register
Halimao Dec 9, 2023
595f8a0
Update src/ops/fuelup_toolchain/export.rs
Halimao Dec 13, 2023
95c3829
Update src/ops/fuelup_toolchain/export.rs
Halimao Dec 13, 2023
a9e684e
Update src/ops/fuelup_toolchain/export.rs
Halimao Dec 13, 2023
7cca4d0
Update src/ops/fuelup_toolchain/export.rs
Halimao Dec 13, 2023
f54e4f0
Update src/ops/fuelup_toolchain/export.rs
Halimao Dec 13, 2023
4365095
Update src/ops/fuelup_toolchain/export.rs
Halimao Dec 13, 2023
359cb0c
update for suggestion changes
Halimao Dec 13, 2023
17c16a6
Merge branch 'master' into feat/export-toolchain
Halimao Dec 13, 2023
4ef6def
sort dependencies
Halimao Dec 13, 2023
fef14f8
fix: ci test
Halimao Dec 13, 2023
08c59c5
fix: ci test
Halimao Dec 13, 2023
92d8f37
fix ci test
Halimao Dec 13, 2023
801f313
Merge branch 'feat/export-toolchain' of github.com:Halimao/fuelup int…
Halimao Dec 13, 2023
f218a3d
removing the unwraps and bail with a message to user
Halimao Dec 19, 2023
45641c6
refactor: using bail! instead of panic!
Halimao Dec 19, 2023
fdcd0a7
Merge branch 'master' into feat/export-toolchain
Halimao Dec 19, 2023
ed60651
using BufReader::new(stdin()) instead of stdin().lock()
Halimao Dec 20, 2023
8ad7550
rename s to channel
Halimao Dec 20, 2023
52bc84b
Merge branch 'master' into feat/export-toolchain
Halimao Jan 3, 2024
9c90e2e
Update src/ops/fuelup_toolchain/export.rs
Halimao Jan 10, 2024
7b05958
Update src/ops/fuelup_toolchain/export.rs
Halimao Jan 10, 2024
78a9915
Update src/ops/fuelup_toolchain/export.rs
Halimao Jan 10, 2024
fe6d1b1
Update src/ops/fuelup_toolchain/export.rs
Halimao Jan 10, 2024
99e6f52
Update src/ops/fuelup_toolchain/export.rs
Halimao Jan 10, 2024
aad52fa
Update src/ops/fuelup_toolchain/export.rs
Halimao Jan 10, 2024
a83e6f7
Update src/ops/fuelup_toolchain/export.rs
Halimao Jan 10, 2024
e14be95
Update src/ops/fuelup_toolchain/export.rs
Halimao Jan 10, 2024
89d5183
address suggestion changes
Halimao Jan 10, 2024
3d08cc6
fix: fix ci test fail
Halimao Jan 10, 2024
0df6763
refactor: rename test func name
Halimao Jan 10, 2024
324da7c
addressed part of suggestion changes
Halimao Jan 11, 2024
931588f
move the message asking for input onto a new line
Halimao Jan 11, 2024
0b056b8
Merge branch 'master' into feat/export-toolchain
Halimao Jan 13, 2024
f67390b
update cargo.lock
Halimao Jan 13, 2024
452df0a
refactor: clearer failures in tests
Halimao Jan 19, 2024
0ae91e4
refactor: sort and split tests
Halimao Jan 19, 2024
cf92052
using `asert_eq!` instead of `matches!`
Halimao Jan 20, 2024
11fc203
Merge branch 'master' into feat/export-toolchain
sdankel Jan 29, 2024
002c787
Fix toml formatting and tests
sdankel Jan 30, 2024
7a3e4fd
Merge branch 'master' into feat/export-toolchain
Halimao Feb 9, 2024
12811c8
refactor: using `DistToolchainDescription::from_str` to parse out cha…
Halimao Feb 9, 2024
d94ac60
Merge branch 'master' into feat/export-toolchain
Halimao Mar 6, 2024
0441023
Merge branch 'master' into feat/export-toolchain
sdankel Mar 19, 2024
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
13 changes: 13 additions & 0 deletions src/commands/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::ops::fuelup_toolchain::install::install;
use crate::ops::fuelup_toolchain::list_revisions::list_revisions;
use crate::ops::fuelup_toolchain::new::new;
use crate::ops::fuelup_toolchain::uninstall::uninstall;
use crate::ops::fuelup_toolchain::export::export;
use crate::target_triple::TargetTriple;
use crate::toolchain::RESERVED_TOOLCHAIN_NAMES;

Expand All @@ -18,6 +19,8 @@ pub enum ToolchainCommand {
Uninstall(UninstallCommand),
/// Fetch the list of published `latest` toolchains, starting from the most recent
ListRevisions(ListRevisionsCommand),
/// Export the toolchain info into fuel-toolchain.toml
Export(ExportCommand),
}

#[derive(Debug, Parser)]
Expand All @@ -42,6 +45,15 @@ pub struct UninstallCommand {
#[derive(Debug, Parser)]
pub struct ListRevisionsCommand {}

#[derive(Debug, Parser)]
pub struct ExportCommand {
/// Toolchain to export, your currently active toolchain will be exported if name isn't specified
pub name: Option<String>,
/// Set true to disregard the existing TOML output file
Halimao marked this conversation as resolved.
Show resolved Hide resolved
#[arg(short, long)]
pub force: bool,
}

fn name_allowed(s: &str) -> Result<String> {
let name = match s.split_once('-') {
Some((prefix, target_triple)) => {
Expand Down Expand Up @@ -70,6 +82,7 @@ pub fn exec(command: ToolchainCommand) -> Result<()> {
ToolchainCommand::New(command) => new(command)?,
ToolchainCommand::Uninstall(command) => uninstall(command)?,
ToolchainCommand::ListRevisions(command) => list_revisions(command)?,
ToolchainCommand::Export(command) => export(command)?,
};

Ok(())
Expand Down
98 changes: 98 additions & 0 deletions src/ops/fuelup_toolchain/export.rs
JoshuaBatty marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use crate::{
commands::toolchain::ExportCommand,
toolchain::Toolchain,
constants::FUEL_TOOLCHAIN_TOML_FILE,
toolchain_override::{self, OverrideCfg, ToolchainCfg, ToolchainOverride},
};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use semver::Version;
use anyhow::{bail, Result};
use component::{self, Components};
use tracing::info;

pub fn export(command: ExportCommand) -> Result<()> {
let ExportCommand { name, force} = command;
let path = PathBuf::from("./").join(FUEL_TOOLCHAIN_TOML_FILE);
Halimao marked this conversation as resolved.
Show resolved Hide resolved
if !force && path.exists() {
bail!("{FUEL_TOOLCHAIN_TOML_FILE} already exists");
Halimao marked this conversation as resolved.
Show resolved Hide resolved
}
let mut toolchain_name = Toolchain::from_settings()?.name;
if let Some(name) = name {
toolchain_name = name;
}
Halimao marked this conversation as resolved.
Show resolved Hide resolved
let export_toolchain = Toolchain::from_path(&toolchain_name);

let mut version_map: HashMap<String, Version> = HashMap::new();
for component in Components::collect_exclude_plugins()? {
let component_executable = export_toolchain.bin_path.join(&component.name);
if let Ok(version) = get_exec_version(component_executable.as_path()) {
version_map.insert(component.name.clone(), version);
};

if component.name == component::FORC {
let forc_executables = component.executables;
for plugin in Components::collect_plugins()? {
if !forc_executables.contains(&plugin.name) {
if !plugin.is_main_executable() {
for executable in plugin.executables.iter() {
let plugin_executable = export_toolchain.bin_path.join(executable);
if let Ok(version) = get_exec_version(plugin_executable.as_path()) {
version_map.insert(executable.clone(), version);
};
}
} else {
let plugin_executable = export_toolchain.bin_path.join(&plugin.name);
if let Ok(version) = get_exec_version(plugin_executable.as_path()) {
version_map.insert(plugin.name.clone(), version);
};
}
}
}
}
}

let toolchain_override = ToolchainOverride {
cfg: OverrideCfg::new(
ToolchainCfg {
channel: toolchain_override::Channel::from_str(&toolchain_name).unwrap(),
Halimao marked this conversation as resolved.
Show resolved Hide resolved
},
Some(version_map),
),
path,
};
let document = toolchain_override.to_toml();
std::fs::write(toolchain_override.path, document.to_string())
.unwrap_or_else(|_| panic!("failed to write {FUEL_TOOLCHAIN_TOML_FILE}"));

info!("exported '{toolchain_name}' into '{FUEL_TOOLCHAIN_TOML_FILE}'");
Ok(())
}

fn get_exec_version(component_executable: &Path) -> Result<Version> {
Halimao marked this conversation as resolved.
Show resolved Hide resolved
match std::process::Command::new(component_executable)
.arg("--version")
.output()
{
Ok(o) => {
let output = String::from_utf8_lossy(&o.stdout).into_owned();
match output.split_whitespace().last() {
Some(v) => {
let version = Version::parse(v)?;
return Ok(version);
}
None => {
bail!("error getting version string");
}
};
}
Err(e) => {
if component_executable.exists() {
bail!("execute '{} --version' error - {}", component_executable.display(), e);
}
}
}

bail!("could not show version: {}", component_executable.display());
}
Halimao marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions src/ops/fuelup_toolchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod install;
pub mod list_revisions;
pub mod new;
pub mod uninstall;
pub mod export;