diff options
| author | ruki <[email protected]> | 2015-06-04 14:08:40 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2015-06-04 14:08:40 +0800 |
| commit | 1481ec887abef9ce33f4cff93f7a0e6ab04b5741 (patch) | |
| tree | 609560fefb90242fb8deabd525201e00cd078b12 | |
| parent | 2f468e35a6c681a22308d1c6d1d37764c386c00c (diff) | |
modify some tool interfaces
| -rw-r--r-- | xmake/scripts/action/_lua.lua | 26 | ||||
| -rw-r--r-- | xmake/scripts/base/makefile.lua | 2 | ||||
| -rw-r--r-- | xmake/scripts/tools/cp.lua | 26 | ||||
| -rw-r--r-- | xmake/scripts/tools/echo.lua | 26 | ||||
| -rw-r--r-- | xmake/scripts/tools/mkdir.lua | 27 | ||||
| -rw-r--r-- | xmake/scripts/tools/mv.lua | 25 | ||||
| -rw-r--r-- | xmake/scripts/tools/rm.lua | 27 | ||||
| -rw-r--r-- | xmake/scripts/tools/rmdir.lua | 27 | ||||
| -rw-r--r-- | xmake/scripts/tools/tools.lua | 14 | ||||
| -rw-r--r-- | xmake/scripts/tools/verbose.lua | 28 |
10 files changed, 170 insertions, 58 deletions
diff --git a/xmake/scripts/action/_lua.lua b/xmake/scripts/action/_lua.lua index 2f7597b03..f693a1514 100644 --- a/xmake/scripts/action/_lua.lua +++ b/xmake/scripts/action/_lua.lua @@ -49,14 +49,6 @@ function _lua.done() arguments = {} end - -- init new environment - local newenv = {} - setmetatable(newenv, {__index = _G}) - newenv.os = os - newenv.path = path - newenv.utils = utils - newenv.string = string - -- is string script? if options.string then @@ -66,7 +58,6 @@ function _lua.done() -- load and run string local script = loadstring(options.script) if script then - setfenv(script, newenv) return script(arguments) end else @@ -83,10 +74,23 @@ function _lua.done() -- load and run the script file if os.isfile(file) then + + -- load script local script = loadfile(file) if script then - setfenv(script, newenv) - return script(arguments) + + -- load module + local module = script() + if module then + + -- init module + if module.init then + module.init() + end + + -- done module + return module.main(arguments) + end end end end diff --git a/xmake/scripts/base/makefile.lua b/xmake/scripts/base/makefile.lua index aa598c916..17ec4aeda 100644 --- a/xmake/scripts/base/makefile.lua +++ b/xmake/scripts/base/makefile.lua @@ -247,7 +247,7 @@ function makefile.build(target) assert(buildir) -- load make - local make = tools.load(platform.tool("make")) + local make = tools.get("make") if not make then utils.error("not found the make command!") return false diff --git a/xmake/scripts/tools/cp.lua b/xmake/scripts/tools/cp.lua index 344e3e1dd..4d400a19b 100644 --- a/xmake/scripts/tools/cp.lua +++ b/xmake/scripts/tools/cp.lua @@ -20,11 +20,25 @@ -- @file cp.lua -- --- cp it -local pathes = ... -if pathes and table.getn(pathes) == 2 then - return os.cp(pathes[1], pathes[2]) +-- define module: cp +local cp = cp or {} + +-- load modules +local os = require("base/os") + +-- the main function +function cp.main(...) + + -- cp it + local pathes = ... + if pathes and table.getn(pathes) == 2 then + return os.cp(pathes[1], pathes[2]) + end + + -- failed + return false + end --- failed -return false +-- return module: cp +return cp diff --git a/xmake/scripts/tools/echo.lua b/xmake/scripts/tools/echo.lua index 2180ce0d4..197cebedc 100644 --- a/xmake/scripts/tools/echo.lua +++ b/xmake/scripts/tools/echo.lua @@ -20,11 +20,25 @@ -- @file echo.lua -- --- echo all -for _, v in ipairs(...) do - io.write(string.format("%s ", v:gsub("%%20", " "))) +-- define module: echo +local echo = echo or {} + +-- load modules +local io = require("base/io") +local string = require("base/string") + +-- the main function +function echo.main(...) + + -- echo all + for _, v in ipairs(...) do + io.write(string.format("%s ", v:gsub("%%20", " "))) + end + io.write("\n") + + -- ok + return true end -io.write("\n") --- ok -return true +-- return module: echo +return echo diff --git a/xmake/scripts/tools/mkdir.lua b/xmake/scripts/tools/mkdir.lua index 0cd1eeafe..4b5ac1a7f 100644 --- a/xmake/scripts/tools/mkdir.lua +++ b/xmake/scripts/tools/mkdir.lua @@ -20,14 +20,27 @@ -- @file mkdir.lua -- --- mkdir all -for _, dir in ipairs(...) do - if not os.exists(dir) then - if not os.mkdir(dir) then - return false +-- define module: mkdir +local mkdir = mkdir or {} + +-- load modules +local os = require("base/os") + +-- the main function +function mkdir.main(...) + + -- mkdir all + for _, dir in ipairs(...) do + if not os.exists(dir) then + if not os.mkdir(dir) then + return false + end end end + + -- ok + return true end --- ok -return true +-- return module: mkdir +return mkdir diff --git a/xmake/scripts/tools/mv.lua b/xmake/scripts/tools/mv.lua index 5fc204f7d..2fa2be735 100644 --- a/xmake/scripts/tools/mv.lua +++ b/xmake/scripts/tools/mv.lua @@ -20,11 +20,24 @@ -- @file mv.lua -- --- mv it -local pathes = ... -if pathes and table.getn(pathes) == 2 then - return os.mv(pathes[1], pathes[2]) +-- define module: mv +local mv = mv or {} + +-- load modules +local os = require("base/os") + +-- the main function +function mv.main(...) + + -- mv it + local pathes = ... + if pathes and table.getn(pathes) == 2 then + return os.mv(pathes[1], pathes[2]) + end + + -- failed + return false end --- failed -return false +-- return module: mv +return mv diff --git a/xmake/scripts/tools/rm.lua b/xmake/scripts/tools/rm.lua index 7759be01d..b05289b3b 100644 --- a/xmake/scripts/tools/rm.lua +++ b/xmake/scripts/tools/rm.lua @@ -20,14 +20,27 @@ -- @file rm.lua -- --- rm all -for _, file_or_dir in ipairs(...) do - if os.exists(file_or_dir) then - if not os.rm(file_or_dir) then - return false +-- define module: rm +local rm = rm or {} + +-- load modules +local os = require("base/os") + +-- the main function +function rm.main(...) + + -- rm all + for _, file_or_dir in ipairs(...) do + if os.exists(file_or_dir) then + if not os.rm(file_or_dir) then + return false + end end end + + -- ok + return true end --- ok -return true +-- return module: rm +return rm diff --git a/xmake/scripts/tools/rmdir.lua b/xmake/scripts/tools/rmdir.lua index 9df57b1c9..23eb8ca61 100644 --- a/xmake/scripts/tools/rmdir.lua +++ b/xmake/scripts/tools/rmdir.lua @@ -20,14 +20,27 @@ -- @file rmdir.lua -- --- rmdir all -for _, dir in ipairs(...) do - if os.isdir(dir) then - if not os.rmdir(dir) then - return false +-- define module: rmdir +local rmdir = rmdir or {} + +-- load modules +local os = require("base/os") + +-- the main function +function rmdir.main(...) + + -- rmdir all + for _, dir in ipairs(...) do + if os.isdir(dir) then + if not os.rmdir(dir) then + return false + end end end + + -- ok + return true end --- ok -return true +-- return module: rmdir +return rmdir diff --git a/xmake/scripts/tools/tools.lua b/xmake/scripts/tools/tools.lua index 09e43dc94..4c0d0da14 100644 --- a/xmake/scripts/tools/tools.lua +++ b/xmake/scripts/tools/tools.lua @@ -101,6 +101,12 @@ function tools.load(name, root) -- check assert(name) + -- get it directly from cache dirst + tools._TOOLS = tools._TOOLS or {} + if tools._TOOLS[name] then + return tools._TOOLS[name] + end + -- find the tool file path local toolpath = tools.find(name, root) @@ -121,10 +127,18 @@ function tools.load(name, root) tool.init() end + -- save tool to the cache + tools._TOOLS[name] = tool + -- ok? return tool end end +-- get the given tool from the current platform +function tools.get(name) + return tools.load(platform.tool(name)) +end + -- return module: tools return tools diff --git a/xmake/scripts/tools/verbose.lua b/xmake/scripts/tools/verbose.lua index 0054ba620..a5fa6e606 100644 --- a/xmake/scripts/tools/verbose.lua +++ b/xmake/scripts/tools/verbose.lua @@ -20,13 +20,27 @@ -- @file verbose.lua -- --- verbose all -if xmake._OPTIONS.verbose then - for _, v in ipairs(...) do - io.write(string.format("%s ", v:gsub("%%20", " "))) + +-- define module: verbose +local verbose = verbose or {} + +-- load modules +local io = require("base/io") + +-- the main function +function verbose.main(...) + + -- verbose all + if xmake._OPTIONS.verbose then + for _, v in ipairs(...) do + io.write(string.format("%s ", v:gsub("%%20", " "))) + end + io.write("\n") end - io.write("\n") + + -- ok + return true end --- ok -return true +-- return module: verbose +return verbose |
