Skip to content

Commit

Permalink
[#199] improve protocol for deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Euen committed May 11, 2015
1 parent c2aefe1 commit b747fac
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
55 changes: 32 additions & 23 deletions src/elvis_project.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"commit.").

-define(DEP_NO_GIT,
"Dependency '~s' is not using git protocol, "
"please change this to something like `git://...`").
"Dependency '~s' is not using appropriate protocol, "
"please change this to something like '~s'").

-define(OLD_CONFIG_FORMAT,
"The current Elvis configuration file has an outdated format. "
Expand All @@ -34,11 +34,13 @@
[elvis_result:item()].
git_for_deps_erlang_mk(_Config, Target, RuleConfig) ->
IgnoreDeps = maps:get(ignore, RuleConfig, []),
Regex = maps:get(regex, RuleConfig, "git://.*"),
Deps = get_erlang_mk_deps(Target),
BadDeps = lists:filter(fun is_erlang_mk_not_git_dep/1, Deps),
BadDeps = lists:filter(fun(Dep) -> is_erlang_mk_not_git_dep(Dep, Regex) end,
Deps),
lists:flatmap(
fun(Line) ->
erlang_mk_dep_to_result(Line, ?DEP_NO_GIT, IgnoreDeps)
erlang_mk_dep_to_result(Line, ?DEP_NO_GIT, {IgnoreDeps, Regex})
end, BadDeps).

-type git_for_deps_rebar_config() :: #{ignore => [module()]}.
Expand All @@ -49,11 +51,13 @@ git_for_deps_erlang_mk(_Config, Target, RuleConfig) ->
[elvis_result:item()].
git_for_deps_rebar(_Config, Target, RuleConfig) ->
IgnoreDeps = maps:get(ignore, RuleConfig, []),
Regex = maps:get(regex, RuleConfig, "git://.*"),
Deps = get_rebar_deps(Target),
BadDeps = lists:filter(fun is_rebar_not_git_dep/1, Deps),
BadDeps = lists:filter(fun(Dep) -> is_rebar_not_git_dep(Dep, Regex) end,
Deps),
lists:flatmap(
fun(Line) ->
rebar_dep_to_result(Line, ?DEP_NO_GIT, IgnoreDeps)
rebar_dep_to_result(Line, ?DEP_NO_GIT, {IgnoreDeps, Regex})
end, BadDeps).

-type no_deps_master_erlang_mk_config() :: #{ignore => [module()]}.
Expand Down Expand Up @@ -130,15 +134,18 @@ is_rebar_master_dep({_AppName, _Vsn, {_SCM, _Location, {branch, "master"}}}) ->
is_rebar_master_dep(_) ->
false.

is_rebar_not_git_dep({_AppName, _Vsn, {_SCM, "git://" ++ _, _Branch}}) -> false;
is_rebar_not_git_dep(_) -> true.
is_rebar_not_git_dep({_AppName, _Vsn, {_SCM, Url, _Branch}}, Regex) ->
nomatch == re:run(Url, Regex, []).

rebar_dep_to_result({AppName, _, _}, Message, {IgnoreDeps, Regex}) ->
case lists:member(AppName, IgnoreDeps) of
true -> [];
false -> [elvis_result:new(item, Message, [AppName, Regex])]
end;
rebar_dep_to_result({AppName, _, _}, Message, IgnoreDeps) ->
case lists:member(AppName, IgnoreDeps) of
true ->
[];
false ->
[elvis_result:new(item, Message, [AppName])]
true -> [];
false -> [elvis_result:new(item, Message, [AppName])]
end.


Expand All @@ -150,33 +157,35 @@ is_erlang_mk_master_dep(Line) ->
_ -> true
end.

is_erlang_mk_not_git_dep(Line) ->
is_erlang_mk_not_git_dep(Line, Regex) ->
[_DepName, Dependency] = binary:split(Line, <<"=">>),
[Protocol, Url | _] =
[_Protocol, Url | _] =
[Part
|| Part <- binary:split(Dependency, <<" ">>, [global, trim])
, Part /= <<>>],
case {Protocol, Url} of
{<<"git">>, <<"git://", _/binary>>} -> false;
{<<"git">>, _} -> true;
{_NotGit, _} -> false
end.
nomatch == re:run(Url, Regex, []).

get_erlang_mk_deps(File) ->
{Src, _} = elvis_file:src(File),
Lines = binary:split(Src, <<"\n">>, [global]),
IsDepsLine = fun(Line) -> re:run(Line, "dep_", []) /= nomatch end,
lists:filter(IsDepsLine, Lines).

erlang_mk_dep_to_result(Line, Message, {IgnoreDeps, Regex}) ->
Opts = [{capture, all_but_first, binary}],
{match, [Name]} = re:run(Line, "dep_([^ ]*)", Opts),
NameAtom = binary_to_atom(Name, utf8),
case lists:member(NameAtom, IgnoreDeps) of
true -> [];
false -> [elvis_result:new(item, Message, [Name, Regex])]
end;
erlang_mk_dep_to_result(Line, Message, IgnoreDeps) ->
Opts = [{capture, all_but_first, binary}],
{match, [Name]} = re:run(Line, "dep_([^ ]*)", Opts),
NameAtom = binary_to_atom(Name, utf8),
case lists:member(NameAtom, IgnoreDeps) of
true ->
[];
false ->
[elvis_result:new(item, Message, [Name])]
true -> [];
false -> [elvis_result:new(item, Message, [Name])]
end.

%% Old config
Expand Down
15 changes: 12 additions & 3 deletions test/project_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,15 @@ verify_git_for_deps_erlang_mk(_Config) ->
File,
RuleConfig),

RuleConfig1 = #{ignore => [sync, meck]},
RuleConfig1 = #{ignore => [sync, meck], regex => "git://.*"},
[_] = elvis_project:git_for_deps_erlang_mk(ElvisConfig,
File,
RuleConfig1).
RuleConfig1),

RuleConfig2 = #{ignore => [sync], regex => "https://.*"},
[_, _, _, _, _] = elvis_project:git_for_deps_erlang_mk(ElvisConfig,
File,
RuleConfig2).

-spec verify_git_for_deps_rebar(config()) -> any().
verify_git_for_deps_rebar(_Config) ->
Expand All @@ -119,7 +124,11 @@ verify_git_for_deps_rebar(_Config) ->
[_] = elvis_project:git_for_deps_rebar(ElvisConfig, File, RuleConfig),

RuleConfig1 = #{ignore => [getopt, lager]},
[] = elvis_project:git_for_deps_rebar(ElvisConfig, File, RuleConfig1).
[] = elvis_project:git_for_deps_rebar(ElvisConfig, File, RuleConfig1),

RuleConfig2 = #{ignore => [meck], regex => "git@.*"},
[_, _, _, _] =
elvis_project:git_for_deps_rebar(ElvisConfig, File, RuleConfig2).

-spec verify_old_config_format(config()) -> any().
verify_old_config_format(_Config) ->
Expand Down

0 comments on commit b747fac

Please sign in to comment.