diff options
| -rwxr-xr-x | src/blueprint | 6 | ||||
| -rw-r--r-- | src/create.lua | 2 | ||||
| -rw-r--r-- | src/lib/builder.lua | 16 | ||||
| -rw-r--r-- | src/lib/path.lua | 13 | ||||
| -rw-r--r-- | tests/recursive/build.lua | 7 | ||||
| -rw-r--r-- | tests/recursive/src/index.html | 15 | ||||
| -rw-r--r-- | tests/recursive/src/second.html | 15 | ||||
| -rw-r--r-- | tests/simple/build.lua | 8 | ||||
| -rw-r--r-- | tests/simple/test.py | 39 |
9 files changed, 113 insertions, 8 deletions
diff --git a/src/blueprint b/src/blueprint index 4e73887..35b9c0e 100755 --- a/src/blueprint +++ b/src/blueprint @@ -21,7 +21,11 @@ local commands = { local command = commands[arg[1]] if command then - command() + local status, ret = pcall(command) + + if not status then + print('error: ' .. ret:gsub('^.+:%d+: (.+)$', '%1')) + end else print([[blueprint <command> [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() diff --git a/tests/recursive/build.lua b/tests/recursive/build.lua new file mode 100644 index 0000000..37e1d8e --- /dev/null +++ b/tests/recursive/build.lua @@ -0,0 +1,7 @@ +local template = Template:new() + +template:add_directory('./src', { + name = template:option('name') +}) + +return template diff --git a/tests/recursive/src/index.html b/tests/recursive/src/index.html new file mode 100644 index 0000000..38b1f0b --- /dev/null +++ b/tests/recursive/src/index.html @@ -0,0 +1,15 @@ +<!DOCTYPE "html"> +<html> + <head> + <title>@{name()}</title> + </head> + <body> + @{function() + local text = "" + for i=1,10 do + text = text .. ' ' .. tostring(i) + end + return text + end} + </body> +</html> diff --git a/tests/recursive/src/second.html b/tests/recursive/src/second.html new file mode 100644 index 0000000..e03a249 --- /dev/null +++ b/tests/recursive/src/second.html @@ -0,0 +1,15 @@ +<!DOCTYPE "html"> +<html> + <head> + <title>@{name()}</title> + </head> + <body> + @{function() + local text = "" + for i=1,50 do + text = text .. ' ' .. tostring(i) + end + return text + end} + </body> +</html> diff --git a/tests/simple/build.lua b/tests/simple/build.lua new file mode 100644 index 0000000..996ccda --- /dev/null +++ b/tests/simple/build.lua @@ -0,0 +1,8 @@ +local template = Template:new() + +template:add_file("./test.py", { + module = "a_module_that_is_different", + name = template:option('name') +}) + +return template diff --git a/tests/simple/test.py b/tests/simple/test.py new file mode 100644 index 0000000..20c1c7e --- /dev/null +++ b/tests/simple/test.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +# This module is called @{name()} + +import @{module} + +print("@{function() + return "Hello World" +end}") + +print("@{function() + local a = { "asdf", "1234" } + return a[1] +end}") + +print(@{function() + local a = { "asdf", "1234" } -- { + return a[2] +end}) + +print("@{function() + local a = "asdf{asdf" + return a:sub(1) +end}") + +print("@{function() + local a = "asdf" + --[[ + some comment } + --]] + return a +end}") + +print("@{function() + local a = [[ + some comment } + ]] + return a:sub(2, 8) +end}") |