1 |
|
%%============================================================================== |
2 |
|
%% Copyright 2014 Erlang Solutions Ltd. |
3 |
|
%% |
4 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
|
%% you may not use this file except in compliance with the License. |
6 |
|
%% You may obtain a copy of the License at |
7 |
|
%% |
8 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
%% |
10 |
|
%% Unless required by applicable law or agreed to in writing, software |
11 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
12 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
|
%% See the License for the specific language governing permissions and |
14 |
|
%% limitations under the License. |
15 |
|
%%============================================================================== |
16 |
|
-module(mongoose_cover_helper). |
17 |
|
|
18 |
|
|
19 |
|
%% API |
20 |
|
-export([start/1, analyze/0, stop/0]). |
21 |
|
|
22 |
|
-ignore_xref([analyze/0, start/1, stop/0]). |
23 |
|
|
24 |
|
-spec start([atom()]) -> list(). |
25 |
|
start(Apps) -> |
26 |
:-( |
cover:start(), |
27 |
:-( |
lists:flatmap(fun cover_compile_app/1, Apps). |
28 |
|
|
29 |
|
analyze() -> |
30 |
:-( |
file:make_dir("priv/cover"), |
31 |
:-( |
cover:export("priv/cover/" ++ atom_to_list(node()) ++ ".coverdata"), |
32 |
:-( |
ok. |
33 |
|
|
34 |
|
stop() -> |
35 |
:-( |
cover:stop(). |
36 |
|
|
37 |
|
cover_compile_app(App) -> |
38 |
:-( |
do_cover_compile_app(App, code:lib_dir(App)). |
39 |
|
|
40 |
|
do_cover_compile_app(App, {error, Error}) -> |
41 |
:-( |
[{error, App, Error}]; |
42 |
|
do_cover_compile_app(App, AppPath) -> |
43 |
:-( |
EbinDir = filename:join(AppPath, "ebin"), |
44 |
:-( |
BeamFilter = fun |
45 |
|
%% modules not compatible with cover |
46 |
|
(File) when File =:= "cover.beam" -> |
47 |
:-( |
false; |
48 |
|
(File) -> |
49 |
:-( |
case filename:extension(File) of |
50 |
:-( |
".beam" -> true; |
51 |
:-( |
_ -> false |
52 |
|
end |
53 |
|
end, |
54 |
:-( |
case file:list_dir(EbinDir) of |
55 |
|
{ok, Files} -> |
56 |
:-( |
BeamFileNames = lists:filter(BeamFilter, Files), |
57 |
:-( |
BeamFiles = [filename:join(EbinDir, File) || File <- BeamFileNames], |
58 |
:-( |
[{App, compile_beams(BeamFiles, [])}]; |
59 |
|
Error -> |
60 |
:-( |
[{error, EbinDir, Error}] |
61 |
|
end. |
62 |
|
|
63 |
|
compile_beams([], Result) -> |
64 |
:-( |
Result; |
65 |
|
compile_beams([File | Files], Result) -> |
66 |
:-( |
case catch cover:compile_beam(File) of |
67 |
|
{ok, Module} -> |
68 |
:-( |
compile_beams(Files, [Module | Result]); |
69 |
|
_E -> |
70 |
:-( |
compile_beams(Files, Result) |
71 |
|
end. |
72 |
|
|