From e371c801fed71bb73bc7c027e3de5ad2a6116673 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Mon, 20 Apr 2026 19:30:28 +0200 Subject: add tests and fix path resolution bug --- src/blueprint | 2 +- src/create.lua | 2 +- src/lib/builder.lua | 16 ++++++++++------ src/lib/path.lua | 13 +++++++++++++ 4 files changed, 25 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/blueprint b/src/blueprint index 5a40650..35b9c0e 100755 --- a/src/blueprint +++ b/src/blueprint @@ -24,7 +24,7 @@ if command then local status, ret = pcall(command) if not status then - print('error: ' .. ret) + print('error: ' .. ret:gsub('^.+:%d+: (.+)$', '%1')) end else print([[blueprint [options...] diff --git a/src/create.lua b/src/create.lua index 19ba7e8..e4bb202 100644 --- a/src/create.lua +++ b/src/create.lua @@ -12,7 +12,7 @@ return function() local path = manager.get(args.template) or Path:new(args.template) - if not path:exists() then + if not path:exists() and not path:is_directory() then error('template does not exist.') end 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() -- cgit v1.2.3-70-g09d2