|
@@ -520,19 +520,18 @@ pub fn build(
|
|
|
}
|
|
|
|
|
|
let cfg = Config::discover(cfg_override)?.expect("Not in workspace.");
|
|
|
+ let cfg_parent = cfg.path().parent().expect("Invalid Anchor.toml");
|
|
|
+
|
|
|
let cargo = Manifest::discover()?;
|
|
|
|
|
|
+ fs::create_dir_all(cfg_parent.join("target/idl"))?;
|
|
|
+ fs::create_dir_all(cfg_parent.join("target/types"))?;
|
|
|
+
|
|
|
let idl_out = match idl {
|
|
|
Some(idl) => Some(PathBuf::from(idl)),
|
|
|
- None => {
|
|
|
- let cfg_parent = match cfg.path().parent() {
|
|
|
- None => return Err(anyhow!("Invalid Anchor.toml")),
|
|
|
- Some(parent) => parent,
|
|
|
- };
|
|
|
- fs::create_dir_all(cfg_parent.join("target/idl"))?;
|
|
|
- Some(cfg_parent.join("target/idl"))
|
|
|
- }
|
|
|
+ None => Some(cfg_parent.join("target/idl")),
|
|
|
};
|
|
|
+ let idl_ts_out = Some(cfg_parent.join("target/types"));
|
|
|
|
|
|
let solana_version = match solana_version.is_some() {
|
|
|
true => solana_version,
|
|
@@ -545,6 +544,7 @@ pub fn build(
|
|
|
&cfg,
|
|
|
cfg.path(),
|
|
|
idl_out,
|
|
|
+ idl_ts_out,
|
|
|
verifiable,
|
|
|
solana_version,
|
|
|
stdout,
|
|
@@ -556,6 +556,7 @@ pub fn build(
|
|
|
&cfg,
|
|
|
cfg.path(),
|
|
|
idl_out,
|
|
|
+ idl_ts_out,
|
|
|
verifiable,
|
|
|
solana_version,
|
|
|
stdout,
|
|
@@ -567,6 +568,7 @@ pub fn build(
|
|
|
&cfg,
|
|
|
cargo.path().to_path_buf(),
|
|
|
idl_out,
|
|
|
+ idl_ts_out,
|
|
|
verifiable,
|
|
|
solana_version,
|
|
|
stdout,
|
|
@@ -585,6 +587,7 @@ fn build_all(
|
|
|
cfg: &WithPath<Config>,
|
|
|
cfg_path: &Path,
|
|
|
idl_out: Option<PathBuf>,
|
|
|
+ idl_ts_out: Option<PathBuf>,
|
|
|
verifiable: bool,
|
|
|
solana_version: Option<String>,
|
|
|
stdout: Option<File>, // Used for the package registry server.
|
|
@@ -600,6 +603,7 @@ fn build_all(
|
|
|
cfg,
|
|
|
p.join("Cargo.toml"),
|
|
|
idl_out.clone(),
|
|
|
+ idl_ts_out.clone(),
|
|
|
verifiable,
|
|
|
solana_version.clone(),
|
|
|
stdout.as_ref().map(|f| f.try_clone()).transpose()?,
|
|
@@ -620,6 +624,7 @@ fn build_cwd(
|
|
|
cfg: &WithPath<Config>,
|
|
|
cargo_toml: PathBuf,
|
|
|
idl_out: Option<PathBuf>,
|
|
|
+ idl_ts_out: Option<PathBuf>,
|
|
|
verifiable: bool,
|
|
|
solana_version: Option<String>,
|
|
|
stdout: Option<File>,
|
|
@@ -631,7 +636,7 @@ fn build_cwd(
|
|
|
Some(p) => std::env::set_current_dir(&p)?,
|
|
|
};
|
|
|
match verifiable {
|
|
|
- false => _build_cwd(idl_out, cargo_args),
|
|
|
+ false => _build_cwd(idl_out, idl_ts_out, cargo_args),
|
|
|
true => build_cwd_verifiable(cfg, cargo_toml, solana_version, stdout, stderr),
|
|
|
}
|
|
|
}
|
|
@@ -696,8 +701,14 @@ fn build_cwd_verifiable(
|
|
|
// Build the idl.
|
|
|
if let Ok(Some(idl)) = extract_idl("src/lib.rs") {
|
|
|
println!("Extracting the IDL");
|
|
|
+
|
|
|
+ // Write out the JSON file.
|
|
|
let out_file = workspace_dir.join(format!("target/idl/{}.json", idl.name));
|
|
|
write_idl(&idl, OutFile::File(out_file))?;
|
|
|
+
|
|
|
+ // Write out the TypeScript type.
|
|
|
+ let ts_file = format!("target/types/{}.ts", idl.name);
|
|
|
+ fs::write(&ts_file, template::idl_ts(&idl)?)?;
|
|
|
}
|
|
|
|
|
|
result
|
|
@@ -860,7 +871,11 @@ fn docker_build(
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
-fn _build_cwd(idl_out: Option<PathBuf>, cargo_args: Vec<String>) -> Result<()> {
|
|
|
+fn _build_cwd(
|
|
|
+ idl_out: Option<PathBuf>,
|
|
|
+ idl_ts_out: Option<PathBuf>,
|
|
|
+ cargo_args: Vec<String>,
|
|
|
+) -> Result<()> {
|
|
|
let exit = std::process::Command::new("cargo")
|
|
|
.arg("build-bpf")
|
|
|
.args(cargo_args)
|
|
@@ -874,12 +889,22 @@ fn _build_cwd(idl_out: Option<PathBuf>, cargo_args: Vec<String>) -> Result<()> {
|
|
|
|
|
|
// Always assume idl is located ar src/lib.rs.
|
|
|
if let Some(idl) = extract_idl("src/lib.rs")? {
|
|
|
+ // JSON out path.
|
|
|
let out = match idl_out {
|
|
|
None => PathBuf::from(".").join(&idl.name).with_extension("json"),
|
|
|
Some(o) => PathBuf::from(&o.join(&idl.name).with_extension("json")),
|
|
|
};
|
|
|
+ // TS out path.
|
|
|
+ let ts_out = match idl_ts_out {
|
|
|
+ None => PathBuf::from(".").join(&idl.name).with_extension("ts"),
|
|
|
+ Some(o) => PathBuf::from(&o.join(&idl.name).with_extension("ts")),
|
|
|
+ };
|
|
|
|
|
|
+ // Write out the JSON file.
|
|
|
write_idl(&idl, OutFile::File(out))?;
|
|
|
+
|
|
|
+ // Write out the TypeScript type.
|
|
|
+ fs::write(ts_out, template::idl_ts(&idl)?)?;
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
@@ -1115,7 +1140,7 @@ fn idl_init(cfg_override: &ConfigOverride, program_id: Pubkey, idl_filepath: Str
|
|
|
with_workspace(cfg_override, |cfg| {
|
|
|
let keypair = cfg.provider.wallet.to_string();
|
|
|
|
|
|
- let bytes = std::fs::read(idl_filepath)?;
|
|
|
+ let bytes = fs::read(idl_filepath)?;
|
|
|
let idl: Idl = serde_json::from_reader(&*bytes)?;
|
|
|
|
|
|
let idl_address = create_idl_account(cfg, &keypair, &program_id, &idl)?;
|
|
@@ -1133,7 +1158,7 @@ fn idl_write_buffer(
|
|
|
with_workspace(cfg_override, |cfg| {
|
|
|
let keypair = cfg.provider.wallet.to_string();
|
|
|
|
|
|
- let bytes = std::fs::read(idl_filepath)?;
|
|
|
+ let bytes = fs::read(idl_filepath)?;
|
|
|
let idl: Idl = serde_json::from_reader(&*bytes)?;
|
|
|
|
|
|
let idl_buffer = create_idl_buffer(cfg, &keypair, &program_id, &idl)?;
|
|
@@ -1383,8 +1408,9 @@ fn write_idl(idl: &Idl, out: OutFile) -> Result<()> {
|
|
|
let idl_json = serde_json::to_string_pretty(idl)?;
|
|
|
match out {
|
|
|
OutFile::Stdout => println!("{}", idl_json),
|
|
|
- OutFile::File(out) => std::fs::write(out, idl_json)?,
|
|
|
+ OutFile::File(out) => fs::write(out, idl_json)?,
|
|
|
};
|
|
|
+
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
@@ -1543,7 +1569,7 @@ fn genesis_flags(cfg: &WithPath<Config>) -> Result<Vec<String>> {
|
|
|
fn stream_logs(config: &WithPath<Config>) -> Result<Vec<std::process::Child>> {
|
|
|
let program_logs_dir = ".anchor/program-logs";
|
|
|
if Path::new(program_logs_dir).exists() {
|
|
|
- std::fs::remove_dir_all(program_logs_dir)?;
|
|
|
+ fs::remove_dir_all(program_logs_dir)?;
|
|
|
}
|
|
|
fs::create_dir_all(program_logs_dir)?;
|
|
|
let mut handles = vec![];
|
|
@@ -1604,10 +1630,10 @@ fn start_test_validator(
|
|
|
let test_ledger_log_filename = ".anchor/test-ledger-log.txt";
|
|
|
|
|
|
if Path::new(test_ledger_filename).exists() {
|
|
|
- std::fs::remove_dir_all(test_ledger_filename)?;
|
|
|
+ fs::remove_dir_all(test_ledger_filename)?;
|
|
|
}
|
|
|
if Path::new(test_ledger_log_filename).exists() {
|
|
|
- std::fs::remove_file(test_ledger_log_filename)?;
|
|
|
+ fs::remove_file(test_ledger_log_filename)?;
|
|
|
}
|
|
|
|
|
|
// Start a validator for testing.
|
|
@@ -1906,7 +1932,7 @@ fn migrate(cfg_override: &ConfigOverride) -> Result<()> {
|
|
|
let module_path = cur_dir.join("migrations/deploy.ts");
|
|
|
let deploy_script_host_str =
|
|
|
template::deploy_ts_script_host(&url, &module_path.display().to_string());
|
|
|
- std::fs::write("deploy.ts", deploy_script_host_str)?;
|
|
|
+ fs::write("deploy.ts", deploy_script_host_str)?;
|
|
|
std::process::Command::new("ts-node")
|
|
|
.arg("deploy.ts")
|
|
|
.env("ANCHOR_WALLET", cfg.provider.wallet.to_string())
|
|
@@ -1917,7 +1943,7 @@ fn migrate(cfg_override: &ConfigOverride) -> Result<()> {
|
|
|
let module_path = cur_dir.join("migrations/deploy.js");
|
|
|
let deploy_script_host_str =
|
|
|
template::deploy_js_script_host(&url, &module_path.display().to_string());
|
|
|
- std::fs::write("deploy.js", deploy_script_host_str)?;
|
|
|
+ fs::write("deploy.js", deploy_script_host_str)?;
|
|
|
std::process::Command::new("node")
|
|
|
.arg("deploy.js")
|
|
|
.env("ANCHOR_WALLET", cfg.provider.wallet.to_string())
|
|
@@ -2019,7 +2045,7 @@ fn shell(cfg_override: &ConfigOverride) -> Result<()> {
|
|
|
.map(|(name, pd)| {
|
|
|
if let Some(idl_fp) = &pd.idl {
|
|
|
let file_str =
|
|
|
- std::fs::read_to_string(idl_fp).expect("Unable to read IDL file");
|
|
|
+ fs::read_to_string(idl_fp).expect("Unable to read IDL file");
|
|
|
let idl = serde_json::from_str(&file_str).expect("Idl not readable");
|
|
|
idls.insert(name.clone(), idl);
|
|
|
}
|
|
@@ -2188,7 +2214,7 @@ fn publish(
|
|
|
// Skip target dir.
|
|
|
if !path_str.contains("target/") && !path_str.contains("/target") {
|
|
|
// Only add the file if it's not empty.
|
|
|
- let metadata = std::fs::File::open(&e)?.metadata()?;
|
|
|
+ let metadata = fs::File::open(&e)?.metadata()?;
|
|
|
if metadata.len() > 0 {
|
|
|
println!("PACKING: {}", e.display().to_string());
|
|
|
if e.is_dir() {
|