1
- use std:: { env, fs, path:: PathBuf } ;
1
+ use std:: { env:: current_dir , fs, path:: PathBuf } ;
2
2
3
3
use anyhow:: Context ;
4
4
use cairo_lang_test_plugin:: TestCompilation ;
@@ -10,19 +10,25 @@ use scarb::{
10
10
ops:: { self , collect_metadata, CompileOpts , MetadataOptions } ,
11
11
} ;
12
12
13
- pub fn prepare_crate_for_exercise ( file_path : & PathBuf , crate_path : PathBuf ) {
14
- let lib_path = crate_path. join ( "src/lib.cairo" ) ;
15
- match fs:: copy ( file_path, lib_path) {
13
+ pub fn prepare_crate_for_exercise ( file_path : & PathBuf ) -> PathBuf {
14
+ let crate_path = current_dir ( ) . unwrap ( ) . join ( PathBuf :: from ( "runner-crate" ) ) ;
15
+ let src_dir = crate_path. join ( "src" ) ;
16
+ if !src_dir. exists ( ) {
17
+ let _ = fs:: create_dir ( & src_dir) ;
18
+ }
19
+ let lib_path = src_dir. join ( "lib.cairo" ) ;
20
+ let file_path = current_dir ( ) . unwrap ( ) . join ( file_path) ;
21
+
22
+ match fs:: copy ( & file_path, & lib_path) {
16
23
Ok ( _) => { }
17
- Err ( err) => panic ! ( "Error occurred while preparing the exercise: \n {err:?}" ) ,
24
+ Err ( err) => panic ! ( "Error occurred while preparing the exercise, \n Exercise: {file_path:?} \n Lib path: {lib_path:?} \n {err:?}" ) ,
18
25
} ;
26
+ crate_path
19
27
}
20
28
21
29
pub fn scarb_build ( file_path : & PathBuf ) -> anyhow:: Result < String > {
22
- let path = env:: current_dir ( ) . unwrap ( ) ;
23
- let ( config, crate_path) = scarb_config ( path. join ( PathBuf :: from ( "runner-crate" ) ) ) ;
24
-
25
- prepare_crate_for_exercise ( file_path, crate_path) ;
30
+ let crate_path = prepare_crate_for_exercise ( file_path) ;
31
+ let config = scarb_config ( crate_path) ;
26
32
27
33
match compile ( & config, false ) {
28
34
Ok ( _) => Ok ( "" . into ( ) ) ,
@@ -31,10 +37,8 @@ pub fn scarb_build(file_path: &PathBuf) -> anyhow::Result<String> {
31
37
}
32
38
33
39
pub fn scarb_test ( file_path : & PathBuf ) -> anyhow:: Result < String > {
34
- let path = env:: current_dir ( ) . unwrap ( ) ;
35
- let ( config, crate_path) = scarb_config ( path. join ( PathBuf :: from ( "runner-crate" ) ) ) ;
36
-
37
- prepare_crate_for_exercise ( file_path, crate_path) ;
40
+ let crate_path = prepare_crate_for_exercise ( file_path) ;
41
+ let config = scarb_config ( crate_path) ;
38
42
39
43
let ws = ops:: read_workspace ( config. manifest_path ( ) , & config) ?;
40
44
@@ -49,7 +53,7 @@ pub fn scarb_test(file_path: &PathBuf) -> anyhow::Result<String> {
49
53
)
50
54
. unwrap ( ) ;
51
55
52
- let profile = env :: var ( "SCARB_PROFILE" ) . unwrap_or ( " dev". into ( ) ) ;
56
+ let profile = " dev";
53
57
let default_target_dir = metadata. runtime_manifest . join ( "target" ) ;
54
58
55
59
let target_dir = metadata
@@ -88,12 +92,12 @@ pub fn scarb_test(file_path: &PathBuf) -> anyhow::Result<String> {
88
92
anyhow:: Ok ( "" . into ( ) )
89
93
}
90
94
91
- pub fn scarb_config ( crate_path : PathBuf ) -> ( Config , PathBuf ) {
95
+ pub fn scarb_config ( crate_path : PathBuf ) -> Config {
92
96
let path = Utf8PathBuf :: from_path_buf ( crate_path. join ( PathBuf :: from ( "Scarb.toml" ) ) ) . unwrap ( ) ;
93
97
94
98
let config = Config :: builder ( path) . build ( ) . unwrap ( ) ;
95
99
96
- ( config, crate_path )
100
+ config
97
101
}
98
102
99
103
pub fn compile ( config : & Config , test_targets : bool ) -> anyhow:: Result < ( ) > {
0 commit comments