@@ -6,6 +6,7 @@ use regex;
6
6
use self :: regex:: Regex ;
7
7
use std:: collections:: HashMap ;
8
8
use std:: path:: Path ;
9
+ use std:: path:: PathBuf ;
9
10
10
11
use crate :: consts;
11
12
use crate :: error:: { MainError , MainResult } ;
@@ -22,48 +23,47 @@ pub fn split_input(
22
23
input : & Input ,
23
24
deps : & [ ( String , String ) ] ,
24
25
prelude_items : & [ String ] ,
26
+ package_path : impl AsRef < Path > ,
25
27
bin_name : & str ,
26
28
script_name : & str ,
27
29
toolchain : Option < String > ,
28
- ) -> MainResult < ( String , String ) > {
30
+ ) -> MainResult < ( String , PathBuf , Option < String > ) > {
29
31
fn contains_main_method ( source : & str ) -> bool {
30
32
let re_shebang: Regex =
31
33
Regex :: new ( r#"(?m)^ *(pub )?(async )?(extern "C" )?fn main *\("# ) . unwrap ( ) ;
32
34
re_shebang. is_match ( source)
33
35
}
34
36
35
- let ( part_mani, source, template, sub_prelude) = match input {
36
- Input :: File ( _, _, content) => {
37
+ let source_in_package = package_path. as_ref ( ) . join ( script_name) ;
38
+ let ( part_mani, source_path, source, template, sub_prelude) = match input {
39
+ Input :: File ( _, path, content) => {
37
40
assert_eq ! ( prelude_items. len( ) , 0 ) ;
38
- let ( content, shebang_used ) = strip_shebang ( content) ;
41
+ let content = strip_shebang ( content) ;
39
42
let ( manifest, source) =
40
43
find_embedded_manifest ( content) . unwrap_or ( ( Manifest :: Toml ( "" ) , content) ) ;
41
44
42
- let source = if contains_main_method ( source) {
43
- if shebang_used {
44
- format ! ( "//\n {}" , source)
45
- } else {
46
- source. to_string ( )
47
- }
45
+ if contains_main_method ( content) {
46
+ ( manifest, path. clone ( ) , source. to_string ( ) , None , false )
48
47
} else {
49
- format ! ( "fn main() -> Result<(), Box<dyn std::error::Error+Sync+Send>> {{ {{\n {} }}\n Ok(())\n }}" , source)
50
- } ;
51
- ( manifest, source, consts:: FILE_TEMPLATE , false )
48
+ ( manifest, source_in_package, content. to_string ( ) , Some ( consts:: FILE_NO_MAIN_TEMPLATE ) , false )
49
+ }
52
50
}
53
51
Input :: Expr ( content) => (
54
52
Manifest :: Toml ( "" ) ,
53
+ source_in_package,
55
54
content. to_string ( ) ,
56
- consts:: EXPR_TEMPLATE ,
55
+ Some ( consts:: EXPR_TEMPLATE ) ,
57
56
true ,
58
57
) ,
59
58
Input :: Loop ( content, count) => (
60
59
Manifest :: Toml ( "" ) ,
60
+ source_in_package,
61
61
content. to_string ( ) ,
62
- if * count {
62
+ Some ( if * count {
63
63
consts:: LOOP_COUNT_TEMPLATE
64
64
} else {
65
65
consts:: LOOP_TEMPLATE
66
- } ,
66
+ } ) ,
67
67
true ,
68
68
) ,
69
69
} ;
@@ -83,13 +83,15 @@ pub fn split_input(
83
83
subs. insert ( consts:: SCRIPT_PRELUDE_SUB , & prelude_str[ ..] ) ;
84
84
}
85
85
86
- let source = templates:: expand ( template, & subs) ?;
86
+ let source = template . map ( |template| templates:: expand ( template, & subs) ) . transpose ( ) ?;
87
87
let part_mani = part_mani. into_toml ( ) ?;
88
88
info ! ( "part_mani: {:?}" , part_mani) ;
89
89
info ! ( "source: {:?}" , source) ;
90
90
91
+ let source_path_str = source_path. to_str ( ) . ok_or_else ( || format ! ( "Unable to stringify {source_path:?}" ) ) ?;
92
+
91
93
// It's-a mergin' time!
92
- let def_mani = default_manifest ( input, bin_name, script_name , toolchain) ;
94
+ let def_mani = default_manifest ( input, bin_name, source_path_str , toolchain) ;
93
95
let dep_mani = deps_manifest ( deps) ?;
94
96
95
97
let mani = merge_manifest ( def_mani, part_mani) ?;
@@ -101,7 +103,7 @@ pub fn split_input(
101
103
let mani_str = format ! ( "{}" , mani) ;
102
104
info ! ( "manifest: {}" , mani_str) ;
103
105
104
- Ok ( ( mani_str, source) )
106
+ Ok ( ( mani_str, source_path , source) )
105
107
}
106
108
107
109
#[ cfg( test) ]
@@ -118,7 +120,7 @@ fn test_split_input() {
118
120
let toolchain = None ;
119
121
macro_rules! si {
120
122
( $i: expr) => {
121
- split_input( & $i, & [ ] , & [ ] , & bin_name, & script_name, toolchain. clone( ) ) . ok( )
123
+ split_input( & $i, & [ ] , & [ ] , "" , & bin_name, & script_name, toolchain. clone( ) ) . ok( )
122
124
} ;
123
125
}
124
126
@@ -129,7 +131,7 @@ fn test_split_input() {
129
131
130
132
macro_rules! r {
131
133
( $m: expr, $r: expr) => {
132
- Some ( ( $m. into( ) , $r. into( ) ) )
134
+ Some ( ( $m. into( ) , "main.rs" . into ( ) , $r. into( ) ) )
133
135
} ;
134
136
}
135
137
@@ -151,7 +153,7 @@ name = "n"
151
153
version = "0.1.0""# ,
152
154
STRIP_SECTION
153
155
) ,
154
- r#"fn main() {}"#
156
+ None
155
157
)
156
158
) ;
157
159
@@ -174,8 +176,7 @@ name = "n"
174
176
version = "0.1.0""# ,
175
177
STRIP_SECTION
176
178
) ,
177
- r#"//
178
- fn main() {}"#
179
+ None
179
180
)
180
181
) ;
181
182
@@ -198,8 +199,7 @@ name = "n"
198
199
version = "0.1.0""# ,
199
200
STRIP_SECTION
200
201
) ,
201
- r#"#[thingy]
202
- fn main() {}"#
202
+ None
203
203
)
204
204
) ;
205
205
@@ -208,6 +208,7 @@ fn main() {}"#
208
208
& f( r#"fn main() {}"# ) ,
209
209
& [ ] ,
210
210
& [ ] ,
211
+ "" ,
211
212
& bin_name,
212
213
"main.rs" ,
213
214
Some ( "stable" . to_string( ) )
@@ -232,7 +233,7 @@ version = "0.1.0"
232
233
toolchain = "stable""# ,
233
234
STRIP_SECTION
234
235
) ,
235
- r#"fn main() {}"#
236
+ None
236
237
)
237
238
) ;
238
239
@@ -258,10 +259,7 @@ name = "n"
258
259
version = "0.1.0""# ,
259
260
STRIP_SECTION
260
261
) ,
261
- r#"
262
- ---
263
- fn main() {}
264
- "#
262
+ None
265
263
)
266
264
) ;
267
265
@@ -287,11 +285,7 @@ name = "n"
287
285
version = "0.1.0""# ,
288
286
STRIP_SECTION
289
287
) ,
290
- r#"[dependencies]
291
- time="0.1.25"
292
- ---
293
- fn main() {}
294
- "#
288
+ None
295
289
)
296
290
) ;
297
291
@@ -317,10 +311,7 @@ name = "n"
317
311
version = "0.1.0""# ,
318
312
STRIP_SECTION
319
313
) ,
320
- r#"
321
- // Cargo-Deps: time="0.1.25"
322
- fn main() {}
323
- "#
314
+ None
324
315
)
325
316
) ;
326
317
@@ -347,10 +338,7 @@ name = "n"
347
338
version = "0.1.0""# ,
348
339
STRIP_SECTION
349
340
) ,
350
- r#"
351
- // Cargo-Deps: time="0.1.25", libc="0.2.5"
352
- fn main() {}
353
- "#
341
+ None
354
342
)
355
343
) ;
356
344
@@ -383,29 +371,19 @@ name = "n"
383
371
version = "0.1.0""# ,
384
372
STRIP_SECTION
385
373
) ,
386
- r#"
387
- /*!
388
- Here is a manifest:
389
-
390
- ```cargo
391
- [dependencies]
392
- time = "0.1.25"
393
- ```
394
- */
395
- fn main() {}
396
- "#
374
+ None
397
375
)
398
376
) ;
399
377
}
400
378
401
379
/**
402
380
Returns a slice of the input string with the leading shebang, if there is one, omitted.
403
381
*/
404
- fn strip_shebang ( s : & str ) -> ( & str , bool ) {
382
+ fn strip_shebang ( s : & str ) -> & str {
405
383
let re_shebang: Regex = Regex :: new ( r"^#![^\[].*?(\r\n|\n)" ) . unwrap ( ) ;
406
384
match re_shebang. find ( s) {
407
- Some ( m) => ( & s[ m. end ( ) ..] , true ) ,
408
- None => ( s , false ) ,
385
+ Some ( m) => & s[ m. end ( ) ..] ,
386
+ None => s ,
409
387
}
410
388
}
411
389
@@ -1096,7 +1074,7 @@ Generates a default Cargo manifest for the given input.
1096
1074
fn default_manifest (
1097
1075
input : & Input ,
1098
1076
bin_name : & str ,
1099
- script_name : & str ,
1077
+ bin_source_path : & str ,
1100
1078
toolchain : Option < String > ,
1101
1079
) -> toml:: value:: Table {
1102
1080
let mut package_map = toml:: map:: Map :: new ( ) ;
@@ -1144,9 +1122,10 @@ fn default_manifest(
1144
1122
"name" . to_string ( ) ,
1145
1123
toml:: value:: Value :: String ( bin_name. to_string ( ) ) ,
1146
1124
) ;
1125
+
1147
1126
bin_map. insert (
1148
1127
"path" . to_string ( ) ,
1149
- toml:: value:: Value :: String ( script_name . to_string ( ) ) ,
1128
+ toml:: value:: Value :: String ( bin_source_path . to_string ( ) ) ,
1150
1129
) ;
1151
1130
1152
1131
let mut mani_map = toml:: map:: Map :: new ( ) ;
0 commit comments