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

Attempt to write exclude_modules from within provider #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
86 changes: 73 additions & 13 deletions src/exerl_prv_consolidate.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,59 @@ do(State) ->
Deps = rebar_state:all_deps(State),
ProjectApps = rebar_state:project_apps(State),

OutDir = rebar_app_info:ebin_dir(hd(ProjectApps)),
TargetApp = hd(ProjectApps),
TargetAppName = ensure_atom(rebar_app_info:name(TargetApp)),
OutDir = rebar_app_info:ebin_dir(TargetApp),
filelib:ensure_path(OutDir),

AppPathsFromDeps = maps:from_list([
{ensure_atom(rebar_app_info:name(A)), rebar_app_info:ebin_dir(A)}
|| A <- ProjectApps ++ Deps
]),

% Through adding the code path (which has previously been updated with
% Elixir's paths), this will also consolidate the builtin protocols.
Paths = lists:uniq([rebar_app_info:ebin_dir(A) || A <- ProjectApps ++ Deps] ++ code:get_path()),
Protos = lists:uniq(?Protocol:extract_protocols(Paths)),
AppPathsFromCode = maps:from_list([{get_app_name(P), P} || P <- code:get_path()]),

AppPaths = maps:without(
[TargetAppName, '$unused_app'],
maps:merge(AppPathsFromCode, AppPathsFromDeps)
),

rebar_api:info("Consolidating ~p protocols ...", [length(Protos)]),
Paths = lists:uniq(maps:values(AppPaths)),

lists:foreach(
fun(Proto) ->
Impls = ?Protocol:extract_impls(Proto, Paths),
rebar_api:debug("Implementations of ~p:~n~p", [Proto, Impls]),
Protos = maps:filtermap(
fun(_, Path) ->
case ?Protocol:extract_protocols([Path]) of
[] ->
false;
P ->
{true, P}
end
end,
AppPaths
),

{ok, Consolidated} = ?Protocol:consolidate(Proto, Impls),
rebar_api:info(
"Consolidating ~p protocols ...",
[length(lists:flatten(maps:values(Protos)))]
),

Name = filename:join(OutDir, atom_to_list(Proto) ++ ".beam"),
file:write_file(Name, Consolidated),
ok
maps:foreach(
fun(App, PerAppProtos) ->
lists:foreach(
fun(Proto) ->
Impls = ?Protocol:extract_impls(Proto, Paths),
rebar_api:debug("Implementations of ~p from ~p:~n~p", [Proto, App, Impls]),

{ok, Consolidated} = ?Protocol:consolidate(Proto, Impls),

Name = filename:join(OutDir, atom_to_list(Proto) ++ ".beam"),
file:write_file(Name, Consolidated),
ok
end,
PerAppProtos
)
end,
Protos
),
Expand All @@ -75,8 +108,35 @@ do(State) ->
Cwd, post, ?PROVIDER, Providers, State
),

{ok, State}.
State1 = update_relx_excludes(Protos, State),

{ok, State1}.

update_relx_excludes(Protos, State) ->
Relx = rebar_state:get(State, relx, []),
ExcludeModules = proplists:get_value(exclude_modules, Relx, []),
ExcludeModules1 = proplists:from_map(Protos) ++ ExcludeModules,

Relx1 = [{exclude_modules, ExcludeModules1} | Relx],

rebar_state:set(State, relx, Relx1).

get_app_name(EbinDir) ->
try
[AppFilename | _] = filelib:wildcard("*.app", EbinDir),
AppName = filename:rootname(AppFilename),
list_to_existing_atom(AppName)
catch
error:_ ->
'$unused_app'
end.

ensure_atom(String) when is_list(String) ->
list_to_atom(String);
ensure_atom(Binary) when is_binary(Binary) ->
binary_to_atom(Binary);
ensure_atom(Atom) when is_atom(Atom) ->
Atom.
-spec format_error(any()) -> iolist().
format_error(no_app) ->
io_lib:format("No so_name or application defined.", []);
Expand Down
Loading