aboutsummaryrefslogtreecommitdiff
path: root/src/lib/instance.lua
blob: 2de2c5fa3f35826b1eae3c632ff4e8b517f67fb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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