summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-07-23 10:40:25 +0800
committerruki <[email protected]>2022-07-23 10:40:25 +0800
commit9731e4d5f8e667dd81add7aa644dbfe09bd3975b (patch)
treeaae117ef084bf740fa52effa833674447fe4ddc6
parent9fce77f97024a1c76fd60d41bc52fc3c3ceb329a (diff)
impl watch action
-rw-r--r--xmake/core/base/fwatcher.lua2
-rw-r--r--xmake/plugins/watch/main.lua59
-rw-r--r--xmake/plugins/watch/xmake.lua2
3 files changed, 61 insertions, 2 deletions
diff --git a/xmake/core/base/fwatcher.lua b/xmake/core/base/fwatcher.lua
index b0c210216..ddff66a06 100644
--- a/xmake/core/base/fwatcher.lua
+++ b/xmake/core/base/fwatcher.lua
@@ -189,7 +189,7 @@ function fwatcher.watchdirs(watchdirs, callback, opt)
local ok = true
local errors = nil
for _, watchdir in ipairs(watchdirs) do
- ok, errors = fwatcher.add(watchdir, opt.recursion)
+ ok, errors = fwatcher.add(watchdir, opt)
if not ok then
break
end
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'"},