diff options
| author | ruki <[email protected]> | 2022-07-23 00:57:52 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-07-23 00:57:52 +0800 |
| commit | 762532f952b8319fde777f657a4fe925dd7a4560 (patch) | |
| tree | 3aacf79496c1f392dfe83c2318f1be40fd89d8b9 | |
| parent | 73f9ee4974fcf8899baf2cf34dbbdf739eaf4975 (diff) | |
add watchdirs
| -rw-r--r-- | tests/modules/fwatcher/watchdirs.lua | 8 | ||||
| -rw-r--r-- | xmake/core/base/fwatcher.lua | 56 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/fwatcher.lua | 8 |
3 files changed, 72 insertions, 0 deletions
diff --git a/tests/modules/fwatcher/watchdirs.lua b/tests/modules/fwatcher/watchdirs.lua new file mode 100644 index 000000000..cbe8914ef --- /dev/null +++ b/tests/modules/fwatcher/watchdirs.lua @@ -0,0 +1,8 @@ +import("core.base.fwatcher") + +function main(watchdir) + print("watch %s ..", watchdir) + fwatcher.watchdirs(watchdir, function (event) + print(event) + end) +end diff --git a/xmake/core/base/fwatcher.lua b/xmake/core/base/fwatcher.lua index 9de827ae0..3bd557a7f 100644 --- a/xmake/core/base/fwatcher.lua +++ b/xmake/core/base/fwatcher.lua @@ -23,6 +23,7 @@ local fwatcher = fwatcher or {} local _instance = _instance or {} -- load modules +local os = require("base/os") local string = require("base/string") local coroutine = require("base/coroutine") local scheduler = require("base/scheduler") @@ -152,16 +153,71 @@ function _instance:_ensure_opened() return true end +-- add watchdir function fwatcher.add(watchdir, opt) return _instance:add(watchdir, opt) end +-- remove watchdir function fwatcher.remove(watchdir) return _instance:remove(watchdir) end +-- wait event function fwatcher.wait(timeout) return _instance:wait(timeout) end +-- watch directories +-- +-- @param watchdirs the watch directories, pattern path string or path list +-- @param callback the event callback +-- @param opt the option, e.g. {timeout = -1, recursion = true} +-- +-- @code +-- fwatcher.watchdirs("/tmp/test_*", function (event) +-- print(event) +-- end, {timeout = -1, recursion = true}) +-- @endcode +function fwatcher.watchdirs(watchdirs, callback, opt) + + -- add watch directories + opt = opt or {} + if type(watchdirs) == "string" then + watchdirs = os.dirs(watchdirs) + end + local ok = true + local errors = nil + for _, watchdir in ipairs(watchdirs) do + ok, errors = fwatcher.add(watchdir, opt.recursion) + if not ok then + break + end + end + + -- do watch + while ok do + local result, event_or_errors = fwatcher.wait(opt.timeout or -1) + if result < 0 then + ok = false + errors = event_or_errors + break + end + if result > 0 then + callback(event_or_errors) + end + end + + -- remove watch directories + for _, watchdir in ipairs(watchdirs) do + local result, rm_errors = fwatcher.remove(watchdir) + if not result then + ok = false + errors = errors or rm_errors + break + end + end + return ok, errors +end + return fwatcher diff --git a/xmake/core/sandbox/modules/import/core/base/fwatcher.lua b/xmake/core/sandbox/modules/import/core/base/fwatcher.lua index af3aa4151..bebd24a73 100644 --- a/xmake/core/sandbox/modules/import/core/base/fwatcher.lua +++ b/xmake/core/sandbox/modules/import/core/base/fwatcher.lua @@ -55,6 +55,14 @@ function sandbox_core_base_fwatcher.wait(timeout) return ok, event_or_errors end +-- wait watchdirs +function sandbox_core_base_fwatcher.watchdirs(watchdirs, callback, opt) + local ok, errors = fwatcher.watchdirs(watchdirs, callback, opt) + if not ok then + raise(errors) + end +end + -- return module return sandbox_core_base_fwatcher |
