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

cli: set I/O stream to Stdio:null if the given solana_version is already installed #2757

Merged
merged 7 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- lang: Eliminate temporary Vec allocations when serializing data with discriminant and set the default capacity to 256 bytes ([#2691](https://github.com/coral-xyz/anchor/pull/2691)).
- lang: Allow custom lifetime in Accounts structure ([#2741](https://github.com/coral-xyz/anchor/pull/2741)).
- lang: Remove `try_to_vec` usage while setting the return data in order to reduce heap memory usage ([#2744](https://github.com/coral-xyz/anchor/pull/2744))
- cli: Show installation progress if Solana tools are not installed when using toolchain overrides ([#2757](https://github.com/coral-xyz/anchor/pull/2757)).

### Breaking

Expand Down
55 changes: 39 additions & 16 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,21 +495,28 @@ fn override_toolchain(cfg_override: &ConfigOverride) -> Result<RestoreToolchainC

let cfg = Config::discover(cfg_override)?;
if let Some(cfg) = cfg {
fn get_current_version(cmd_name: &str) -> Result<String> {
let output = std::process::Command::new(cmd_name)
.arg("--version")
.output()?;
let output_version = std::str::from_utf8(&output.stdout)?;
let version = Regex::new(r"(\d+\.\d+\.\S+)")
fn parse_version(text: &str) -> String {
Regex::new(r"(\d+\.\d+\.\S+)")
.unwrap()
.captures_iter(output_version)
.captures_iter(text)
.next()
.unwrap()
.get(0)
.unwrap()
.as_str()
.to_string();
.to_string()
}

fn get_current_version(cmd_name: &str) -> Result<String> {
let output = std::process::Command::new(cmd_name)
.arg("--version")
.output()?;
if !output.status.success() {
return Err(anyhow!("Failed to run `{cmd_name} --version`"));
}

let output_version = std::str::from_utf8(&output.stdout)?;
let version = parse_version(output_version);
Ok(version)
}

Expand All @@ -519,15 +526,33 @@ fn override_toolchain(cfg_override: &ConfigOverride) -> Result<RestoreToolchainC
// We are overriding with `solana-install` command instead of using the binaries
// from `~/.local/share/solana/install/releases` because we use multiple Solana
// binaries in various commands.
fn override_solana_version(version: String) -> std::io::Result<bool> {
fn override_solana_version(version: String) -> Result<bool> {
let output = std::process::Command::new("solana-install")
.arg("list")
.output()?;
if !output.status.success() {
return Err(anyhow!("Failed to list installed `solana` versions"));
}

// Hide the installation progress if the version is already installed
let is_installed = std::str::from_utf8(&output.stdout)?
.lines()
.any(|line| parse_version(line) == version);
let (stderr, stdout) = if is_installed {
(Stdio::null(), Stdio::null())
} else {
(Stdio::inherit(), Stdio::inherit())
};

std::process::Command::new("solana-install")
.arg("init")
.arg(&version)
.stderr(Stdio::null())
.stdout(Stdio::null())
.stderr(stderr)
.stdout(stdout)
.spawn()?
.wait()
.map(|status| status.success())
.map_err(|err| anyhow!("Failed to run `solana-install` command: {err}"))
}

match override_solana_version(solana_version.to_owned())? {
Expand All @@ -537,12 +562,10 @@ fn override_toolchain(cfg_override: &ConfigOverride) -> Result<RestoreToolchainC
false => Err(anyhow!("Failed to restore `solana` version")),
}
})),
false => {
eprintln!(
"Failed to override `solana` version to {solana_version}, \
false => eprintln!(
"Failed to override `solana` version to {solana_version}, \
using {current_version} instead"
);
}
),
}
}
}
Expand Down
Loading