Bläddra i källkod

cli: Add version name from programs Cargo.toml to IDL (#1061)

Tom Linton 3 år sedan
förälder
incheckning
1c5f503a75
4 ändrade filer med 26 tillägg och 8 borttagningar
  1. 1 0
      CHANGELOG.md
  2. 18 5
      cli/src/config.rs
  3. 5 1
      cli/src/lib.rs
  4. 2 2
      lang/syn/src/idl/file.rs

+ 1 - 0
CHANGELOG.md

@@ -14,6 +14,7 @@ incremented for features.
 ### Fixes
 
 * lang: Add `deprecated` attribute to `ProgramAccount` ([#1014](https://github.com/project-serum/anchor/pull/1014)).
+* cli: Add version number from programs `Cargo.toml` into extracted IDL
 
 ### Features
 

+ 18 - 5
cli/src/config.rs

@@ -79,10 +79,21 @@ impl Manifest {
         }
     }
 
-    // Climbs each parent directory until we find a Cargo.toml.
+    pub fn version(&self) -> String {
+        match &self.package {
+            Some(package) => package.version.to_string(),
+            _ => "0.0.0".to_string(),
+        }
+    }
+
+    // Climbs each parent directory from the current dir until we find a Cargo.toml
     pub fn discover() -> Result<Option<WithPath<Manifest>>> {
-        let _cwd = std::env::current_dir()?;
-        let mut cwd_opt = Some(_cwd.as_path());
+        Manifest::discover_from_path(std::env::current_dir()?)
+    }
+
+    // Climbs each parent directory from a given starting directory until we find a Cargo.toml.
+    pub fn discover_from_path(start_from: PathBuf) -> Result<Option<WithPath<Manifest>>> {
+        let mut cwd_opt = Some(start_from.as_path());
 
         while let Some(cwd) = cwd_opt {
             for f in fs::read_dir(cwd)? {
@@ -145,8 +156,10 @@ impl WithPath<Config> {
     pub fn read_all_programs(&self) -> Result<Vec<Program>> {
         let mut r = vec![];
         for path in self.get_program_list()? {
-            let idl = anchor_syn::idl::file::parse(path.join("src/lib.rs"))?;
-            let lib_name = Manifest::from_path(&path.join("Cargo.toml"))?.lib_name()?;
+            let cargo = Manifest::from_path(&path.join("Cargo.toml"))?;
+            let lib_name = cargo.lib_name()?;
+            let version = cargo.version();
+            let idl = anchor_syn::idl::file::parse(path.join("src/lib.rs"), version)?;
             r.push(Program {
                 lib_name,
                 path,

+ 5 - 1
cli/src/lib.rs

@@ -1243,7 +1243,11 @@ fn fetch_idl(cfg_override: &ConfigOverride, idl_addr: Pubkey) -> Result<Idl> {
 
 fn extract_idl(file: &str) -> Result<Option<Idl>> {
     let file = shellexpand::tilde(file);
-    anchor_syn::idl::file::parse(&*file)
+    let manifest_from_path =
+        std::env::current_dir()?.join(PathBuf::from(&*file).parent().unwrap().to_path_buf());
+    let cargo = Manifest::discover_from_path(manifest_from_path)?
+        .ok_or_else(|| anyhow!("Cargo.toml not found"))?;
+    anchor_syn::idl::file::parse(&*file, cargo.version())
 }
 
 fn idl(cfg_override: &ConfigOverride, subcmd: IdlCommand) -> Result<()> {

+ 2 - 2
lang/syn/src/idl/file.rs

@@ -14,7 +14,7 @@ const DERIVE_NAME: &str = "Accounts";
 const ERROR_CODE_OFFSET: u32 = 300;
 
 // Parse an entire interface file.
-pub fn parse(filename: impl AsRef<Path>) -> Result<Option<Idl>> {
+pub fn parse(filename: impl AsRef<Path>, version: String) -> Result<Option<Idl>> {
     let ctx = CrateContext::parse(filename)?;
 
     let program_mod = match parse_program_mod(&ctx) {
@@ -224,7 +224,7 @@ pub fn parse(filename: impl AsRef<Path>) -> Result<Option<Idl>> {
     }
 
     Ok(Some(Idl {
-        version: "0.0.0".to_string(),
+        version,
         name: p.name.to_string(),
         state,
         instructions,