From 3cf39a0e949b645fa3ffafa81bcd505bd01ac21c Mon Sep 17 00:00:00 2001 From: brammert010 Date: Mon, 16 Sep 2013 16:16:24 +0200 Subject: [PATCH 1/6] Update genconf.lua * added support for use of vars in target path and filename * added support for auto-creation of target paths based on added 'paths' section to config file * added support for configurable template path (--tplpath) * added support for configurable config file (--config) * disabled stop-criteria in case given var on the command-line is not known in configuration (just ignore) --- genconf.lua | 72 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/genconf.lua b/genconf.lua index 07bc5c2..02bd299 100644 --- a/genconf.lua +++ b/genconf.lua @@ -1,7 +1,17 @@ --[[ -- genconf.lua - a generic configuration files generator -- --- https://github.com/henix/genconf.lua +-- original project: https://github.com/henix/genconf.lua +-- forked to: https://github.com/brammert010/genconf.lua +-- +-- following modifications/additions were made in this fork: +-- +-- * added support for configurable template path (--tplpath) +-- * added support for configurable config file (--config) +-- * added support for auto-creation of target paths based on added 'paths' section to config file +-- * added support for use of vars in target paths and target files +-- * disabled stop-criteria in case given var on the command-line is not known in configuration for flexibility +-- --]] -- ## 0 Common functions @@ -42,6 +52,12 @@ else end end +function createDir (dirname) + print("create directory " .. dirname) + os.execute("mkdir " .. dirname) +end + + -- ### 0.3 string function startsWith(str, prefix) return (string.sub(str, 1, string.len(prefix)) == prefix) @@ -68,6 +84,9 @@ local Action = { -- enum local action = nil local cmdValues = nil +local tplpath = nil +local config = nil + do function parseArgs(args) local action = nil @@ -82,13 +101,21 @@ do elseif param == '--use-cached' then if action ~= nil then throw('Option conflict: '..action..' and '..param) end action = Action.UseCached + elseif startsWith(param, '--config') then + local i = string.find(param, '=', 1, true) + tassert(i ~= nil, "Can't find '=' in : "..param) + config = string.sub(param, i + 1) + elseif startsWith(param, '--tplpath') then + local i = string.find(param, '=', 1, true) + tassert(i ~= nil, "Can't find '=' in : "..param) + tplpath = string.sub(param, i + 1) elseif startsWith(param, '--') then throw('Unknown option: '..param) else - local i = string.find(line, '=', 1, true) + local i = string.find(param, '=', 1, true) tassert(i ~= nil, "Can't find '=' in : "..param) - local name = string.sub(line, 1, i - 1) - local value = string.sub(line, i + 1) + local name = string.sub(param, 1, i - 1) + local value = string.sub(param, i + 1) tassert(cmdValues[name] == nil, 'Duplicated name in cmdline: ' .. name) cmdValues[name] = value end @@ -107,9 +134,17 @@ do action = action_err end +if tplpath == nil then + tplpath = 'genconf' +end + +if config == nil then + config = tplpath..os.pathsep..'genconf.conf.lua' +end + -- ### 1.2 print help if action == Action.PrintHelp then - print('Usage: lua genconf.lua [--gitignore | --help | --use-cached] name1=value1 name2=value2 ...') + print('Usage: lua genconf.lua [--gitignore | --help | --use-cached] [--tplpath=genconf] [--config=${tplpath}/genconf.conf.lua] name1=value1 name2=value2 ...') os.exit(0) end @@ -118,15 +153,15 @@ local VARNAME_PATT = '[%w._-]+' do local ok, err = pcall(function() - dofile('genconf'..os.pathsep..'genconf.conf.lua') + dofile(config) tassert(vars, 'genconf.conf.lua: vars not defined') tassert(templates, 'genconf.conf.lua: templates not defined') for _, varname in ipairs(vars) do tassert(string.match(varname, '^'..VARNAME_PATT..'$') ~= nil, 'Invalid var name: '..varname..' (must match '..VARNAME_PATT..')') end - for k, v in pairs(cmdValues) do - tassert(table.indexOf(vars, k), 'name is not in vars: '..k) - end +-- for k, v in pairs(cmdValues) do +-- tassert(table.indexOf(vars, k), 'name is not in vars: '..k) +-- end end) if not ok then io.write(err, '\n') @@ -223,8 +258,16 @@ end cache.save(values) -- ## 3. generate +for _, path in ipairs(paths) do + local realpath = string.gsub(path, '%${('..VARNAME_PATT..')}', function(name) + -- if not exists, leave it unchanged + return (values[name] or '${'..name..'}') + end) + createDir(normalizePath(realpath)) +end + for _, file in ipairs(templates) do - local ftmpl = assert(io.open('genconf' .. os.pathsep .. normalizePath(file.name))) + local ftmpl = assert(io.open(tplpath.. os.pathsep .. normalizePath(file.name))) local all = ftmpl:read('*a') io.close(ftmpl) @@ -234,8 +277,13 @@ for _, file in ipairs(templates) do end) local outname = normalizePath(file.target) - io.write('generating ', outname) - local fout = assert(io.open(outname, 'w')) + local realoutname = string.gsub(outname, '%${('..VARNAME_PATT..')}', function(name) + -- if not exists, leave it unchanged + return (values[name] or '${'..name..'}') + end) + + io.write('generating ', realoutname) + local fout = assert(io.open(realoutname, 'w')) fout:write(result) io.close(fout) io.write(' ... done.\n') From e00dacab7357927a51e644806b6ceafba79340a0 Mon Sep 17 00:00:00 2001 From: brammert010 Date: Mon, 16 Sep 2013 16:28:12 +0200 Subject: [PATCH 2/6] Added notes about this fork and a link to the original project --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index d487789..21a5984 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,11 @@ A generic configuration files generator +PLEASE NOTE: +- this is a fork of the project https://github.com/henix/genconf.lua all credits for henix for paving the road +- main reason for existance of this fork is to allow me to add some flexibility for generation of similar configuration files to different paths +- documentation of this fork is not *yet* up-to-date + ## Motivation In your application there are many configurations (db host/port/username/password...), and you may save these in some configuration files. From dc2dc2c85c6292b57d6f49a363a4dafd543572e3 Mon Sep 17 00:00:00 2001 From: brammert010 Date: Mon, 16 Sep 2013 16:28:28 +0200 Subject: [PATCH 3/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 21a5984..7a24840 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A generic configuration files generator PLEASE NOTE: - this is a fork of the project https://github.com/henix/genconf.lua all credits for henix for paving the road - main reason for existance of this fork is to allow me to add some flexibility for generation of similar configuration files to different paths -- documentation of this fork is not *yet* up-to-date +- documentation of this fork is not **yet** up-to-date ## Motivation From 62a832048f4cf81367b509b9299ae6592284aa72 Mon Sep 17 00:00:00 2001 From: brammert010 Date: Mon, 16 Sep 2013 16:39:13 +0200 Subject: [PATCH 4/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7a24840..785636f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A generic configuration files generator PLEASE NOTE: - this is a fork of the project https://github.com/henix/genconf.lua all credits for henix for paving the road - main reason for existance of this fork is to allow me to add some flexibility for generation of similar configuration files to different paths -- documentation of this fork is not **yet** up-to-date +- documentation of this fork is **not** (yet) up-to-date ## Motivation From bc5f28b9badc830015e5ef3710d329eef4c2a03c Mon Sep 17 00:00:00 2001 From: brammert010 Date: Mon, 16 Sep 2013 16:42:01 +0200 Subject: [PATCH 5/6] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 785636f..32ccd5f 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,9 @@ PLEASE NOTE: - main reason for existance of this fork is to allow me to add some flexibility for generation of similar configuration files to different paths - documentation of this fork is **not** (yet) up-to-date +* auto-gen TOC: +{:toc} + ## Motivation In your application there are many configurations (db host/port/username/password...), and you may save these in some configuration files. From 146bdff9147638b083b1c9ed10eb0f594900d884 Mon Sep 17 00:00:00 2001 From: brammert010 Date: Mon, 16 Sep 2013 16:44:05 +0200 Subject: [PATCH 6/6] Update README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 32ccd5f..785636f 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,6 @@ PLEASE NOTE: - main reason for existance of this fork is to allow me to add some flexibility for generation of similar configuration files to different paths - documentation of this fork is **not** (yet) up-to-date -* auto-gen TOC: -{:toc} - ## Motivation In your application there are many configurations (db host/port/username/password...), and you may save these in some configuration files.