From 0686b40f979f4607b3fd8cca21c463e98f617666 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Mon, 20 Apr 2026 13:43:01 +0200 Subject: implement builder and instance --- src/lib/instance.lua | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/lib/instance.lua (limited to 'src/lib/instance.lua') diff --git a/src/lib/instance.lua b/src/lib/instance.lua new file mode 100644 index 0000000..2de2c5f --- /dev/null +++ b/src/lib/instance.lua @@ -0,0 +1,54 @@ +Instance = {} + +function Instance:new() + local instance = { + entries = {}, + } + + setmetatable(instance, self) + self.__index = self + + return instance +end + +function Instance:register(path, content) + self.entries[#self.entries + 1] = { + path = path, + content = content, + } +end + +function Instance:save_to(target) + for _, entry in ipairs(self.entries) do + local target_entry = Path:new(target) / entry.path + + if entry.content == nil then + target_entry:make_directory { create_parents = true } + else + target_entry:parent():make_directory { create_parents = true } + local file = target_entry:open('w') + file:write(entry.content) + file:close() + end + end +end + +function Instance:__tostring() + local output = [[{ + entries = { +]] + + for _, entry in ipairs(self.entries) do + output = output .. [[ { + path = ]] .. tostring(entry.path) .. [[, + content = ]] .. (entry.content and ("[[\n " .. entry.content:gsub('\n', '\n ') .. "\n ]]") or "nil") .. [[, + }, +]] + end + + output = output .. [[ } +} +]] + + return output +end -- cgit v1.2.3-70-g09d2