-
Notifications
You must be signed in to change notification settings - Fork 162
/
sync_options.erl
144 lines (119 loc) · 3.59 KB
/
sync_options.erl
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
%% vim: ts=4 sw=4 et
-module(sync_options).
-behaviour(gen_server).
%% API
-export([
start_link/0,
get_onsync/0,
set_onsync/1,
clear_onsync/0,
get_options/1,
set_options/2,
autotest/1,
enable_autotest/0
]).
%% gen_server callbacks
-export([
init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3
]).
-define(SERVER, ?MODULE).
-define(PRINT(Var), io:format("DEBUG: ~p:~p - ~p~n~n ~p~n~n", [?MODULE, ?LINE, ??Var, Var])).
-record(state, {
onsync_fun,
options_table
}).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
get_onsync() ->
gen_server:call(?SERVER, get_onsync).
set_onsync(Fun) ->
gen_server:call(?SERVER, {set_onsync, Fun}).
clear_onsync() ->
set_onsync(undefined).
%% @private If options are not found for this directory, keep checking the
%% parent directories for options
get_options([]) ->
undefined;
get_options(SrcDir) ->
case gen_server:call(?SERVER, {get_options, SrcDir}) of
{ok, Options} ->
{ok, Options};
undefined ->
get_options(filename:dirname(SrcDir))
end.
enable_autotest() ->
set_onsync(fun ?MODULE:autotest/1).
autotest([Mod|Rest]) ->
case erlang:function_exported(Mod, test, 0) of
true ->
try Mod:test() of
ok ->
Msg = io_lib:format("~s: All Tests Passed~n", [Mod]),
sync_notify:log_success(Msg);
Reason ->
Msg = io_lib:format("~s: Tests Failed: ~p~n", [Mod, Reason]),
sync_notify:log_errors(Msg),
sync_notify:growl_errors(Msg)
catch
T:E:S ->
Msg = io_lib:format("~s: Tests Crashed: ~p:~p~n",[Mod, T, E]),
WithStacktrace = [Msg, io_lib:format("Stacktrace: ~p~n",[S])],
sync_notify:log_errors(WithStacktrace),
sync_notify:growl_errors(Msg)
end;
false ->
ok %% nothing to do here
end,
autotest(Rest);
autotest([]) ->
ok.
set_options(SrcDir, Options) ->
gen_server:call(?SERVER, {set_options, SrcDir, Options}).
init([]) ->
%% Create the state and return...
State = #state {
options_table = ets:new(options_table, [set, named_table, private])
},
{ok, State}.
handle_call(get_onsync, _From, State) ->
OnSync = State#state.onsync_fun,
{reply, OnSync, State};
handle_call({set_onsync, Fun}, _From, State) ->
State2 = State#state{
onsync_fun = Fun},
{reply, ok, State2};
handle_call({get_options, SrcDir}, _From, State) ->
%% Look up the compile options for a SrcDir...
case ets:lookup(State#state.options_table, SrcDir) of
[{SrcDir, Options}] ->
{reply, {ok, Options}, State};
_ ->
{reply, undefined, State}
end;
handle_call({set_options, SrcDir, Options}, _From, State) ->
%% Set the compile options for a SrcDir...
Table = State#state.options_table,
case ets:lookup(Table, SrcDir) of
[{SrcDir, OldOptions}] ->
NewOptions = lists:usort(Options ++ OldOptions),
ets:insert(Table, {SrcDir, NewOptions});
_ ->
ets:insert(Table, {SrcDir, Options})
end,
{reply, ok, State};
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.