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

Maintenance updates #573

Merged
merged 23 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
bebcbc6
Preemptively accept format results
paulo-ferraz-oliveira Aug 4, 2023
cc4e764
Kill it with fire
paulo-ferraz-oliveira Aug 4, 2023
94ec896
Dialyzer it
paulo-ferraz-oliveira Aug 4, 2023
d9d6a88
Handle error
paulo-ferraz-oliveira Aug 5, 2023
2cbe5eb
Accept elvis_core's change
paulo-ferraz-oliveira Aug 5, 2023
5a340ee
Adapt to recent elvis_core changes
paulo-ferraz-oliveira Aug 5, 2023
1d34489
Validate rebar3_ex_doc results
paulo-ferraz-oliveira Aug 6, 2023
edb770f
Fix as per `rebar3 as test test` results
paulo-ferraz-oliveira Aug 6, 2023
7896a1e
Ease maintenance of .gitignore
paulo-ferraz-oliveira Aug 6, 2023
d606bb9
Update go-to place for discussion
paulo-ferraz-oliveira Aug 7, 2023
3059cb0
Move http:// to https://
paulo-ferraz-oliveira Aug 7, 2023
a8013cd
Accept further format results
paulo-ferraz-oliveira Aug 7, 2023
c177f62
Lint README.md and adapt it to ex_doc
paulo-ferraz-oliveira Aug 7, 2023
86d300f
Ease pre-pull request fork CI
paulo-ferraz-oliveira Aug 7, 2023
86d4143
Tweak ci.yml
paulo-ferraz-oliveira Aug 7, 2023
78aa207
Tweak rebar.config
paulo-ferraz-oliveira Aug 7, 2023
345ac83
Move meta CT suites to GitHub CI
paulo-ferraz-oliveira Aug 7, 2023
9e59629
Try with out-of-the-box git
paulo-ferraz-oliveira Aug 7, 2023
af885ca
Act on self-review: make for faster CI by caching _build and ~/.cache…
paulo-ferraz-oliveira Aug 7, 2023
1f322ff
Act on self-review: generate a proper link
paulo-ferraz-oliveira Aug 7, 2023
9c3ee12
Act on review result: remove meaningless test
paulo-ferraz-oliveira Aug 8, 2023
be744d8
Act on review result: make for more readable code in a simple case
paulo-ferraz-oliveira Aug 8, 2023
4f957d8
Act on review result: make for more readable code in a simple case
paulo-ferraz-oliveira Aug 8, 2023
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
178 changes: 104 additions & 74 deletions src/elvis.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

%% Public API

-export([ main/1
, default_config/0
]).
-export([main/1, default_config/0]).

-elvis([{elvis_style, no_debug_call, disable}]).

Expand All @@ -14,6 +12,20 @@
-define(DEFAULT_CONFIG_PATH, "./elvis.config").
-define(DEFAULT_REBAR_CONFIG_PATH, "./rebar.config").

-type option() ::
commands |
help |
keep_rocking |
quiet |
verbose |
version |
{code_path, [any()]} |
{config, [any()]} |
{output_format, [any()]} |
{parallel, [any()]}.

-export_type([option/0]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Public API
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand All @@ -29,15 +41,19 @@ main(Args) ->
%% Load the application to be able to access its information
%% (e.g. --version option)
ok =
case application:load(elvis) of
ok -> ok;
{error, {already_loaded, elvis}} -> ok
end,
case application:load(elvis) of
ok ->
ok;
{error, {already_loaded, elvis}} ->
ok
end,
ok =
case application:load(elvis_core) of
ok -> ok;
{error, {already_loaded, elvis_core}} -> ok
end,
case application:load(elvis_core) of
ok ->
ok;
{error, {already_loaded, elvis_core}} ->
ok
end,
OptSpecList = option_spec_list(),
case getopt:parse(OptSpecList, Args) of
{ok, {[], []}} ->
Expand All @@ -58,53 +74,45 @@ main(Args) ->

-spec option_spec_list() -> [getopt:option_spec()].
option_spec_list() ->
Commands = "Provide the path to the configuration file. "
"When none is provided elvis checks if there's "
"an elvis.config file.",
OutputFormat = "It allows you to display the results in plain text. When "
"none is provided elvis displays the results in colors. "
"The options allowed are (plain | colors | parsable).",
KeepRocking = "Won't stop rocking on first error"
" when given a list of files",
Parallel = "Allows to analyze files concurrently. Provide max number of"
" concurrent workers, or specify \"auto\" to peek default value"
" based on the number of schedulers.",
[
{help, $h, "help", undefined,
"Show this help information."},
{config, $c, "config", string,
Commands},
{commands, undefined, "commands", undefined,
"Show available commands."},
{output_format, undefined, "output-format", string,
OutputFormat},
Commands =
"Provide the path to the configuration file. "
"When none is provided elvis checks if there's "
"an elvis.config file.",
OutputFormat =
"It allows you to display the results in plain text. When "
"none is provided elvis displays the results in colors. "
"The options allowed are (plain | colors | parsable).",
KeepRocking =
"Won't stop rocking on first error"
" when given a list of files",
Parallel =
"Allows to analyze files concurrently. Provide max number of"
" concurrent workers, or specify \"auto\" to peek default value"
" based on the number of schedulers.",
[{help, $h, "help", undefined, "Show this help information."},
{config, $c, "config", string, Commands},
{commands, undefined, "commands", undefined, "Show available commands."},
{output_format, undefined, "output-format", string, OutputFormat},
{parallel, $P, "parallel", string, Parallel},
{quiet, $q, "quiet", undefined,
"Suppress all output."},
{verbose, $V, "verbose", undefined,
"Enable verbose output."},
{version, $v, "version", undefined,
"Output the current elvis version."},
{code_path, $p, "code-path", string,
"Add the directory in the code path."},
{keep_rocking, $k, "keep-rocking", undefined,
KeepRocking}
].

-spec process_options([atom() | tuple()], [string()]) -> ok.
{quiet, $q, "quiet", undefined, "Suppress all output."},
{verbose, $V, "verbose", undefined, "Enable verbose output."},
{version, $v, "version", undefined, "Output the current elvis version."},
{code_path, $p, "code-path", string, "Add the directory in the code path."},
{keep_rocking, $k, "keep-rocking", undefined, KeepRocking}].

-spec process_options([option()], [string()]) -> ok.
process_options(Options, Commands) ->
try
Config = default_config(),
AtomCommands = lists:map(fun list_to_atom/1, Commands),
process_options(Options, AtomCommands, Config)
catch
throw:Exception ->
_:Exception ->
elvis_utils:error_prn("~p.", [Exception]),
elvis_utils:erlang_halt(1)
end.

-spec process_options([atom() | tuple()], [string()], elvis_config:configs()) ->
ok.
-spec process_options([option()], [string()], elvis_config:configs()) -> ok.
process_options([help | Opts], Cmds, Config) ->
help(),
process_options(Opts, Cmds, Config);
Expand Down Expand Up @@ -134,29 +142,40 @@ process_options([{code_path, Path} | Opts], Cmds, Config) ->
process_options(Opts, Cmds, Config);
process_options([{parallel, Num} | Opts], Cmds, Config) ->
N = case Num of
"auto" -> erlang:system_info(schedulers);
_ -> erlang:list_to_integer(Num)
"auto" ->
erlang:system_info(schedulers);
_ ->
erlang:list_to_integer(Num)
end,
ok = application:set_env(elvis_core, parallel, N),
process_options(Opts, Cmds, Config);
process_options([], Cmds, Config) ->
process_commands(Cmds, Config).

-spec process_commands([string()], elvis_config:configs()) -> ok.
-spec process_commands([rock |
help |
[install | 'git-hook'] |
'git-hook' |
'git-branch' |
string()],
elvis_config:configs()) ->
ok.
process_commands([rock | Files], Config) ->
case Files of
[] ->
case elvis_core:rock(Config) of
{fail, _} -> elvis_utils:erlang_halt(1);
ok -> ok
{fail, _} ->
elvis_utils:erlang_halt(1);
ok ->
ok
end;
_ ->
lists:map(fun(F) -> rock_one_song(F, Config) end, Files)
end;
process_commands([help | Cmds], Config) ->
Config = help(Config),
process_commands(Cmds, Config);
process_commands(['install', 'git-hook' | Cmds], Config) ->
process_commands([install, 'git-hook' | Cmds], Config) ->
elvis_git:install_hook(),
process_commands(Cmds, Config);
process_commands(['git-hook' | Cmds], Config) ->
Expand All @@ -177,15 +196,15 @@ help() ->
OptSpecList = option_spec_list(),
getopt:usage(OptSpecList, ?APP_NAME, standard_io).

-spec help(elvis_config:configs()) ->
elvis_config:configs().
-spec help(elvis_config:configs()) -> elvis_config:configs().
help(Config) ->
help(),
Config.

-spec commands() -> ok.
commands() ->
Commands = <<"Elvis will do the following things for you when asked nicely:
Commands =
<<"Elvis will do the following things for you when asked nicely:

rock [file...] Rock your socks off by running all rules to your source files.

Expand All @@ -201,46 +220,57 @@ install git-hook
Installs Elvis as a pre-commit hook in your current working
directory, which should be a git repository.
">>,
io:put_chars(Commands).
io:put_chars(Commands).

-spec version() -> ok.
version() ->
{ok, ElvisCoreAppConfig} = application:get_all_key(elvis_core),
{ok, ElvisShellAppConfig} = application:get_all_key(elvis),
ElvisCoreVsn = proplists:get_value(vsn, ElvisCoreAppConfig),
ElvisShellVsn = proplists:get_value(vsn, ElvisShellAppConfig),
Version = " ______ _ \n"
" / __/ / __(_)__\n"
" / _// / |/ / (_-<\n"
"/___/_/|___/_/___/\n"
"Version: ~s\n"
"Elvis Core Version: ~s\n",
Version =
" ______ _ \n"
" / __/ / __(_)__\n"
" / _// / |/ / (_-<\n"
"/___/_/|___/_/___/\n"
"Version: ~s\n"
"Elvis Core Version: ~s\n",
io:format(Version, [ElvisShellVsn, ElvisCoreVsn]).

rock_one_song(FileName, Config) ->
F = atom_to_list(FileName),
case elvis_core:rock_this(F, Config) of
{fail, _} ->
case application:get_env(elvis_core, keep_rocking, false) of
false -> elvis_utils:erlang_halt(1);
true -> ok
false ->
elvis_utils:erlang_halt(1);
true ->
ok
end;
ok -> ok
ok ->
ok
end.

-spec default_config() -> elvis_config:configs().
default_config() ->
default_config([ fun() -> elvis_config:from_file(?DEFAULT_CONFIG_PATH) end
, fun() -> elvis_config:from_rebar(?DEFAULT_REBAR_CONFIG_PATH) end
]).
default_config([fun() -> elvis_config:from_file(?DEFAULT_CONFIG_PATH) end,
fun() -> elvis_config:from_rebar(?DEFAULT_REBAR_CONFIG_PATH) end]).

-spec default_config(list(Fun)) -> elvis_config:configs() when
Fun :: fun(() -> elvis_config:config()).
-spec default_config([Fun]) -> elvis_config:configs()
when Fun :: fun(() -> elvis_config:config()).
default_config([Fun | Funs]) ->
Config = try Fun() catch _:_ -> [] end,
Config =
try
Fun()
catch
_:_ ->
[]
end,
case Config of
[] -> default_config(Funs);
Config -> Config
[] ->
default_config(Funs);
Config ->
Config
end;
default_config([]) ->
application:get_env(elvis, config, []).
Loading