feat: detect the project entry file

This commit is contained in:
Shunsuke Shibayama 2024-11-14 01:44:52 +09:00
parent 16fef0b04a
commit 6e644dacd9
1 changed files with 32 additions and 1 deletions

View File

@ -1,13 +1,36 @@
use std::env; use std::env;
use std::path::PathBuf; use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
use erg_common::config::{ErgConfig, ErgMode}; use erg_common::config::{ErgConfig, ErgMode};
use erg_common::io::Input; use erg_common::io::Input;
use erg_common::pathutil::project_entry_file_of;
use erg_common::switch_lang; use erg_common::switch_lang;
use crate::copy::clear_cache; use crate::copy::clear_cache;
fn entry_file() -> Option<PathBuf> {
project_entry_file_of(&env::current_dir().ok()?).or_else(|| {
let mut opt_path = None;
for ent in Path::new(".").read_dir().ok()? {
let ent = ent.ok()?;
if ent.file_type().ok()?.is_file() {
let path = ent.path();
if path.file_name().is_some_and(|name| name == "__init__.py") {
return Some(path);
} else if path.extension().map_or(false, |ext| ext == "py") {
if opt_path.is_some() {
return None;
} else {
opt_path = Some(path);
}
}
}
}
opt_path
})
}
fn command_message() -> &'static str { fn command_message() -> &'static str {
switch_lang!( switch_lang!(
"japanese" => "japanese" =>
@ -174,6 +197,14 @@ For more information try `pylyzer --help`"
} }
} }
} }
if cfg.input.is_repl() {
if let Some(entry) = entry_file() {
cfg.input = Input::file(entry);
} else {
eprintln!("No entry file found in the current project");
std::process::exit(1);
}
}
cfg.runtime_args = runtime_args.into(); cfg.runtime_args = runtime_args.into();
cfg cfg
} }