2011-02-10 21:27:29 +00:00
|
|
|
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
|
|
|
|
%% ex: ts=4 sw=4 et
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% rebar: Erlang Build Tools
|
|
|
|
%%
|
|
|
|
%% Copyright (c) 2011 Joe Williams (joe@joetify.com)
|
|
|
|
%%
|
|
|
|
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
%% of this software and associated documentation files (the "Software"), to deal
|
|
|
|
%% in the Software without restriction, including without limitation the rights
|
|
|
|
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
%% copies of the Software, and to permit persons to whom the Software is
|
|
|
|
%% furnished to do so, subject to the following conditions:
|
|
|
|
%%
|
|
|
|
%% The above copyright notice and this permission notice shall be included in
|
|
|
|
%% all copies or substantial portions of the Software.
|
|
|
|
%%
|
|
|
|
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
%% THE SOFTWARE.
|
|
|
|
%% ------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(rebar_appups).
|
|
|
|
|
|
|
|
-include("rebar.hrl").
|
|
|
|
|
|
|
|
-export(['generate-appups'/2]).
|
|
|
|
|
2012-11-10 20:59:19 +00:00
|
|
|
%% for internal use only
|
|
|
|
-export([info/2]).
|
|
|
|
|
2011-02-10 21:27:29 +00:00
|
|
|
-define(APPUPFILEFORMAT, "%% appup generated for ~p by rebar (~p)~n"
|
|
|
|
"{~p, [{~p, ~p}], [{~p, []}]}.~n").
|
|
|
|
|
|
|
|
%% ====================================================================
|
|
|
|
%% Public API
|
|
|
|
%% ====================================================================
|
|
|
|
|
2012-04-22 19:53:32 +00:00
|
|
|
'generate-appups'(Config, ReltoolFile) ->
|
2011-02-10 21:27:29 +00:00
|
|
|
%% Get the old release path
|
2012-04-22 19:53:32 +00:00
|
|
|
{Config1, ReltoolConfig} = rebar_rel_utils:load_config(Config, ReltoolFile),
|
2012-07-14 21:44:47 +00:00
|
|
|
TargetParentDir = rebar_rel_utils:get_target_parent_dir(Config,
|
|
|
|
ReltoolConfig),
|
2011-06-30 21:50:56 +00:00
|
|
|
|
2012-07-14 21:44:47 +00:00
|
|
|
PrevRelPath = rebar_rel_utils:get_previous_release_path(Config),
|
|
|
|
OldVerPath = filename:join([TargetParentDir, PrevRelPath]),
|
2011-02-10 21:27:29 +00:00
|
|
|
|
|
|
|
%% Get the new and old release name and versions
|
2011-07-12 16:08:34 +00:00
|
|
|
{Name, _Ver} = rebar_rel_utils:get_reltool_release_info(ReltoolConfig),
|
2011-06-30 21:50:56 +00:00
|
|
|
NewVerPath = filename:join([TargetParentDir, Name]),
|
2011-02-16 17:46:40 +00:00
|
|
|
{NewName, NewVer} = rebar_rel_utils:get_rel_release_info(Name, NewVerPath),
|
|
|
|
{OldName, OldVer} = rebar_rel_utils:get_rel_release_info(Name, OldVerPath),
|
2011-02-10 21:27:29 +00:00
|
|
|
|
|
|
|
%% Run some simple checks
|
|
|
|
true = rebar_utils:prop_check(NewVer =/= OldVer,
|
|
|
|
"New and old .rel versions match~n", []),
|
|
|
|
true = rebar_utils:prop_check(
|
|
|
|
NewName == OldName,
|
|
|
|
"Reltool and .rel release names do not match~n", []),
|
|
|
|
|
|
|
|
%% Find all the apps that have been upgraded
|
2011-05-24 01:17:03 +00:00
|
|
|
{_Added, _Removed, Upgraded} = get_apps(Name, OldVerPath, NewVerPath),
|
2011-02-10 21:27:29 +00:00
|
|
|
|
|
|
|
%% Get a list of any appup files that exist in the new release
|
|
|
|
NewAppUpFiles = rebar_utils:find_files(
|
2011-06-30 21:50:56 +00:00
|
|
|
filename:join([NewVerPath, "lib"]), "^.*.appup$"),
|
2011-02-10 21:27:29 +00:00
|
|
|
|
|
|
|
%% Convert the list of appup files into app names
|
2011-06-02 19:03:09 +00:00
|
|
|
AppUpApps = [file_to_name(File) || File <- NewAppUpFiles],
|
2011-02-10 21:27:29 +00:00
|
|
|
|
|
|
|
%% Create a list of apps that don't already have appups
|
2011-05-24 01:17:03 +00:00
|
|
|
UpgradeApps = genappup_which_apps(Upgraded, AppUpApps),
|
2011-02-10 21:27:29 +00:00
|
|
|
|
2011-05-24 01:17:03 +00:00
|
|
|
%% Generate appup files for upgraded apps
|
2011-06-30 21:50:56 +00:00
|
|
|
generate_appup_files(NewVerPath, OldVerPath, UpgradeApps),
|
2011-02-10 21:27:29 +00:00
|
|
|
|
2012-04-22 19:53:32 +00:00
|
|
|
{ok, Config1}.
|
2011-02-10 21:27:29 +00:00
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ===================================================================
|
|
|
|
|
2012-11-10 20:59:19 +00:00
|
|
|
info(help, 'generate-appups') ->
|
|
|
|
?CONSOLE("Generate appup files.~n"
|
|
|
|
"~n"
|
|
|
|
"Valid command line options:~n"
|
|
|
|
" previous_release=path~n",
|
|
|
|
[]).
|
|
|
|
|
2011-05-24 01:17:03 +00:00
|
|
|
get_apps(Name, OldVerPath, NewVerPath) ->
|
2011-03-03 18:39:44 +00:00
|
|
|
OldApps = rebar_rel_utils:get_rel_apps(Name, OldVerPath),
|
2011-05-24 01:17:03 +00:00
|
|
|
?DEBUG("Old Version Apps: ~p~n", [OldApps]),
|
|
|
|
|
2011-03-03 18:39:44 +00:00
|
|
|
NewApps = rebar_rel_utils:get_rel_apps(Name, NewVerPath),
|
2011-05-24 01:17:03 +00:00
|
|
|
?DEBUG("New Version Apps: ~p~n", [NewApps]),
|
|
|
|
|
|
|
|
Added = app_list_diff(NewApps, OldApps),
|
|
|
|
?DEBUG("Added: ~p~n", [Added]),
|
|
|
|
|
|
|
|
Removed = app_list_diff(OldApps, NewApps),
|
|
|
|
?DEBUG("Removed: ~p~n", [Removed]),
|
|
|
|
|
|
|
|
PossiblyUpgraded = proplists:get_keys(NewApps),
|
|
|
|
|
|
|
|
UpgradedApps = [upgraded_app(AppName,
|
|
|
|
proplists:get_value(AppName, OldApps),
|
|
|
|
proplists:get_value(AppName, NewApps))
|
|
|
|
|| AppName <- PossiblyUpgraded],
|
|
|
|
|
|
|
|
Upgraded = lists:dropwhile(fun(Elem) ->
|
|
|
|
Elem == false
|
|
|
|
end, lists:sort(UpgradedApps)),
|
2011-03-03 18:39:44 +00:00
|
|
|
|
2011-05-24 01:17:03 +00:00
|
|
|
?DEBUG("Upgraded: ~p~n", [Upgraded]),
|
2011-03-03 18:39:44 +00:00
|
|
|
|
2011-05-24 01:17:03 +00:00
|
|
|
{Added, Removed, Upgraded}.
|
2011-03-03 18:39:44 +00:00
|
|
|
|
2011-05-24 01:17:03 +00:00
|
|
|
upgraded_app(AppName, OldAppVer, NewAppVer) when OldAppVer /= NewAppVer ->
|
|
|
|
{AppName, {OldAppVer, NewAppVer}};
|
|
|
|
upgraded_app(_, _, _) ->
|
|
|
|
false.
|
2011-03-03 18:39:44 +00:00
|
|
|
|
2011-05-24 01:17:03 +00:00
|
|
|
app_list_diff(List1, List2) ->
|
|
|
|
List3 = lists:umerge(lists:sort(proplists:get_keys(List1)),
|
|
|
|
lists:sort(proplists:get_keys(List2))),
|
2011-06-02 19:03:09 +00:00
|
|
|
List3 -- proplists:get_keys(List2).
|
2011-02-10 21:27:29 +00:00
|
|
|
|
|
|
|
file_to_name(File) ->
|
|
|
|
filename:rootname(filename:basename(File)).
|
|
|
|
|
|
|
|
genappup_which_apps(UpgradedApps, [First|Rest]) ->
|
2011-02-16 15:48:51 +00:00
|
|
|
List = proplists:delete(list_to_atom(First), UpgradedApps),
|
2011-02-10 21:27:29 +00:00
|
|
|
genappup_which_apps(List, Rest);
|
|
|
|
genappup_which_apps(Apps, []) ->
|
|
|
|
Apps.
|
|
|
|
|
2011-07-11 06:00:17 +00:00
|
|
|
generate_appup_files(NewVerPath, OldVerPath, [{_App, {undefined, _}}|Rest]) ->
|
|
|
|
generate_appup_files(NewVerPath, OldVerPath, Rest);
|
2011-06-30 21:50:56 +00:00
|
|
|
generate_appup_files(NewVerPath, OldVerPath, [{App, {OldVer, NewVer}}|Rest]) ->
|
|
|
|
OldEbinDir = filename:join([OldVerPath, "lib",
|
2011-02-10 21:27:29 +00:00
|
|
|
atom_to_list(App) ++ "-" ++ OldVer, "ebin"]),
|
2011-06-30 21:50:56 +00:00
|
|
|
NewEbinDir = filename:join([NewVerPath, "lib",
|
2011-02-10 21:27:29 +00:00
|
|
|
atom_to_list(App) ++ "-" ++ NewVer, "ebin"]),
|
|
|
|
|
|
|
|
{AddedFiles, DeletedFiles, ChangedFiles} = beam_lib:cmp_dirs(NewEbinDir,
|
|
|
|
OldEbinDir),
|
|
|
|
|
|
|
|
Added = [generate_instruction(added, File) || File <- AddedFiles],
|
|
|
|
Deleted = [generate_instruction(deleted, File) || File <- DeletedFiles],
|
|
|
|
Changed = [generate_instruction(changed, File) || File <- ChangedFiles],
|
|
|
|
|
|
|
|
Inst = lists:append([Added, Deleted, Changed]),
|
|
|
|
|
|
|
|
AppUpFile = filename:join([NewEbinDir, atom_to_list(App) ++ ".appup"]),
|
|
|
|
|
|
|
|
ok = file:write_file(AppUpFile,
|
|
|
|
io_lib:fwrite(?APPUPFILEFORMAT,
|
|
|
|
[App, rebar_utils:now_str(), NewVer,
|
|
|
|
OldVer, Inst, OldVer])),
|
|
|
|
|
|
|
|
?CONSOLE("Generated appup for ~p~n", [App]),
|
2011-06-30 21:50:56 +00:00
|
|
|
generate_appup_files(NewVerPath, OldVerPath, Rest);
|
2011-02-10 21:27:29 +00:00
|
|
|
generate_appup_files(_, _, []) ->
|
|
|
|
?CONSOLE("Appup generation complete~n", []).
|
|
|
|
|
|
|
|
generate_instruction(added, File) ->
|
|
|
|
Name = list_to_atom(file_to_name(File)),
|
|
|
|
{add_module, Name};
|
|
|
|
generate_instruction(deleted, File) ->
|
|
|
|
Name = list_to_atom(file_to_name(File)),
|
|
|
|
{delete_module, Name};
|
|
|
|
generate_instruction(changed, {File, _}) ->
|
|
|
|
{ok, {Name, List}} = beam_lib:chunks(File, [attributes, exports]),
|
|
|
|
Behavior = get_behavior(List),
|
|
|
|
CodeChange = is_code_change(List),
|
|
|
|
generate_instruction_advanced(Name, Behavior, CodeChange).
|
|
|
|
|
|
|
|
generate_instruction_advanced(Name, undefined, undefined) ->
|
|
|
|
%% Not a behavior or code change, assume purely functional
|
|
|
|
{load_module, Name};
|
|
|
|
generate_instruction_advanced(Name, [supervisor], _) ->
|
|
|
|
%% Supervisor
|
|
|
|
{update, Name, supervisor};
|
|
|
|
generate_instruction_advanced(Name, _, code_change) ->
|
|
|
|
%% Includes code_change export
|
|
|
|
{update, Name, {advanced, []}};
|
|
|
|
generate_instruction_advanced(Name, _, _) ->
|
|
|
|
%% Anything else
|
2011-07-01 17:28:48 +00:00
|
|
|
{load_module, Name}.
|
2011-02-10 21:27:29 +00:00
|
|
|
|
|
|
|
get_behavior(List) ->
|
|
|
|
Attributes = proplists:get_value(attributes, List),
|
2011-06-02 19:03:09 +00:00
|
|
|
case proplists:get_value(behavior, Attributes) of
|
|
|
|
undefined -> proplists:get_value(behaviour, Attributes);
|
|
|
|
Else -> Else
|
|
|
|
end.
|
2011-02-10 21:27:29 +00:00
|
|
|
|
|
|
|
is_code_change(List) ->
|
|
|
|
Exports = proplists:get_value(exports, List),
|
|
|
|
case proplists:is_defined(code_change, Exports) of
|
|
|
|
true ->
|
|
|
|
code_change;
|
|
|
|
false ->
|
|
|
|
undefined
|
|
|
|
end.
|