@@ -123,10 +123,33 @@ fn try_main() -> MainResult<i32> {
123123
124124 let script_path = std:: env:: current_dir ( ) ?. join ( path) ;
125125
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)
127152 }
128- ( expr, true , false ) => Input :: Expr ( expr) ,
129- ( loop_, false , true ) => Input :: Loop ( loop_, args. count ) ,
130153 ( _, _, _) => {
131154 panic ! ( "Internal error: Invalid args" ) ;
132155 }
@@ -499,14 +522,9 @@ fn decide_action_for(
499522
500523 let script_name = format ! ( "{}.rs" , input. safe_name( ) ) ;
501524
502- let base_path = match & args. base_path {
503- Some ( path) => Path :: new ( path) . into ( ) ,
504- None => input. base_path ( ) ,
505- } ;
506-
507525 let ( mani_str, script_path, script_str) = manifest:: split_input (
508526 input,
509- & base_path,
527+ input . base_path ( ) ,
510528 & deps,
511529 & prelude,
512530 & pkg_path,
@@ -566,23 +584,23 @@ pub enum Input {
566584 /**
567585 The input is a script file.
568586
569- The tuple members are: the name, absolute path, script contents.
587+ The tuple members are: the name, absolute path, script contents, base path .
570588 */
571- File ( String , PathBuf , String ) ,
589+ File ( String , PathBuf , String , PathBuf ) ,
572590
573591 /**
574592 The input is an expression.
575593
576- The tuple member is: the script contents.
594+ The tuple member is: the script contents, base path .
577595 */
578- Expr ( String ) ,
596+ Expr ( String , PathBuf ) ,
579597
580598 /**
581599 The input is a loop expression.
582600
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 .
584602 */
585- Loop ( String , bool ) ,
603+ Loop ( String , bool , PathBuf ) ,
586604}
587605
588606impl Input {
@@ -593,7 +611,7 @@ impl Input {
593611 use crate :: Input :: * ;
594612
595613 match self {
596- File ( _, path, _) => Some ( path) ,
614+ File ( _, path, _, _ ) => Some ( path) ,
597615 Expr ( ..) => None ,
598616 Loop ( ..) => None ,
599617 }
@@ -608,7 +626,7 @@ impl Input {
608626 use crate :: Input :: * ;
609627
610628 match self {
611- File ( name, _, _) => name,
629+ File ( name, _, _, _ ) => name,
612630 Expr ( ..) => "expr" ,
613631 Loop ( ..) => "loop" ,
614632 }
@@ -646,15 +664,11 @@ impl Input {
646664 /**
647665 Base directory for resolving relative paths.
648666 */
649- pub fn base_path ( & self ) -> PathBuf {
667+ pub fn base_path ( & self ) -> & PathBuf {
650668 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,
658672 }
659673 }
660674
@@ -680,7 +694,7 @@ impl Input {
680694 } ;
681695
682696 match self {
683- File ( _, path, _) => {
697+ File ( _, path, _, _ ) => {
684698 let mut hasher = Sha1 :: new ( ) ;
685699
686700 // Hash the path to the script.
@@ -692,7 +706,7 @@ impl Input {
692706 id. push ( & * digest) ;
693707 id
694708 }
695- Expr ( content) => {
709+ Expr ( content, _ ) => {
696710 let mut hasher = hash_deps ( ) ;
697711
698712 hasher. update ( content) ;
@@ -703,7 +717,7 @@ impl Input {
703717 id. push ( & * digest) ;
704718 id
705719 }
706- Loop ( content, count) => {
720+ Loop ( content, count, _ ) => {
707721 let mut hasher = hash_deps ( ) ;
708722
709723 // 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() {
757771 "Script" . to_string ( ) ,
758772 Path :: new ( "path" ) . into ( ) ,
759773 "script" . to_string ( ) ,
774+ Path :: new ( "path" ) . into ( ) ,
760775 ) ;
761776 assert_eq ! ( "script" , input. package_name( ) ) ;
762777 let input = Input :: File (
763778 "1Script" . to_string ( ) ,
764779 Path :: new ( "path" ) . into ( ) ,
765780 "script" . to_string ( ) ,
781+ Path :: new ( "path" ) . into ( ) ,
766782 ) ;
767783 assert_eq ! ( "_1script" , input. package_name( ) ) ;
768784}
0 commit comments