Você não pode selecionar mais de 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.

149 linhas
5.6 KiB

15 anos atrás
15 anos atrás
15 anos atrás
15 anos atrás
15 anos atrás
15 anos atrás
15 anos atrás
15 anos atrás
15 anos atrás
  1. %% -------------------------------------------------------------------
  2. %%
  3. %% rebar: Erlang Build Tools
  4. %%
  5. %% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
  6. %%
  7. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  8. %% of this software and associated documentation files (the "Software"), to deal
  9. %% in the Software without restriction, including without limitation the rights
  10. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. %% copies of the Software, and to permit persons to whom the Software is
  12. %% furnished to do so, subject to the following conditions:
  13. %%
  14. %% The above copyright notice and this permission notice shall be included in
  15. %% all copies or substantial portions of the Software.
  16. %%
  17. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. %% THE SOFTWARE.
  24. %% -------------------------------------------------------------------
  25. -module(rebar_otp_app).
  26. -export([compile/2,
  27. install/2]).
  28. -include("rebar.hrl").
  29. %% ===================================================================
  30. %% Public API
  31. %% ===================================================================
  32. compile(Config, File) ->
  33. %% Load the app name and version from the .app file and construct
  34. %% the app identifier
  35. {ok, AppName, AppData} = rebar_app_utils:load_app_file(File),
  36. validate_name(AppName, File),
  37. validate_modules(AppName, proplists:get_value(modules, AppData)),
  38. ok.
  39. install(Config, File) ->
  40. %% Load the app name and version from the .app file and construct
  41. %% the app identifier
  42. {ok, AppName, AppData} = rebar_app_utils:load_app_file(File),
  43. Vsn = proplists:get_value(vsn, AppData),
  44. AppId = ?FMT("~s-~s", [AppName, Vsn]),
  45. ?CONSOLE("Installing: ~s\n", [AppId]),
  46. %% Check the erlang lib directory to see if this app identifier
  47. %% is already present.
  48. AppDir = filename:join([code:lib_dir(), AppId]),
  49. case filelib:is_dir(AppDir) of
  50. true ->
  51. %% Already exists -- check for force=1 global flag and only
  52. %% continue if it's set
  53. case rebar_config:get_global(force, "0") of
  54. "0" ->
  55. ?ERROR("~s already exists. Installation failed.\n", [AppId]),
  56. ?FAIL;
  57. "1" ->
  58. ?WARN("~s already exists, but forcibly overwriting.\n", [AppId])
  59. end;
  60. false ->
  61. ok
  62. end,
  63. %% Wipe out any previous versions
  64. ok = rebar_file_utils:rm_rf(AppDir),
  65. %% Re-create target
  66. ok = rebar_file_utils:mkdir_p(AppDir),
  67. %% By default we copy the ebin, include, src and priv directories
  68. ok = rebar_file_utils:cp_r(["ebin", "src", "priv", "include"],
  69. AppDir),
  70. %% Check the config to see if we have any binaries that need to be
  71. %% linked into the erlang path
  72. case rebar_config:get_list(Config, app_bin, []) of
  73. [] ->
  74. ok;
  75. List ->
  76. %% code:root_dir() gives $OTPROOT/lib/erlang on a stock install
  77. %% so find the bin dir relative to that.
  78. BinDir = filename:join([code:root_dir(), "..", "..", "bin"]),
  79. install_binaries(List, AppDir, BinDir)
  80. end.
  81. %% ===================================================================
  82. %% Internal functions
  83. %% ===================================================================
  84. install_binaries([], _AppDir, _BinDir) ->
  85. ok;
  86. install_binaries([Bin | Rest], AppDir, BinDir) ->
  87. FqBin = filename:join([AppDir, Bin]),
  88. rebar_file_utils:ln_sf(FqBin, BinDir),
  89. install_binaries(Rest, AppDir, BinDir).
  90. validate_name(AppName, File) ->
  91. %% Convert the .app file name to an atom -- check it against the identifier within the file
  92. ExpApp = list_to_atom(filename:basename(File, ".app")),
  93. case ExpApp == AppName of
  94. true ->
  95. ok;
  96. false ->
  97. ?ERROR("Invalid ~s: name of application (~p) must match filename.\n", [File, AppName]),
  98. ?FAIL
  99. end.
  100. validate_modules(AppName, undefined) ->
  101. ?ERROR("Missing modules declaration in~p.app:\n~s", [AppName]),
  102. ?FAIL;
  103. validate_modules(AppName, Mods) ->
  104. %% Construct two sets -- one for the actual .beam files in ebin/ and one for the modules
  105. %% listed in the .app file
  106. EbinSet = ordsets:from_list([beam_to_mod(N) || N <- filelib:wildcard("ebin/*.beam")]),
  107. ModSet = ordsets:from_list(Mods),
  108. %% Identify .beam files listed in the .app, but not present in ebin/
  109. case ordsets:subtract(ModSet, EbinSet) of
  110. [] ->
  111. ok;
  112. MissingBeams ->
  113. Msg1 = lists:flatten([io_lib:format("\t* ~p\n", [M]) || M <- MissingBeams]),
  114. ?ERROR("One or more modules listed in ~p.app are not present in ebin/*.beam:\n~s",
  115. [AppName, Msg1]),
  116. ?FAIL
  117. end,
  118. %% Identify .beam files NOT list in the .app, but present in ebin/
  119. case ordsets:subtract(EbinSet, ModSet) of
  120. [] ->
  121. ok;
  122. MissingMods ->
  123. Msg2 = lists:flatten([io_lib:format("\t* ~p\n", [M]) || M <- MissingMods]),
  124. ?ERROR("On or more .beam files exist that are not listed in ~p.app:\n~s",
  125. [AppName, Msg2]),
  126. ?FAIL
  127. end.
  128. beam_to_mod(Filename) ->
  129. list_to_atom(filename:basename(Filename, ".beam")).