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 | |
| parent | 916078fdb2904340ef2003a46057d9232194511a (diff) | |
improve template
| -rw-r--r-- | xmake/actions/config/scangen.lua | 3 | ||||
| -rw-r--r-- | xmake/actions/create/main.lua | 90 | ||||
| -rw-r--r-- | xmake/actions/create/xmake.lua | 4 | ||||
| -rw-r--r-- | xmake/core/package/package.lua | 15 | ||||
| -rw-r--r-- | xmake/core/project/template.lua | 304 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/project/template.lua | 15 | ||||
| -rw-r--r-- | xmake/scripts/faq.lua | 68 |
7 files changed, 212 insertions, 287 deletions
diff --git a/xmake/actions/config/scangen.lua b/xmake/actions/config/scangen.lua index c459491f2..872c1ead4 100644 --- a/xmake/actions/config/scangen.lua +++ b/xmake/actions/config/scangen.lua @@ -21,7 +21,6 @@ -- imports import("core.project.config") import("core.project.project") -import("core.project.template") import("core.language.language") -- scan project and generate xmake.lua automaticlly if the project codes exist @@ -145,7 +144,7 @@ function main() end -- add FAQ - file:print(template.faq()) + file:print(io.readfile(path.join(os.programdir(), "scripts", "faq.lua"))) -- exit file file:close() 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 diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index bfc742087..8fa4b63b9 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -1145,10 +1145,7 @@ function package.load_from_system(packagename) end -- new an instance - local instance, errors = _instance.new(packagename, scopeinfo.new("package", packageinfo)) - if not instance then - return nil, errors - end + local instance = _instance.new(packagename, scopeinfo.new("package", packageinfo)) -- mark as system or 3rd package instance._isSys = true @@ -1195,10 +1192,7 @@ function package.load_from_project(packagename, project) end -- new an instance - local instance, errors = _instance.new(packagename, packages[packagename]) - if not instance then - return nil, errors - end + local instance = _instance.new(packagename, packages[packagename]) -- save instance to the cache package._PACKAGES[packagename] = instance @@ -1259,10 +1253,7 @@ function package.load_from_repository(packagename, repo, packagedir, packagefile end -- new an instance - local instance, errors = _instance.new(packagename, packageinfo, path.directory(scriptpath)) - if not instance then - return nil, errors - end + local instance = _instance.new(packagename, packageinfo, path.directory(scriptpath)) -- save repository instance._REPO = repo diff --git a/xmake/core/project/template.lua b/xmake/core/project/template.lua index 98593be24..13cf1076e 100644 --- a/xmake/core/project/template.lua +++ b/xmake/core/project/template.lua @@ -19,7 +19,8 @@ -- -- define module: template -local template = template or {} +local template = template or {} +local _instance = _instance or {} -- load modules local os = require("base/os") @@ -28,11 +29,36 @@ local path = require("base/path") local table = require("base/table") local utils = require("base/utils") local string = require("base/string") -local option = require("base/option") -local sandbox = require("sandbox/sandbox") -local project = require("project/project") local interpreter = require("base/interpreter") -local deprecated = require("base/deprecated") + +-- new an instance +function _instance.new(name, info, scriptdir) + local instance = table.inherit(_instance) + instance._NAME = name + instance._INFO = info + instance._SCRIPTDIR = scriptdir + return instance +end + +-- get the package name +function _instance:name() + return self._NAME +end + +-- get the package configure +function _instance:get(name) + + -- get it from info first + local value = self._INFO:get(name) + if value ~= nil then + return value + end +end + +-- get the script directory +function _instance:scriptdir() + return self._SCRIPTDIR +end -- the interpreter function template._interpreter() @@ -46,29 +72,18 @@ function template._interpreter() local interp = interpreter.new() assert(interp) - -- define apis (only root scope) + -- define apis interp:api_define { values = { - -- set_xxx - "set_name" - , "set_description" - , "set_projectdir" -- add_xxx - , "add_macrofiles" + "template.add_configfiles" } , script = { -- on_xxx - "on_create" - } - , dictionary = - { - -- set_xxx - "set_macros" - -- add_xxx - , "add_macros" + "template.on_create" } } @@ -79,32 +94,6 @@ function template._interpreter() return interp end --- replace macros -function template._replace(macros, macrofiles) - - -- check - assert(macros and macrofiles) - - -- make all files - local files = {} - for _, macrofile in ipairs(table.wrap(macrofiles)) do - local matchfiles = os.files(macrofile) - if matchfiles then - table.join2(files, matchfiles) - end - end - - -- replace all files - for _, file in ipairs(files) do - for macro, value in pairs(macros) do - io.gsub(file, "%[" .. macro .. "%]", value) - end - end - - -- ok - return true -end - -- get the language list function template.languages() @@ -147,225 +136,32 @@ function template.templates(language) os.raise(errors) end - -- load templates - local results, errors = interp:make(nil, true, true) + -- load template + local results, errors = interp:make("template", true, true) if not results then - -- trace os.raise(errors) end - -- save template directory - results:set("_DIRECTORY", path.directory(templatefile)) - - -- insert to templates - table.insert(templates, results:info()) - end - end - return templates -end - --- create project from template -function template.create(language, templateid, targetname) - - -- check the language - if not language then - return false, "no language!" - end - - -- check the template id - if not templateid then - return false, "no template id!" - end - - -- get interpreter - local interp = template._interpreter() - assert(interp) - - -- get project directory - local projectdir = path.absolute(option.get("project") or path.join(os.curdir(), targetname)) - - -- set filter - interp:filter():register("template", function (variable) - - -- init maps - local maps = - { - targetname = targetname - , projectdir = projectdir - } - - -- map it - local result = maps[variable] - if result ~= nil then - return result - end - - -- ok? - return variable - - end) - - -- 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 module - local module = nil - if templates then - for _, t in ipairs(templates) do - if t.name == templateid then - module = t + -- get the template name and info + local templatename = nil + local templateinfo = nil + for name, info in pairs(results) do + templatename = name + templateinfo = info break end - end - end - - -- load the template module - if not module then - return false, string.format("invalid template id: %s!", templateid) - end - - -- enter the template directory - if not module._DIRECTORY or not os.cd(module._DIRECTORY) then - return false, string.format("not found template id: %s!", templateid) - end - - -- check the template project - if not module.projectdir or not os.isdir(module.projectdir) then - return false, string.format("the template project not exists!") - end - - -- ensure the project directory - if not os.isdir(projectdir) then - os.mkdir(projectdir) - end - - -- copy the project files - local ok, errors = os.cp(path.join(module.projectdir, "*"), projectdir) - if not ok then - return false, errors - end - - -- enter the project directory - if not os.cd(projectdir) then - return false, string.format("can not enter %s!", projectdir) - end - - -- replace macros - if module.macros and module.macrofiles then - ok, errors = template._replace(module.macros, module.macrofiles) - if not ok then - return false, errors - end - end + if not templateinfo then + return nil, string.format("%s: package not found!", templatefile) + end - -- create project - if module.create then - local ok, errors = sandbox.load(module.create) - if not ok then - utils.error(errors) - return false - end - end + -- new an instance + local instance = _instance.new(templatename, templateinfo, path.directory(templatefile)) - -- append FAQ to xmake.lua - local projectfile = path.join(projectdir, "xmake.lua") - if os.isfile(projectfile) then - local file = io.open(projectfile, "a+") - if file then - file:print("") - file:print(template.faq()) - file:close() + -- insert to templates + table.insert(templates, instance) end end - - -- generate .gitignore - os.cp(path.join(os.programdir(), "scripts", "gitignore"), path.join(projectdir, ".gitignore")) - - -- ok - return true -end - --- get FAQ -function template.faq() - return [[ --- --- FAQ --- --- You can enter the project directory firstly before building project. --- --- $ cd projectdir --- --- 1. How to build project? --- --- $ xmake --- --- 2. How to configure project? --- --- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release] --- --- 3. Where is the build output directory? --- --- The default output directory is `./build` and you can configure the output directory. --- --- $ xmake f -o outputdir --- $ xmake --- --- 4. How to run and debug target after building project? --- --- $ xmake run [targetname] --- $ xmake run -d [targetname] --- --- 5. How to install target to the system directory or other output directory? --- --- $ xmake install --- $ xmake install -o installdir --- --- 6. Add some frequently-used compilation flags in xmake.lua --- --- @code --- -- add macro defination --- add_defines("NDEBUG", "_GNU_SOURCE=1") --- --- -- set warning all as error --- set_warnings("all", "error") --- --- -- set language: c99, c++11 --- set_languages("c99", "cxx11") --- --- -- set optimization: none, faster, fastest, smallest --- set_optimize("fastest") --- --- -- add include search directories --- add_includedirs("/usr/include", "/usr/local/include") --- --- -- add link libraries and search directories --- add_links("tbox", "z", "pthread") --- add_linkdirs("/usr/local/lib", "/usr/lib") --- --- -- add compilation and link flags --- add_cxflags("-stdnolib", "-fno-strict-aliasing") --- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true}) --- --- @endcode --- --- 7. If you want to known more usage about xmake, please see http://xmake.io/#/home --- - ]] + return templates end -- return module: template diff --git a/xmake/core/sandbox/modules/import/core/project/template.lua b/xmake/core/sandbox/modules/import/core/project/template.lua index 59b3d9e67..83ab6407d 100644 --- a/xmake/core/sandbox/modules/import/core/project/template.lua +++ b/xmake/core/sandbox/modules/import/core/project/template.lua @@ -47,20 +47,5 @@ function sandbox_core_project_template.templates(language) return templates end --- create project from template -function sandbox_core_project_template.create(language, templateid, targetname) - - -- create it - local ok, errors = template.create(language, templateid, targetname) - if not ok then - raise(errors) - end -end - --- get FAQ -function sandbox_core_project_template.faq() - return template.faq() -end - -- return module return sandbox_core_project_template diff --git a/xmake/scripts/faq.lua b/xmake/scripts/faq.lua new file mode 100644 index 000000000..602c89c34 --- /dev/null +++ b/xmake/scripts/faq.lua @@ -0,0 +1,68 @@ +-- +-- FAQ +-- +-- You can enter the project directory firstly before building project. +-- +-- $ cd projectdir +-- +-- 1. How to build project? +-- +-- $ xmake +-- +-- 2. How to configure project? +-- +-- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release] +-- +-- 3. Where is the build output directory? +-- +-- The default output directory is `./build` and you can configure the output directory. +-- +-- $ xmake f -o outputdir +-- $ xmake +-- +-- 4. How to run and debug target after building project? +-- +-- $ xmake run [targetname] +-- $ xmake run -d [targetname] +-- +-- 5. How to install target to the system directory or other output directory? +-- +-- $ xmake install +-- $ xmake install -o installdir +-- +-- 6. Add some frequently-used compilation flags in xmake.lua +-- +-- @code +-- -- add debug and release modes +-- add_rules("mode.debug", "mode.release") +-- +-- -- add macro defination +-- add_defines("NDEBUG", "_GNU_SOURCE=1") +-- +-- -- set warning all as error +-- set_warnings("all", "error") +-- +-- -- set language: c99, c++11 +-- set_languages("c99", "c++11") +-- +-- -- set optimization: none, faster, fastest, smallest +-- set_optimize("fastest") +-- +-- -- add include search directories +-- add_includedirs("/usr/include", "/usr/local/include") +-- +-- -- add link libraries and search directories +-- add_links("tbox") +-- add_linkdirs("/usr/local/lib", "/usr/lib") +-- +-- -- add system link libraries +-- add_syslinks("z", "pthread") +-- +-- -- add compilation and link flags +-- add_cxflags("-stdnolib", "-fno-strict-aliasing") +-- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true}) +-- +-- @endcode +-- +-- 7. If you want to known more usage about xmake, please see https://xmake.io +-- |
