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