-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: world functions into seprate file
- Loading branch information
1 parent
f9cbef6
commit c113fa8
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
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,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(), | ||
), | ||
)] | ||
} |
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,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(()) | ||
}) | ||
} |