-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
watcher.ex
72 lines (54 loc) 路 1.3 KB
/
watcher.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
defmodule MixTestWatch.Watcher do
use GenServer
alias MixTestWatch, as: MTW
alias MixTestWatch.Config
require Logger
@moduledoc """
A server that runs tests whenever source files change.
"""
#
# Client API
#
def start_link(_args) do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end
def run_tasks do
GenServer.cast(__MODULE__, :run_tasks)
end
#
# Genserver callbacks
#
@spec init(String.t()) :: {:ok, Keyword.t()}
def init(_) do
opts = [dirs: [Path.absname("")], name: :mix_test_watcher]
case FileSystem.start_link(opts) do
{:ok, _} ->
FileSystem.subscribe(:mix_test_watcher)
{:ok, []}
other ->
Logger.warning("Could not start the file system monitor.")
other
end
end
def handle_cast(:run_tasks, state) do
config = get_config()
MTW.Runner.run(config)
{:noreply, state}
end
def handle_info({:file_event, _, {path, _events}}, state) do
config = get_config()
path = to_string(path)
if MTW.Path.watching?(path, config) do
MTW.Runner.run(config)
MTW.MessageInbox.flush()
end
{:noreply, state}
end
#
# Internal functions
#
@spec get_config() :: %Config{}
defp get_config do
Application.get_env(:mix_test_watch, :__config__, %Config{})
end
end