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

Add apps option to rebar3 commands #538

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 21 additions & 0 deletions examples/rebar3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,24 @@ Files to not type check. Subtracts the list of included files
type: `boolean()`

if 'true' stop type checking at the first error, if 'false' continue checking all functions in the given file and all files in the given directory

## apps

types: `string()`

Apps to type check. In the case of umbrella projects, it would only run the type check on the list of apps defined. The list should be a comma separated list.

For example:
```
rebar3 gradualizer --apps=app1,app2
```

It can also be defined in `rebar.config`. Note that the list of apps defined in this file will only be used if no other app is passed to the `rebar3 gradualizer` command.
```
{gradualizer_opts, [
{apps, [
app1,
app2
]}
]}.
```
9 changes: 6 additions & 3 deletions gradualize-ignore.lst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
ebin/gradualizer_file_utils.beam: Call to undefined function epp:open/5 on line 66 at column 16
ebin/rebar_prv_gradualizer.beam: Undefined remote type rebar_state:t/0 on line 23 at column 11
ebin/rebar_prv_gradualizer.beam: Undefined remote type rebar_state:t/0 on line 37 at column 9
ebin/rebar_prv_gradualizer.beam: Undefined remote type rebar_app_info:t/0 on line 52 at column 28
ebin/rebar_prv_gradualizer.beam: Undefined remote type rebar_app_info:t/0 on line 70 at column 21
ebin/rebar_prv_gradualizer.beam: Undefined remote type rebar_state:t/0 on line 40 at column 9
ebin/rebar_prv_gradualizer.beam: Undefined remote type rebar_app_info:t/0 on line 58 at column 28
ebin/rebar_prv_gradualizer.beam: Undefined remote type rebar_app_info:t/0 on line 76 at column 21
ebin/rebar_prv_gradualizer.beam: Call to undefined function rebar_dir:src_dirs/2 on line 111 at column 24
ebin/rebar_prv_gradualizer.beam: Call to undefined function rebar_dir:src_dirs/2 on line 119 at column 24
ebin/rebar_prv_gradualizer.beam: Call to undefined function rebar_state:get/3 on line 133 at column 25
ebin/rebar_prv_gradualizer.beam: Call to undefined function rebar_state:project_apps/1 on line 148 at column 34
45 changes: 42 additions & 3 deletions src/rebar_prv_gradualizer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ init(State) ->
{bare, true},
{deps, ?DEPS},
{example, "rebar gradualizer"},
{opts, [{use_beams, $b, "use_beams", boolean, "use beam files as input"}]},
{opts, [
{use_beams, $b, "use_beams", boolean, "use beam files as input"},
{apps, $a, "apps", string, "Comma separated list of applications to perform type check"}
]},
{short_desc, "typecheck the project with gradualizer"},
{desc, ""}
]),
Expand All @@ -39,10 +42,13 @@ do(State) ->
{ok, _} = application:ensure_all_started(gradualizer),
{Opts, _} = rebar_state:command_parsed_args(State),
UseBeams = proplists:get_value(use_beams, Opts, false),

Apps = get_apps(proplists:get_value(apps, Opts, ""), State),

code:add_pathsa(rebar_state:code_paths(State, all_deps)),
CheckedApps = lists:map(
fun (App) -> gradualizer_check_app(App, UseBeams) end,
rebar_state:project_apps(State)),
Apps),
HasNok = lists:member(nok, CheckedApps),
if
HasNok -> {error, {?MODULE, undefined}};
Expand Down Expand Up @@ -102,7 +108,9 @@ files_to_check(App) ->
not lists:member(File, ExpandedExclude)
end, ExpandedFiles).

-spec format_error(any()) -> string().
-spec format_error(any()) -> iolist().
erszcz marked this conversation as resolved.
Show resolved Hide resolved
format_error({unknown_application, App}) ->
lists:flatten(io_lib:format("Unknown app: ~p", [App]));
format_error(_) ->
"Gradualizer found errors.".

Expand All @@ -119,3 +127,34 @@ normalize_src_dirs(SrcDirs, ExtraDirs) ->
S = lists:usort(SrcDirs),
E = lists:subtract(lists:usort(ExtraDirs), S),
{S, E}.

get_apps("", State) ->
% if no apps have been given to the command, check rebar.config for defined apps
Config = rebar_state:get(State, gradualizer_opts, []),
case proplists:get_value(apps, Config, []) of
[] ->
rebar_state:project_apps(State);
OnlyApps ->
filter_apps(OnlyApps, State)
end;
get_apps(CommandApps, State) ->
Only = [list_to_atom(App)|| App <- string:lexemes(CommandApps, [$,])],
filter_apps(Only, State).

filter_apps(Apps, State) ->
AllApps = maps:from_list(
[
{binary_to_atom(rebar_app_info:name(App)), App}
|| App <- rebar_state:project_apps(State)
]
),
lists:map(
fun(App) ->
case AllApps of
#{App := AppState} -> AppState;
#{} ->
throw({error, {?MODULE, {unknown_application, App}}})
end
end,
Apps
).