Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 213 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ edition = "2021"

[features]
no-entrypoint = []
codama = ["dep:codama"]

[dependencies]
bytemuck = { version = "1.23.1", features = ["derive"] }
codama = { version = "0.6.4", optional = true }
num-derive = "0.4"
num-traits = "0.2"
solana-account-info = "3.0.0"
Expand All @@ -32,6 +34,11 @@ solana-system-interface = "2"
solana-transaction = "3.0.0"
solana-transaction-error = "3.0.0"

[build-dependencies]
codama = "0.6.4"
codama-korok-plugins = "0.6.4"
serde_json = "1.0"

[lib]
crate-type = ["cdylib", "lib"]

Expand Down
55 changes: 55 additions & 0 deletions program/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! Codama IDL build script.

use {
codama::Codama,
codama_korok_plugins::DefaultPlugin,
std::{env, fs, path::Path},
};

fn main() {
// Run the build script if the source files have changed, or if the
// developer provides the GENERATE_IDL environment variable.
//
// ```
// `GENERATE_IDL=1 cargo build`
// ```
//
// The environment variable approach is useful if the local Codama has been
// updated.
println!("cargo:rerun-if-changed=src/");
println!("cargo:rerun-if-env-changed=GENERATE_IDL");

if let Err(e) = generate_idl() {
println!("cargo:warning=Failed to generate IDL: {}", e);
}
}

fn generate_idl() -> Result<(), Box<dyn std::error::Error>> {
let manifest_dir = env::var("CARGO_MANIFEST_DIR")?;
let crate_path = Path::new(&manifest_dir);

let codama = Codama::load(crate_path)?
.without_default_plugin()
.add_plugin(DefaultPlugin); // Standard parsing

let idl_json = codama.get_json_idl()?;

// Parse and format the JSON with pretty printing.
let parsed: serde_json::Value = serde_json::from_str(&idl_json)?;
let mut formatted_json = serde_json::to_string_pretty(&parsed)?;

// Add newline at the end to match standard formatting.
formatted_json.push('\n');

// Define output directory
let out_dir = Path::new(&manifest_dir).join("idl");
fs::create_dir_all(&out_dir)?;

// Write to `spl_record.json`
let idl_path = out_dir.join("spl_record.json");
fs::write(&idl_path, formatted_json)?;

println!("cargo:warning=IDL written to: {}", idl_path.display());

Ok(())
}
Loading