Skip to content

Instantly share code, notes, and snippets.

@notactuallypagemcconnell
Created February 25, 2019 00:25
Show Gist options
  • Save notactuallypagemcconnell/105ee138d3f634c9a7bc87f2942ee5eb to your computer and use it in GitHub Desktop.
Save notactuallypagemcconnell/105ee138d3f634c9a7bc87f2942ee5eb to your computer and use it in GitHub Desktop.
# have this dep for dev AND test, not just test
# https://github.com/lpil/mix-test.watch/blob/master/lib/mix_test_watch/path.ex
defmodule Foobar.Recompiler do
use GenServer
def start_link(args) do
GenServer.start_link(__MODULE__, args)
end
def init(args) do
{:ok, watcher_pid} = FileSystem.start_link(dirs: ["./"], latency: 0)
FileSystem.subscribe(watcher_pid)
{:ok, %{watcher_pid: watcher_pid}}
end
def handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid} = state) do
case String.ends_with?(path, ".ex") do
true -> Mix.Tasks.Compile.Elixir.run(["--ignore-module-conflict"])
false -> :noop
end
{:noreply, state}
end
def handle_info({:file_event, watcher_pid, :stop}, %{watcher_pid: watcher_pid}=state) do
# do we need logic for the monitor stop? we want it always running...
{:noreply, state}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment