Skip to content

Commit 6f72b62

Browse files
committed
config items
1 parent c468b45 commit 6f72b62

File tree

4 files changed

+100
-36
lines changed

4 files changed

+100
-36
lines changed

src/cli.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use strum_macros::{Display, EnumString};
55
#[command(arg_required_else_help = true)]
66
pub struct Cli {
77
#[command(subcommand)]
8-
pub create: Create,
8+
pub(crate) create: Create,
99
}
1010

1111
#[derive(Subcommand, Clone)]
@@ -30,8 +30,21 @@ pub enum Template {
3030
#[derive(Parser, Clone)]
3131
pub struct TemplateCmd {
3232
#[arg(help = "Name of the app. Also works as a directory path for your project")]
33-
pub name: String,
34-
#[arg(help = "Template to create; Options are ept, fpt, cpt")]
33+
pub(crate) name: String,
34+
#[arg(
35+
help = "Template to create; Options are 'ept', 'fpt', 'cpt'. Leave empty for default parachain template"
36+
)]
3537
#[arg(default_value = "vanilla")]
36-
pub template: Template,
38+
pub(crate) template: Template,
39+
#[arg(long, short, help = "Token Symbol", default_value = "UNIT")]
40+
pub(crate) symbol: Option<String>,
41+
#[arg(long, short, help = "Token Decimals", default_value = "12")]
42+
pub(crate) decimals: Option<String>,
43+
#[arg(
44+
long = "endowment",
45+
short,
46+
help = "Token Endowment for dev accounts",
47+
default_value = "1u64 << 60"
48+
)]
49+
pub(crate) initial_endowment: Option<String>,
3750
}

src/generator.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
1+
//! TODO: Generators should reference files that live in the repository
2+
13
use std::{fs::OpenOptions, path::Path};
24

35
use askama::Template;
46

5-
#[derive(Template)]
6-
#[template(path = "test.templ", escape = "none")]
7-
struct Runtime<'a> {
8-
code: &'a str,
9-
balances: bool,
10-
}
11-
127
// TODO: This should be coupled with Runtime in the sense that pallets part of a Runtime may need a default genesis config
138
#[derive(Template)]
149
#[template(path = "chain_spec.templ", escape = "none")]
15-
struct ChainSpec<'a> {
16-
token_symbol : &'a str,
17-
decimals: u8,
18-
initial_endowment: &'a str,
10+
pub(crate) struct ChainSpec {
11+
pub(crate) token_symbol: String,
12+
pub(crate) decimals: u8,
13+
pub(crate) initial_endowment: String,
1914
}
2015
// todo : generate directory structure
21-
16+
// todo : This is only for development
2217
pub fn generate() {
2318
let cs = ChainSpec {
24-
token_symbol: "DOT",
19+
token_symbol: "DOT".to_owned(),
2520
decimals: 10,
26-
initial_endowment: "1u64 << 15"
21+
initial_endowment: "1u64 << 15".to_owned(),
2722
};
2823
let rendered = cs.render().unwrap();
2924
write_to_file(Path::new("src/x.rs"), &rendered);
3025
}
3126

32-
fn write_to_file<'a>(path: &Path, contents: &'a str) {
27+
// TODO: Check the usage of `expect`. We don't want to leave the outdir in a unhygenic state
28+
pub(crate) fn write_to_file<'a>(path: &Path, contents: &'a str) {
3329
use std::io::Write;
3430
let mut file = OpenOptions::new()
3531
.write(true)

src/main.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
mod cli;
2-
mod template;
32
mod generator;
3+
mod template;
44

55
use cli::Cli;
66
use std::path::Path;
77

8+
use crate::template::Config;
9+
810
fn main() -> anyhow::Result<()> {
9-
generator::generate();
10-
std::process::exit(0);
11-
11+
// eprintln!("DEBUG: Generator code is only used for development purposes");
12+
// generator::generate();
13+
// std::process::exit(0);
14+
1215
let cli = <Cli as clap::Parser>::parse();
13-
let (app_name, template) = match cli.create {
14-
cli::Create::Create(cli::TemplateCmd { name, template }) => (name, template),
16+
let (app_name, template, config) = match cli.create {
17+
cli::Create::Create(cli::TemplateCmd {
18+
name,
19+
template,
20+
symbol,
21+
decimals,
22+
initial_endowment,
23+
}) => (
24+
name,
25+
template,
26+
Config {
27+
symbol: symbol.expect("default values"),
28+
decimals: decimals.expect("default values").parse::<u8>()?,
29+
initial_endowment: initial_endowment.expect("default values"),
30+
},
31+
),
1532
};
1633
println!("Starting {} on `{}`!", template, app_name);
1734
let destination_path = Path::new(&app_name);
18-
template::instantiate_template_dir(&template, destination_path)?;
35+
template::instantiate_template_dir(&template, destination_path, config)?;
1936
println!("cd into `{app_name}` and enjoy hacking! 🚀");
2037

2138
Ok(())

src/template.rs

+48-10
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1-
use crate::cli::Template;
1+
use crate::{
2+
cli::Template,
3+
generator::{write_to_file, ChainSpec},
4+
};
25
use anyhow::Result;
36
use git2::Repository;
47
use std::{fs, path::Path};
58
use walkdir::WalkDir;
69

7-
pub struct Config;
10+
#[derive(Debug, Clone)]
11+
pub struct Config {
12+
pub(crate) symbol: String,
13+
pub(crate) decimals: u8,
14+
pub(crate) initial_endowment: String,
15+
}
816

9-
pub fn instantiate_template_dir(
10-
template: &Template,
11-
target: &Path,
12-
// config: &Config,
13-
) -> Result<()> {
17+
pub fn instantiate_template_dir(template: &Template, target: &Path, config: Config) -> Result<()> {
1418
use Template::*;
1519
// TODO : if target folder exists, prompt user to clean dir or abort
20+
sanitize(target)?;
1621
let url = match template {
1722
EPT => "https://github.com/paritytech/extended-parachain-template.git",
1823
FPT => "https://github.com/paritytech/frontier-parachain-template.git",
1924
Contracts => "https://github.com/paritytech/substrate-contracts-node.git",
2025
Vanilla => {
21-
return instantiate_vanilla_template(target, Some(Config));
26+
return instantiate_vanilla_template(target, config);
2227
}
2328
};
2429
Repository::clone(url, target)?;
2530
Ok(())
2631
}
2732
// TODO: The config will shape the emitted template
28-
pub fn instantiate_vanilla_template(target: &Path, config: Option<Config>) -> Result<()> {
33+
pub fn instantiate_vanilla_template(target: &Path, config: Config) -> Result<()> {
2934
let temp_dir = ::tempfile::TempDir::new_in(std::env::temp_dir())?;
3035
let temp_path = temp_dir.path();
31-
println!("Temporary directory created at {:?}", temp_path);
36+
// println!("Temporary directory created at {:?}", temp_path);
3237

3338
Repository::clone("https://github.com/weezy20/DoTemplate.git", temp_path)?;
3439
let source = temp_path.join("templates/vanilla-parachain");
@@ -45,6 +50,39 @@ pub fn instantiate_vanilla_template(target: &Path, config: Option<Config>) -> Re
4550
fs::copy(source_path, &destination_path)?;
4651
}
4752
}
53+
let chainspec = ChainSpec {
54+
token_symbol: config.symbol,
55+
decimals: config.decimals,
56+
initial_endowment: config.initial_endowment,
57+
};
58+
use askama::Template;
59+
write_to_file(
60+
&target.join("node/src/chain_spec.rs"),
61+
chainspec.render().expect("infallible").as_ref(),
62+
);
63+
64+
Ok(())
65+
}
4866

67+
fn sanitize(target: &Path) -> Result<()> {
68+
use std::io::{stdin, stdout, Write};
69+
if target.exists() {
70+
print!(
71+
"\"{}\" folder exists. Do you want to clean it? [y/n]: ",
72+
target.display()
73+
);
74+
stdout().flush()?;
75+
76+
let mut input = String::new();
77+
stdin().read_line(&mut input)?;
78+
79+
if input.trim().to_lowercase() == "y" {
80+
fs::remove_dir_all(target)?;
81+
} else {
82+
return Err(anyhow::anyhow!(
83+
"User aborted due to existing target folder."
84+
));
85+
}
86+
}
4987
Ok(())
5088
}

0 commit comments

Comments
 (0)