diff options
| author | ruki <[email protected]> | 2016-02-13 21:41:59 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2016-02-15 19:10:27 +0800 |
| commit | 9464b2b9c7c48ff032d0f91472ba071598f1ac6d (patch) | |
| tree | 8443aada3af7bf17f1aa902375a49df9dd2e8f2b | |
| parent | 271a251ea5ead16ac3cf20a522a8fc910b3dcc85 (diff) | |
add create script for template
| -rw-r--r-- | xmake/core/base/project.lua | 18 | ||||
| -rw-r--r-- | xmake/core/base/template.lua | 92 | ||||
| -rw-r--r-- | xmake/templates/c++/console_tbox/template.lua | 11 | ||||
| -rw-r--r-- | xmake/templates/c++/shared_library_tbox/template.lua | 23 | ||||
| -rw-r--r-- | xmake/templates/c++/static_library_tbox/template.lua | 23 | ||||
| -rw-r--r-- | xmake/templates/c/console_tbox/template.lua | 11 | ||||
| -rw-r--r-- | xmake/templates/c/shared_library_tbox/template.lua | 23 | ||||
| -rw-r--r-- | xmake/templates/c/static_library_tbox/template.lua | 23 |
8 files changed, 114 insertions, 110 deletions
diff --git a/xmake/core/base/project.lua b/xmake/core/base/project.lua index 966474cdb..7929c4d2c 100644 --- a/xmake/core/base/project.lua +++ b/xmake/core/base/project.lua @@ -460,13 +460,17 @@ function project._interpreter() local result = config.get(variable) if not result or type(result) ~= "string" then - -- $(projectdir) - if variable == "projectdir" then result = xmake._PROJECT_DIR - -- $(os) - elseif variable == "os" then result = platform.os() - -- $(prefix): pass to makeconf - elseif variable == "prefix" then result = "$(prefix)" - end + -- init maps + local maps = + { + os = platform.os() + , prefix = "$(prefix)" + , projectdir = xmake._PROJECT_DIR + } + + -- map it + result = maps[variable] + end -- ok? diff --git a/xmake/core/base/template.lua b/xmake/core/base/template.lua index 19e347e20..06750f309 100644 --- a/xmake/core/base/template.lua +++ b/xmake/core/base/template.lua @@ -31,6 +31,7 @@ local table = require("base/table") local utils = require("base/utils") local string = require("base/string") local filter = require("base/filter") +local sandbox = require("base/sandbox") local interpreter = require("base/interpreter") -- get interpreter @@ -53,9 +54,10 @@ function template._interpreter() interp:api_register_add_values(nil, nil, "macrofiles") -- register api: add_keyvalues() for root - interp:api_register_add_keyvalues(nil, nil, "macros" - , "copydirs" - , "movedirs") + interp:api_register_add_keyvalues(nil, nil, "macros") + + -- register api: set_script() for root + interp:api_register_set_script(nil, nil, "createscript") -- save interpreter template._INTERPRETER = interp @@ -64,42 +66,6 @@ function template._interpreter() return interp end --- copy directories -function template._copydirs(copydirs) - - -- check - assert(copydirs) - - -- copy them - for srcdir, dstdir in pairs(copydirs) do - local ok, errors = os.cp(srcdir, dstdir) - if not ok then - return false, errors - end - end - - -- ok - return true -end - --- move directories -function template._movedirs(movedirs) - - -- check - assert(movedirs) - - -- copy them - for srcdir, dstdir in pairs(movedirs) do - local ok, errors = os.mv(srcdir, dstdir) - if not ok then - return false, errors - end - end - - -- ok - return true -end - -- replace macros function template._replace(macros, macrofiles) @@ -208,13 +174,19 @@ function template.create(language, templateid, targetname) -- set filter interp:filter_set(filter.init(function (variable) - -- replace targetname - if variable == "targetname" then - variable = targetname - -- replace packagesdir - elseif variable == "packagesdir" then - variable = xmake._PACKAGES_DIR - end + -- init maps + local maps = + { + targetname = targetname + , projectdir = xmake._PROJECT_DIR + , packagesdir = xmake._PACKAGES_DIR + } + + -- map it + local result = maps[variable] + if result ~= nil then + return result + end -- ok? return variable @@ -240,6 +212,7 @@ function template.create(language, templateid, targetname) 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(xmake._PROJECT_DIR) then @@ -257,28 +230,25 @@ function template.create(language, templateid, targetname) return false, string.format("can not enter %s!", xmake._PROJECT_DIR) end - -- copy directories - if module.copydirs then - ok, errors = template._copydirs(module.copydirs) + -- 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 - -- move directories - if module.movedirs then - ok, errors = template._movedirs(module.movedirs) - if not ok then - return false, errors - end + -- check + if not module.createscript then + utils.error("please set_createscript() first!") + return false 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 + -- create project + local ok, errors = sandbox.load(module.createscript) + if not ok then + utils.errors(errors) + return false end -- ok diff --git a/xmake/templates/c++/console_tbox/template.lua b/xmake/templates/c++/console_tbox/template.lua index 602566ca6..7bcf3001c 100644 --- a/xmake/templates/c++/console_tbox/template.lua +++ b/xmake/templates/c++/console_tbox/template.lua @@ -10,6 +10,11 @@ add_macros("targetname", "$(targetname)") -- add macro files add_macrofiles("src/xmake.lua") --- add copy directories -add_copydirs("$(packagesdir)/tbox.pkg", "pkg/tbox.pkg") -add_copydirs("$(packagesdir)/base.pkg", "pkg/base.pkg") +-- set create script +set_createscript(function () + + -- copy packages + os.cp(vformat("$(packagesdir)/tbox.pkg"), "pkg/tbox.pkg") + os.cp(vformat("$(packagesdir)/base.pkg"), "pkg/base.pkg") + +end) diff --git a/xmake/templates/c++/shared_library_tbox/template.lua b/xmake/templates/c++/shared_library_tbox/template.lua index b2fcede6d..89d0ecedf 100644 --- a/xmake/templates/c++/shared_library_tbox/template.lua +++ b/xmake/templates/c++/shared_library_tbox/template.lua @@ -9,14 +9,19 @@ add_macros("targetname", "$(targetname)") -- add macro files add_macrofiles("xmake.lua") -add_macrofiles("src/$(targetname)_demo/main.c") -add_macrofiles("src/$(targetname)_demo/xmake.lua") -add_macrofiles("src/$(targetname)/xmake.lua") +add_macrofiles("src/_demo/main.c") +add_macrofiles("src/_demo/xmake.lua") +add_macrofiles("src/_library/xmake.lua") --- add move directories -add_movedirs("src/_library", "src/$(targetname)") -add_movedirs("src/_demo", "src/$(targetname)_demo") +-- set create script +set_createscript(function () --- add copy directories -add_copydirs("$(packagesdir)/tbox.pkg", "pkg/tbox.pkg") -add_copydirs("$(packagesdir)/base.pkg", "pkg/base.pkg") + -- rename target directory + os.mv("src/_library", vformat("src/$(targetname)")) + os.mv("src/_demo", vformat("src/$(targetname)_demo")) + + -- copy packages + os.cp(vformat("$(packagesdir)/tbox.pkg"), "pkg/tbox.pkg") + os.cp(vformat("$(packagesdir)/base.pkg"), "pkg/base.pkg") + +end) diff --git a/xmake/templates/c++/static_library_tbox/template.lua b/xmake/templates/c++/static_library_tbox/template.lua index 926e78a47..6b0ae2801 100644 --- a/xmake/templates/c++/static_library_tbox/template.lua +++ b/xmake/templates/c++/static_library_tbox/template.lua @@ -9,14 +9,19 @@ add_macros("targetname", "$(targetname)") -- add macro files add_macrofiles("xmake.lua") -add_macrofiles("src/$(targetname)_demo/main.c") -add_macrofiles("src/$(targetname)_demo/xmake.lua") -add_macrofiles("src/$(targetname)/xmake.lua") +add_macrofiles("src/_demo/main.c") +add_macrofiles("src/_demo/xmake.lua") +add_macrofiles("src/_library/xmake.lua") --- add move directories -add_movedirs("src/_library", "src/$(targetname)") -add_movedirs("src/_demo", "src/$(targetname)_demo") +-- set create script +set_createscript(function () --- add copy directories -add_copydirs("$(packagesdir)/tbox.pkg", "pkg/tbox.pkg") -add_copydirs("$(packagesdir)/base.pkg", "pkg/base.pkg") + -- rename target directory + os.mv("src/_library", vformat("src/$(targetname)")) + os.mv("src/_demo", vformat("src/$(targetname)_demo")) + + -- copy packages + os.cp(vformat("$(packagesdir)/tbox.pkg"), "pkg/tbox.pkg") + os.cp(vformat("$(packagesdir)/base.pkg"), "pkg/base.pkg") + +end) diff --git a/xmake/templates/c/console_tbox/template.lua b/xmake/templates/c/console_tbox/template.lua index 602566ca6..7bcf3001c 100644 --- a/xmake/templates/c/console_tbox/template.lua +++ b/xmake/templates/c/console_tbox/template.lua @@ -10,6 +10,11 @@ add_macros("targetname", "$(targetname)") -- add macro files add_macrofiles("src/xmake.lua") --- add copy directories -add_copydirs("$(packagesdir)/tbox.pkg", "pkg/tbox.pkg") -add_copydirs("$(packagesdir)/base.pkg", "pkg/base.pkg") +-- set create script +set_createscript(function () + + -- copy packages + os.cp(vformat("$(packagesdir)/tbox.pkg"), "pkg/tbox.pkg") + os.cp(vformat("$(packagesdir)/base.pkg"), "pkg/base.pkg") + +end) diff --git a/xmake/templates/c/shared_library_tbox/template.lua b/xmake/templates/c/shared_library_tbox/template.lua index b2fcede6d..89d0ecedf 100644 --- a/xmake/templates/c/shared_library_tbox/template.lua +++ b/xmake/templates/c/shared_library_tbox/template.lua @@ -9,14 +9,19 @@ add_macros("targetname", "$(targetname)") -- add macro files add_macrofiles("xmake.lua") -add_macrofiles("src/$(targetname)_demo/main.c") -add_macrofiles("src/$(targetname)_demo/xmake.lua") -add_macrofiles("src/$(targetname)/xmake.lua") +add_macrofiles("src/_demo/main.c") +add_macrofiles("src/_demo/xmake.lua") +add_macrofiles("src/_library/xmake.lua") --- add move directories -add_movedirs("src/_library", "src/$(targetname)") -add_movedirs("src/_demo", "src/$(targetname)_demo") +-- set create script +set_createscript(function () --- add copy directories -add_copydirs("$(packagesdir)/tbox.pkg", "pkg/tbox.pkg") -add_copydirs("$(packagesdir)/base.pkg", "pkg/base.pkg") + -- rename target directory + os.mv("src/_library", vformat("src/$(targetname)")) + os.mv("src/_demo", vformat("src/$(targetname)_demo")) + + -- copy packages + os.cp(vformat("$(packagesdir)/tbox.pkg"), "pkg/tbox.pkg") + os.cp(vformat("$(packagesdir)/base.pkg"), "pkg/base.pkg") + +end) diff --git a/xmake/templates/c/static_library_tbox/template.lua b/xmake/templates/c/static_library_tbox/template.lua index 926e78a47..6b0ae2801 100644 --- a/xmake/templates/c/static_library_tbox/template.lua +++ b/xmake/templates/c/static_library_tbox/template.lua @@ -9,14 +9,19 @@ add_macros("targetname", "$(targetname)") -- add macro files add_macrofiles("xmake.lua") -add_macrofiles("src/$(targetname)_demo/main.c") -add_macrofiles("src/$(targetname)_demo/xmake.lua") -add_macrofiles("src/$(targetname)/xmake.lua") +add_macrofiles("src/_demo/main.c") +add_macrofiles("src/_demo/xmake.lua") +add_macrofiles("src/_library/xmake.lua") --- add move directories -add_movedirs("src/_library", "src/$(targetname)") -add_movedirs("src/_demo", "src/$(targetname)_demo") +-- set create script +set_createscript(function () --- add copy directories -add_copydirs("$(packagesdir)/tbox.pkg", "pkg/tbox.pkg") -add_copydirs("$(packagesdir)/base.pkg", "pkg/base.pkg") + -- rename target directory + os.mv("src/_library", vformat("src/$(targetname)")) + os.mv("src/_demo", vformat("src/$(targetname)_demo")) + + -- copy packages + os.cp(vformat("$(packagesdir)/tbox.pkg"), "pkg/tbox.pkg") + os.cp(vformat("$(packagesdir)/base.pkg"), "pkg/base.pkg") + +end) |
