forked from coral-xyz/anchor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IDL: Use cargo-expand to expand macros in code (#23)
cargo-expand is already used for the `anchor expand` subcommand. In contrast to using `cargo +nightly rustc --profile=check -- -Zunpretty=expanded`, it works without problems in all of our Anchor programs. The code for using cargo-expand used to be a part of anchor-cli crate, but since we had to use it in anchor-syn, this change moves it to a separate crate.
- Loading branch information
1 parent
e317087
commit 89d86a6
Showing
9 changed files
with
134 additions
and
81 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ members = [ | |
"lang", | ||
"lang/attribute/*", | ||
"lang/derive/*", | ||
"lang/expand", | ||
"lang/syn", | ||
"spl", | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "light-anchor-expand" | ||
version = "0.28.0" | ||
authors = ["Anchor Maintainers <[email protected]>"] | ||
repository = "https://github.com/coral-xyz/anchor" | ||
license = "Apache-2.0" | ||
description = "Utility library for calling cargo-expand" | ||
rust-version = "1.60" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
anyhow = "1" | ||
cargo_toml = "0.13.0" | ||
chrono = "0.4.19" | ||
heck = "0.4.0" | ||
serde_json = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::{ffi::OsString, fs, path::PathBuf, process::Stdio}; | ||
|
||
use anyhow::Result; | ||
|
||
pub fn expand_program( | ||
root: PathBuf, | ||
package_name: &str, | ||
version: &str, | ||
expansions_path: Option<PathBuf>, | ||
cargo_args: &[String], | ||
) -> Result<Vec<u8>> { | ||
let target_dir_arg = match expansions_path { | ||
Some(ref expansions_path) => { | ||
let mut target_dir_arg = OsString::from("--target-dir="); | ||
target_dir_arg.push(expansions_path.join("expand-target")); | ||
Some(target_dir_arg) | ||
} | ||
None => None, | ||
}; | ||
|
||
let mut cmd = std::process::Command::new("cargo"); | ||
let cmd = cmd.arg("expand"); | ||
if let Some(target_dir_arg) = target_dir_arg { | ||
cmd.arg(target_dir_arg); | ||
} | ||
let exit = cmd | ||
.current_dir(root) | ||
.arg(&format!("--package={package_name}")) | ||
.args(cargo_args) | ||
.stderr(Stdio::inherit()) | ||
.output() | ||
.map_err(|e| anyhow::format_err!("{}", e.to_string()))?; | ||
if !exit.status.success() { | ||
eprintln!("'cargo expand' failed. Perhaps you have not installed 'cargo-expand'? https://github.com/dtolnay/cargo-expand#installation"); | ||
std::process::exit(exit.status.code().unwrap_or(1)); | ||
} | ||
|
||
if let Some(ref expansions_path) = expansions_path { | ||
let program_expansions_path = expansions_path.join(package_name); | ||
fs::create_dir_all(&program_expansions_path)?; | ||
|
||
// let version = cargo.version(); | ||
let time = chrono::Utc::now().to_string().replace(' ', "_"); | ||
let file_path = program_expansions_path.join(format!("{package_name}-{version}-{time}.rs")); | ||
fs::write(&file_path, &exit.stdout) | ||
.map_err(|e| anyhow::format_err!("{}", e.to_string()))?; | ||
|
||
println!( | ||
"Expanded {} into file {}\n", | ||
package_name, | ||
file_path.to_string_lossy() | ||
); | ||
} | ||
|
||
Ok(exit.stdout) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters