Native watch
This commit is contained in:
@@ -100,6 +100,25 @@ defmodule Mix.Tasks.Aoc do
|
||||
create_file("#{tests_dir(day)}#{test}.#{part}", IO.read(:stdio, :eof))
|
||||
end
|
||||
|
||||
defp run(day, ["test", "watch"]) do
|
||||
{:ok, pid} = Mix.Tasks.Aoc.Watcher.start_link({[dirs: ["lib/", "test/day#{day}/"]], self()})
|
||||
Process.monitor(pid)
|
||||
loop = fn (loop) ->
|
||||
IO.puts(IO.ANSI.clear() <> IO.ANSI.cursor(0, 0))
|
||||
IO.puts "At #{DateTime.utc_now(:second)}:"
|
||||
System.shell("mix compile")
|
||||
:code.purge(module(day))
|
||||
:code.purge(Aoc2023.Common)
|
||||
:code.load_file(module(day))
|
||||
run(day, ["test"])
|
||||
receive do
|
||||
:update -> loop.(loop)
|
||||
{:DOWN, _, :process, ^pid, _} -> nil
|
||||
end
|
||||
end
|
||||
loop.(loop)
|
||||
end
|
||||
|
||||
defp run(day, ["test"]) do
|
||||
mod = module(day)
|
||||
tests = tests(day)
|
||||
@@ -166,4 +185,28 @@ defmodule Mix.Tasks.Aoc do
|
||||
defp run_file(day, file \\ nil) do
|
||||
module(day).main(if file, do: read(file), else: IO.read(:stdio, :eof))
|
||||
end
|
||||
|
||||
|
||||
defmodule Watcher do
|
||||
use GenServer
|
||||
|
||||
def start_link(args) do
|
||||
GenServer.start_link(__MODULE__, args)
|
||||
end
|
||||
|
||||
def init({args, callback}) do
|
||||
{:ok, watcher_pid} = FileSystem.start_link(args)
|
||||
FileSystem.subscribe(watcher_pid)
|
||||
{:ok, %{watcher_pid: watcher_pid, callback: callback}}
|
||||
end
|
||||
|
||||
def handle_info({:file_event, watcher_pid, {_path, _events}}, %{watcher_pid: watcher_pid, callback: callback} = state) do
|
||||
send(callback, :update)
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
def handle_info({:file_event, _watcher_pid, :stop}, state) do
|
||||
{:noreply, state}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user