Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filewatcher #2555

Closed
Immortalin opened this issue Jul 10, 2022 · 5 comments
Closed

Filewatcher #2555

Immortalin opened this issue Jul 10, 2022 · 5 comments

Comments

@Immortalin
Copy link

Is your feature request related to a problem? Please describe.

Package managers often have a "watch" feature where code is automatically rebuilt and ran when the source is changed.

Something like https://github.com/watchexec/cargo-watch

Describe the solution you'd like

xmake watch command

Describe alternatives you've considered

N/A

Additional context

Each OS has different filewatcher APIs, use inotify on Linux, FSEvents on Mac, and ReadDirectoryChangesW for Windows. (BSD has something called kqueue but I am not sure)

@waruqi
Copy link
Member

waruqi commented Jul 11, 2022

xmake does not need to provide the xmake watch command, but I will consider providing some api modules related to file watcher. With these api modules and custom tasks, you can easily implement any watch function by yourself, including xmake watch.

like this

task("watch")
     on_run(function (target)
         os.watchdir("src", function()
             -- do some thing
             os.exec("xmake build")
         end)
     end)
$ xmake watch

@OuYangPaste
Copy link

OuYangPaste commented Jul 13, 2022

I used to implement similar features based on xmake plugin and entr, for private

@waruqi waruqi added this to the v2.7.1 milestone Jul 17, 2022
@waruqi waruqi mentioned this issue Jul 22, 2022
@waruqi
Copy link
Member

waruqi commented Jul 23, 2022

I have supported it in #2592 and add new xmake watch plugin command.

xmake watch plugin

Watch project directory and build project

$ xmake watch
watching /private/tmp/test/src/** ..
watching /private/tmp/test/* ..
/private/tmp/test/src/main.cpp modified
[ 25%]: ccache compiling.release src/main.cpp
[ 50%]: linking.release test
[100%]: build ok!

Watch the given directories

$ xmake watch -d src
$ xmake watch -d "src;tests/*"

Watch and run multiple given commands

$ xmake watch -c "xmake; xmake run"

Run an arbitrary command

$ xmake watch -- echo hello xmake!
$ xmake watch -- xmake run --help

Watch and run given lua script file

$ xmake watch -s /tmp/test.lua

Watch and build the given target

$ xmake watch -t targetname

Watch and run targets

$ xmake watch -r
$ xmake watch --run
[100%]: build ok!
hello world!

Watch modules

basic api

import("core.base.fwatcher")

function main(watchdir)
    print("watch %s ..", watchdir)
    fwatcher.add(watchdir)
    while true do
        local ok, event = fwatcher.wait(-1)
        if ok > 0 then
            print(event)
        end
    end
end

callback api

import("core.base.fwatcher")

function main(watchdir)
    print("watch %s ..", watchdir)
    fwatcher.watchdirs(watchdir, 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)
    end)
end

specific event callback api

import("core.base.fwatcher")

function main(watchdir)
    print("watch %s ..", watchdir)
    fwatcher.on_modified(watchdir, function (filepath)
        print(filepath, "modified")
    end)
end
import("core.base.fwatcher")

function main(watchdir)
    print("watch %s ..", watchdir)
    fwatcher.on_created(watchdir, function (filepath)
        print(filepath, "created")
    end)
end
import("core.base.fwatcher")

function main(watchdir)
    print("watch %s ..", watchdir)
    fwatcher.on_deleted(watchdir, function (filepath)
        print(filepath, "deleted")
    end)
end

@waruqi
Copy link
Member

waruqi commented Jul 23, 2022

There are still some bugs in the fsevent based implementation on the mac, I will continue to improve it in the next few days.

@waruqi
Copy link
Member

waruqi commented Jul 23, 2022

I have fixed all bugs and It is now fully available.

@waruqi waruqi closed this as completed Jul 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants