wip
This commit is contained in:
		
							
								
								
									
										82
									
								
								examples/scripts/basics/directories.rhai
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								examples/scripts/basics/directories.rhai
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,82 @@
 | 
			
		||||
 | 
			
		||||
// Create a test directory structure
 | 
			
		||||
let base_dir = "rhai_dir_test";
 | 
			
		||||
let sub_dir = base_dir + "/tmp/test";
 | 
			
		||||
 | 
			
		||||
println("Creating directory structure...");
 | 
			
		||||
let base_result = mkdir(base_dir+"/subdir");
 | 
			
		||||
println(`Base directory creation result: ${base_result}`);
 | 
			
		||||
 | 
			
		||||
let sub_result = mkdir(sub_dir);
 | 
			
		||||
println(`Subdirectory creation result: ${sub_result}`);
 | 
			
		||||
 | 
			
		||||
// Create a test file in the base directory
 | 
			
		||||
let base_file = base_dir + "/base_file.txt";
 | 
			
		||||
let base_content = "This is a file in the base directory.";
 | 
			
		||||
// First touch the file
 | 
			
		||||
run_command(`touch ${base_file}`);
 | 
			
		||||
// Then write to it with a separate command
 | 
			
		||||
run_command(`echo ${base_content} > ${base_file}`);
 | 
			
		||||
 | 
			
		||||
// Create a test file in the subdirectory
 | 
			
		||||
let sub_file = sub_dir + "/sub_file.txt";
 | 
			
		||||
let sub_content = "This is a file in the subdirectory.";
 | 
			
		||||
// First touch the file
 | 
			
		||||
run_command(`touch ${sub_file}`);
 | 
			
		||||
// Then write to it with a separate command
 | 
			
		||||
run_command(`echo ${sub_content} > ${sub_file}`);
 | 
			
		||||
 | 
			
		||||
// Get the current working directory before changing
 | 
			
		||||
let pwd_before = run_command("pwd");
 | 
			
		||||
println(`Current directory before chdir: ${pwd_before.stdout.trim()}`);
 | 
			
		||||
 | 
			
		||||
// Change to the base directory
 | 
			
		||||
println(`Changing directory to: ${base_dir}`);
 | 
			
		||||
let chdir_result = chdir(base_dir);
 | 
			
		||||
println(`Directory change result: ${chdir_result}`);
 | 
			
		||||
 | 
			
		||||
// Get the current working directory after changing
 | 
			
		||||
let pwd_after = run_command("pwd");
 | 
			
		||||
println(`Current directory after chdir: ${pwd_after.stdout.trim()}`);
 | 
			
		||||
 | 
			
		||||
// List files in the current directory (which should now be the base directory)
 | 
			
		||||
println("Files in the current directory:");
 | 
			
		||||
let files = find_files(".", "*");
 | 
			
		||||
println("Files found:");
 | 
			
		||||
for file in files {
 | 
			
		||||
    println(`- ${file}`);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Change to the subdirectory
 | 
			
		||||
println(`Changing directory to: subdir`);
 | 
			
		||||
let chdir_sub_result = chdir("subdir");
 | 
			
		||||
println(`Directory change result: ${chdir_sub_result}`);
 | 
			
		||||
 | 
			
		||||
// Get the current working directory after changing to subdirectory
 | 
			
		||||
let pwd_final = run_command("pwd");
 | 
			
		||||
println(`Current directory after second chdir: ${pwd_final.stdout.trim()}`);
 | 
			
		||||
 | 
			
		||||
// List files in the subdirectory
 | 
			
		||||
println("Files in the subdirectory:");
 | 
			
		||||
let subdir_files = find_files(".", "*");
 | 
			
		||||
println("Files found:");
 | 
			
		||||
for file in subdir_files {
 | 
			
		||||
    println(`- ${file}`);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Change back to the parent directory
 | 
			
		||||
println("Changing directory back to parent...");
 | 
			
		||||
let chdir_parent_result = chdir("..");
 | 
			
		||||
println(`Directory change result: ${chdir_parent_result}`);
 | 
			
		||||
 | 
			
		||||
// Clean up (uncomment to actually delete the files)
 | 
			
		||||
// println("Cleaning up...");
 | 
			
		||||
// Change back to the original directory first
 | 
			
		||||
// chdir(pwd_before.stdout.trim());
 | 
			
		||||
// delete(sub_file);
 | 
			
		||||
// delete(base_file);
 | 
			
		||||
// delete(sub_dir);
 | 
			
		||||
// delete(base_dir);
 | 
			
		||||
// println("Cleanup complete");
 | 
			
		||||
 | 
			
		||||
"Directory operations script completed successfully!"
 | 
			
		||||
							
								
								
									
										64
									
								
								examples/scripts/basics/files.rhai
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								examples/scripts/basics/files.rhai
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,64 @@
 | 
			
		||||
// 02_file_operations.rhai
 | 
			
		||||
// Demonstrates file system operations using SAL
 | 
			
		||||
 | 
			
		||||
// Create a test directory
 | 
			
		||||
let test_dir = "/tmp/rhai_test_dir";
 | 
			
		||||
println(`Creating directory: ${test_dir}`);
 | 
			
		||||
let mkdir_result = mkdir(test_dir);
 | 
			
		||||
println(`Directory creation result: ${mkdir_result}`);
 | 
			
		||||
 | 
			
		||||
// Check if the directory exists
 | 
			
		||||
let dir_exists = exist(test_dir);
 | 
			
		||||
println(`Directory exists: ${dir_exists}`);
 | 
			
		||||
 | 
			
		||||
// Create a test file
 | 
			
		||||
let test_file = test_dir + "/test_file.txt";
 | 
			
		||||
let file_content = "This is a test file created by Rhai script.";
 | 
			
		||||
 | 
			
		||||
// Create the file using a different approach
 | 
			
		||||
println(`Creating file: ${test_file}`);
 | 
			
		||||
// First ensure the directory exists
 | 
			
		||||
run_command(`mkdir -p ${test_dir}`);
 | 
			
		||||
// Then create the file using a simpler approach
 | 
			
		||||
// First touch the file
 | 
			
		||||
let touch_cmd = `touch ${test_file}`;
 | 
			
		||||
run_command(touch_cmd);
 | 
			
		||||
// Then write to it with a separate command
 | 
			
		||||
let echo_cmd = `echo ${file_content} > ${test_file}`;
 | 
			
		||||
let write_result = run_command(echo_cmd);
 | 
			
		||||
println(`File creation result: ${write_result.success}`);
 | 
			
		||||
 | 
			
		||||
// Wait a moment to ensure the file is created
 | 
			
		||||
run_command("sleep 1");
 | 
			
		||||
 | 
			
		||||
// Check if the file exists
 | 
			
		||||
let file_exists = exist(test_file);
 | 
			
		||||
println(`File exists: ${file_exists}`);
 | 
			
		||||
 | 
			
		||||
// Get file size
 | 
			
		||||
if file_exists {
 | 
			
		||||
    let size = file_size(test_file);
 | 
			
		||||
    println(`File size: ${size} bytes`);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Copy the file
 | 
			
		||||
let copied_file = test_dir + "/copied_file.txt";
 | 
			
		||||
println(`Copying file to: ${copied_file}`);
 | 
			
		||||
let copy_result = copy(test_file, copied_file);
 | 
			
		||||
println(`File copy result: ${copy_result}`);
 | 
			
		||||
 | 
			
		||||
// Find files in the directory
 | 
			
		||||
println("Finding files in the test directory:");
 | 
			
		||||
let files = find_files(test_dir, "*.txt");
 | 
			
		||||
for file in files {
 | 
			
		||||
    println(`  - ${file}`);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Clean up (uncomment to actually delete the files)
 | 
			
		||||
// println("Cleaning up...");
 | 
			
		||||
// delete(copied_file);
 | 
			
		||||
// delete(test_file);
 | 
			
		||||
// delete(test_dir);
 | 
			
		||||
// println("Cleanup complete");
 | 
			
		||||
 | 
			
		||||
"File operations script completed successfully!"
 | 
			
		||||
							
								
								
									
										39
									
								
								examples/scripts/basics/hello.rhai
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								examples/scripts/basics/hello.rhai
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
// 01_hello_world.rhai
 | 
			
		||||
// A simple hello world script to demonstrate basic Rhai functionality
 | 
			
		||||
 | 
			
		||||
// Print a message
 | 
			
		||||
println("Hello from Rhai!");
 | 
			
		||||
 | 
			
		||||
// Define a function
 | 
			
		||||
fn greet(name) {
 | 
			
		||||
    "Hello, " + name + "!"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Call the function and print the result
 | 
			
		||||
let greeting = greet("SAL User");
 | 
			
		||||
println(greeting);
 | 
			
		||||
 | 
			
		||||
// Do some basic calculations
 | 
			
		||||
let a = 5;
 | 
			
		||||
let b = 7;
 | 
			
		||||
println(`${a} + ${b} = ${a + b}`);
 | 
			
		||||
println(`${a} * ${b} = ${a * b}`);
 | 
			
		||||
 | 
			
		||||
// Create and use an array
 | 
			
		||||
let numbers = [1, 2, 3, 4, 5];
 | 
			
		||||
println("Numbers: " + numbers);
 | 
			
		||||
println("Sum of numbers: " + numbers.reduce(|sum, n| sum + n, 0));
 | 
			
		||||
 | 
			
		||||
// Create and use a map
 | 
			
		||||
let person = #{
 | 
			
		||||
    name: "John Doe",
 | 
			
		||||
    age: 30,
 | 
			
		||||
    occupation: "Developer"
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
println("Person: " + person);
 | 
			
		||||
println("Name: " + person.name);
 | 
			
		||||
println("Age: " + person.age);
 | 
			
		||||
 | 
			
		||||
// Return a success message
 | 
			
		||||
"Hello world script completed successfully!"
 | 
			
		||||
		Reference in New Issue
	
	Block a user