Skip to content

Commit

Permalink
👷 Reorganizing templates, using bolt.workspace an… (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo authored Nov 28, 2024
1 parent 7cff0e3 commit c913c28
Show file tree
Hide file tree
Showing 44 changed files with 924 additions and 1,040 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [
"cli",
"programs/*",
Expand Down
4 changes: 0 additions & 4 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ edition = { workspace = true }
name = "bolt"
path = "src/bin/main.rs"

[profile.release]
opt-level = 3
lto = true

[features]
dev = []

Expand Down
13 changes: 13 additions & 0 deletions cli/src/templates/component/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use bolt_lang::*;

declare_id!("{program_id}");

#[component]
#[derive(Default)]
pub struct {program_name} {{
pub x: i64,
pub y: i64,
pub z: i64,
#[max_len(20)]
pub description: String,
}}
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,11 @@ use crate::rust_template::convert_idl_type_to_str; // Import the trait

/// Create a component which holds position data.
pub fn create_component_template_simple(name: &str, program_path: &Path) -> Files {
let program_id = anchor_cli::rust_template::get_or_create_program_id(name);
let program_name = name.to_upper_camel_case();
vec![(
program_path.join("src").join("lib.rs"),
format!(
r#"use bolt_lang::*;
declare_id!("{}");
#[component]
#[derive(Default)]
pub struct {} {{
pub x: i64,
pub y: i64,
pub z: i64,
#[max_len(20)]
pub description: String,
}}
"#,
anchor_cli::rust_template::get_or_create_program_id(name),
name.to_upper_camel_case(),
),
format!(include_str!("lib.rs"), program_id=program_id, program_name=program_name)
)]
}

Expand Down
108 changes: 0 additions & 108 deletions cli/src/templates/program.rs

This file was deleted.

4 changes: 4 additions & 0 deletions cli/src/templates/program/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use anchor_lang::prelude::*;

#[constant]
pub const SEED: &str = "anchor";
7 changes: 7 additions & 0 deletions cli/src/templates/program/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use anchor_lang::prelude::*;

#[error_code]
pub enum ErrorCode {
#[msg("Custom error message")]
CustomError,
}
8 changes: 8 additions & 0 deletions cli/src/templates/program/instructions/initialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use anchor_lang::prelude::*;

#[derive(Accounts)]
pub struct Initialize {}

pub fn handler(_ctx: Context<Initialize>) -> Result<()> {
Ok(())
}
3 changes: 3 additions & 0 deletions cli/src/templates/program/instructions/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod initialize;

pub use initialize::*;
45 changes: 45 additions & 0 deletions cli/src/templates/program/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use anchor_cli::Files;
use heck::ToSnakeCase;
use std::path::Path; // Import the trait

pub fn create_program_template_single(name: &str, program_path: &Path) -> Files {
let program_id = anchor_cli::rust_template::get_or_create_program_id(name);
let program_name = name.to_snake_case();
vec![(
program_path.join("src").join("lib.rs"),
format!(include_str!("single.lib.rs"), program_id=program_id, program_name=program_name),
)]
}

/// Create a program with multiple files for instructions, state...
pub fn create_program_template_multiple(name: &str, program_path: &Path) -> Files {
let src_path = program_path.join("src");
let program_id = anchor_cli::rust_template::get_or_create_program_id(name);
let program_name = name.to_snake_case();
vec![
(
src_path.join("lib.rs"),
format!(include_str!("multiple.lib.rs"), program_id=program_id, program_name=program_name),
),
(
src_path.join("constants.rs"),
include_str!("constants.rs").into(),
),
(
src_path.join("error.rs"),
include_str!("error.rs").into(),
),
(
src_path.join("instructions").join("mod.rs"),
include_str!("instructions/mod.rs").into(),
),
(
src_path.join("instructions").join("initialize.rs"),
include_str!("instructions/initialize.rs").into(),
),
(
src_path.join("state").join("mod.rs"),
include_str!("state/mod.rs").into(),
),
]
}
21 changes: 21 additions & 0 deletions cli/src/templates/program/multiple.lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pub mod constants;
pub mod error;
pub mod instructions;
pub mod state;

use anchor_lang::prelude::*;

pub use constants::*;
pub use instructions::*;
pub use state::*;

declare_id!("{program_id}");

#[program]
pub mod {program_name} {{
use super::*;

pub fn initialize(ctx: Context<Initialize>) -> Result<()> {{
initialize::handler(ctx)
}}
}}
15 changes: 15 additions & 0 deletions cli/src/templates/program/single.lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use anchor_lang::prelude::*;

declare_id!("{program_id}");

#[program]
pub mod {program_name} {{
use super::*;

pub fn initialize(_ctx: Context<Initialize>) -> Result<()> {{
Ok(())
}}
}}

#[derive(Accounts)]
pub struct Initialize {{}}
Empty file.
36 changes: 0 additions & 36 deletions cli/src/templates/system.rs

This file was deleted.

21 changes: 21 additions & 0 deletions cli/src/templates/system/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use bolt_lang::*;
use position::Position;

declare_id!("{program_id}");

#[system]
pub mod {program_name} {{

pub fn execute(ctx: Context<Components>, _args_p: Vec<u8>) -> Result<Components> {{
let position = &mut ctx.accounts.position;
position.x += 1;
position.y += 1;
Ok(ctx.accounts)
}}

#[system_input]
pub struct Components {{
pub position: Position,
}}

}}
13 changes: 13 additions & 0 deletions cli/src/templates/system/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use anchor_cli::Files;
use heck::ToSnakeCase;
use std::path::Path;

/// Create a system which operates on a Position component.
pub fn create_system_template_simple(name: &str, program_path: &Path) -> Files {
let program_id = anchor_cli::rust_template::get_or_create_program_id(name);
let program_name = name.to_snake_case();
vec![(
program_path.join("src").join("lib.rs"),
format!(include_str!("lib.rs"), program_id=program_id, program_name=program_name)
)]
}
Loading

0 comments on commit c913c28

Please sign in to comment.