[Add] Load command scripts from filesystem.

This commit is contained in:
Ritchie Cunningham 2025-09-22 19:24:44 +01:00
parent cb6022116a
commit a502fa64cf
4 changed files with 36 additions and 30 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
# Build output. # Build output.
bin/ /bin/
build/ build/
# Editor/IDE, whatever people use. # Editor/IDE, whatever people use.

14
assets/scripts/bin/ls.lua Normal file
View File

@ -0,0 +1,14 @@
-- /bin/ls - Lists files in a directory.
local dir = current_dir -- Get directory object from C++.
local output = ""
-- Iterate over the 'children' map exposed via C++.
for name, node in pairs(dir.children) do
output = output .. name
if node.type == 1 then -- 1 is DIR_NODE enum from C++.
output = output .. "/"
end
output = output .. " "
end
return output

View File

@ -0,0 +1,3 @@
local file_to_remove = arg[1]
if not file_to_remove then return "rm: missing operand" end
return { action = "rm", target = file_to_remove }

View File

@ -1,3 +1,7 @@
#include <filesystem>
#include <fstream>
#include <sstream>
#include "vfs_manager.h" #include "vfs_manager.h"
#include "vfs.h" #include "vfs.h"
@ -38,35 +42,20 @@ VFSManager::VFSManager(void) {
_vfs_root->read_only = true; _vfs_root->read_only = true;
bin->read_only = true; bin->read_only = true;
/* TODO: /* Load all scripts from assets/scripts/bin into the VFS. */
* Load all scripts from assets/scripts/bin into the bind node. const std::string path = "assets/scripts/bin";
* We'll create ls.lua manually for now. for(const auto & entry : std::filesystem::directory_iterator(path)) {
*/ if(entry.is_regular_file() && entry.path().extension() == ".lua") {
vfs_node* ls_script = new_node("ls.lua", FILE_NODE, bin); std::ifstream t(entry.path());
ls_script->content = R"lua(-- /bin/ls.lua - Lists files in a directory. std::stringstream buffer;
local dir = current_dir -- Get directory object from C++. buffer << t.rdbuf();
local output = "" std::string filename = entry.path().filename().string();
vfs_node* script_node = new_node(filename, FILE_NODE, bin);
-- Iterate over the 'children' map exposed from c++. script_node->content = buffer.str();
for name, node in pairs(dir.children) do bin->children[filename] = script_node;
output = output .. name fprintf(stderr, "Loaded script: /bin/%s\n", filename.c_str());
if node.type == 1 then }
output = output .. "/" }
end
output = output .. " "
end
return output
)lua";
bin->children["ls.lua"] = ls_script;
vfs_node* rm_script = new_node("rm.lua", FILE_NODE, bin);
rm_script->content = R"lua(-- /bin/rm.lua - Removes a file.
local file_to_remove = arg[1]
if not file_to_remove then return "rm: missing operand" end
return { action = "rm", target = file_to_remove }
)lua";
bin->children["rm.lua"] = rm_script;
} }
VFSManager::~VFSManager(void) { VFSManager::~VFSManager(void) {