Skip to content

Commit

Permalink
Merge pull request #102 from inaka/jfacorro.18.no.deps.master
Browse files Browse the repository at this point in the history
[Closes #18] No deps in revision 'master'.
  • Loading branch information
Brujo Benavides committed Sep 9, 2014
2 parents 4079ff5 + c3aaad8 commit b453328
Show file tree
Hide file tree
Showing 22 changed files with 570 additions and 139 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ERLC_OPTS += +warn_export_vars +warn_exported_vars +warn_missing_spec +warn_unty
# Commont Test Config

TEST_ERLC_OPTS += +'{parse_transform, lager_transform}'
CT_SUITES = elvis rules git
CT_SUITES = elvis style project git
CT_OPTS = -cover test/elvis.coverspec -erl_args -config config/test.config

# Builds the elvis escript.
Expand All @@ -31,4 +31,4 @@ escript: all
./elvis help

shell: app
erl -pa ebin -pa deps/*/ebin -s sync -s elvis -s lager -config config/app.config
erl -pa ebin -pa deps/*/ebin -s sync -s elvis -s lager -config config/elvis.config
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ define rules of your own as long as the functions that implement them respect
this arity.

There's currently no default configuration for `elvis`, but in the meantime
you can take the one in `config/app.config` as a starting point.
you can take the one in `config/elvis.config` as a starting point.

The GitHub configuration parameters `github_user` and `github_password` are
required only when `elvis` is used as a [webhook](#webhook).
Expand Down
49 changes: 29 additions & 20 deletions config/elvis-test.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,36 @@
elvis,
[
{config,
#{src_dirs => ["../../src"],
rules => [{elvis_style, line_length, [80]},
{elvis_style, no_tabs, []},
{elvis_style, macro_names, []},
{elvis_style, macro_module_names, []},
{elvis_style, operator_spaces, [{right, ","},
{right, "++"},
{left, "++"}]},
{elvis_style, nesting_level, [3]},
{elvis_style, god_modules, [25]},
{elvis_style, no_if_expression, []},
{elvis_style, invalid_dynamic_call, [elvis]},
{elvis_style, used_ignored_variable, []},
{elvis_style, no_behavior_info, []},
{
elvis_style,
module_naming_convention,
["^([a-z][a-z0-9]*_?)*(_SUITE)?$", []]
}
]
[#{src_dirs => ["../../src"],
rules => [{elvis_style, line_length, [80]},
{elvis_style, no_tabs, []},
{elvis_style, macro_names, []},
{elvis_style, macro_module_names, []},
{elvis_style, operator_spaces, [{right, ","},
{right, "++"},
{left, "++"}]},
{elvis_style, nesting_level, [3]},
{elvis_style, god_modules, [25]},
{elvis_style, no_if_expression, []},
{elvis_style, invalid_dynamic_call, [elvis]},
{elvis_style, used_ignored_variable, []},
{elvis_style, no_behavior_info, []},
{
elvis_style,
module_naming_convention,
["^([a-z][a-z0-9]*_?)*(_SUITE)?$", []]
}
]
},
#{dirs => ["."],
filter => "Makefile",
rules => [{elvis_project, no_deps_master_erlang_mk, []}]
},
#{dirs => ["."],
filter => "rebar.config",
rules => [{elvis_project, no_deps_master_rebar, []}]
}
]
}
]
}
Expand Down
50 changes: 30 additions & 20 deletions config/elvis.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,37 @@
elvis,
[
{config,
#{src_dirs => ["src"],
rules => [{elvis_style, line_length, [80]},
{elvis_style, no_tabs, []},
{elvis_style, macro_names, []},
{elvis_style, macro_module_names, []},
{elvis_style, operator_spaces, [{right, ","},
{right, "++"},
{left, "++"}]},
{elvis_style, nesting_level, [3]},
{elvis_style, god_modules, [25]},
{elvis_style, no_if_expression, []},
{elvis_style, invalid_dynamic_call, [elvis]},
{elvis_style, used_ignored_variable, []},
{elvis_style, no_behavior_info, []},
{
elvis_style,
module_naming_convention,
["^([a-z][a-z0-9]*_?)*(_SUITE)?$", []]
}
]
[#{dirs => ["src"],
filter => "*.erl",
rules => [{elvis_style, line_length, [80]},
{elvis_style, no_tabs, []},
{elvis_style, macro_names, []},
{elvis_style, macro_module_names, []},
{elvis_style, operator_spaces, [{right, ","},
{right, "++"},
{left, "++"}]},
{elvis_style, nesting_level, [3]},
{elvis_style, god_modules, [25]},
{elvis_style, no_if_expression, []},
{elvis_style, invalid_dynamic_call, [elvis]},
{elvis_style, used_ignored_variable, []},
{elvis_style, no_behavior_info, []},
{
elvis_style,
module_naming_convention,
["^([a-z][a-z0-9]*_?)*(_SUITE)?$", []]
}
]
},
#{dirs => ["."],
filter => "Makefile",
rules => [{elvis_project, no_deps_master_erlang_mk, []}]
},
#{dirs => ["."],
filter => "rebar.config",
rules => [{elvis_project, no_deps_master_rebar, []}]
}
]
}
]
}
Expand Down
30 changes: 30 additions & 0 deletions config/old/elvis-test.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
elvis,
[
{config,
#{src_dirs => ["../../src"],
rules => [{elvis_style, line_length, [80]},
{elvis_style, no_tabs, []},
{elvis_style, macro_names, []},
{elvis_style, macro_module_names, []},
{elvis_style, operator_spaces, [{right, ","},
{right, "++"},
{left, "++"}]},
{elvis_style, nesting_level, [3]},
{elvis_style, god_modules, [25]},
{elvis_style, no_if_expression, []},
{elvis_style, invalid_dynamic_call, [elvis]},
{elvis_style, used_ignored_variable, []},
{elvis_style, no_behavior_info, []},
{
elvis_style,
module_naming_convention,
["^([a-z][a-z0-9]*_?)*(_SUITE)?$", []]
}
]
}
}
]
}
].
File renamed without changes.
50 changes: 30 additions & 20 deletions config/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,37 @@
elvis,
[
{config,
#{src_dirs => ["../../test/examples"],
rules => [{elvis_style, line_length, [80]},
{elvis_style, no_tabs, []},
{elvis_style, macro_names, []},
{elvis_style, macro_module_names, []},
{elvis_style, operator_spaces, [{right, ","},
{right, "++"},
{left, "++"}]},
{elvis_style, nesting_level, [3]},
{elvis_style, god_modules, [25]},
{elvis_style, no_if_expression, []},
{elvis_style, invalid_dynamic_call, [elvis]},
{elvis_style, used_ignored_variable, []},
{elvis_style, no_behavior_info, []},
{
elvis_style,
module_naming_convention,
["^([a-z][a-z0-9]*_?)*(_SUITE)?$", []]
}
]
[#{dirs => ["../../test/examples"],
filter => "*.erl",
rules => [{elvis_style, line_length, [80]},
{elvis_style, no_tabs, []},
{elvis_style, macro_names, []},
{elvis_style, macro_module_names, []},
{elvis_style, operator_spaces, [{right, ","},
{right, "++"},
{left, "++"}]},
{elvis_style, nesting_level, [3]},
{elvis_style, god_modules, [25]},
{elvis_style, no_if_expression, []},
{elvis_style, invalid_dynamic_call, [elvis]},
{elvis_style, used_ignored_variable, []},
{elvis_style, no_behavior_info, []},
{
elvis_style,
module_naming_convention,
["^([a-z][a-z0-9]*_?)*(_SUITE)?$", []]
}
]
},
#{dirs => ["."],
filter => "Makefile",
rules => [{elvis_project, no_deps_master_erlang_mk, []}]
},
#{dirs => ["."],
filter => "rebar.config",
rules => [{elvis_project, no_deps_master_rebar, []}]
}
]
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion erlang.mk
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ else
endif

TEST_ERLC_OPTS ?= +debug_info +warn_export_vars +warn_shadow_vars +warn_obsolete_guard
TEST_ERLC_OPTS += -DTEST=1 -DEXTRA=1 +'{parse_transform, eunit_autoexport}'
TEST_ERLC_OPTS += -DTEST=1 -DEXTRA=1

# Core targets.

Expand Down
32 changes: 24 additions & 8 deletions src/elvis.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ rock() ->
Config = elvis_config:default(),
rock(Config).

-spec rock(elvis_config:config()) -> ok | {fail, elvis_result:file()}.
-spec rock(elvis_config:config()) -> ok | {fail, [elvis_result:file()]}.
rock(Config) when is_list(Config) ->
Results = lists:map(fun rock/1, Config),
lists:foldl(fun combine_results/2, ok, Results);
rock(Config = #{files := Files, rules := _Rules}) ->
elvis_utils:info("Loading files...~n"),
Fun = fun (File) ->
Expand All @@ -64,10 +67,15 @@ rock(Config = #{files := Files, rules := _Rules}) ->
fail -> {fail, Results};
ok -> ok
end;
rock(Config = #{src_dirs := SrcDirs, rules := _Rules}) ->
Files = elvis_utils:find_files(SrcDirs),

rock(Config#{files => Files});
rock(Config = #{dirs := _Dirs, rules := _Rules}) ->
NewConfig = elvis_config:resolve_files(Config),
rock(NewConfig);
rock(Config = #{src_dirs := Dirs}) ->
%% NOTE: Provided for backwards compatibility.
%% Rename 'src_dirs' key to 'dirs'.
Config1 = maps:remove(src_dirs, Config),
Config2 = Config1#{dirs => Dirs},
rock(Config2);
rock(Config) ->
throw({invalid_config, Config}).

Expand All @@ -76,9 +84,7 @@ rock(Config) ->
-spec git_hook(elvis_config:config()) -> ok.
git_hook(Config) ->
Files = elvis_git:staged_files(),
ErlFiles = elvis_utils:filter_files(Files),

NewConfig = Config#{files => ErlFiles},
NewConfig = elvis_config:resolve_files(Config, Files),

case rock(NewConfig) of
{fail, _} -> elvis_utils:erlang_halt(1);
Expand All @@ -102,6 +108,16 @@ webhook(Credentials, Request) ->
%%% Private
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-spec combine_results(ok | {fail, [elvis_result:file()]},
ok | {fail, [elvis_result:file()]}) ->
ok | {fail, [elvis_result:file()]}.
combine_results(ok, Acc) ->
Acc;
combine_results(Item, ok) ->
Item;
combine_results({fail, ItemResults}, {fail, AccResults}) ->
{fail, ItemResults ++ AccResults}.

-spec apply_rules(elvis_config:config(), elvis_utils:file()) ->
elvis_result:file().
apply_rules(Config = #{rules := Rules}, File) ->
Expand Down
2 changes: 1 addition & 1 deletion src/elvis_code.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
-spec parse_tree(elvis_config:config(), string() | binary()) ->
[{ok | error, erl_parse:abstract_form()}].
parse_tree(Config, Source) ->
IncludeDirs = elvis_utils:maps_get(src_dirs, Config, []),
IncludeDirs = elvis_config:dirs(Config),
SourceStr = elvis_utils:to_str(Source),
ScanOpts = [text, return_comments],
{ok, Tokens, _} = erl_scan:string(SourceStr, {1, 1}, ScanOpts),
Expand Down
56 changes: 53 additions & 3 deletions src/elvis_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
-export([
default/0,
load_file/1,
load/1
load/1,
dirs/1,
resolve_files/1,
resolve_files/2,
apply_to_files/2
]).

-export_type([
Expand All @@ -13,6 +17,7 @@
-type config() :: map().

-define(DEFAULT_CONFIG_PATH, "./elvis.config").
-define(DEFAULT_FILTER, "*.erl").

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Public
Expand All @@ -24,7 +29,8 @@ default() ->
{ok, [Config]} ->
load(Config);
{error, enoent} ->
application:get_env(elvis, config, #{});
Config = application:get_env(elvis, config, []),
ensure_config_list(Config);
{error, Reason} ->
throw(Reason)
end.
Expand All @@ -41,4 +47,48 @@ load_file(Path) ->
-spec load(term()) -> config().
load(AppConfig) ->
ElvisConfig = proplists:get_value(elvis, AppConfig, []),
proplists:get_value(config, ElvisConfig, #{}).
Config = proplists:get_value(config, ElvisConfig, []),
ensure_config_list(Config).

ensure_config_list(Config) when is_map(Config) ->
[Config];
ensure_config_list(Config) ->
Config.

-spec dirs(config()) -> [string()].
dirs(Config) when is_list(Config) ->
lists:flatmap(fun dirs/1, Config);
dirs(_RuleGroup = #{dirs := Dirs}) ->
Dirs;
dirs(#{}) ->
[].

-spec resolve_files(config(), [elvis_utils:file()]) -> config().
resolve_files(Config, Files) when is_list(Config) ->
Fun = fun(RuleGroup) -> resolve_files(RuleGroup, Files) end,
lists:map(Fun, Config);
resolve_files(RuleGroup = #{filter := Filter}, Files) ->
Filter = maps:get(filter, RuleGroup),
FilteredFiles = elvis_utils:filter_files(Files, Filter),
RuleGroup#{files => FilteredFiles};
resolve_files(RuleGroup, Files) ->
FilteredFiles = elvis_utils:filter_files(Files, ?DEFAULT_FILTER),
RuleGroup#{files => FilteredFiles}.

-spec resolve_files(config()) -> config().
resolve_files(Config) when is_list(Config) ->
lists:map(fun resolve_files/1, Config);
resolve_files(RuleGroup = #{dirs := Dirs, filter := Filter}) ->
Files = elvis_utils:find_files(Dirs, Filter, local),
RuleGroup#{files => Files};
resolve_files(RuleGroup = #{dirs := Dirs}) ->
Files = elvis_utils:find_files(Dirs, ?DEFAULT_FILTER),
RuleGroup#{files => Files}.

-spec apply_to_files(fun(), config()) -> config().
apply_to_files(Fun, Config) when is_list(Config) ->
ApplyFun = fun(RuleGroup) -> apply_to_files(Fun, RuleGroup) end,
lists:map(ApplyFun, Config);
apply_to_files(Fun, RuleGroup = #{files := Files}) ->
NewFiles = lists:map(Fun, Files),
RuleGroup#{files => NewFiles}.
Loading

0 comments on commit b453328

Please sign in to comment.