Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

200 linhas
7.9 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. -define(APPUPFILEFORMAT, "%% appup generated for ~p by rebar (~p)~n"
  31. "{~p, [{~p, ~p}], [{~p, []}]}.~n").
  32. %% ====================================================================
  33. %% Public API
  34. %% ====================================================================
  35. 'generate-appups'(Config, ReltoolFile) ->
  36. %% Get the old release path
  37. {Config1, ReltoolConfig} = rebar_rel_utils:load_config(Config, ReltoolFile),
  38. TargetParentDir = rebar_rel_utils:get_target_parent_dir(Config,
  39. ReltoolConfig),
  40. PrevRelPath = rebar_rel_utils:get_previous_release_path(Config),
  41. OldVerPath = filename:join([TargetParentDir, PrevRelPath]),
  42. %% Get the new and old release name and versions
  43. {Name, _Ver} = rebar_rel_utils:get_reltool_release_info(ReltoolConfig),
  44. NewVerPath = filename:join([TargetParentDir, Name]),
  45. {NewName, NewVer} = rebar_rel_utils:get_rel_release_info(Name, NewVerPath),
  46. {OldName, OldVer} = rebar_rel_utils:get_rel_release_info(Name, OldVerPath),
  47. %% Run some simple checks
  48. true = rebar_utils:prop_check(NewVer =/= OldVer,
  49. "New and old .rel versions match~n", []),
  50. true = rebar_utils:prop_check(
  51. NewName == OldName,
  52. "Reltool and .rel release names do not match~n", []),
  53. %% Find all the apps that have been upgraded
  54. {_Added, _Removed, Upgraded} = get_apps(Name, OldVerPath, NewVerPath),
  55. %% Get a list of any appup files that exist in the new release
  56. NewAppUpFiles = rebar_utils:find_files(
  57. filename:join([NewVerPath, "lib"]), "^.*.appup$"),
  58. %% Convert the list of appup files into app names
  59. AppUpApps = [file_to_name(File) || File <- NewAppUpFiles],
  60. %% Create a list of apps that don't already have appups
  61. UpgradeApps = genappup_which_apps(Upgraded, AppUpApps),
  62. %% Generate appup files for upgraded apps
  63. generate_appup_files(NewVerPath, OldVerPath, UpgradeApps),
  64. {ok, Config1}.
  65. %% ===================================================================
  66. %% Internal functions
  67. %% ===================================================================
  68. get_apps(Name, OldVerPath, NewVerPath) ->
  69. OldApps = rebar_rel_utils:get_rel_apps(Name, OldVerPath),
  70. ?DEBUG("Old Version Apps: ~p~n", [OldApps]),
  71. NewApps = rebar_rel_utils:get_rel_apps(Name, NewVerPath),
  72. ?DEBUG("New Version Apps: ~p~n", [NewApps]),
  73. Added = app_list_diff(NewApps, OldApps),
  74. ?DEBUG("Added: ~p~n", [Added]),
  75. Removed = app_list_diff(OldApps, NewApps),
  76. ?DEBUG("Removed: ~p~n", [Removed]),
  77. PossiblyUpgraded = proplists:get_keys(NewApps),
  78. UpgradedApps = [upgraded_app(AppName,
  79. proplists:get_value(AppName, OldApps),
  80. proplists:get_value(AppName, NewApps))
  81. || AppName <- PossiblyUpgraded],
  82. Upgraded = lists:dropwhile(fun(Elem) ->
  83. Elem == false
  84. end, lists:sort(UpgradedApps)),
  85. ?DEBUG("Upgraded: ~p~n", [Upgraded]),
  86. {Added, Removed, Upgraded}.
  87. upgraded_app(AppName, OldAppVer, NewAppVer) when OldAppVer /= NewAppVer ->
  88. {AppName, {OldAppVer, NewAppVer}};
  89. upgraded_app(_, _, _) ->
  90. false.
  91. app_list_diff(List1, List2) ->
  92. List3 = lists:umerge(lists:sort(proplists:get_keys(List1)),
  93. lists:sort(proplists:get_keys(List2))),
  94. List3 -- proplists:get_keys(List2).
  95. file_to_name(File) ->
  96. filename:rootname(filename:basename(File)).
  97. genappup_which_apps(UpgradedApps, [First|Rest]) ->
  98. List = proplists:delete(list_to_atom(First), UpgradedApps),
  99. genappup_which_apps(List, Rest);
  100. genappup_which_apps(Apps, []) ->
  101. Apps.
  102. generate_appup_files(NewVerPath, OldVerPath, [{_App, {undefined, _}}|Rest]) ->
  103. generate_appup_files(NewVerPath, OldVerPath, Rest);
  104. generate_appup_files(NewVerPath, OldVerPath, [{App, {OldVer, NewVer}}|Rest]) ->
  105. OldEbinDir = filename:join([OldVerPath, "lib",
  106. atom_to_list(App) ++ "-" ++ OldVer, "ebin"]),
  107. NewEbinDir = filename:join([NewVerPath, "lib",
  108. atom_to_list(App) ++ "-" ++ NewVer, "ebin"]),
  109. {AddedFiles, DeletedFiles, ChangedFiles} = beam_lib:cmp_dirs(NewEbinDir,
  110. OldEbinDir),
  111. Added = [generate_instruction(added, File) || File <- AddedFiles],
  112. Deleted = [generate_instruction(deleted, File) || File <- DeletedFiles],
  113. Changed = [generate_instruction(changed, File) || File <- ChangedFiles],
  114. Inst = lists:append([Added, Deleted, Changed]),
  115. AppUpFile = filename:join([NewEbinDir, atom_to_list(App) ++ ".appup"]),
  116. ok = file:write_file(AppUpFile,
  117. io_lib:fwrite(?APPUPFILEFORMAT,
  118. [App, rebar_utils:now_str(), NewVer,
  119. OldVer, Inst, OldVer])),
  120. ?CONSOLE("Generated appup for ~p~n", [App]),
  121. generate_appup_files(NewVerPath, OldVerPath, Rest);
  122. generate_appup_files(_, _, []) ->
  123. ?CONSOLE("Appup generation complete~n", []).
  124. generate_instruction(added, File) ->
  125. Name = list_to_atom(file_to_name(File)),
  126. {add_module, Name};
  127. generate_instruction(deleted, File) ->
  128. Name = list_to_atom(file_to_name(File)),
  129. {delete_module, Name};
  130. generate_instruction(changed, {File, _}) ->
  131. {ok, {Name, List}} = beam_lib:chunks(File, [attributes, exports]),
  132. Behavior = get_behavior(List),
  133. CodeChange = is_code_change(List),
  134. generate_instruction_advanced(Name, Behavior, CodeChange).
  135. generate_instruction_advanced(Name, undefined, undefined) ->
  136. %% Not a behavior or code change, assume purely functional
  137. {load_module, Name};
  138. generate_instruction_advanced(Name, [supervisor], _) ->
  139. %% Supervisor
  140. {update, Name, supervisor};
  141. generate_instruction_advanced(Name, _, code_change) ->
  142. %% Includes code_change export
  143. {update, Name, {advanced, []}};
  144. generate_instruction_advanced(Name, _, _) ->
  145. %% Anything else
  146. {load_module, Name}.
  147. get_behavior(List) ->
  148. Attributes = proplists:get_value(attributes, List),
  149. case proplists:get_value(behavior, Attributes) of
  150. undefined -> proplists:get_value(behaviour, Attributes);
  151. Else -> Else
  152. end.
  153. is_code_change(List) ->
  154. Exports = proplists:get_value(exports, List),
  155. case proplists:is_defined(code_change, Exports) of
  156. true ->
  157. code_change;
  158. false ->
  159. undefined
  160. end.