diff options
| author | ruki <[email protected]> | 2021-02-20 11:14:26 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-02-20 11:14:26 +0800 |
| commit | 3749765647847d0c0155f34dde8d1e32447b87b9 (patch) | |
| tree | a2f3231ecfefeda9f20a72569e21e1f414953137 | |
| parent | 5f25a00cd3f3708ad133dbd1c376cfdd34a3de94 (diff) | |
add tip and progress tip
| -rw-r--r-- | xmake/modules/private/utils/batchcmds.lua | 93 | ||||
| -rw-r--r-- | xmake/plugins/project/clang/compile_commands.lua | 8 | ||||
| -rw-r--r-- | xmake/rules/protobuf/proto.lua | 4 | ||||
| -rw-r--r-- | xmake/rules/protobuf/xmake.lua | 6 |
4 files changed, 87 insertions, 24 deletions
diff --git a/xmake/modules/private/utils/batchcmds.lua b/xmake/modules/private/utils/batchcmds.lua index 4b019e2ad..f4f5b84a8 100644 --- a/xmake/modules/private/utils/batchcmds.lua +++ b/xmake/modules/private/utils/batchcmds.lua @@ -21,10 +21,68 @@ -- imports import("core.base.option") import("core.base.object") +import("core.base.tty") +import("core.base.colors") import("core.project.depend") +import("core.theme.theme") +import("private.utils.progress", {alias = "progress_utils"}) -- define module -local batchcmds = batchcmds or object { _init = {"_TARGET", "_CMDS", "_DEPS", "_TIPS"}} +local batchcmds = batchcmds or object { _init = {"_TARGET", "_CMDS", "_DEPS", "_tip"}} + +-- show the tip message +local function _showtip(tip, progress) + if option.get("verbose") then + cprint(tip) + else + local is_scroll = _g.is_scroll + if is_scroll == nil then + is_scroll = theme.get("text.build.progress_style") == "scroll" + _g.is_scroll = is_scroll + end + if is_scroll then + cprint(tip) + else + tty.erase_line_to_start().cr() + local msg = tip + local msg_plain = colors.translate(msg, {plain = true}) + local maxwidth = os.getwinsize().width + if #msg_plain <= maxwidth then + cprintf(msg) + else + -- windows width is too small? strip the partial message in middle + local partlen = math.floor(maxwidth / 2) - 3 + local sep = msg_plain:sub(partlen + 1, #msg_plain - partlen - 1) + local split = msg:split(sep, {plain = true, strict = true}) + cprintf(table.concat(split, "...")) + end + if math.floor(progress) == 100 then + print("") + _g.showing_without_scroll = false + else + _g.showing_without_scroll = true + end + io.flush() + end + end +end + +-- run the given commands +local function _runcmds(cmds, opt) + for _, cmd in ipairs(cmds) do + local tip = cmd.tip + if tip then + _showtip(tip, cmd.progress) + end + if cmd.program then + 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 +end -- is empty? no commands function batchcmds:empty() @@ -41,6 +99,20 @@ function batchcmds:add_cmd(program, argv, opt) table.insert(self:cmds(), {program = program, argv = argv, runopt = opt}) end +-- add command tip +function batchcmds:add_tip(format, ...) + local tip = string.format(format, ...) + table.insert(self:cmds(), {tip = tip}) +end + +-- add command tip with progress +function batchcmds:add_progress_tip(progress, format, ...) + if progress then + local tip = progress_utils.text(progress, format, ...) + table.insert(self:cmds(), {tip = tip, progress = progress}) + end +end + -- get deps function batchcmds:deps() return self._DEPS @@ -82,27 +154,12 @@ function batchcmds:run(opt) 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() + _runcmds(self:cmds(), opt) end, self:deps()) else - _runcmds() + _runcmds(self:cmds(), opt) end end diff --git a/xmake/plugins/project/clang/compile_commands.lua b/xmake/plugins/project/clang/compile_commands.lua index 1d3f02b6d..41d83bd57 100644 --- a/xmake/plugins/project/clang/compile_commands.lua +++ b/xmake/plugins/project/clang/compile_commands.lua @@ -102,7 +102,9 @@ function _make_commands_for_objectrules(jsonfile, target, sourcebatch, suffix) script(target, batchcmds_, sourcebatch, {}) if not batchcmds_:empty() then for _, cmd in ipairs(batchcmds_:cmds()) do - _make_arguments(jsonfile, table.join(cmd.program, cmd.argv)) + if cmd.program then + _make_arguments(jsonfile, table.join(cmd.program, cmd.argv)) + end end end end @@ -118,7 +120,9 @@ function _make_commands_for_objectrules(jsonfile, target, sourcebatch, suffix) script(target, batchcmds_, sourcefile, {}) if not batchcmds_:empty() then for _, cmd in ipairs(batchcmds_:cmds()) do - _make_arguments(jsonfile, table.join(cmd.program, cmd.argv)) + if cmd.program then + _make_arguments(jsonfile, table.join(cmd.program, cmd.argv)) + end end end end diff --git a/xmake/rules/protobuf/proto.lua b/xmake/rules/protobuf/proto.lua index f449e0ebc..faa0cd2ee 100644 --- a/xmake/rules/protobuf/proto.lua +++ b/xmake/rules/protobuf/proto.lua @@ -103,8 +103,8 @@ function buildcmd(target, batchcmds, sourcefile_proto, opt, sourcekind) table.insert(argv, (sourcekind == "cxx" and "--cpp_out=" or "--c_out=") .. sourcefile_dir) -- add commands - local progress_text = opt.progress and progress.text(opt.progress, "${color.build.object}compiling.proto %s", sourcefile_proto) - batchcmds:add_cmd(protoc, argv, {tips = progress_text}) + batchcmds:add_progress_tip(opt.progress, "${color.build.object}compiling.proto %s", sourcefile_proto) + batchcmds:add_cmd(protoc, argv) batchcmds:add_cmd(compinst:compargv(sourcefile_cx, objectfile, {compflags = compflags})) -- add deps diff --git a/xmake/rules/protobuf/xmake.lua b/xmake/rules/protobuf/xmake.lua index b9d0bafdb..d812cc1a7 100644 --- a/xmake/rules/protobuf/xmake.lua +++ b/xmake/rules/protobuf/xmake.lua @@ -24,10 +24,11 @@ rule("protobuf.cpp") -- set extension set_extensions(".proto") + --[[ -- build protobuf file before_build_file(function (target, sourcefile_proto, opt) import("proto").build(target, sourcefile_proto, opt, "cxx") - end) + end)]] -- generate build commands before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt) @@ -41,10 +42,11 @@ rule("protobuf.c") -- set extension set_extensions(".proto") + --[[ -- build protobuf file before_build_file(function (target, sourcefile_proto, opt) import("proto").build(target, sourcefile_proto, opt, "cc") - end) + end)]] -- generate build commands before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt) |
