@@ -18,6 +18,39 @@ static TEMPLATES_DIR: Dir = include_dir!("snforge_templates");
18
18
const DEFAULT_ASSERT_MACROS : Version = Version :: new ( 0 , 1 , 0 ) ;
19
19
const MINIMAL_SCARB_FOR_CORRESPONDING_ASSERT_MACROS : Version = Version :: new ( 2 , 8 , 0 ) ;
20
20
21
+ struct Dependency {
22
+ name : String ,
23
+ version : String ,
24
+ dev : bool ,
25
+ }
26
+
27
+ struct TemplateManifestConfig {
28
+ dependencies : Vec < Dependency > ,
29
+ contract_target : bool ,
30
+ }
31
+
32
+ impl TryFrom < & Template > for TemplateManifestConfig {
33
+ type Error = anyhow:: Error ;
34
+
35
+ fn try_from ( template : & Template ) -> Result < Self > {
36
+ let cairo_version = ScarbCommand :: version ( ) . run ( ) ?. cairo ;
37
+ match template {
38
+ Template :: CairoProgram => Ok ( TemplateManifestConfig {
39
+ dependencies : vec ! [ ] ,
40
+ contract_target : false ,
41
+ } ) ,
42
+ Template :: BalanceContract => Ok ( TemplateManifestConfig {
43
+ dependencies : vec ! [ Dependency {
44
+ name: "starknet" . to_string( ) ,
45
+ version: cairo_version. to_string( ) ,
46
+ dev: false ,
47
+ } ] ,
48
+ contract_target : true ,
49
+ } ) ,
50
+ }
51
+ }
52
+ }
53
+
21
54
fn create_snfoundry_manifest ( path : & PathBuf ) -> Result < ( ) > {
22
55
fs:: write (
23
56
path,
@@ -56,7 +89,7 @@ fn add_template_to_scarb_manifest(path: &PathBuf) -> Result<()> {
56
89
Ok ( ( ) )
57
90
}
58
91
59
- fn overwrite_or_copy_files (
92
+ fn overwrite_or_copy_template_files (
60
93
dir : & Dir ,
61
94
template_path : & Path ,
62
95
project_path : & Path ,
@@ -68,7 +101,7 @@ fn overwrite_or_copy_files(
68
101
match entry {
69
102
DirEntry :: Dir ( dir) => {
70
103
fs:: create_dir_all ( & destination) ?;
71
- overwrite_or_copy_files ( dir, template_path, project_path, project_name) ?;
104
+ overwrite_or_copy_template_files ( dir, template_path, project_path, project_name) ?;
72
105
}
73
106
DirEntry :: File ( file) => {
74
107
let contents = file. contents ( ) ;
@@ -87,13 +120,16 @@ fn replace_project_name(contents: &[u8], project_name: &str) -> Result<Vec<u8>>
87
120
Ok ( contents. into_bytes ( ) )
88
121
}
89
122
90
- fn update_config ( config_path : & Path , template : & Template ) -> Result < ( ) > {
91
- let config_file = fs:: read_to_string ( config_path) ?;
92
- let mut document = config_file
123
+ fn update_config (
124
+ scarb_manifest_path : & Path ,
125
+ template_config : & TemplateManifestConfig ,
126
+ ) -> Result < ( ) > {
127
+ let scarb_toml_content = fs:: read_to_string ( scarb_manifest_path) ?;
128
+ let mut document = scarb_toml_content
93
129
. parse :: < DocumentMut > ( )
94
130
. context ( "invalid document" ) ?;
95
131
96
- if ! matches ! ( template , Template :: CairoProgram ) {
132
+ if template_config . contract_target {
97
133
add_target_to_toml ( & mut document) ;
98
134
}
99
135
@@ -102,7 +138,7 @@ fn update_config(config_path: &Path, template: &Template) -> Result<()> {
102
138
add_assert_macros ( & mut document) ?;
103
139
add_allow_prebuilt_macros ( & mut document) ?;
104
140
105
- fs:: write ( config_path , document. to_string ( ) ) ?;
141
+ fs:: write ( scarb_manifest_path , document. to_string ( ) ) ?;
106
142
107
143
Ok ( ( ) )
108
144
}
@@ -192,6 +228,7 @@ pub fn new(
192
228
template,
193
229
} : NewArgs ,
194
230
) -> Result < ( ) > {
231
+ ScarbCommand :: new ( ) . ensure_available ( ) ?;
195
232
if !overwrite {
196
233
ensure ! (
197
234
!path. exists( ) || path. read_dir( ) . is_ok_and( |mut i| i. next( ) . is_none( ) ) ,
@@ -238,12 +275,14 @@ pub fn new(
238
275
create_snfoundry_manifest ( & snfoundry_manifest_path) ?;
239
276
}
240
277
241
- add_dependencies_to_scarb_toml ( & project_path , & template) ?;
242
- update_config ( & scarb_manifest_path, & template ) ?;
243
- extend_gitignore ( & project_path ) ?;
278
+ let template_config = TemplateManifestConfig :: try_from ( & template) ?;
279
+ add_dependencies ( & scarb_manifest_path, & template_config ) ?;
280
+ update_config ( & scarb_manifest_path , & template_config ) ?;
244
281
245
282
let template_dir = get_template_dir ( & template) ?;
246
- overwrite_or_copy_files ( & template_dir, template_dir. path ( ) , & project_path, & name) ?;
283
+ overwrite_or_copy_template_files ( & template_dir, template_dir. path ( ) , & project_path, & name) ?;
284
+
285
+ extend_gitignore ( & project_path) ?;
247
286
248
287
// Fetch to create lock file.
249
288
ScarbCommand :: new_with_stdio ( )
@@ -255,46 +294,40 @@ pub fn new(
255
294
Ok ( ( ) )
256
295
}
257
296
258
- fn add_dependencies_to_scarb_toml ( project_path : & PathBuf , template : & Template ) -> Result < ( ) > {
259
- let snforge_version = env ! ( "CARGO_PKG_VERSION" ) ;
260
- let cairo_version = ScarbCommand :: version ( ) . run ( ) ? . cairo ;
261
-
297
+ fn add_dependencies (
298
+ scarb_manifest_path : & PathBuf ,
299
+ template_config : & TemplateManifestConfig ,
300
+ ) -> Result < ( ) > {
262
301
if env:: var ( "DEV_DISABLE_SNFORGE_STD_DEPENDENCY" ) . is_err ( ) {
263
- add_dependency ( project_path, "snforge_std" , snforge_version, true ) ?;
302
+ let snforge_version = env ! ( "CARGO_PKG_VERSION" ) ;
303
+ add_dependency (
304
+ scarb_manifest_path,
305
+ & Dependency {
306
+ name : "snforge_std" . to_string ( ) ,
307
+ version : snforge_version. to_string ( ) ,
308
+ dev : true ,
309
+ } ,
310
+ ) ?;
264
311
}
265
312
266
- match template {
267
- Template :: BalanceContract => {
268
- add_dependency ( project_path, "starknet" , & cairo_version. to_string ( ) , false ) ?;
269
- }
270
- Template :: CairoProgram => { }
313
+ for dep in & template_config. dependencies {
314
+ add_dependency ( scarb_manifest_path, dep) ?;
271
315
}
272
316
273
317
Ok ( ( ) )
274
318
}
275
319
276
- fn add_dependency (
277
- project_path : & PathBuf ,
278
- dep_name : & str ,
279
- version : & str ,
280
- is_dev : bool ,
281
- ) -> Result < ( ) > {
282
- let scarb_manifest_path = project_path. join ( "Scarb.toml" ) ;
283
-
320
+ fn add_dependency ( scarb_manifest_path : & PathBuf , dependency : & Dependency ) -> Result < ( ) > {
284
321
let mut cmd = ScarbCommand :: new_with_stdio ( ) ;
322
+ cmd. manifest_path ( scarb_manifest_path) . offline ( ) . arg ( "add" ) ;
285
323
286
- cmd. current_dir ( project_path)
287
- . manifest_path ( scarb_manifest_path. clone ( ) )
288
- . offline ( )
289
- . arg ( "add" ) ;
290
-
291
- if is_dev {
324
+ if dependency. dev {
292
325
cmd. arg ( "--dev" ) ;
293
326
}
294
327
295
- cmd. arg ( format ! ( "{dep_name }@{version}" ) )
328
+ cmd. arg ( format ! ( "{}@{}" , dependency . name , dependency . version ) )
296
329
. run ( )
297
- . context ( format ! ( "Failed to add {dep_name } dependency" ) ) ?;
330
+ . context ( format ! ( "Failed to add {} dependency" , dependency . name ) ) ?;
298
331
299
332
Ok ( ( ) )
300
333
}
0 commit comments