From 1c5f503a75e9c962e5dad08d7d67c2de7da3d56c Mon Sep 17 00:00:00 2001 From: Tom Linton Date: Sun, 28 Nov 2021 06:06:01 +1300 Subject: [PATCH] cli: Add version name from programs Cargo.toml to IDL (#1061) --- CHANGELOG.md | 1 + cli/src/config.rs | 23 ++++++++++++++++++----- cli/src/lib.rs | 6 +++++- lang/syn/src/idl/file.rs | 4 ++-- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8717ed47..eb4a6556 100644 --- a/CHANGELOG.md +++ b/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 diff --git a/cli/src/config.rs b/cli/src/config.rs index 84eada6a..86c69c6b 100644 --- a/cli/src/config.rs +++ b/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>> { - 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>> { + 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 { pub fn read_all_programs(&self) -> Result> { 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, diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 0b1ea0e0..59007be3 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1243,7 +1243,11 @@ fn fetch_idl(cfg_override: &ConfigOverride, idl_addr: Pubkey) -> Result { fn extract_idl(file: &str) -> Result> { 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<()> { diff --git a/lang/syn/src/idl/file.rs b/lang/syn/src/idl/file.rs index 2ec66a91..88d27228 100644 --- a/lang/syn/src/idl/file.rs +++ b/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) -> Result> { +pub fn parse(filename: impl AsRef, version: String) -> Result> { let ctx = CrateContext::parse(filename)?; let program_mod = match parse_program_mod(&ctx) { @@ -224,7 +224,7 @@ pub fn parse(filename: impl AsRef) -> Result> { } Ok(Some(Idl { - version: "0.0.0".to_string(), + version, name: p.name.to_string(), state, instructions,