Skip to content

Commit

Permalink
Merge pull request #95 from Web3-Builders-Alliance/main
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanMarchetto authored Jun 28, 2023
2 parents cf9f28a + 6bb578f commit 0ce66c3
Show file tree
Hide file tree
Showing 57 changed files with 1,680 additions and 916 deletions.
69 changes: 14 additions & 55 deletions cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "soda-cli"
version = "0.0.1"
version = "0.0.4"
edition = "2021"
license = "Apache-2.0"
description = "Generates Solana Projects from an IDL"
Expand All @@ -9,6 +9,5 @@ description = "Generates Solana Projects from an IDL"

[dependencies]
serde_json = "1.0.93"
env_logger = "0.10"
clap = { version = "4.1.8", features = ["derive"] }
soda_sol = { path = "../crate" }
soda_sol = "0.0.4"
2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@use_soda/soda-cli",
"version": "0.0.1",
"version": "0.0.4",
"description": "Generates Solana Projects from an IDL",
"main": "start.js",
"directories": {
Expand Down
4 changes: 2 additions & 2 deletions cli/pre-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ if (fs.existsSync(cargoDir)) {

const features = process.env.npm_config_features ? `--features ${process.env.npm_config_features.replace(",", " ")}` : "";

console.log(`Installing and compiling soda-cli 0.0.1 ${features} ...`);
exec(`cargo install soda-cli --vers 0.0.1 ${features}`, (error, stdout, stderr) => {
console.log(`Installing and compiling soda-cli 0.0.2 ${features} ...`);
exec(`cargo install soda-cli --vers 0.0.2 ${features}`, (error, stdout, stderr) => {
console.log(stdout);
if (error || stderr) {
console.log(error || stderr);
Expand Down
92 changes: 76 additions & 16 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#![allow(non_snake_case, non_camel_case_types)]
use clap::Parser;
use soda_sol::{generate_from_idl, IDL};
use soda_sol::*;
use std::error::Error;
use std::fs::{canonicalize, File};
use std::io::Write;

const IDL_DEFAULT_PATH: &str = "./idl.json";
const TEMPLATE_DEFAULT_PATH: &str = "./template/";
const TEMPLATE_DEFAULT_PATH: &str = "./template";
const TEMPLATE_FILE_NAME: &str = "template.soda";

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
Expand All @@ -14,21 +16,79 @@ struct Cli {
}

fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();
let mut template_path = TEMPLATE_DEFAULT_PATH;
let mut idl_path = IDL_DEFAULT_PATH;
let cli = Cli::parse();
if !cli.paths.is_empty() {
idl_path = &cli.paths[0];
}
if cli.paths.len() > 1 {
template_path = &cli.paths[1];
}

let json_file_path = canonicalize(idl_path).unwrap();
let file = File::open(json_file_path).unwrap();
let idl: IDL = serde_json::from_reader(file).expect("error while reading json");
generate_from_idl(".", idl, template_path);
println!("Project Generated!");
match &cli.paths[0] {
command if command == "pack-template" => {
let template_path = if cli.paths.len() > 1 {
&cli.paths[1]
} else {
TEMPLATE_DEFAULT_PATH
};
let file_path = if cli.paths.len() > 2 {
&cli.paths[2]
} else {
TEMPLATE_FILE_NAME };
let template = get_template_from_fs(template_path);
save_template(template, file_path );
println!("Template File Generated!");
}
command if command == "unpack-template" => {
let template_path = if cli.paths.len() > 1 {
&cli.paths[1]
} else {
TEMPLATE_FILE_NAME
};
let base_path = if cli.paths.len() > 2 {
&cli.paths[2]
} else {
TEMPLATE_DEFAULT_PATH
};
let template = load_template(template_path);
write_project_to_fs(template.files, &format!("{}/files", base_path));
let mut helpers = vec![];
for helper in template.helpers {
helpers.push(TemplateFile {
path: format!("helpers/{}.hbs", helper.helper_name),
content: Content::String(helper.script),
});
}
write_project_to_fs(helpers, base_path);
let metadata = serde_json::to_string(&template.metadata).unwrap();
let mut metadata_file = File::create(format!("{}/metadata.json", base_path)).unwrap();
metadata_file.write_all(metadata.as_bytes()).unwrap();
println!("Template Unpacked!");
}
command if command == "create-project" => {
let idl_path = if cli.paths.len() > 1 {
&cli.paths[1]
} else {
IDL_DEFAULT_PATH
};
let template_path = if cli.paths.len() > 2 {
&cli.paths[2]
} else {
TEMPLATE_DEFAULT_PATH
};
let json_file_path = canonicalize(idl_path).unwrap();
let file = File::open(json_file_path).unwrap();
let idl: IDL = serde_json::from_reader(file).expect("error while reading json");
generate_from_idl(".", idl, template_path);
println!("Project Generated!");
}
_ => {
display_help();
}
}
} else {
display_help();
};
Ok(())
}

fn display_help() {
println!("Commands:");
println!(" create-project [idl_path] [template_path]");
println!(" pack-template [template_path] [file_path]");
println!(" unpack-template [template_path]");
}
Loading

0 comments on commit 0ce66c3

Please sign in to comment.