...
This commit is contained in:
@@ -16,7 +16,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.short("p")
|
||||
.long("path")
|
||||
.value_name("PATH")
|
||||
.help("Path to directory containing Rhai scripts")
|
||||
.help("Path to a Rhai script file or directory containing Rhai scripts")
|
||||
.required(true)
|
||||
.takes_value(true),
|
||||
)
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
41
src/os/fs.rs
41
src/os/fs.rs
@@ -20,6 +20,7 @@ pub enum FsError {
|
||||
NotAFile(String),
|
||||
UnknownFileType(String),
|
||||
MetadataError(io::Error),
|
||||
ChangeDirFailed(io::Error),
|
||||
}
|
||||
|
||||
// Implement Display for FsError
|
||||
@@ -38,6 +39,7 @@ impl fmt::Display for FsError {
|
||||
FsError::NotAFile(path) => write!(f, "Path '{}' is not a regular file", path),
|
||||
FsError::UnknownFileType(path) => write!(f, "Unknown file type at '{}'", path),
|
||||
FsError::MetadataError(e) => write!(f, "Failed to get file metadata: {}", e),
|
||||
FsError::ChangeDirFailed(e) => write!(f, "Failed to change directory: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,6 +54,7 @@ impl Error for FsError {
|
||||
FsError::CommandExecutionError(e) => Some(e),
|
||||
FsError::InvalidGlobPattern(e) => Some(e),
|
||||
FsError::MetadataError(e) => Some(e),
|
||||
FsError::ChangeDirFailed(e) => Some(e),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -578,3 +581,41 @@ pub fn rsync(src: &str, dest: &str) -> Result<String, FsError> {
|
||||
Err(e) => Err(FsError::CommandExecutionError(e)),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the current working directory.
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `path` - The path to change to
|
||||
*
|
||||
* # Returns
|
||||
*
|
||||
* * `Ok(String)` - A success message indicating the directory was changed
|
||||
* * `Err(FsError)` - An error if the directory change failed
|
||||
*
|
||||
* # Examples
|
||||
*
|
||||
* ```
|
||||
* let result = chdir("/path/to/directory")?;
|
||||
* println!("{}", result);
|
||||
* ```
|
||||
*/
|
||||
pub fn chdir(path: &str) -> Result<String, FsError> {
|
||||
let path_obj = Path::new(path);
|
||||
|
||||
// Check if directory exists
|
||||
if !path_obj.exists() {
|
||||
return Err(FsError::DirectoryNotFound(path.to_string()));
|
||||
}
|
||||
|
||||
// Check if it's a directory
|
||||
if !path_obj.is_dir() {
|
||||
return Err(FsError::NotADirectory(path.to_string()));
|
||||
}
|
||||
|
||||
// Change directory
|
||||
std::env::set_current_dir(path_obj).map_err(FsError::ChangeDirFailed)?;
|
||||
|
||||
Ok(format!("Successfully changed directory to '{}'", path))
|
||||
}
|
||||
|
@@ -30,6 +30,7 @@ pub fn register_os_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>>
|
||||
engine.register_fn("mkdir", mkdir);
|
||||
engine.register_fn("file_size", file_size);
|
||||
engine.register_fn("rsync", rsync);
|
||||
engine.register_fn("chdir", chdir);
|
||||
|
||||
// Register download functions
|
||||
engine.register_fn("download", download);
|
||||
@@ -128,6 +129,13 @@ pub fn rsync(src: &str, dest: &str) -> Result<String, Box<EvalAltResult>> {
|
||||
os::rsync(src, dest).to_rhai_error()
|
||||
}
|
||||
|
||||
/// Wrapper for os::chdir
|
||||
///
|
||||
/// Change the current working directory.
|
||||
pub fn chdir(path: &str) -> Result<String, Box<EvalAltResult>> {
|
||||
os::chdir(path).to_rhai_error()
|
||||
}
|
||||
|
||||
//
|
||||
// Download Function Wrappers
|
||||
//
|
||||
|
Reference in New Issue
Block a user