@@ -9,7 +9,10 @@ use anyhow::{anyhow, Result};
9
9
use clap:: { value_parser, Arg , ArgMatches , Command } ;
10
10
use godot_manager:: { GodotManager , InstallOutcome } ;
11
11
use i18n:: I18n ;
12
- use std:: io:: { self , Write } ;
12
+ use std:: {
13
+ io:: { self , Write } ,
14
+ path:: Path ,
15
+ } ;
13
16
14
17
use version_utils:: GodotVersion ;
15
18
@@ -36,26 +39,6 @@ fn main() -> Result<()> {
36
39
// Pass all arguments to Godot
37
40
let args: Vec < String > = std:: env:: args ( ) . skip ( 1 ) . collect ( ) ;
38
41
39
- // Search for the first argument that is a valid file and change to its directory
40
- // This is to make sure that we are using the working directory of the project when checking
41
- // for the Godot version to run
42
- if let Some ( file) = args. iter ( ) . find ( |arg| std:: path:: Path :: new ( arg) . exists ( ) ) {
43
- let file_path = std:: path:: Path :: new ( file) ;
44
-
45
- // Resolve to absolute path
46
- let abs_path: std:: path:: PathBuf = if file_path. is_absolute ( ) {
47
- file_path. to_path_buf ( )
48
- } else {
49
- std:: env:: current_dir ( ) ?. join ( file_path)
50
- } ;
51
-
52
- // Get the parent directory of the file
53
- if let Some ( file_dir) = abs_path. parent ( ) {
54
- // Change the current working directory to the file's directory
55
- std:: env:: set_current_dir ( file_dir) ?;
56
- }
57
- }
58
-
59
42
if let Err ( err) = sub_run_inner ( RunConfig {
60
43
i18n : & i18n,
61
44
manager : & GodotManager :: new ( & i18n) ?,
@@ -403,12 +386,37 @@ fn sub_run_inner(config: RunConfig) -> Result<()> {
403
386
force_on_mismatch,
404
387
} = config;
405
388
389
+ // Try to see if a path was given in raw_args. First, by checking if the --path flag was given
390
+ // and then by checking if the first argument is a path. Prefer the --path flag if both are
391
+ // given.
392
+ let mut possible_paths: Vec < & str > = Vec :: new ( ) ;
393
+ for arg in raw_args. iter ( ) {
394
+ if arg == "--path" {
395
+ if let Some ( p) = raw_args. get ( raw_args. iter ( ) . position ( |x| x == "--path" ) . unwrap ( ) + 1 )
396
+ {
397
+ possible_paths. clear ( ) ;
398
+ possible_paths. push ( p) ;
399
+ break ;
400
+ }
401
+ } else if arg. starts_with ( '-' ) {
402
+ continue ;
403
+ } else {
404
+ possible_paths. push ( arg) ;
405
+ }
406
+ }
407
+
406
408
let resolved_version = if let Some ( v) = version_input {
407
409
let mut requested_version = GodotVersion :: from_match_str ( v) ?;
408
410
409
411
requested_version. is_csharp = Some ( csharp_flag) ;
410
412
411
- if warn_project_version_mismatch ( i18n, manager, & requested_version, false ) {
413
+ if warn_project_version_mismatch (
414
+ i18n,
415
+ manager,
416
+ & requested_version,
417
+ false ,
418
+ Some ( & possible_paths) ,
419
+ ) {
412
420
if force_on_mismatch {
413
421
eprintln_i18n ! (
414
422
i18n,
@@ -428,7 +436,7 @@ fn sub_run_inner(config: RunConfig) -> Result<()> {
428
436
429
437
manager. auto_install_version ( & requested_version) ?
430
438
} else if let Some ( pinned) = manager. get_pinned_version ( ) {
431
- if warn_project_version_mismatch ( i18n, manager, & pinned, true ) {
439
+ if warn_project_version_mismatch :: < & Path > ( i18n, manager, & pinned, true , None ) {
432
440
if force_on_mismatch {
433
441
eprintln_i18n ! (
434
442
i18n,
@@ -447,7 +455,10 @@ fn sub_run_inner(config: RunConfig) -> Result<()> {
447
455
}
448
456
449
457
manager. auto_install_version ( & pinned) ?
450
- } else if let Some ( project_version) = manager. determine_version ( ) {
458
+ } else if let Some ( project_version) = possible_paths
459
+ . iter ( )
460
+ . find_map ( |p| manager. determine_version ( Some ( p) ) )
461
+ {
451
462
eprintln_i18n ! (
452
463
i18n,
453
464
"warning-using-project-version" ,
@@ -476,13 +487,22 @@ fn sub_run_inner(config: RunConfig) -> Result<()> {
476
487
}
477
488
478
489
/// Show a warning if the project version is different from the pinned version
479
- fn warn_project_version_mismatch (
490
+ fn warn_project_version_mismatch < P : AsRef < Path > > (
480
491
i18n : & I18n ,
481
492
manager : & GodotManager ,
482
493
requested : & GodotVersion ,
483
494
is_pin : bool ,
495
+ paths : Option < & [ P ] > ,
484
496
) -> bool {
485
- if let Some ( project_version) = manager. determine_version ( ) {
497
+ let determined_version = if let Some ( paths) = paths {
498
+ paths
499
+ . iter ( )
500
+ . find_map ( |p| manager. determine_version ( Some ( p) ) )
501
+ } else {
502
+ manager. determine_version :: < P > ( None )
503
+ } ;
504
+
505
+ if let Some ( project_version) = determined_version {
486
506
// Check if they don't match (project versions at most specify major.minor or
487
507
// major.minor.patch, and if .patch is not specified, it's assumed to allow any patch)
488
508
if project_version. major . is_some ( ) && requested. major . is_some ( ) && project_version. major != requested. major // Check major if both are Some
@@ -642,7 +662,7 @@ fn sub_pin(i18n: &I18n, manager: &GodotManager, matches: &ArgMatches) -> Result<
642
662
643
663
version. is_csharp = Some ( csharp) ;
644
664
645
- warn_project_version_mismatch ( i18n, manager, & version, true ) ;
665
+ warn_project_version_mismatch :: < & Path > ( i18n, manager, & version, true , None ) ;
646
666
647
667
let resolved_version = manager. auto_install_version ( & version) ?;
648
668
0 commit comments