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 /xmake/core/base | |
| parent | 73f9ee4974fcf8899baf2cf34dbbdf739eaf4975 (diff) | |
add watchdirs
Diffstat (limited to 'xmake/core/base')
| -rw-r--r-- | xmake/core/base/fwatcher.lua | 56 |
1 files changed, 56 insertions, 0 deletions
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 |
