diff options
| author | ruki <[email protected]> | 2019-09-19 00:49:34 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-09-18 22:57:25 +0800 |
| commit | bb3455b845e0b776937283b3ddae4ae557eea52e (patch) | |
| tree | 7625b08ec58620e048410017bfe18ff3fa536548 /xmake/actions/create | |
| parent | 916078fdb2904340ef2003a46057d9232194511a (diff) | |
improve template
Diffstat (limited to 'xmake/actions/create')
| -rw-r--r-- | xmake/actions/create/main.lua | 90 | ||||
| -rw-r--r-- | xmake/actions/create/xmake.lua | 4 |
2 files changed, 90 insertions, 4 deletions
diff --git a/xmake/actions/create/main.lua b/xmake/actions/create/main.lua index bfd07e7bf..d2737c258 100644 --- a/xmake/actions/create/main.lua +++ b/xmake/actions/create/main.lua @@ -23,6 +23,92 @@ import("core.base.option") import("core.project.project") import("core.project.template") +-- get the builtin variables +function _get_builtinvars(tempinst, targetname) + return {TARGETNAME = targetname, + FAQ = function() return io.readfile(path.join(os.programdir(), "scripts", "faq.lua")) end} +end + +-- create project from template +function _create_project(tempinst, sourcedir, targetname) + + -- get project directory + local projectdir = path.absolute(option.get("project") or path.join(os.curdir(), targetname)) + + -- ensure the project directory + if not os.isdir(projectdir) then + os.mkdir(projectdir) + end + + -- copy the project files + os.cp(path.join(sourcedir, "*"), projectdir) + + -- get the builtin variables + local builtinvars = _get_builtinvars(tempinst, targetname) + + -- replace all variables + for _, configfile in ipairs(tempinst:get("configfiles")) do + local pattern = "%${(.-)}" + io.gsub(configfile, "(" .. pattern .. ")", function(_, variable) + variable = variable:trim() + local value = builtinvars[variable] + return type(value) == "function" and value() or value + end) + end + + -- generate .gitignore + os.cp(path.join(os.programdir(), "scripts", "gitignore"), path.join(projectdir, ".gitignore")) +end + +-- create project or files from template +function _create_project_or_files(language, templateid, targetname) + + -- check the language + assert(language, "no language!") + + -- check the template id + assert(templateid, "no template id!") + + -- load all templates for the given language + local templates = template.templates(language) + + -- TODO: deprecated + -- in order to be compatible with the old version template + local templates_old = { quickapp_qt = "qt.quickapp", + widgetapp_qt = "qt.widgetapp", + console_qt = "qt.console", + static_qt = "qt.static", + shared_qt = "qt.shared", + console_tbox = "tbox.console", + static_tbox = "tbox.static", + shared_tbox = "tbox.shared"} + if templates_old[templateid] then +-- deprecated.add("template(" .. templates_old[templateid] .. ")", "template(" .. templateid .. ")") + templateid = templates_old[templateid] + end + + -- get the given template instance + local tempinst = nil + if templates then + for _, t in ipairs(templates) do + if t:name() == templateid then + tempinst = t + break + end + end + end + assert(tempinst and tempinst:scriptdir(), "invalid template id: %s!", templateid) + + -- create project + local sourcedir = path.join(tempinst:scriptdir(), "project") + if os.isdir(sourcedir) then + _create_project(tempinst, sourcedir, targetname) + else + raise("template(%s): project and files not found!", templateid) + end +end + + -- main function main() @@ -35,8 +121,8 @@ function main() -- trace cprint("${bright}create %s ...", targetname) - -- create project from template - template.create(option.get("language"), option.get("template"), targetname) + -- create project or files from template + _create_project_or_files(option.get("language"), option.get("template"), targetname) -- trace cprint("${bright}create ok!${ok_hand}") diff --git a/xmake/actions/create/xmake.lua b/xmake/actions/create/xmake.lua index dee04b4b8..26157adc9 100644 --- a/xmake/actions/create/xmake.lua +++ b/xmake/actions/create/xmake.lua @@ -68,8 +68,8 @@ task("create") local templates = {} for _, l in ipairs(template.languages()) do for _, t in ipairs(template.templates(l)) do - templates[t.name] = templates[t.name] or {} - table.insert(templates[t.name], l) + templates[t:name()] = templates[t:name()] or {} + table.insert(templates[t:name()], l) end end |
