1:- use_module(library(aggregate)).    2:- use_module(library(apply)).    3:- use_module(library(lists)).    4:- use_module(library(sort)).    5:- use_module(library(yall)).    6:- use_module(library(http/json)).    7:- use_module(library(canny/pack)).    8:- use_module(library(canny/cover)).    9:- use_module(library(print/table)).   10:- use_module(library(gh/api)).   11
   12:- initialization(cov).   13
   14cov :-
   15    module_coverages(ModuleCoverages),
   16    print_module_coverages(ModuleCoverages),
   17    aggregate_all(
   18        all(sum(Clauses), sum(Cov), sum(Fail), count),
   19        member(_-coverage{
   20                     clauses:Clauses,
   21                     cov:Cov,
   22                     fail:Fail
   23                 }, ModuleCoverages),
   24        all(AllClauses, AllCov, AllFail, AllModule)),
   25    AvgCov is AllCov / AllModule,
   26    AvgFail is AllFail / AllModule,
   27    format('Modules:~t~d~40|~n', [AllModule]),
   28    format('Clauses:~t~d~40|~n', [AllClauses]),
   29    format('Cov:~t~f~40|%~n', [AvgCov]),
   30    format('Fail:~t~f~40|%~n', [AvgFail]),
   31    (   getenv('CANNY_COV_GIST_ID', GistID)
   32    ->  shield_files([cov-AvgCov, fail-AvgFail], Files),
   33        ghapi_update_gist(GistID, json(json([files=Files])), _, [])
   34    ;   true
   35    ).
   36
   37module_coverages(ModuleCoverages) :-
   38    load_pack_modules(canny_tudor, Modules),
   39    findall(
   40        Module-Coverage,
   41        coverage_for_modules(run_tests, Modules, Module, Coverage),
   42        ModuleCoverages).
   43
   44print_module_coverages(ModuleCoverages) :-
   45    predsort(compare_cov_fail, ModuleCoverages, SortedModuleCoverages),
   46    print_table(
   47        member(
   48            Module-coverage{
   49                       clauses:Clauses,
   50                       cov:Cov,
   51                       fail:Fail
   52                   }, SortedModuleCoverages), [Module, Clauses, Cov, Fail]).
   53
   54compare_cov_fail(Order, _-Coverage1, _-Coverage2) :-
   55    compare(Order0, Coverage1.cov, Coverage2.cov),
   56    compare_fail(Order, Order0, Coverage1.fail, Coverage2.fail),
   57    !.
   58compare_cov_fail(>, _, _).
   59
   60compare_fail(<, <, _, _) :- !.
   61compare_fail(<, =, Fail1, Fail2) :- compare(>, Fail1, Fail2), !.
   62compare_fail(>, _, _, _).
   63
   64shield_files(Pairs, json(Files)) :-
   65    maplist([Label-Percent, File=json([content=Content])]>>
   66            (   atom_concat(Label, '.json', File),
   67                format(atom(Message), '~1f%', [Percent]),
   68                shield_color(Percent, Color),
   69                atom_json_term(Content, json([ schemaVersion=1,
   70                                               label=Label,
   71                                               message=Message,
   72                                               color=Color
   73                                             ]), []),
   74                format('raw/~s~n', [File])
   75            ), Pairs, Files).
   76
   77shield_color(Percent, red) :- Percent < 20, !.
   78shield_color(Percent, orange) :- Percent < 40, !.
   79shield_color(Percent, yellow) :- Percent < 60, !.
   80shield_color(Percent, yellowgreen) :- Percent < 80, !.
   81shield_color(_, green)