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

Dromaz/avm use #2553

Merged
merged 5 commits into from
Jul 20, 2023
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 @@ -16,6 +16,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- client: Add a helper struct `DynSigner` to simplify use of `Client<C> where <C: Clone + Deref<Target = impl Signer>>` with Solana clap CLI utils that loads `Signer` as `Box<dyn Signer>` ([#2550](https://github.com/coral-xyz/anchor/pull/2550)).
- lang: Allow CPI calls matching an interface without pinning program ID ([#2559](https://github.com/coral-xyz/anchor/pull/2559)).
- cli, lang: Add IDL generation through compilation. `anchor build` still uses parsing method to generate IDLs, use `anchor idl build` to generate IDLs with the build method ([#2011](https://github.com/coral-xyz/anchor/pull/2011)).
- avm: Add support for the `.anchorversion` file to facilitate switching between different versions of the `anchor-cli` ([#2553](https://github.com/coral-xyz/anchor/pull/2553)).

### Fixes

Expand Down
41 changes: 38 additions & 3 deletions avm/src/lib.rs
acheroncrypto marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ pub fn version_binary_path(version: &Version) -> PathBuf {
}

/// Update the current version to a new version
pub fn use_version(version: &Version) -> Result<()> {
pub fn use_version(opt_version: Option<Version>) -> Result<()> {
let version = match opt_version {
Some(version) => version,
None => read_anchorversion_file()?,
};

let installed_versions = read_installed_versions();
// Make sure the requested version is installed
if !installed_versions.contains(version) {
if !installed_versions.contains(&version) {
if let Ok(current) = current_version() {
println!("Version {version} is not installed, staying on version {current}.");
} else {
Expand Down Expand Up @@ -118,7 +123,7 @@ pub fn install_version(version: &Version, force: bool) -> Result<()> {
current_version_file.write_all(version.to_string().as_bytes())?;
}

use_version(version)
use_version(Some(version.clone()))
}

/// Remove an installed version of anchor-cli
Expand All @@ -134,6 +139,14 @@ pub fn uninstall_version(version: &Version) -> Result<()> {
Ok(())
}

/// Read version from .anchorversion
pub fn read_anchorversion_file() -> Result<Version> {
fs::read_to_string(".anchorversion")
.map_err(|e| anyhow!(".anchorversion file not found: {e}"))
.map(|content| Version::parse(content.trim()))?
.map_err(|e| anyhow!("Unable to parse version: {e}"))
}

/// Ensure the users home directory is setup with the paths required by AVM.
pub fn ensure_paths() {
let home_dir = AVM_HOME.to_path_buf();
Expand Down Expand Up @@ -237,9 +250,31 @@ pub fn read_installed_versions() -> Vec<semver::Version> {
mod tests {
use crate::*;
use semver::Version;
use std::env;
use std::fs;
use std::io::Write;

#[test]
fn test_read_anchorversion() {
ensure_paths();
let mut dir = env::current_dir().unwrap();
dir.push(".anchorversion");
let mut file_created = fs::File::create(&dir).unwrap();
let test_version = "0.26.0";
file_created.write(test_version.as_bytes()).unwrap();

let version = read_anchorversion_file();
match version {
Ok(v) => {
assert_eq!(v.to_string(), test_version);
}
Err(_e) => {
assert!(false);
}
}
fs::remove_file(&dir).unwrap();
}

#[test]
fn test_ensure_paths() {
ensure_paths();
Expand Down
6 changes: 3 additions & 3 deletions avm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub struct Cli {
pub enum Commands {
#[clap(about = "Use a specific version of Anchor")]
Use {
#[clap(value_parser = parse_version)]
version: Version,
#[clap(value_parser = parse_version, required = false)]
version: Option<Version>,
},
#[clap(about = "Install a version of Anchor")]
Install {
Expand Down Expand Up @@ -48,7 +48,7 @@ fn parse_version(version: &str) -> Result<Version, Error> {
}
pub fn entry(opts: Cli) -> Result<()> {
match opts.command {
Commands::Use { version } => avm::use_version(&version),
Commands::Use { version } => avm::use_version(version),
Commands::Install { version, force } => avm::install_version(&version, force),
Commands::Uninstall { version } => avm::uninstall_version(&version),
Commands::List {} => avm::list_versions(),
Expand Down