36 lines
932 B
Elixir
36 lines
932 B
Elixir
defmodule Pandoc.Watcher do
|
|
@moduledoc false
|
|
|
|
use GenServer
|
|
|
|
def start_link(args) do
|
|
GenServer.start_link(__MODULE__, args)
|
|
end
|
|
|
|
# Callbacks
|
|
|
|
@impl true
|
|
def init([profile, options, pattern, extra_args]) do
|
|
{:ok, watcher_pid} = FileSystem.start_link(options)
|
|
FileSystem.subscribe(watcher_pid)
|
|
{:ok, %{watcher_pid: watcher_pid, profile: profile, pattern: pattern, extra_args: extra_args}}
|
|
end
|
|
|
|
@impl true
|
|
def handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid} = state) do
|
|
case {String.match?(path, state[:pattern]), :closed in events or :deleted in events} do
|
|
{true, true} ->
|
|
Pandoc.install_and_run(state[:profile], [Path.basename(path) | state[:extra_args]])
|
|
|
|
_ ->
|
|
nil
|
|
end
|
|
|
|
{:noreply, state}
|
|
end
|
|
|
|
def handle_info({:file_event, watcher_pid, :stop}, %{watcher_pid: watcher_pid} = state) do
|
|
{:noreply, state}
|
|
end
|
|
end
|