Skip to content

Commit

Permalink
edit ts type String to Str
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrinathNR committed Nov 29, 2024
1 parent 82ef730 commit fb79d76
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 115 deletions.
72 changes: 44 additions & 28 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::{
collections::HashMap, env, fs, io::{BufRead, BufReader}, path::{Path, PathBuf}, process::{Command, Stdio}
collections::HashMap,
env, fs,
io::{BufRead, BufReader},
path::{Path, PathBuf},
process::{Command, Stdio},
};

use anyhow::{Context, Result};
use convert_case::{Case, Casing};
use regex::{RegexBuilder, Regex};
use regex::{Regex, RegexBuilder};
use swc_ecma_ast::Module;
use toml::Value;

Expand Down Expand Up @@ -73,7 +77,8 @@ pub fn init(name: &String) {
let program_ids = extract_program_ids(&anchor_toml)
.unwrap_or_else(|_| panic!("Failed to extract program IDs from Anchor.toml"));

let program_id = program_ids.get(&toml_program_name)
let program_id = program_ids
.get(&toml_program_name)
.unwrap_or_else(|| panic!("Program ID not found for {}", name));

// Create the ts-programs src/{programName}.ts file and add default content
Expand All @@ -99,7 +104,9 @@ pub fn init(name: &String) {
pub fn build_workspace() -> Result<()> {
// Verify we're in a workspace root
if !Path::new("Anchor.toml").exists() {
return Err(anyhow::anyhow!("Anchor.toml not found. Are you in the workspace root?"));
return Err(anyhow::anyhow!(
"Anchor.toml not found. Are you in the workspace root?"
));
}

// Get all programs from the programs directory
Expand Down Expand Up @@ -129,8 +136,10 @@ pub fn build_workspace() -> Result<()> {

// Create/ensure src directory exists
let src_dir = program_dir.join("src");
fs::create_dir_all(&src_dir)
.context(format!("Failed to create src directory for {}", program_name))?;
fs::create_dir_all(&src_dir).context(format!(
"Failed to create src directory for {}",
program_name
))?;

// Look for corresponding TypeScript file
let ts_file = PathBuf::from("ts-programs")
Expand Down Expand Up @@ -159,7 +168,9 @@ pub fn build_workspace() -> Result<()> {
pub fn run_tests() -> Result<()> {
// Verify we're in a workspace root by checking for Anchor.toml
if !Path::new("Anchor.toml").exists() {
return Err(anyhow::anyhow!("Anchor.toml not found. Are you in the workspace root?"));
return Err(anyhow::anyhow!(
"Anchor.toml not found. Are you in the workspace root?"
));
}

println!("Running anchor tests...");
Expand All @@ -172,8 +183,8 @@ pub fn run_tests() -> Result<()> {
cmd.arg("test");

// Stream the test output
let output = execute_cmd_with_output(&mut cmd)
.context("Failed to execute anchor test command")?;
let output =
execute_cmd_with_output(&mut cmd).context("Failed to execute anchor test command")?;

// Check if tests passed
if output.status.success() {
Expand All @@ -191,16 +202,15 @@ pub fn sync_program_ids() -> Result<()> {
let mut cmd = Command::new("anchor");
cmd.args(["keys", "sync"]);

let output = execute_cmd_with_output(&mut cmd)
.context("Failed to execute 'anchor keys sync'")?;
let output =
execute_cmd_with_output(&mut cmd).context("Failed to execute 'anchor keys sync'")?;

if !output.status.success() {
return Err(anyhow::anyhow!("Failed to run 'anchor keys sync'"));
}

// Read program IDs from Anchor.toml
let anchor_toml = fs::read_to_string("Anchor.toml")
.context("Failed to read Anchor.toml")?;
let anchor_toml = fs::read_to_string("Anchor.toml").context("Failed to read Anchor.toml")?;
let program_ids = extract_program_ids(&anchor_toml)?;

// Update TypeScript files
Expand All @@ -213,12 +223,17 @@ pub fn sync_program_ids() -> Result<()> {
let ts_program_file_name = program_name.to_case(Case::Camel);
let ts_file = ts_programs_dir.join(format!("{}.ts", ts_program_file_name));
if !ts_file.exists() {
println!("Warning: TypeScript file not found for program: {}", program_name);
println!(
"Warning: TypeScript file not found for program: {}",
program_name
);
continue;
}

update_program_id_in_ts(&ts_file, &program_id)
.context(format!("Failed to update program ID in {}.ts", program_name))?;
update_program_id_in_ts(&ts_file, &program_id).context(format!(
"Failed to update program ID in {}.ts",
program_name
))?;

println!("Updated program ID for {} to {}", program_name, program_id);
}
Expand All @@ -228,8 +243,7 @@ pub fn sync_program_ids() -> Result<()> {
}

fn extract_program_ids(anchor_toml: &str) -> Result<HashMap<String, String>> {
let toml_value: Value = anchor_toml.parse()
.context("Failed to parse Anchor.toml")?;
let toml_value: Value = anchor_toml.parse().context("Failed to parse Anchor.toml")?;

let mut program_ids = HashMap::new();

Expand All @@ -253,8 +267,7 @@ fn extract_program_ids(anchor_toml: &str) -> Result<HashMap<String, String>> {
}

fn update_program_id_in_ts(file_path: &Path, program_id: &str) -> Result<()> {
let content = fs::read_to_string(file_path)
.context("Failed to read TypeScript file")?;
let content = fs::read_to_string(file_path).context("Failed to read TypeScript file")?;

// Create a regex that matches both possible patterns
let re = RegexBuilder::new(r#"static PROGRAM_ID = new Pubkey\(["']([^"']*)["']\)"#)
Expand All @@ -264,24 +277,27 @@ fn update_program_id_in_ts(file_path: &Path, program_id: &str) -> Result<()> {

let new_content = if let Some(capture) = re.captures(&content) {
// Replace the program ID while preserving the exact casing and spacing
content.replace(&capture[0], &format!(r#"static PROGRAM_ID = new Pubkey("{}")"#, program_id))
content.replace(
&capture[0],
&format!(r#"static PROGRAM_ID = new Pubkey("{}")"#, program_id),
)
} else {
println!("Warning: PROGRAM_ID not found in expected format in {}", file_path.display());
println!(
"Warning: PROGRAM_ID not found in expected format in {}",
file_path.display()
);
content
};

fs::write(file_path, new_content)
.context("Failed to write updated TypeScript file")?;
fs::write(file_path, new_content).context("Failed to write updated TypeScript file")?;

Ok(())
}

fn get_program_name_from_cargo(cargo_path: &Path) -> Result<String> {
let content = fs::read_to_string(cargo_path)
.context("Failed to read Cargo.toml")?;
let content = fs::read_to_string(cargo_path).context("Failed to read Cargo.toml")?;

let cargo_toml: Value = content.parse()
.context("Failed to parse Cargo.toml")?;
let cargo_toml: Value = content.parse().context("Failed to parse Cargo.toml")?;

// Get the package name from Cargo.toml
let package_name = cargo_toml
Expand Down
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ pub enum PoseidonError {
AtomNotFound,
#[error("expected a authority for the Token account")]
AuthorityNotFound,
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use clap::{Parser as ClapParser, Subcommand};
use parse_ts::parse_ts;
use swc_ecma_ast::Module;

use cli::{init, build_workspace, run_tests, sync_program_ids};
use cli::{build_workspace, init, run_tests, sync_program_ids};
use transpiler::transpile;

#[derive(ClapParser, Debug)]
Expand Down
Loading

0 comments on commit fb79d76

Please sign in to comment.