diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/arg.lua | 9 | ||||
| -rwxr-xr-x | src/blueprint | 28 | ||||
| -rw-r--r-- | src/create.lua | 23 | ||||
| -rw-r--r-- | src/install.lua | 14 | ||||
| -rw-r--r-- | src/lib/builder.lua | 4 | ||||
| -rw-r--r-- | src/lib/init.lua | 1 | ||||
| -rw-r--r-- | src/lib/manager.lua | 40 | ||||
| -rw-r--r-- | src/lib/path.lua | 14 | ||||
| -rw-r--r-- | src/list.lua | 7 | ||||
| -rw-r--r-- | src/remove.lua | 8 |
10 files changed, 132 insertions, 16 deletions
diff --git a/src/arg.lua b/src/arg.lua index de19c77..ea71955 100644 --- a/src/arg.lua +++ b/src/arg.lua @@ -3,13 +3,15 @@ local function help(opts) io.write(Path:new(arg[0]):name()) io.write(" ") + io.write(arg[1]) + io.write(" ") for _, v in ipairs(opts) do if v.kind == nil then - io.write("[" .. v.name .. "] ") + io.write("<" .. v.name .. "> ") end end - io.write("\n") + io.write("[options...]\n") local max_width = 0 @@ -65,7 +67,8 @@ return function (opts) end end - for _, value in ipairs(arg) do + for index = 2, #arg do + local value = arg[index] local key = nil if current_flag == nil then local flag = value:flag_name() diff --git a/src/blueprint b/src/blueprint index 871105d..4e73887 100755 --- a/src/blueprint +++ b/src/blueprint @@ -12,17 +12,23 @@ setup_package() require('extensions') require('lib') -local args = require('arg') { - strict = false, - { name = "target" }, - { name = "template", description = "some description", kind = "property", required = true }, +local commands = { + c = require('create'), create = require('create'), + l = require('list'), list = require('list'), + i = require('install'), install = require('install'), + r = require('remove'), remove = require('remove'), } -local template = loadfile('./example/build.lua', nil, Env.Sandbox { - Template = Template -}) +local command = commands[arg[1]] +if command then + command() +else + print([[blueprint <command> [options...] -local builder = Builder:from_template('./example/build.lua') - -local instance = builder:build(args) -instance:save_to(args.target) +commands: + create -- new directory structure from template + list -- show all installed templates + install -- install template from URL or file + remove -- remove installed template +]]) +end diff --git a/src/create.lua b/src/create.lua new file mode 100644 index 0000000..19ba7e8 --- /dev/null +++ b/src/create.lua @@ -0,0 +1,23 @@ +return function() + local args = require('arg') { + strict = false, + { name = "target" }, + { + name = "template", + description = "template name or path to build.lua", + kind = "property", + required = true, + }, + } + + local path = manager.get(args.template) or Path:new(args.template) + + if not path:exists() then + error('template does not exist.') + end + + local builder = Builder:from_template(path) + + local instance = builder:build(args) + instance:save_to(args.target) +end diff --git a/src/install.lua b/src/install.lua new file mode 100644 index 0000000..46b1d7f --- /dev/null +++ b/src/install.lua @@ -0,0 +1,14 @@ +return function() + local args = require('arg') { + strict = true, + { name = "path" }, + { + name = "name", + description = "template name (default: name of the directory)", + kind = "property", + required = false, + } + } + + manager.install(Path:new(args.path), args.name) +end diff --git a/src/lib/builder.lua b/src/lib/builder.lua index 7390257..34c3c8c 100644 --- a/src/lib/builder.lua +++ b/src/lib/builder.lua @@ -3,13 +3,13 @@ Builder = {} function Builder:from_template(path) local builder = { template = loadfile( - path, + tostring(path), nil, Env.Sandbox { Template = Template } )(), - source = Path:new(path), + source = path, } builder.source_directory = builder.source:parent() diff --git a/src/lib/init.lua b/src/lib/init.lua index 2db3367..d87921c 100644 --- a/src/lib/init.lua +++ b/src/lib/init.lua @@ -3,3 +3,4 @@ require('lib.env') require('lib.template') require('lib.builder') require('lib.instance') +require('lib.manager') diff --git a/src/lib/manager.lua b/src/lib/manager.lua new file mode 100644 index 0000000..4ad2051 --- /dev/null +++ b/src/lib/manager.lua @@ -0,0 +1,40 @@ +manager = {} + +manager.base_path = Path:new(os.getenv('HOME')) / '.local/share/blueprint' +manager.install_path = manager.base_path / 'templates' + +manager.base_path:make_directory { create_parents = true } +manager.install_path:make_directory { create_parents = true } + +function manager.list() + return manager.install_path:entries() +end + +function manager.install(path, name) + if not (path / 'build.lua'):exists() then + error('build.lua does not exist.') + end + + local target = (manager.install_path / (name or path:name())) + + if target:exists() then + error('template already installed.') + end + + path:copy_to(target) +end + +function manager.remove(name) + if name == nil or not (manager.install_path / name):exists() then + error('template does not exist.') + end + + (manager.install_path / name):remove { recursive = true, force = true } +end + +function manager.get(name) + if (manager.install_path / name):exists() then + return manager.install_path / name / 'build.lua' + end + return nil +end diff --git a/src/lib/path.lua b/src/lib/path.lua index f99585b..a0be1e5 100644 --- a/src/lib/path.lua +++ b/src/lib/path.lua @@ -112,6 +112,20 @@ function Path:make_directory(opts) pipe:close() end +function Path:copy_to(target) + local pipe = io.popen('cp -r ' .. tostring(self) .. ' ' .. tostring(target)) + pipe:close() +end + +function Path:remove(opts) + local pipe = io.popen('rm ' .. + (opts.recursive and '-r ' or ' ') .. + (opts.force and '-f ' or ' ') .. + tostring(self) + ) + pipe:close() +end + return { tests = { function() diff --git a/src/list.lua b/src/list.lua new file mode 100644 index 0000000..d4dd1de --- /dev/null +++ b/src/list.lua @@ -0,0 +1,7 @@ +return function() + io.write('Templates:\n') + + for template in manager.list() do + io.write(' ' .. template:name() .. '\n') + end +end diff --git a/src/remove.lua b/src/remove.lua new file mode 100644 index 0000000..3877324 --- /dev/null +++ b/src/remove.lua @@ -0,0 +1,8 @@ +return function() + local args = require('arg') { + strict = true, + { name = "template" }, + } + + manager.remove(args.template) +end |