src_monitor.ex (1222B)
1 require Logger 2 3 defmodule ExSync.SrcMonitor do 4 def start_link do 5 GenServer.start_link(__MODULE__, []) 6 end 7 8 def init([]) do 9 {:ok, watcher_pid} = 10 FileSystem.start_link( 11 dirs: ExSync.Config.src_dirs(), 12 backend: Application.get_env(:file_system, :backend) 13 ) 14 15 FileSystem.subscribe(watcher_pid) 16 {:ok, %{watcher_pid: watcher_pid}} 17 end 18 19 def handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid}=state) do 20 if Path.extname(path) in ExSync.Config.src_extensions do 21 # This may also vary based on editor - when saving a file in neovim on linux, 22 # events received ar3e: 23 # :modified 24 # :modified, :closed 25 # :attribute 26 # Rather than coding specific behaviors for each OS, look for the modified event in 27 # isolation to trigger things. 28 # TODO: untested assumption that this behavior is common across Mac/Linux/Win 29 if :modified in events do 30 ExSync.Utils.recomplete 31 end 32 end 33 34 {:noreply, state} 35 end 36 37 def handle_info({:file_event, watcher_pid, :stop}, %{watcher_pid: watcher_pid} = state) do 38 Logger.debug("ExSync src monitor stopped.") 39 {:noreply, state} 40 end 41 end