1
- use crate :: cli:: Template ;
1
+ use crate :: {
2
+ cli:: Template ,
3
+ generator:: { write_to_file, ChainSpec } ,
4
+ } ;
2
5
use anyhow:: Result ;
3
6
use git2:: Repository ;
4
7
use std:: { fs, path:: Path } ;
5
8
use walkdir:: WalkDir ;
6
9
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
+ }
8
16
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 < ( ) > {
14
18
use Template :: * ;
15
19
// TODO : if target folder exists, prompt user to clean dir or abort
20
+ sanitize ( target) ?;
16
21
let url = match template {
17
22
EPT => "https://github.com/paritytech/extended-parachain-template.git" ,
18
23
FPT => "https://github.com/paritytech/frontier-parachain-template.git" ,
19
24
Contracts => "https://github.com/paritytech/substrate-contracts-node.git" ,
20
25
Vanilla => {
21
- return instantiate_vanilla_template ( target, Some ( Config ) ) ;
26
+ return instantiate_vanilla_template ( target, config ) ;
22
27
}
23
28
} ;
24
29
Repository :: clone ( url, target) ?;
25
30
Ok ( ( ) )
26
31
}
27
32
// 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 < ( ) > {
29
34
let temp_dir = :: tempfile:: TempDir :: new_in ( std:: env:: temp_dir ( ) ) ?;
30
35
let temp_path = temp_dir. path ( ) ;
31
- println ! ( "Temporary directory created at {:?}" , temp_path) ;
36
+ // println!("Temporary directory created at {:?}", temp_path);
32
37
33
38
Repository :: clone ( "https://github.com/weezy20/DoTemplate.git" , temp_path) ?;
34
39
let source = temp_path. join ( "templates/vanilla-parachain" ) ;
@@ -45,6 +50,39 @@ pub fn instantiate_vanilla_template(target: &Path, config: Option<Config>) -> Re
45
50
fs:: copy ( source_path, & destination_path) ?;
46
51
}
47
52
}
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
+ }
48
66
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
+ }
49
87
Ok ( ( ) )
50
88
}
0 commit comments