@@ -6,6 +6,7 @@ use crate::utils::{
6
6
7
7
use std:: collections:: HashMap ;
8
8
use std:: ffi:: OsStr ;
9
+ use std:: path:: PathBuf ;
9
10
10
11
fn args ( ) -> Result < Option < Vec < String > > , String > {
11
12
// We skip the binary and the "cargo" option.
@@ -42,18 +43,31 @@ pub fn run() -> Result<(), String> {
42
43
// We first need to go to the original location to ensure that the config setup will go as
43
44
// expected.
44
45
let current_dir = std:: env:: current_dir ( )
46
+ . and_then ( |path| path. canonicalize ( ) )
45
47
. map_err ( |error| format ! ( "Failed to get current directory path: {:?}" , error) ) ?;
46
48
let current_exe = std:: env:: current_exe ( )
49
+ . and_then ( |path| path. canonicalize ( ) )
47
50
. map_err ( |error| format ! ( "Failed to get current exe path: {:?}" , error) ) ?;
48
- let parent_dir = match current_exe. parent ( ) {
49
- Some ( parent) => parent,
50
- None => {
51
+ let mut parent_dir = current_exe
52
+ . components ( )
53
+ . map ( |comp| comp. as_os_str ( ) )
54
+ . collect :: < Vec < _ > > ( ) ;
55
+ // We run this script from "build_system/target/release/y", so we need to remove these elements.
56
+ for to_remove in & [ "y" , "release" , "target" , "build_system" ] {
57
+ if parent_dir
58
+ . last ( )
59
+ . map ( |part| part == to_remove)
60
+ . unwrap_or ( false )
61
+ {
62
+ parent_dir. pop ( ) ;
63
+ } else {
51
64
return Err ( format ! (
52
- "Cannot get parent of current executable path `{}` " ,
53
- current_exe. display( )
65
+ "Build script not executed from `build_system/target/release/y` (in path {}) " ,
66
+ current_exe. display( ) ,
54
67
) ) ;
55
68
}
56
- } ;
69
+ }
70
+ let parent_dir = PathBuf :: from ( parent_dir. join ( & OsStr :: new ( "/" ) ) ) ;
57
71
std:: env:: set_current_dir ( & parent_dir) . map_err ( |error| {
58
72
format ! (
59
73
"Failed to go to `{}` folder: {:?}" ,
0 commit comments