@@ -123,10 +123,33 @@ fn try_main() -> MainResult<i32> {
123
123
124
124
let script_path = std:: env:: current_dir ( ) ?. join ( path) ;
125
125
126
- Input :: File ( script_name, script_path, body)
126
+ let base_path = if let Some ( base_path_arg) = & args. base_path {
127
+ Path :: new ( base_path_arg) . into ( )
128
+ } else {
129
+ script_path
130
+ . parent ( )
131
+ . expect ( "couldn't get parent directory for file input base path" )
132
+ . into ( )
133
+ } ;
134
+
135
+ Input :: File ( script_name, script_path, body, base_path)
136
+ }
137
+ ( expr, true , false ) => {
138
+ let base_path = if let Some ( base_path_arg) = & args. base_path {
139
+ Path :: new ( base_path_arg) . into ( )
140
+ } else {
141
+ std:: env:: current_dir ( ) . expect ( "couldn't get current directory for input base path" )
142
+ } ;
143
+ Input :: Expr ( expr, base_path)
144
+ }
145
+ ( loop_, false , true ) => {
146
+ let base_path = if let Some ( base_path_arg) = & args. base_path {
147
+ Path :: new ( base_path_arg) . into ( )
148
+ } else {
149
+ std:: env:: current_dir ( ) . expect ( "couldn't get current directory for input base path" )
150
+ } ;
151
+ Input :: Loop ( loop_, args. count , base_path)
127
152
}
128
- ( expr, true , false ) => Input :: Expr ( expr) ,
129
- ( loop_, false , true ) => Input :: Loop ( loop_, args. count ) ,
130
153
( _, _, _) => {
131
154
panic ! ( "Internal error: Invalid args" ) ;
132
155
}
@@ -499,14 +522,9 @@ fn decide_action_for(
499
522
500
523
let script_name = format ! ( "{}.rs" , input. safe_name( ) ) ;
501
524
502
- let base_path = match & args. base_path {
503
- Some ( path) => Path :: new ( path) . into ( ) ,
504
- None => input. base_path ( ) ,
505
- } ;
506
-
507
525
let ( mani_str, script_path, script_str) = manifest:: split_input (
508
526
input,
509
- & base_path,
527
+ input . base_path ( ) ,
510
528
& deps,
511
529
& prelude,
512
530
& pkg_path,
@@ -566,23 +584,23 @@ pub enum Input {
566
584
/**
567
585
The input is a script file.
568
586
569
- The tuple members are: the name, absolute path, script contents.
587
+ The tuple members are: the name, absolute path, script contents, base path .
570
588
*/
571
- File ( String , PathBuf , String ) ,
589
+ File ( String , PathBuf , String , PathBuf ) ,
572
590
573
591
/**
574
592
The input is an expression.
575
593
576
- The tuple member is: the script contents.
594
+ The tuple member is: the script contents, base path .
577
595
*/
578
- Expr ( String ) ,
596
+ Expr ( String , PathBuf ) ,
579
597
580
598
/**
581
599
The input is a loop expression.
582
600
583
- The tuple member is: the script contents, whether the `--count` flag was given.
601
+ The tuple member is: the script contents, whether the `--count` flag was given, base path .
584
602
*/
585
- Loop ( String , bool ) ,
603
+ Loop ( String , bool , PathBuf ) ,
586
604
}
587
605
588
606
impl Input {
@@ -593,7 +611,7 @@ impl Input {
593
611
use crate :: Input :: * ;
594
612
595
613
match self {
596
- File ( _, path, _) => Some ( path) ,
614
+ File ( _, path, _, _ ) => Some ( path) ,
597
615
Expr ( ..) => None ,
598
616
Loop ( ..) => None ,
599
617
}
@@ -608,7 +626,7 @@ impl Input {
608
626
use crate :: Input :: * ;
609
627
610
628
match self {
611
- File ( name, _, _) => name,
629
+ File ( name, _, _, _ ) => name,
612
630
Expr ( ..) => "expr" ,
613
631
Loop ( ..) => "loop" ,
614
632
}
@@ -646,15 +664,11 @@ impl Input {
646
664
/**
647
665
Base directory for resolving relative paths.
648
666
*/
649
- pub fn base_path ( & self ) -> PathBuf {
667
+ pub fn base_path ( & self ) -> & PathBuf {
650
668
match self {
651
- Self :: File ( _, path, _) => path
652
- . parent ( )
653
- . expect ( "couldn't get parent directory for file input base path" )
654
- . into ( ) ,
655
- Self :: Expr ( ..) | Self :: Loop ( ..) => {
656
- std:: env:: current_dir ( ) . expect ( "couldn't get current directory for input base path" )
657
- }
669
+ Input :: File ( _, _, _, base_path)
670
+ | Input :: Expr ( _, base_path)
671
+ | Input :: Loop ( _, _, base_path) => base_path,
658
672
}
659
673
}
660
674
@@ -680,7 +694,7 @@ impl Input {
680
694
} ;
681
695
682
696
match self {
683
- File ( _, path, _) => {
697
+ File ( _, path, _, _ ) => {
684
698
let mut hasher = Sha1 :: new ( ) ;
685
699
686
700
// Hash the path to the script.
@@ -692,7 +706,7 @@ impl Input {
692
706
id. push ( & * digest) ;
693
707
id
694
708
}
695
- Expr ( content) => {
709
+ Expr ( content, _ ) => {
696
710
let mut hasher = hash_deps ( ) ;
697
711
698
712
hasher. update ( content) ;
@@ -703,7 +717,7 @@ impl Input {
703
717
id. push ( & * digest) ;
704
718
id
705
719
}
706
- Loop ( content, count) => {
720
+ Loop ( content, count, _ ) => {
707
721
let mut hasher = hash_deps ( ) ;
708
722
709
723
// Make sure to include the [non-]presence of the `--count` flag in the flag, since it changes the actual generated script output.
@@ -757,12 +771,14 @@ fn test_package_name() {
757
771
"Script" . to_string ( ) ,
758
772
Path :: new ( "path" ) . into ( ) ,
759
773
"script" . to_string ( ) ,
774
+ Path :: new ( "path" ) . into ( ) ,
760
775
) ;
761
776
assert_eq ! ( "script" , input. package_name( ) ) ;
762
777
let input = Input :: File (
763
778
"1Script" . to_string ( ) ,
764
779
Path :: new ( "path" ) . into ( ) ,
765
780
"script" . to_string ( ) ,
781
+ Path :: new ( "path" ) . into ( ) ,
766
782
) ;
767
783
assert_eq ! ( "_1script" , input. package_name( ) ) ;
768
784
}
0 commit comments