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

This commit is contained in:
Tom Linton 2021-11-28 06:06:01 +13:00 committed by GitHub
parent a4002dfb95
commit 1c5f503a75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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