Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Store hank output in json file #184

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
{rebar3_format, "~> 1.2.0"},
{rebar3_lint, "~> 1.0.2"},
{rebar3_sheldon, "~> 0.4.2"},
{rebar3_ex_doc, "~> 0.2.9"}]}.
{rebar3_ex_doc, "~> 0.2.9"},
{jsx, "~> 2.10"}]}.
naman070 marked this conversation as resolved.
Show resolved Hide resolved

{dialyzer, [{warnings, [no_return, unmatched_returns, error_handling, underspecs]}]}.

Expand Down
90 changes: 89 additions & 1 deletion src/rebar3_hank_prv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ opts() ->
$u,
"unused_ignores",
boolean,
"Warn on unused ignores (default: true)."}].
"Warn on unused ignores (default: true)."},
{output_json_file,
$o,
"output_json_file",
string,
"Output Json File Name (default: empty string)"}
elbrujohalcon marked this conversation as resolved.
Show resolved Hide resolved
].

%% @private
-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, iodata()}.
Expand Down Expand Up @@ -69,6 +75,7 @@ do(State) ->
unused_ignores := UnusedIgnores,
stats := Stats} ->
instrument(Stats, UnusedIgnores, State),
write_data_to_json_file(Results, State),
{error, format_results(Results)}
catch
Kind:Error:Stack ->
Expand Down Expand Up @@ -120,6 +127,87 @@ format_result(#{file := File,
text := Msg}) ->
hank_utils:format_text("~ts:~tp: ~ts", [File, Line, Msg]).

-spec write_data_to_json_file([hank_rule:result()], rebar_state:t()) -> ok.
write_data_to_json_file(Result, State) ->
naman070 marked this conversation as resolved.
Show resolved Hide resolved
{Args, _} = rebar_state:command_parsed_args(State),
JsonFilePath =
case lists:keyfind(output_json_file, 1, Args) of
{output_json_file, Value} ->
Value;
_ ->
undefined
end,
case JsonFilePath of
naman070 marked this conversation as resolved.
Show resolved Hide resolved
undefined ->
ok;
FilePath ->
case valid_json_format(FilePath) of
true ->
ConvertedResult = convert_data_to_binary(Result),
EncodedResult = jsx:encode(ConvertedResult),
ok = file:write_file(FilePath, EncodedResult);
false ->
ok
end
end.

-spec valid_json_format(string()) -> boolean().
valid_json_format(JsonFilePath) ->
JsonFileName = lists:last(string:tokens(JsonFilePath, "/")),
case lists:last(string:tokens(JsonFileName, ".")) of
"json" ->
true;
_ ->
false
end.
elbrujohalcon marked this conversation as resolved.
Show resolved Hide resolved

-spec convert_data_to_binary([hank_rule:result()]) -> list().
convert_data_to_binary(Data) ->
Func =
fun(RuleDetailMap) ->
#{file := FileName, line := Line, rule := RuleBroken, text := Description} = RuleDetailMap,
#{
<<"path">> => to_binary(FileName),
<<"start_line">> => Line,
<<"hank_rule_broken">> => to_binary(RuleBroken),
<<"title">> => compute_title(RuleBroken),
<<"message">> => to_binary(Description)}
end,
[Func(RuleDetails) || RuleDetails <- Data].
elbrujohalcon marked this conversation as resolved.
Show resolved Hide resolved

-spec compute_title(atom()) -> binary().
compute_title(RuleBroken) ->
case RuleBroken of
unused_macros ->
<<"Unused Macros">>;
single_use_hrl_attrs ->
<<"Macro is only used once">>;
unused_record_fields ->
<<"Field in the record is unused">>;
unused_hrls ->
<<"Unused hrl files">>;
unused_configuration_options ->
<<"Unused config">>;
unused_callbacks ->
<<"Unused callback functions">>;
unnecessary_function_arguments ->
<<"Unused function arguments found">>;
single_use_hrls ->
<<"Hrl is only used once">>
end.

to_binary(Input) when is_atom(Input) ->
atom_to_binary(Input, utf8);
to_binary(Input) when is_integer(Input) ->
integer_to_binary(Input);
to_binary(Input) when is_float(Input) ->
float_to_binary(Input, [{decimals, 10}, compact]);
to_binary(Input) when is_list(Input) ->
list_to_binary(Input);
to_binary(Input) when is_pid(Input) ->
list_to_binary(pid_to_list(Input));
to_binary(Input) -> Input.
elbrujohalcon marked this conversation as resolved.
Show resolved Hide resolved

%% @private
%% @doc Determines files that should be fully hidden to Hank.
is_hidden(Filename) ->
Expand Down