Native watch

This commit is contained in:
bluepython508
2023-12-02 19:20:27 +00:00
parent c6c501af2a
commit 2784245b2d
3 changed files with 46 additions and 1 deletions

View File

@@ -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