...
This commit is contained in:
@@ -14,17 +14,17 @@ use std::process;
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `script_path` - Path to the directory containing Rhai scripts
|
||||
/// * `script_path` - Path to a Rhai script file or directory containing Rhai scripts
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Result indicating success or failure
|
||||
pub fn run(script_path: &str) -> Result<(), Box<dyn Error>> {
|
||||
let script_dir = Path::new(script_path);
|
||||
let path = Path::new(script_path);
|
||||
|
||||
// Check if the directory exists
|
||||
if !script_dir.exists() || !script_dir.is_dir() {
|
||||
eprintln!("Error: '{}' is not a valid directory", script_path);
|
||||
// Check if the path exists
|
||||
if !path.exists() {
|
||||
eprintln!("Error: '{}' does not exist", script_path);
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
@@ -37,25 +37,57 @@ pub fn run(script_path: &str) -> Result<(), Box<dyn Error>> {
|
||||
// Register all SAL modules with the engine
|
||||
crate::rhai::register(&mut engine)?;
|
||||
|
||||
// Find all .rhai files in the directory
|
||||
let mut script_files: Vec<PathBuf> = fs::read_dir(script_dir)?
|
||||
.filter_map(Result::ok)
|
||||
.filter(|entry| {
|
||||
entry.path().is_file() &&
|
||||
entry.path().extension().map_or(false, |ext| ext == "rhai")
|
||||
})
|
||||
.map(|entry| entry.path())
|
||||
.collect();
|
||||
// Determine if the path is a file or directory
|
||||
let script_files: Vec<PathBuf> = if path.is_file() {
|
||||
// Check if it's a .rhai file
|
||||
if path.extension().map_or(false, |ext| ext == "rhai") {
|
||||
vec![path.to_path_buf()]
|
||||
} else {
|
||||
eprintln!("Error: '{}' is not a Rhai script file", script_path);
|
||||
process::exit(1);
|
||||
}
|
||||
} else if path.is_dir() {
|
||||
// Find all .rhai files in the directory recursively
|
||||
let mut files: Vec<PathBuf> = Vec::new();
|
||||
|
||||
// Helper function to recursively find .rhai files
|
||||
fn find_rhai_files(dir: &Path, files: &mut Vec<PathBuf>) -> std::io::Result<()> {
|
||||
if dir.is_dir() {
|
||||
for entry in fs::read_dir(dir)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
|
||||
if path.is_dir() {
|
||||
find_rhai_files(&path, files)?;
|
||||
} else if path.is_file() &&
|
||||
path.extension().map_or(false, |ext| ext == "rhai") {
|
||||
files.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Find all .rhai files recursively
|
||||
find_rhai_files(path, &mut files)?;
|
||||
|
||||
// Sort the script files by name
|
||||
files.sort();
|
||||
|
||||
if files.is_empty() {
|
||||
println!("No Rhai scripts found in '{}'", script_path);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
files
|
||||
} else {
|
||||
eprintln!("Error: '{}' is neither a file nor a directory", script_path);
|
||||
process::exit(1);
|
||||
};
|
||||
|
||||
// Sort the script files by name
|
||||
script_files.sort();
|
||||
|
||||
if script_files.is_empty() {
|
||||
println!("No Rhai scripts found in '{}'", script_path);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
println!("Found {} Rhai scripts to execute:", script_files.len());
|
||||
println!("Found {} Rhai script{} to execute:",
|
||||
script_files.len(),
|
||||
if script_files.len() == 1 { "" } else { "s" });
|
||||
|
||||
// Execute each script in sorted order
|
||||
for script_file in script_files {
|
||||
@@ -74,7 +106,8 @@ pub fn run(script_path: &str) -> Result<(), Box<dyn Error>> {
|
||||
},
|
||||
Err(err) => {
|
||||
eprintln!("Error executing script: {}", err);
|
||||
// Continue with the next script instead of stopping
|
||||
// Exit with error code when a script fails
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user