-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from inaka/jfacorro.2.console.tool
[#2] Implemented elvis for console usage
- Loading branch information
Showing
15 changed files
with
598 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ ebin | |
*.beam | ||
*.plt | ||
erl_crash.dump | ||
logs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
PROJECT = elvis | ||
|
||
DEPS = lager | ||
DEPS = lager sync | ||
|
||
dep_lager = https://github.com/basho/lager.git master | ||
dep_sync = https://github.com/rustyio/sync.git master | ||
|
||
include erlang.mk | ||
|
||
ERLC_OPTS += +'{parse_transform, lager_transform}' | ||
ERLC_OPTS += +warn_unused_vars +warn_export_all +warn_shadow_vars +warn_unused_import +warn_unused_function | ||
ERLC_OPTS += +warn_bif_clash +warn_unused_record +warn_deprecated_function +warn_obsolete_guard +strict_validation | ||
ERLC_OPTS += +warn_export_vars +warn_exported_vars +warn_missing_spec +warn_untyped_record +debug_info | ||
|
||
# Commont Test Config | ||
|
||
TEST_ERLC_OPTS += +'{parse_transform, lager_transform}' | ||
CT_SUITES = elvis | ||
CT_OPTS = -cover test/elvis.coverspec -erl_args -config config/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[ | ||
{ | ||
elvis, | ||
[ | ||
{src_dirs, ["src"]}, | ||
{rules, | ||
[ | ||
{elvis_style, line_length, [80]}, | ||
{elvis_style, no_tabs, []} | ||
] | ||
} | ||
] | ||
} | ||
]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[ | ||
{ | ||
elvis, | ||
[ | ||
{src_dirs, ["../../test/examples"]}, | ||
{rules, | ||
[ | ||
{elvis_style, line_length, [80]}, | ||
{elvis_style, no_tabs, []} | ||
] | ||
} | ||
] | ||
} | ||
]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
-module(elvis). | ||
|
||
%% Public API | ||
|
||
-export([ | ||
rock/0, | ||
rock/1 | ||
]). | ||
|
||
-export_type([ | ||
config/0 | ||
]). | ||
|
||
-type config() :: [{atom(), term()}]. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Public API | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec rock() -> ok. | ||
rock() -> | ||
Config = application:get_all_env(elvis), | ||
rock(Config). | ||
|
||
-spec rock(config()) -> ok. | ||
rock(Config) -> | ||
case elvis_utils:validate_config(Config) of | ||
valid -> | ||
run(Config); | ||
invalid -> | ||
throw(invalid_config) | ||
end. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Private | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec run(config()) -> ok. | ||
run(Config) -> | ||
SrcDirs = elvis_utils:source_dirs(Config), | ||
Pattern = "*.erl", | ||
FilePaths = elvis_utils:find_files(SrcDirs, Pattern), | ||
Results = lists:map(fun (Path) -> apply_rules(Config, Path) end, FilePaths), | ||
|
||
elvis_result:print(Results). | ||
|
||
apply_rules(Config, FilePath) -> | ||
Rules = elvis_utils:rules(Config), | ||
Acc = {[], Config, FilePath}, | ||
{RuleResults, _, _} = lists:foldl(fun apply_rule/2, Acc, Rules), | ||
elvis_result:new(file, FilePath, RuleResults). | ||
|
||
apply_rule({Module, Function, Args}, {Result, Config, FilePath}) -> | ||
Results = Module:Function(Config, FilePath, Args), | ||
RuleResult = elvis_result:new(rule, Function, Results), | ||
{[RuleResult | Result], Config, FilePath}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
-module(elvis_result). | ||
|
||
%% API | ||
-export([ | ||
new/3, | ||
print/1 | ||
]). | ||
|
||
%% Types | ||
-export_type([ | ||
item_result/0, | ||
rule_result/0, | ||
file_result/0 | ||
]). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Records | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-record(item_result, | ||
{ | ||
message = "" :: string(), | ||
info = [] :: list() | ||
}). | ||
|
||
-record(rule_result, | ||
{ | ||
name :: atom(), | ||
results = [] :: [item_result()] | ||
}). | ||
|
||
-record(file_result, | ||
{ | ||
path = "" :: string(), | ||
rules = [] :: list() | ||
}). | ||
|
||
-type item_result() :: #item_result{}. | ||
-type rule_result() :: #rule_result{}. | ||
-type file_result() :: #file_result{}. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Public | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec new(item | rule | file, any(), any()) -> | ||
item_result() | rule_result() | file_result(). | ||
new(item, Msg, Info) -> | ||
#item_result{message = Msg, info= Info}; | ||
new(rule, Name, Results) -> | ||
#rule_result{name = Name, results = Results}; | ||
new(file, Path, Rules) -> | ||
#file_result{path = Path, rules = Rules}. | ||
|
||
-spec print(item_result() | rule_result() | [file_result()]) -> ok. | ||
print([]) -> | ||
ok; | ||
print([Result | Results]) -> | ||
print(Result), | ||
print(Results); | ||
|
||
print(#file_result{path = Path, rules = Rules}) -> | ||
Status = case file_status(Rules) of | ||
ok -> "OK"; | ||
fail -> "FAIL" | ||
end, | ||
|
||
io:format("# ~s [~s]~n", [Path, Status]), | ||
print(Rules); | ||
|
||
print(#rule_result{results = []}) -> | ||
ok; | ||
print(#rule_result{name = Name, results = Results}) -> | ||
io:format(" - ~s~n", [atom_to_list(Name)]), | ||
print(Results); | ||
|
||
print(#item_result{message = Msg, info = Info}) -> | ||
io:format(" - " ++ Msg ++ "~n", Info). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Private | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec file_status([rule_result()]) -> ok | fail. | ||
file_status([]) -> | ||
ok; | ||
file_status([#rule_result{results = []} | Rules]) -> | ||
file_status(Rules); | ||
file_status(_Rules) -> | ||
fail. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
-module(elvis_style). | ||
|
||
-export([ | ||
line_length/3, | ||
no_tabs/3 | ||
]). | ||
|
||
-define(LINE_LENGTH_MSG, "Line ~p is too long: ~p."). | ||
-define(NO_TABS_MSG, "Line ~p has a tab at column ~p."). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Rules | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
%% @doc Target can be either a filename or the | ||
%% name of a module. | ||
-spec line_length(elvis:config(), string(), [term()]) -> | ||
[elvis_result:item_result()]. | ||
line_length(Config, Target, [Limit]) -> | ||
{ok, Src} = elvis_utils:src(Config, Target), | ||
elvis_utils:check_lines(Src, fun check_line_length/3, [Limit]). | ||
|
||
-spec no_tabs(elvis:config(), string(), [term()]) -> | ||
[elvis_result:item_result()]. | ||
no_tabs(Config, Target, []) -> | ||
{ok, Src} = elvis_utils:src(Config, Target), | ||
elvis_utils:check_lines(Src, fun check_no_tabs/3, []). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Private | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec check_line_length(binary(), integer(), [term()]) -> | ||
no_result | {ok, elvis_result:item_result()}. | ||
check_line_length(Line, Num, [Limit]) -> | ||
case byte_size(Line) of | ||
Large when Large > Limit -> | ||
Msg = ?LINE_LENGTH_MSG, | ||
Info = [Num, binary_to_list(Line)], | ||
Result = elvis_result:new(item, Msg, Info), | ||
{ok, Result}; | ||
_ -> | ||
no_result | ||
end. | ||
|
||
-spec check_no_tabs(binary(), integer(), [term()]) -> | ||
no_result | {ok, elvis_result:item_result()}. | ||
check_no_tabs(Line, Num, _Args) -> | ||
case binary:match(Line, <<"\t">>) of | ||
nomatch -> | ||
no_result; | ||
{Index, _} -> | ||
Msg = ?NO_TABS_MSG, | ||
Result = elvis_result:new(item, Msg, [Num, Index]), | ||
{ok, Result} | ||
end. |
Oops, something went wrong.