-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scaffold): allow for multiple generators + walletd client
- Loading branch information
Showing
19 changed files
with
490 additions
and
136 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,14 @@ | ||
# Tari Scaffolder | ||
|
||
## Basic usage | ||
|
||
```shell | ||
cargo run --release --bin tari_scaffolder -- -c config.json --generator rust-template-cli scaffold /path/to/the/template/source/or/wasm-binary.wasm | ||
``` | ||
|
||
## Generators | ||
|
||
- rust-template-cli - generates a rust cli application for the template | ||
- react-admin-web (TODO) - generates a basic admin web interface for the template using vite.js and React. | ||
|
||
|
File renamed without changes.
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
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
181 changes: 181 additions & 0 deletions
181
applications/tari_scaffolder/src/generators/liquid/mod.rs
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,181 @@ | ||
// Copyright 2024 The Tari Project | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
mod snake_case; | ||
|
||
use std::fs; | ||
|
||
use convert_case::{Case, Casing}; | ||
use tari_dan_engine::abi; | ||
|
||
use crate::generators::{CodeGenerator, GeneratorOpts, TemplateDefinition}; | ||
|
||
pub enum LiquidTemplate { | ||
RustCli, | ||
} | ||
|
||
impl LiquidTemplate { | ||
const fn get_template(&self) -> &'static [(&'static str, &'static str)] { | ||
match self { | ||
LiquidTemplate::RustCli => &[ | ||
( | ||
"Cargo.toml", | ||
include_str!(concat!( | ||
env!("CARGO_MANIFEST_DIR"), | ||
"/liquid_templates/rust_cli/Cargo.toml.liquid" | ||
)), | ||
), | ||
( | ||
".gitignore", | ||
include_str!(concat!( | ||
env!("CARGO_MANIFEST_DIR"), | ||
"/liquid_templates/rust_cli/.gitignore.liquid" | ||
)), | ||
), | ||
( | ||
"src/main.rs", | ||
include_str!(concat!( | ||
env!("CARGO_MANIFEST_DIR"), | ||
"/liquid_templates/rust_cli/src/main.rs.liquid" | ||
)), | ||
), | ||
( | ||
"src/cli.rs", | ||
include_str!(concat!( | ||
env!("CARGO_MANIFEST_DIR"), | ||
"/liquid_templates/rust_cli/src/cli.rs.liquid" | ||
)), | ||
), | ||
( | ||
"src/daemon_client.rs", | ||
include_str!(concat!( | ||
env!("CARGO_MANIFEST_DIR"), | ||
"/liquid_templates/rust_cli/src/daemon_client.rs.liquid" | ||
)), | ||
), | ||
], | ||
} | ||
} | ||
} | ||
|
||
pub struct LiquidGenerator { | ||
template: LiquidTemplate, | ||
opts: GeneratorOpts, | ||
} | ||
|
||
impl LiquidGenerator { | ||
pub const fn new(template: LiquidTemplate, opts: GeneratorOpts) -> Self { | ||
Self { template, opts } | ||
} | ||
|
||
fn build_vars(&self, template: &TemplateDefinition) -> liquid_core::Object { | ||
let opts = self.opts.liquid.as_ref().unwrap(); | ||
|
||
let mut globals = liquid::object!({ | ||
"template_name": &template.name, | ||
"commands": [] | ||
}); | ||
globals.extend( | ||
opts.variables | ||
.iter() | ||
.map(|(k, v)| (k.clone().into(), json_value_to_liquid_value(v.clone()))), | ||
); | ||
|
||
for f in template.template.functions() { | ||
let mut args = vec![]; | ||
let mut is_method = false; | ||
let mut requires_buckets = false; | ||
let mut bucket_output = false; | ||
for a in &f.arguments { | ||
args.push(liquid::object!({ | ||
"name": a.name, | ||
"arg_type": a.arg_type.to_string(), | ||
})); | ||
if a.arg_type.to_string() == "Bucket" { | ||
requires_buckets = true; | ||
} | ||
if a.name == "self" { | ||
is_method = true; | ||
} | ||
} | ||
|
||
if let abi::Type::Other { name } = &f.output { | ||
if name == "Bucket" { | ||
bucket_output = true; | ||
} | ||
} | ||
|
||
let arr = globals.get_mut("commands").unwrap().as_array_mut().unwrap(); | ||
arr.push(liquid_core::Value::Object(liquid::object!({ | ||
"name": f.name, | ||
"title": f.name.to_case(Case::UpperCamel), | ||
"args" : args, | ||
"is_method": is_method, | ||
"is_mut": f.is_mut, | ||
"output": f.output.to_string(), | ||
"requires_buckets": requires_buckets, | ||
"bucket_output": bucket_output, | ||
}))); | ||
} | ||
globals | ||
} | ||
} | ||
|
||
impl CodeGenerator for LiquidGenerator { | ||
fn generate(&self, template: &TemplateDefinition) -> anyhow::Result<()> { | ||
let opts = &self.opts; | ||
fs::create_dir_all(opts.output_path.join("src"))?; | ||
|
||
let templates = self.template.get_template(); | ||
|
||
let vars = self.build_vars(template); | ||
|
||
for (out_file, content) in templates { | ||
fs::write(opts.output_path.join(out_file), replace_tokens(content, &vars)?)?; | ||
} | ||
|
||
if !self.opts.liquid.as_ref().unwrap().skip_format { | ||
std::process::Command::new("cargo") | ||
.args(["fmt"]) | ||
.current_dir(&opts.output_path) | ||
.status()?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
fn replace_tokens(in_file: &str, globals: &liquid_core::Object) -> anyhow::Result<String> { | ||
let template = liquid::ParserBuilder::with_stdlib() | ||
.filter(snake_case::SnakeCase) | ||
.build()? | ||
.parse(in_file)?; | ||
|
||
let built_template = template.render(globals)?; | ||
Ok(built_template) | ||
} | ||
|
||
fn json_value_to_liquid_value(value: serde_json::Value) -> liquid_core::Value { | ||
match value { | ||
serde_json::Value::Null => liquid_core::Value::Nil, | ||
serde_json::Value::Bool(b) => liquid_core::Value::scalar(b), | ||
serde_json::Value::Number(n) => { | ||
if n.is_i64() { | ||
liquid_core::Value::scalar(n.as_i64().unwrap()) | ||
} else { | ||
liquid_core::Value::scalar(n.as_f64().unwrap()) | ||
} | ||
}, | ||
serde_json::Value::String(s) => liquid_core::Value::scalar(s), | ||
serde_json::Value::Array(a) => liquid_core::Value::Array( | ||
a.into_iter() | ||
.map(json_value_to_liquid_value) | ||
.collect::<Vec<liquid_core::Value>>(), | ||
), | ||
serde_json::Value::Object(o) => liquid_core::Value::Object( | ||
o.into_iter() | ||
.map(|(k, v)| (k.into(), json_value_to_liquid_value(v))) | ||
.collect::<liquid_core::Object>(), | ||
), | ||
} | ||
} |
File renamed without changes.
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,56 @@ | ||
// Copyright 2024 The Tari Project | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
use std::{collections::HashMap, path::PathBuf, str::FromStr}; | ||
|
||
use serde::Deserialize; | ||
use tari_dan_engine::{abi::TemplateDef, template::LoadedTemplate}; | ||
|
||
pub mod liquid; | ||
|
||
pub struct TemplateDefinition { | ||
pub name: String, | ||
pub template: TemplateDef, | ||
} | ||
|
||
impl From<LoadedTemplate> for TemplateDefinition { | ||
fn from(loaded_template: LoadedTemplate) -> Self { | ||
Self { | ||
name: loaded_template.template_name().to_string(), | ||
template: loaded_template.template_def().clone(), | ||
} | ||
} | ||
} | ||
|
||
pub trait CodeGenerator { | ||
fn generate(&self, template: &TemplateDefinition) -> anyhow::Result<()>; | ||
} | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
pub struct GeneratorOpts { | ||
pub output_path: PathBuf, | ||
pub liquid: Option<LiquidGeneratorOpts>, | ||
} | ||
|
||
#[derive(Debug, Clone, Deserialize)] | ||
pub struct LiquidGeneratorOpts { | ||
#[serde(default)] | ||
pub skip_format: bool, | ||
pub variables: HashMap<String, serde_json::Value>, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum GeneratorType { | ||
RustTemplateCli, | ||
} | ||
|
||
impl FromStr for GeneratorType { | ||
type Err = anyhow::Error; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"rust-template-cli" => Ok(GeneratorType::RustTemplateCli), | ||
_ => Err(anyhow::anyhow!("Invalid generator type")), | ||
} | ||
} | ||
} |
Oops, something went wrong.