summaryrefslogtreecommitdiff
path: root/xmake/modules/private/utils/batchcmds.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2021-02-20 10:25:49 +0800
committerruki <[email protected]>2021-02-20 10:25:49 +0800
commit5f25a00cd3f3708ad133dbd1c376cfdd34a3de94 (patch)
tree7815e0fce42dec2fb8a2b749959d0f50f0678e2b /xmake/modules/private/utils/batchcmds.lua
parentbf08fd02abb79831f72598a9347e9a7b3ac87989 (diff)
add batchcmds
Diffstat (limited to 'xmake/modules/private/utils/batchcmds.lua')
-rw-r--r--xmake/modules/private/utils/batchcmds.lua116
1 files changed, 116 insertions, 0 deletions
diff --git a/xmake/modules/private/utils/batchcmds.lua b/xmake/modules/private/utils/batchcmds.lua
new file mode 100644
index 000000000..4b019e2ad
--- /dev/null
+++ b/xmake/modules/private/utils/batchcmds.lua
@@ -0,0 +1,116 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file batchcmds.lua
+--
+
+-- imports
+import("core.base.option")
+import("core.base.object")
+import("core.project.depend")
+
+-- define module
+local batchcmds = batchcmds or object { _init = {"_TARGET", "_CMDS", "_DEPS", "_TIPS"}}
+
+-- is empty? no commands
+function batchcmds:empty()
+ return #self:cmds() == 0
+end
+
+-- get commands
+function batchcmds:cmds()
+ return self._CMDS
+end
+
+-- add command
+function batchcmds:add_cmd(program, argv, opt)
+ table.insert(self:cmds(), {program = program, argv = argv, runopt = opt})
+end
+
+-- get deps
+function batchcmds:deps()
+ return self._DEPS
+end
+
+-- add dependent files
+function batchcmds:add_depfiles(...)
+ local deps = self._DEPS or {}
+ deps.files = deps.files or {}
+ table.join2(deps.files, ...)
+ self._DEPS = deps
+end
+
+-- add dependent values
+function batchcmds:add_depvalues(...)
+ local deps = self._DEPS or {}
+ deps.values = deps.values or {}
+ table.join2(deps.values, ...)
+ self._DEPS = deps
+end
+
+-- set the last mtime of dependent files and values
+function batchcmds:set_depmtime(lastmtime)
+ local deps = self._DEPS or {}
+ deps.lastmtime = lastmtime
+ self._DEPS = deps
+end
+
+-- set cache file of depend info
+function batchcmds:set_depcache(cachefile)
+ local deps = self._DEPS or {}
+ deps.dependfile = cachefile
+ self._DEPS = deps
+end
+
+-- run cmds
+function batchcmds:run(opt)
+ opt = opt or {}
+ if self:empty() then
+ return
+ end
+ local function _runcmds()
+ for _, cmd in ipairs(self:cmds()) do
+ local tips = cmd.runopt and cmd.runopt.tips
+ if tips then
+ for _, tip in ipairs(tips) do
+ cprint(tip)
+ end
+ end
+ if opt.dryrun then
+ vprint(os.args(table.join(cmd.program, cmd.argv)))
+ else
+ os.vrunv(cmd.program, cmd.argv, cmd.runopt)
+ end
+ end
+ end
+ if self:deps() then
+ depend.on_changed(function ()
+ _runcmds()
+ end, self:deps())
+ else
+ _runcmds()
+ end
+end
+
+-- new a batch commands for rule/xx_xxcmd_xxx()
+--
+-- @params opt options, e.g. {target = ..}
+--
+function new(opt)
+ opt = opt or {}
+ return batchcmds {_TARGET = opt.target, _CMDS = {}}
+end