diff options
| author | ruki <[email protected]> | 2022-07-23 00:02:11 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-07-23 00:02:11 +0800 |
| commit | 67a7d807940ff5ebdda3a5054265422f31fca6a8 (patch) | |
| tree | f79e4ac5c5da0f255ce0bd70f7e84c6b6816275a | |
| parent | b9327d0744edbf212120e77b345008954eac5851 (diff) | |
add some fwatcher api
| -rw-r--r-- | tests/modules/fwatcher/on_created.lua | 8 | ||||
| -rw-r--r-- | tests/modules/fwatcher/on_deleted.lua | 8 | ||||
| -rw-r--r-- | tests/modules/fwatcher/on_modified.lua | 8 | ||||
| -rw-r--r-- | xmake/core/base/fwatcher.lua | 57 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/fwatcher.lua | 26 |
5 files changed, 106 insertions, 1 deletions
diff --git a/tests/modules/fwatcher/on_created.lua b/tests/modules/fwatcher/on_created.lua new file mode 100644 index 000000000..3dee56635 --- /dev/null +++ b/tests/modules/fwatcher/on_created.lua @@ -0,0 +1,8 @@ +import("core.base.fwatcher") + +function main(watchdir) + print("watch %s ..", watchdir) + fwatcher.on_created(watchdir, function (filepath) + print(filepath, "created") + end) +end diff --git a/tests/modules/fwatcher/on_deleted.lua b/tests/modules/fwatcher/on_deleted.lua new file mode 100644 index 000000000..b69757157 --- /dev/null +++ b/tests/modules/fwatcher/on_deleted.lua @@ -0,0 +1,8 @@ +import("core.base.fwatcher") + +function main(watchdir) + print("watch %s ..", watchdir) + fwatcher.on_deleted(watchdir, function (filepath) + print(filepath, "deleted") + end) +end diff --git a/tests/modules/fwatcher/on_modified.lua b/tests/modules/fwatcher/on_modified.lua new file mode 100644 index 000000000..834c182bf --- /dev/null +++ b/tests/modules/fwatcher/on_modified.lua @@ -0,0 +1,8 @@ +import("core.base.fwatcher") + +function main(watchdir) + print("watch %s ..", watchdir) + fwatcher.on_modified(watchdir, function (filepath) + print(filepath, "modified") + end) +end diff --git a/xmake/core/base/fwatcher.lua b/xmake/core/base/fwatcher.lua index 3bd557a7f..b0c210216 100644 --- a/xmake/core/base/fwatcher.lua +++ b/xmake/core/base/fwatcher.lua @@ -220,4 +220,61 @@ function fwatcher.watchdirs(watchdirs, callback, opt) return ok, errors end +-- watch the created file path in 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.on_created("/tmp/test_*", function (filepath) +-- print(filepath) +-- end, {timeout = -1, recursion = true}) +-- @endcode +function fwatcher.on_created(watchdirs, callback, opt) + return fwatcher.watchdirs(watchdirs, function (event) + if event and event.type == fwatcher.ET_CREATE then + callback(event.path) + end + end, opt) +end + +-- watch the modified file path in 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.on_modified("/tmp/test_*", function (filepath) +-- print(filepath) +-- end, {timeout = -1, recursion = true}) +-- @endcode +function fwatcher.on_modified(watchdirs, callback, opt) + return fwatcher.watchdirs(watchdirs, function (event) + if event and event.type == fwatcher.ET_MODIFY then + callback(event.path) + end + end, opt) +end + +-- watch the deleted file path in 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.on_deleted("/tmp/test_*", function (filepath) +-- print(filepath) +-- end, {timeout = -1, recursion = true}) +-- @endcode +function fwatcher.on_deleted(watchdirs, callback, opt) + return fwatcher.watchdirs(watchdirs, function (event) + if event and event.type == fwatcher.ET_DELETE then + callback(event.path) + end + end, opt) +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 bebd24a73..9bf68442e 100644 --- a/xmake/core/sandbox/modules/import/core/base/fwatcher.lua +++ b/xmake/core/sandbox/modules/import/core/base/fwatcher.lua @@ -55,7 +55,7 @@ function sandbox_core_base_fwatcher.wait(timeout) return ok, event_or_errors end --- wait watchdirs +-- watch watchdirs function sandbox_core_base_fwatcher.watchdirs(watchdirs, callback, opt) local ok, errors = fwatcher.watchdirs(watchdirs, callback, opt) if not ok then @@ -63,6 +63,30 @@ function sandbox_core_base_fwatcher.watchdirs(watchdirs, callback, opt) end end +-- watch created file path +function sandbox_core_base_fwatcher.on_created(watchdirs, callback, opt) + local ok, errors = fwatcher.on_created(watchdirs, callback, opt) + if not ok then + raise(errors) + end +end + +-- watch modified file path +function sandbox_core_base_fwatcher.on_modified(watchdirs, callback, opt) + local ok, errors = fwatcher.on_modified(watchdirs, callback, opt) + if not ok then + raise(errors) + end +end + +-- watch deleted file path +function sandbox_core_base_fwatcher.on_deleted(watchdirs, callback, opt) + local ok, errors = fwatcher.on_deleted(watchdirs, callback, opt) + if not ok then + raise(errors) + end +end + -- return module return sandbox_core_base_fwatcher |
