Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

210 righe
8.1 KiB

  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. %% -------------------------------------------------------------------
  4. %%
  5. %% rebar: Erlang Build Tools
  6. %%
  7. %% Copyright (c) 2011 Joe Williams (joe@joetify.com)
  8. %%
  9. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  10. %% of this software and associated documentation files (the "Software"), to deal
  11. %% in the Software without restriction, including without limitation the rights
  12. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. %% copies of the Software, and to permit persons to whom the Software is
  14. %% furnished to do so, subject to the following conditions:
  15. %%
  16. %% The above copyright notice and this permission notice shall be included in
  17. %% all copies or substantial portions of the Software.
  18. %%
  19. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. %% THE SOFTWARE.
  26. %% ------------------------------------------------------------------
  27. -module(rebar_appups).
  28. -include("rebar.hrl").
  29. -export(['generate-appups'/2]).
  30. %% for internal use only
  31. -export([info/2]).
  32. -define(APPUPFILEFORMAT, "%% appup generated for ~p by rebar (~p)~n"
  33. "{~p, [{~p, ~p}], [{~p, []}]}.~n").
  34. %% ====================================================================
  35. %% Public API
  36. %% ====================================================================
  37. 'generate-appups'(Config, ReltoolFile) ->
  38. %% Get the old release path
  39. {Config1, ReltoolConfig} = rebar_rel_utils:load_config(Config, ReltoolFile),
  40. TargetParentDir = rebar_rel_utils:get_target_parent_dir(Config,
  41. ReltoolConfig),
  42. PrevRelPath = rebar_rel_utils:get_previous_release_path(Config),
  43. OldVerPath = filename:join([TargetParentDir, PrevRelPath]),
  44. %% Get the new and old release name and versions
  45. {Name, _Ver} = rebar_rel_utils:get_reltool_release_info(ReltoolConfig),
  46. NewVerPath = filename:join([TargetParentDir, Name]),
  47. {NewName, NewVer} = rebar_rel_utils:get_rel_release_info(Name, NewVerPath),
  48. {OldName, OldVer} = rebar_rel_utils:get_rel_release_info(Name, OldVerPath),
  49. %% Run some simple checks
  50. true = rebar_utils:prop_check(NewVer =/= OldVer,
  51. "New and old .rel versions match~n", []),
  52. true = rebar_utils:prop_check(
  53. NewName == OldName,
  54. "Reltool and .rel release names do not match~n", []),
  55. %% Find all the apps that have been upgraded
  56. {_Added, _Removed, Upgraded} = get_apps(Name, OldVerPath, NewVerPath),
  57. %% Get a list of any appup files that exist in the new release
  58. NewAppUpFiles = rebar_utils:find_files(
  59. filename:join([NewVerPath, "lib"]), "^.*.appup$"),
  60. %% Convert the list of appup files into app names
  61. AppUpApps = [file_to_name(File) || File <- NewAppUpFiles],
  62. %% Create a list of apps that don't already have appups
  63. UpgradeApps = genappup_which_apps(Upgraded, AppUpApps),
  64. %% Generate appup files for upgraded apps
  65. generate_appup_files(NewVerPath, OldVerPath, UpgradeApps),
  66. {ok, Config1}.
  67. %% ===================================================================
  68. %% Internal functions
  69. %% ===================================================================
  70. info(help, 'generate-appups') ->
  71. ?CONSOLE("Generate appup files.~n"
  72. "~n"
  73. "Valid command line options:~n"
  74. " previous_release=path~n",
  75. []).
  76. get_apps(Name, OldVerPath, NewVerPath) ->
  77. OldApps = rebar_rel_utils:get_rel_apps(Name, OldVerPath),
  78. ?DEBUG("Old Version Apps: ~p~n", [OldApps]),
  79. NewApps = rebar_rel_utils:get_rel_apps(Name, NewVerPath),
  80. ?DEBUG("New Version Apps: ~p~n", [NewApps]),
  81. Added = app_list_diff(NewApps, OldApps),
  82. ?DEBUG("Added: ~p~n", [Added]),
  83. Removed = app_list_diff(OldApps, NewApps),
  84. ?DEBUG("Removed: ~p~n", [Removed]),
  85. PossiblyUpgraded = proplists:get_keys(NewApps),
  86. UpgradedApps = [upgraded_app(AppName,
  87. proplists:get_value(AppName, OldApps),
  88. proplists:get_value(AppName, NewApps))
  89. || AppName <- PossiblyUpgraded],
  90. Upgraded = lists:dropwhile(fun(Elem) ->
  91. Elem == false
  92. end, lists:sort(UpgradedApps)),
  93. ?DEBUG("Upgraded: ~p~n", [Upgraded]),
  94. {Added, Removed, Upgraded}.
  95. upgraded_app(AppName, OldAppVer, NewAppVer) when OldAppVer /= NewAppVer ->
  96. {AppName, {OldAppVer, NewAppVer}};
  97. upgraded_app(_, _, _) ->
  98. false.
  99. app_list_diff(List1, List2) ->
  100. List3 = lists:umerge(lists:sort(proplists:get_keys(List1)),
  101. lists:sort(proplists:get_keys(List2))),
  102. List3 -- proplists:get_keys(List2).
  103. file_to_name(File) ->
  104. filename:rootname(filename:basename(File)).
  105. genappup_which_apps(UpgradedApps, [First|Rest]) ->
  106. List = proplists:delete(list_to_atom(First), UpgradedApps),
  107. genappup_which_apps(List, Rest);
  108. genappup_which_apps(Apps, []) ->
  109. Apps.
  110. generate_appup_files(NewVerPath, OldVerPath, [{_App, {undefined, _}}|Rest]) ->
  111. generate_appup_files(NewVerPath, OldVerPath, Rest);
  112. generate_appup_files(NewVerPath, OldVerPath, [{App, {OldVer, NewVer}}|Rest]) ->
  113. OldEbinDir = filename:join([OldVerPath, "lib",
  114. atom_to_list(App) ++ "-" ++ OldVer, "ebin"]),
  115. NewEbinDir = filename:join([NewVerPath, "lib",
  116. atom_to_list(App) ++ "-" ++ NewVer, "ebin"]),
  117. {AddedFiles, DeletedFiles, ChangedFiles} = beam_lib:cmp_dirs(NewEbinDir,
  118. OldEbinDir),
  119. Added = [generate_instruction(added, File) || File <- AddedFiles],
  120. Deleted = [generate_instruction(deleted, File) || File <- DeletedFiles],
  121. Changed = [generate_instruction(changed, File) || File <- ChangedFiles],
  122. Inst = lists:append([Added, Deleted, Changed]),
  123. AppUpFile = filename:join([NewEbinDir, atom_to_list(App) ++ ".appup"]),
  124. ok = file:write_file(AppUpFile,
  125. io_lib:fwrite(?APPUPFILEFORMAT,
  126. [App, rebar_utils:now_str(), NewVer,
  127. OldVer, Inst, OldVer])),
  128. ?CONSOLE("Generated appup for ~p~n", [App]),
  129. generate_appup_files(NewVerPath, OldVerPath, Rest);
  130. generate_appup_files(_, _, []) ->
  131. ?CONSOLE("Appup generation complete~n", []).
  132. generate_instruction(added, File) ->
  133. Name = list_to_atom(file_to_name(File)),
  134. {add_module, Name};
  135. generate_instruction(deleted, File) ->
  136. Name = list_to_atom(file_to_name(File)),
  137. {delete_module, Name};
  138. generate_instruction(changed, {File, _}) ->
  139. {ok, {Name, List}} = beam_lib:chunks(File, [attributes, exports]),
  140. Behavior = get_behavior(List),
  141. CodeChange = is_code_change(List),
  142. generate_instruction_advanced(Name, Behavior, CodeChange).
  143. generate_instruction_advanced(Name, undefined, undefined) ->
  144. %% Not a behavior or code change, assume purely functional
  145. {load_module, Name};
  146. generate_instruction_advanced(Name, [supervisor], _) ->
  147. %% Supervisor
  148. {update, Name, supervisor};
  149. generate_instruction_advanced(Name, _, code_change) ->
  150. %% Includes code_change export
  151. {update, Name, {advanced, []}};
  152. generate_instruction_advanced(Name, _, _) ->
  153. %% Anything else
  154. {load_module, Name}.
  155. get_behavior(List) ->
  156. Attributes = proplists:get_value(attributes, List),
  157. case proplists:get_value(behavior, Attributes) of
  158. undefined -> proplists:get_value(behaviour, Attributes);
  159. Else -> Else
  160. end.
  161. is_code_change(List) ->
  162. Exports = proplists:get_value(exports, List),
  163. case proplists:is_defined(code_change, Exports) of
  164. true ->
  165. code_change;
  166. false ->
  167. undefined
  168. end.