diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-04-20 19:30:28 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-04-20 19:30:28 +0200 |
| commit | e371c801fed71bb73bc7c027e3de5ad2a6116673 (patch) | |
| tree | e6818b34fed4723d8c44d71f8c6d1a68e44c4c8d /src/lib | |
| parent | 47a5607a691a576928896f21bcf563c011dc64e5 (diff) | |
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/builder.lua | 16 | ||||
| -rw-r--r-- | src/lib/path.lua | 13 |
2 files changed, 23 insertions, 6 deletions
diff --git a/src/lib/builder.lua b/src/lib/builder.lua index adb55c8..1a9b0af 100644 --- a/src/lib/builder.lua +++ b/src/lib/builder.lua @@ -12,6 +12,10 @@ function Builder:from_template(path) source = path, } + if not builder.template or getmetatable(builder.template) ~= Template then + error('invalid template') + end + builder.source_directory = builder.source:parent() setmetatable(builder, self) @@ -41,13 +45,13 @@ function Builder:entries() for _, value in ipairs(self.template.files) do entries[#entries + 1] = { - path = Path:new(value.path), + path = self.source_directory / value.path, env = value.env, } end for _, value in ipairs(self.template.directories) do - for entry in Path:new(value.path):children() do + for entry in (self.source_directory / value.path):children() do entries[#entries + 1] = { path = entry, env = value.env, @@ -68,8 +72,8 @@ function Builder:build(args) for entry in self:entries() do instance:register( - entry.path, - entry.path.is_directory and nil or self:process_file(entry) + entry.path:relative_to_parent(self.source_directory), + not entry.path:is_directory() and self:process_file(entry) or nil ) end @@ -77,11 +81,11 @@ function Builder:build(args) end function Builder:process_file(file) - if not (self.source_directory / file.path):exists() then + if not file.path:exists() then error(tostring(file.path) .. ' does not exist.') end - local f = (self.source_directory / file.path):open("r") + local f = file.path:open("r") local raw = f:read("*all") f:close() diff --git a/src/lib/path.lua b/src/lib/path.lua index a0be1e5..925f017 100644 --- a/src/lib/path.lua +++ b/src/lib/path.lua @@ -8,6 +8,7 @@ function Path:new(p) p = p:gsub("/$", "") p = p:gsub("^%./", "") p = p:gsub("/[^/]+/%.%.", "") + p = p:gsub("^[^/]+/%.%./", "") local path = { segments = p:split('/'), @@ -126,6 +127,18 @@ function Path:remove(opts) pipe:close() end +function Path:is_parent_of(child) + return tostring(child):starts_with(tostring(self)) +end + +function Path:relative_to_parent(path) + if not path:is_parent_of(self) then + return nil + end + + return Path:new(tostring(self):sub(#tostring(path) + 2)) +end + return { tests = { function() |