Skip to content

Commit

Permalink
Merge pull request #84 from clojerl/83-include-clojerl-modules-app-file
Browse files Browse the repository at this point in the history
[#83] Update .app files with modules before creating a release
  • Loading branch information
jfacorro authored Jul 10, 2020
2 parents 8c543b0 + 7695ad8 commit 3d93ef0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 42 deletions.
55 changes: 13 additions & 42 deletions src/rebar3_clojerl_prv_compile.erl
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,21 @@ backup_duplicates(DepsDirs, Config) ->
BeamFilepaths = rebar_utils:find_files(ProtoDir, ".beam$"),
BeamFilenames = [filename:basename(F) || F <- BeamFilepaths],

Dirs = DepsDirs -- [ProtoDir],
Dirs = DepsDirs -- [ProtoDir],
rebar_api:debug("Finding duplicates for:~n~p~nin~n~p", [BeamFilenames, Dirs]),
Deleted = [backup_duplicates_from_dir(BeamFilenames, Dir) || Dir <- Dirs],

ok = lists:foreach(fun update_app_file/1, Deleted).
[backup_duplicates_from_dir(BeamFilenames, Dir) || Dir <- Dirs],
ok.

-spec backup_duplicates_from_dir([file:name()], file:name()) ->
{file:name(), [file:name()]}.
-spec backup_duplicates_from_dir([file:name()], file:name()) -> [file:name()].
backup_duplicates_from_dir(BeamFilenames, Dir) ->
Filepaths = [ begin
rebar_api:debug("Backup duplicate ~s", [F]),
ok = file:rename(F, F ++ ".backup"),
F
end
|| F <- rebar_utils:find_files(Dir, ".beam$"),
lists:member(filename:basename(F), BeamFilenames)
],
{Dir, Filepaths}.
[ begin
rebar_api:debug("Backup duplicate ~s", [F]),
ok = file:rename(F, F ++ ".backup"),
F
end
|| F <- rebar_utils:find_files(Dir, ".beam$"),
lists:member(filename:basename(F), BeamFilenames)
].

-spec restore_duplicates([file:name()]) -> ok.
restore_duplicates(Dirs) ->
Expand All @@ -148,32 +145,6 @@ restore_duplicates(Dirs) ->
],
ok.

-spec update_app_file({file:name(), [file:name()]}) -> ok.
update_app_file({_Dir, []}) ->
ok;
update_app_file({Dir, Filepaths}) ->
case rebar_utils:find_files(Dir, ".app$", false) of
[AppFile] ->
DeletedModules = [ list_to_atom(filename:basename(Path, ".beam"))
|| Path <- Filepaths
],
{ok, [{application, AppName, AppDetail0}]} = file:consult(AppFile),
rebar_api:debug("Updating app file for ~p", [AppName]),
rebar_api:debug("Removing modules:~n~p", [DeletedModules]),
AppModules0 = proplists:get_value(modules, AppDetail0, []),
AppModules1 = AppModules0 -- DeletedModules,

AppDetail1 = lists:keyreplace( modules
, 1
, AppDetail0
, {modules, AppModules1}
),
Spec = io_lib:format("~p.\n", [{application, AppName, AppDetail1}]),
ok = rebar_file_utils:write_file_if_contents_differ(AppFile, Spec, utf8),
ok;
[] -> ok
end.

-spec compile(rebar_app_info:t(), config(), providers:t(), rebar_state:t()) ->
boolean().
compile(AppInfo0, Config0, Providers, State) ->
Expand Down Expand Up @@ -364,7 +335,7 @@ clje_compile_first(AppInfo) ->
{Positions, _} = lists:foldl(Fun, {#{}, length(CljeFirst)}, CljeFirst),
Positions.

-spec src_to_target(file:name()) -> file:name().
-spec src_to_target(file:name()) -> file:filename_all().
src_to_target(Src) ->
SrcBin = iolist_to_binary(Src),
Filename0 = clj_utils:resource_to_ns(SrcBin),
Expand Down
16 changes: 16 additions & 0 deletions src/rebar3_clojerl_prv_release.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,24 @@ init(State) ->

-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do(State) ->
update_all_app_files(State),
%% Generate release
rebar_relx:do(rlx_prv_release, "release", ?PROVIDER, State).

-spec format_error(any()) -> iolist().
format_error(Reason) ->
io_lib:format("~p", [Reason]).

%% =============================================================================
%% Internal functions
%% =============================================================================

-spec update_all_app_files(rebar_state:t()) -> ok.
update_all_app_files(State) ->
%% Update .app file for all apps
DepsPaths = rebar_state:code_paths(State, all_deps),
ProjectApps = rebar_state:project_apps(State),
AppsPaths = [rebar_app_info:ebin_dir(AppInfo) || AppInfo <- ProjectApps],
AllPaths = DepsPaths ++ AppsPaths,
[rebar3_clojerl_utils:update_app_file(Dir)|| Dir <- AllPaths],
ok.
37 changes: 37 additions & 0 deletions src/rebar3_clojerl_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
, find_app/2
, filter_app/2
, maybe_set_sname/1
, update_app_file/1
]).

-type opts() :: [{atom(), any()}].
Expand All @@ -20,6 +21,42 @@ ensure_clojerl() ->
rebar_api:abort("Application Clojerl could not be started: ~p", [Reason])
end.

%% @doc Updates the list of modules in the .app file for the specified
%% directory.
%%
%% The .app file will be update to include all modules in its
%% `modules' entry. The modules listed are resolved by looking for all
%% files with the extension `.beam' in `Dir'.
-spec update_app_file(file:name()) -> ok.
update_app_file(Dir) ->
case rebar_utils:find_files(Dir, ".app$", false) of
[AppFile] ->
{ok, [{application, AppName, AppDetail0}]} = file:consult(AppFile),

BeamPaths = rebar_utils:find_files(Dir, ".beam$", false),
Modules = [ list_to_atom(filename:basename(Path, ".beam"))
|| Path <- BeamPaths
],
AppDetail1 = lists:keyreplace( modules
, 1
, AppDetail0
, {modules, Modules}
),
SpecBefore = io_lib:format("~p.\n", [{application, AppName, AppDetail0}]),
SpecAfter = io_lib:format("~p.\n", [{application, AppName, AppDetail1}]),

rebar_api:debug("Updating app file for ~p", [AppName]),
rebar_api:debug("~p.app (BEFORE):~n~s", [AppName, SpecBefore]),
rebar_api:debug("~p.app (AFTER):~n~s", [AppName, SpecAfter]),

ok = rebar_file_utils:write_file_if_contents_differ( AppFile
, SpecAfter
, utf8
),
ok;
[] -> ok
end.

-spec all_apps(rebar_state:t()) -> [rebar_app_info:t()].
all_apps(State) ->
lists:usort(rebar_state:all_deps(State)) ++ rebar_state:project_apps(State).
Expand Down

0 comments on commit 3d93ef0

Please sign in to comment.