Skip to content

Commit b98676b

Browse files
refactor!: don't modify args sent to godot
1 parent e49d405 commit b98676b

File tree

2 files changed

+53
-40
lines changed

2 files changed

+53
-40
lines changed

src/godot_manager.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -521,16 +521,6 @@ impl<'a> GodotManager<'a> {
521521
}
522522
}
523523

524-
// If no args pointing to files are provided, but the current directory includes a
525-
// project.godot file, add it to the args. Otherwise, Godot may error out with a
526-
// "No main scene set" message.
527-
let current_dir = std::env::current_dir()?;
528-
let project_file = current_dir.join("project.godot");
529-
let mut godot_args = godot_args.to_vec();
530-
if project_file.exists() && godot_args.iter().all(|arg| !Path::new(arg).exists()) {
531-
godot_args.push(project_file.to_string_lossy().to_string());
532-
}
533-
534524
if console {
535525
// Run the process attached to the terminal and wait for it to exit
536526
std::process::Command::new(&path)
@@ -921,10 +911,13 @@ impl<'a> GodotManager<'a> {
921911
}
922912

923913
/// Try to determine the version to use based on the current Godot project
924-
pub fn determine_version(&self) -> Option<GodotVersion> {
925-
let current_dir = std::env::current_dir().ok()?;
914+
pub fn determine_version<P: AsRef<Path>>(&self, path: Option<P>) -> Option<GodotVersion> {
915+
let current_dir = match path {
916+
Some(p) => p.as_ref().to_path_buf(),
917+
None => std::env::current_dir().ok()?,
918+
};
926919

927-
project_version_detector::detect_godot_version_in_path(self.i18n, current_dir)
920+
project_version_detector::detect_godot_version_in_path(self.i18n, &current_dir)
928921
}
929922

930923
/// Pin a version to .gdvmrc in the current directory

src/main.rs

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use anyhow::{anyhow, Result};
99
use clap::{value_parser, Arg, ArgMatches, Command};
1010
use godot_manager::{GodotManager, InstallOutcome};
1111
use i18n::I18n;
12-
use std::io::{self, Write};
12+
use std::{
13+
io::{self, Write},
14+
path::Path,
15+
};
1316

1417
use version_utils::GodotVersion;
1518

@@ -36,26 +39,6 @@ fn main() -> Result<()> {
3639
// Pass all arguments to Godot
3740
let args: Vec<String> = std::env::args().skip(1).collect();
3841

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-
5942
if let Err(err) = sub_run_inner(RunConfig {
6043
i18n: &i18n,
6144
manager: &GodotManager::new(&i18n)?,
@@ -403,12 +386,37 @@ fn sub_run_inner(config: RunConfig) -> Result<()> {
403386
force_on_mismatch,
404387
} = config;
405388

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+
406408
let resolved_version = if let Some(v) = version_input {
407409
let mut requested_version = GodotVersion::from_match_str(v)?;
408410

409411
requested_version.is_csharp = Some(csharp_flag);
410412

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+
) {
412420
if force_on_mismatch {
413421
eprintln_i18n!(
414422
i18n,
@@ -428,7 +436,7 @@ fn sub_run_inner(config: RunConfig) -> Result<()> {
428436

429437
manager.auto_install_version(&requested_version)?
430438
} 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) {
432440
if force_on_mismatch {
433441
eprintln_i18n!(
434442
i18n,
@@ -447,7 +455,10 @@ fn sub_run_inner(config: RunConfig) -> Result<()> {
447455
}
448456

449457
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+
{
451462
eprintln_i18n!(
452463
i18n,
453464
"warning-using-project-version",
@@ -476,13 +487,22 @@ fn sub_run_inner(config: RunConfig) -> Result<()> {
476487
}
477488

478489
/// 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>>(
480491
i18n: &I18n,
481492
manager: &GodotManager,
482493
requested: &GodotVersion,
483494
is_pin: bool,
495+
paths: Option<&[P]>,
484496
) -> 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 {
486506
// Check if they don't match (project versions at most specify major.minor or
487507
// major.minor.patch, and if .patch is not specified, it's assumed to allow any patch)
488508
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<
642662

643663
version.is_csharp = Some(csharp);
644664

645-
warn_project_version_mismatch(i18n, manager, &version, true);
665+
warn_project_version_mismatch::<&Path>(i18n, manager, &version, true, None);
646666

647667
let resolved_version = manager.auto_install_version(&version)?;
648668

0 commit comments

Comments
 (0)