Ver código fonte

avm: Add support for the `.anchorversion` file (#2553)

Co-authored-by: acheron <acheroncrypto@gmail.com>
dromaz 2 anos atrás
pai
commit
10eb698912
3 arquivos alterados com 42 adições e 6 exclusões
  1. 1 0
      CHANGELOG.md
  2. 38 3
      avm/src/lib.rs
  3. 3 3
      avm/src/main.rs

+ 1 - 0
CHANGELOG.md

@@ -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
 

+ 38 - 3
avm/src/lib.rs

@@ -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 {
@@ -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
@@ -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();
@@ -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();

+ 3 - 3
avm/src/main.rs

@@ -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 {
@@ -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(),