Skip to content

Commit

Permalink
avm improvements (#1670)
Browse files Browse the repository at this point in the history
  • Loading branch information
italoacasas committed Mar 23, 2022
1 parent 4e4ca55 commit e999511
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
14 changes: 11 additions & 3 deletions CHANGELOG.md
Expand Up @@ -6,13 +6,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

**Note:** Version 0 of Semantic Versioning is handled differently from version 1 and above.
The minor version will be incremented upon a breaking change and the patch version will be
incremented for features.
The minor version will be incremented upon a breaking change and the patch version will be incremented for features.

## [Unreleased]

### Features

* avm: New `avm update` command to update the Anchor CLI to the latest version ([#1670](https://github.com/project-serum/anchor/pull/1670)).

### Fixes

* avm: `avm install` no longer downloads the version if already installed in the machine ([#1670](https://github.com/project-serum/anchor/pull/1670)).

### Breaking

* avm: `amv install` switches to the newly installed version after installation finishes ([#1670](https://github.com/project-serum/anchor/pull/1670)).
* spl: Re-export the `spl_token` crate ([#1665](https://github.com/project-serum/anchor/pull/1665)).

## [0.23.0] - 2022-03-20
Expand Down Expand Up @@ -567,4 +575,4 @@ Initial release.
* spl: `anchor-spl` crate providing CPI clients for Anchor programs.
* client: `anchor-client` crate providing Rust clients for Anchor programs.
* ts: `@project-serum/anchor` package for generating TypeScript clients.
* cli: Command line interface for managing Anchor programs.
* cli: Command line interface for managing Anchor programs.
20 changes: 18 additions & 2 deletions avm/src/lib.rs
Expand Up @@ -70,8 +70,23 @@ pub fn use_version(version: &Version) -> Result<()> {
Ok(())
}

/// Update to the latest version
pub fn update() -> Result<()> {
// Find last stable version
let version = &get_latest_version();

install_version(version, false)
}

/// Install a version of anchor-cli
pub fn install_version(version: &Version) -> Result<()> {
pub fn install_version(version: &Version, force: bool) -> Result<()> {
// If version is already installed we ignore the request.
let installed_versions = read_installed_versions();
if installed_versions.contains(version) && !force {
println!("Version {} is already installed", version);
return Ok(());
}

let exit = std::process::Command::new("cargo")
.args(&[
"install",
Expand Down Expand Up @@ -105,7 +120,8 @@ pub fn install_version(version: &Version) -> Result<()> {
let mut current_version_file = fs::File::create(current_version_file_path().as_path())?;
current_version_file.write_all(version.to_string().as_bytes())?;
}
Ok(())

use_version(version)
}

/// Remove an installed version of anchor-cli
Expand Down
9 changes: 8 additions & 1 deletion avm/src/main.rs
Expand Up @@ -22,6 +22,10 @@ pub enum Commands {
Install {
#[clap(parse(try_from_str = parse_version))]
version: Version,
#[clap(long)]
/// Flag to force installation even if the version
/// is already installed
force: bool,
},
#[clap(about = "Uninstall a version of Anchor")]
Uninstall {
Expand All @@ -30,6 +34,8 @@ pub enum Commands {
},
#[clap(about = "List available versions of Anchor")]
List {},
#[clap(about = "Update to the latest Anchor version")]
Update {},
}

// If `latest` is passed use the latest available version.
Expand All @@ -43,9 +49,10 @@ 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::Install { version } => avm::install_version(&version),
Commands::Install { version, force } => avm::install_version(&version, force),
Commands::Uninstall { version } => avm::uninstall_version(&version),
Commands::List {} => avm::list_versions(),
Commands::Update {} => avm::update(),
}
}

Expand Down

0 comments on commit e999511

Please sign in to comment.