diff options
| author | ruki <[email protected]> | 2016-05-31 20:18:34 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2016-05-31 20:18:34 +0800 |
| commit | a976651039f008aa7e0e7ec6cfee57bfb2aea6fa (patch) | |
| tree | ee79d06f607042c95c11e84bad519677be414ddb | |
| parent | 5769dba5932aad3a97c8db36c80a15064b71f556 (diff) | |
impl macro plugin
| -rw-r--r-- | xmake/core/project/task.lua | 2 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/project/history.lua | 7 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/os.lua | 15 | ||||
| -rwxr-xr-x | xmake/plugins/macro/main.lua | 252 | ||||
| -rwxr-xr-x | xmake/plugins/macro/xmake.lua | 31 |
5 files changed, 304 insertions, 3 deletions
diff --git a/xmake/core/project/task.lua b/xmake/core/project/task.lua index a50f4e6e2..76c2819f7 100644 --- a/xmake/core/project/task.lua +++ b/xmake/core/project/task.lua @@ -131,7 +131,7 @@ function task._translate_menu(menu) -- add common options table.insert(options, 1, {'v', "verbose", "k", nil, "Print lots of verbose information." }) - table.insert(options, 1, {'b', "backtrace", "k", nil, "Print backtrace information for debugging." }) + table.insert(options, 1, {nil, "backtrace", "k", nil, "Print backtrace information for debugging." }) table.insert(options, 2, {nil, "version", "k", nil, "Print the version number and exit." }) table.insert(options, 3, {'h', "help", "k", nil, "Print this help message and exit." }) table.insert(options, 4, {}) diff --git a/xmake/core/sandbox/modules/import/core/project/history.lua b/xmake/core/sandbox/modules/import/core/project/history.lua index fd10a22e7..ee1d6d9b4 100644 --- a/xmake/core/sandbox/modules/import/core/project/history.lua +++ b/xmake/core/sandbox/modules/import/core/project/history.lua @@ -39,5 +39,12 @@ function sandbox_core_project_history.load(key) return history.load(key) end +-- save the history data +function sandbox_core_project_history.save(key, value) + + -- save it + history.save(key, value) +end + -- return module return sandbox_core_project_history diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index df2093f9f..923cc5852 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -216,7 +216,20 @@ function sandbox_os.corun(cmd, ...) end end --- execute shell +-- execute shell +function sandbox_os.exec(cmd, ...) + + -- make command + cmd = vformat(cmd, ...) + + -- run it + local retval = os.execute(cmd) + if retval ~= 0 then + os.raise("exec %s failed(%d)!", cmd, retval) + end +end + +-- execute shell and return error code function sandbox_os.execute(cmd, ...) -- make command diff --git a/xmake/plugins/macro/main.lua b/xmake/plugins/macro/main.lua new file mode 100755 index 000000000..4462929c7 --- /dev/null +++ b/xmake/plugins/macro/main.lua @@ -0,0 +1,252 @@ +--!The Automatic Cross-platform Build Tool +-- +-- XMake is free software; you can redistribute it and/or modify +-- it under the terms of the GNU Lesser General Public License as published by +-- the Free Software Foundation; either version 2.1 of the License, or +-- (at your option) any later version. +-- +-- XMake is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU Lesser General Public License for more details. +-- +-- You should have received a copy of the GNU Lesser General Public License +-- along with XMake; +-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a> +-- +-- Copyright (C) 2015 - 2016, ruki All rights reserved. +-- +-- @author ruki +-- @file main.lua +-- + +-- imports +import("core.base.option") +import("core.project.cache") +import("core.project.config") +import("core.project.history") + +-- the macro directory +function _directory() + + -- get it + return path.join(config.directory(), "macros") +end + +-- the macro file +function _file(macroname) + + -- is anonymous? + if macroname == '.' then + macroname = "anonymous" + end + + -- get it + return path.join(_directory(), macroname .. ".lua") +end + +-- list macros +function _list() + + -- trace + print("macros:") + + -- find all macros + local macrofiles = os.match(path.join(_directory(), "*.lua")) + for _, macrofile in ipairs(macrofiles) do + + -- get macro name + local macroname = path.basename(macrofile) + if macroname == "anonymous" then + macroname = ".<anonymous>" + end + + -- show it + print(" " .. macroname) + end +end + +-- show macro +function _show(macroname) + + -- show it + local file = _file(macroname) + if os.isfile(file) then + io.cat(file) + else + raise("macro(%s) not found!", macroname) + end +end + +-- delete macro +function _delete(macroname) + + -- remove it + os.rm(_file(macroname)) + + -- trace + print("delete macro(%s) ok!", macroname) +end + +-- import macro +function _import(macrofile, macroname) + + -- import it + os.cp(macrofile, _file(macroname)) + + -- trace + print("import macro(%s) ok!", macroname) +end + +-- export macro +function _export(macrofile, macroname) + + -- export it + os.cp(_file(macroname), macrofile) + + -- trace + print("export macro(%s) ok!", macroname) +end + +-- begin to record macro +function _begin() + + -- patch begin tag to the history: cmdlines + history.save("cmdlines", "__macro_begin__") +end + +-- end to record macro +function _end(macroname) + + -- load the history: cmdlines + local cmdlines = history.load("cmdlines") + + -- get the last macro block + local begin = false + local block = {} + if cmdlines then + local total = #cmdlines + local index = total + while index ~= 0 do + + -- the command line + local cmdline = cmdlines[index] + + -- found begin? break it + if cmdline == "__macro_begin__" then + begin = true + break + end + + -- found end? break it + if cmdline == "__macro_end__" then + break + end + + -- ignore "xmake m .." and "xmake macro .." + if not cmdline:find("xmake%s+macro%s*") and not cmdline:find("xmake%s+m%s*") then + + -- save this command line to block + table.insert(block, 1, cmdline) + end + + -- the previous line + index = index - 1 + end + end + + -- the begin tag not found? + if not begin then + raise("please run: 'xmake macro --begin' first!") + end + + -- patch end tag to the history: cmdlines + history.save("cmdlines", "__macro_end__") + + -- open the macro file + local file = io.open(_file(macroname), "w") + + -- save the macro begin + file:print("function main(argv)") + + -- save the macro block + for _, cmdline in ipairs(block) do + + -- save command line + file:print(" os.exec(\"%s\")", cmdline) + end + + -- save the macro end + file:print("end") + + -- exit the macro file + file:close() + + -- show this macro + _show(macroname) + + -- trace + print("define macro(%s) ok!", macroname) +end + +-- run macro +function _run(macroname) + + -- is anonymous? + if macroname == '.' then + macroname = "anonymous" + end + + -- import macro + macro = import(macroname, {rootdir = _directory()}) + + -- run macro + macro.main(option.get("argv")) + + -- trace + print("run macro(%s) ok!", macroname) +end + +-- main +function main() + + -- list macros + if option.get("list") then + + _list() + + -- show macro + elseif option.get("show") then + + _show(option.get("name")) + + -- delete macro + elseif option.get("delete") then + + _delete(option.get("name")) + + -- import macro + elseif option.get("import") then + + _import(option.get("import"), option.get("name")) + + -- export macro + elseif option.get("export") then + + _export(option.get("export"), option.get("name")) + + -- begin to record macro + elseif option.get("begin") then + + _begin() + + -- end to record macro + elseif option.get("end") then + + _end(option.get("name")) + + -- run macro + else + _run(option.get("name")) + end +end diff --git a/xmake/plugins/macro/xmake.lua b/xmake/plugins/macro/xmake.lua index d0148bddc..dc4f9ca1a 100755 --- a/xmake/plugins/macro/xmake.lua +++ b/xmake/plugins/macro/xmake.lua @@ -26,6 +26,9 @@ task("macro") -- set category set_category("plugin") + -- on run + on_run("main") + -- set menu set_menu({ -- usage @@ -40,6 +43,32 @@ task("macro") -- options , options = { - {nil, "name", "v", nil, "Configure for the given macro name." } + {'b', "begin", "k", nil, "Start to record macro." + , ".e.g" + , "Record macro with name: test" + , " xmake macro --begin" + , " xmake config --plat=macosx" + , " xmake clean" + , " xmake -r" + , " xmake package" + , " xmake macro --end test" } + , {'e', "end", "k", nil, "Stop to record macro." } + , {} + , {nil, "show", "k", nil, "Show the content of the given macro." } + , {'l', "list", "k", nil, "List all macros." } + , {'d', "delete", "k", nil, "Delete the given macro." } + , {} + , {nil, "import", "kv", nil, "Import the given macro file." + , ".e.g" + , " xmake macro --import=/xxx/macro.lua test" } + , {nil, "export", "kv", nil, "Export the given macro to file." + , ".e.g" + , " xmake macro --export=/xxx/macro.lua test" } + , {} + , {'a', "argv", "kv", nil, "Set the macro arguments." } + , {nil, "name", "v", ".", "The given macro name." + , ".e.g" + , " Run the given macro: xmake macro test" + , " Run the anonymous macro: xmake macro ." } } }) |
