diff options
| author | ruki <[email protected]> | 2022-07-23 10:40:25 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-07-23 10:40:25 +0800 |
| commit | 9731e4d5f8e667dd81add7aa644dbfe09bd3975b (patch) | |
| tree | aae117ef084bf740fa52effa833674447fe4ddc6 /xmake/plugins | |
| parent | 9fce77f97024a1c76fd60d41bc52fc3c3ceb329a (diff) | |
impl watch action
Diffstat (limited to 'xmake/plugins')
| -rw-r--r-- | xmake/plugins/watch/main.lua | 59 | ||||
| -rw-r--r-- | xmake/plugins/watch/xmake.lua | 2 |
2 files changed, 60 insertions, 1 deletions
diff --git a/xmake/plugins/watch/main.lua b/xmake/plugins/watch/main.lua index 9e297a02a..835ba531d 100644 --- a/xmake/plugins/watch/main.lua +++ b/xmake/plugins/watch/main.lua @@ -21,7 +21,66 @@ -- imports import("core.base.option") import("core.base.fwatcher") +import("core.project.config") + +-- get watchdirs +function _get_watchdirs() + local results = {} + local watchdirs = option.get("watchdirs") + if watchdirs then + for _, watchdir in ipairs(path.splitenv(watchdirs)) do + local dirs = os.dirs(watchdir) + if dirs then + table.join2(results, dirs) + end + end + elseif os.isfile(os.projectfile()) then + watchdirs = os.dirs(path.join(os.projectdir(), "*|.*")) + for _, watchdir in ipairs(watchdirs) do + local buildir = path.absolute(config.buildir()) + if path.absolute(watchdir) ~= buildir then + table.insert(results, watchdir) + end + end + else + table.insert(results, os.curdir()) + end + return results +end + +-- run command +function _run_command() + if os.isfile(os.projectfile()) then + os.execv(os.programfile(), {"build", "-w"}) + end +end function main() + -- get watchdirs + local watchdirs = _get_watchdirs() + if #watchdirs > 0 then + for _, watchdir in ipairs(watchdirs) do + cprint("watching ${bright}%s${clear} ..", watchdir) + end + else + raise("no any watch directories!") + end + + -- do watch + fwatcher.watchdirs(watchdirs, function (event) + local status + if event.type == fwatcher.ET_CREATE then + status = "created" + elseif event.type == fwatcher.ET_MODIFY then + status = "modified" + elseif event.type == fwatcher.ET_DELETE then + status = "deleted" + end + print(event.path, status) + + -- run command + _run_command() + + end, {recursion = option.get("recursion")}) end diff --git a/xmake/plugins/watch/xmake.lua b/xmake/plugins/watch/xmake.lua index f5a48ba09..9d2f8b5ce 100644 --- a/xmake/plugins/watch/xmake.lua +++ b/xmake/plugins/watch/xmake.lua @@ -33,7 +33,7 @@ task("watch") {'s', "script" , "kv" , nil , "Run the given lua script file.", "e.g.", " $ xmake watch -s /tmp/watch.lua"}, - {'d', "dir" , "kv" , nil , "Set the given watch directories, the project directories will be watched by default.", + {'d', "watchdirs" , "kv" , nil , "Set the given watch directories, the project directories will be watched by default.", "e.g.", " $ xmake watch -d src", " $ xmake watch -d 'src/*" .. path.envsep() .. "tests/**/subdir'"}, |
