Skip to content

Commit

Permalink
refactor: world functions into seprate file
Browse files Browse the repository at this point in the history
  • Loading branch information
iamnamananand996 committed Oct 6, 2024
1 parent f9cbef6 commit c113fa8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cli/src/templates/world.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use anchor_cli::Files;
use heck::ToSnakeCase;
use std::path::Path;

/// Create a world which holds position data.
pub fn create_world_template_simple(name: &str, program_path: &Path) -> Files {
vec![(
program_path.join("src").join("lib.rs"),
format!(
r#"use anchor_lang::prelude::*;
declare_id!("{}");
#[program]
pub mod {} {{
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {{
Ok(())
}}
}}
#[derive(Accounts)]
pub struct Initialize {{}}
"#,
anchor_cli::rust_template::get_or_create_program_id(name),
name.to_snake_case(),
),
)]
}
42 changes: 42 additions & 0 deletions cli/src/world.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::{rust_template::create_world, workspace::with_workspace};
use anchor_cli::config::ConfigOverride;
use anyhow::{anyhow, Result};
use std::fs;

// Create a new component from the template
pub fn new_world(cfg_override: &ConfigOverride, name: String) -> Result<()> {
with_workspace(cfg_override, |cfg| {
match cfg.path().parent() {
None => {
println!("Unable to make new world");
}
Some(parent) => {
std::env::set_current_dir(parent)?;

let cluster = cfg.provider.cluster.clone();
let programs = cfg.programs.entry(cluster).or_default();
if programs.contains_key(&name) {
return Err(anyhow!("Program already exists"));
}

programs.insert(
name.clone(),
anchor_cli::config::ProgramDeployment {
address: {
create_world(&name)?;
anchor_cli::rust_template::get_or_create_program_id(&name)
},
path: None,
idl: None,
},
);

let toml = cfg.to_string();
fs::write("Anchor.toml", toml)?;

println!("Created new world: {}", name);
}
};
Ok(())
})
}

0 comments on commit c113fa8

Please sign in to comment.