You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.2 KiB

15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
14 years ago
14 years ago
  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) 2009 Dave Smith (dizzyd@dizzyd.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_app_utils).
  28. -export([is_app_dir/0, is_app_dir/1,
  29. is_app_src/1,
  30. app_src_to_app/1,
  31. app_name/1,
  32. app_applications/1,
  33. app_vsn/1]).
  34. -export([load_app_file/1]). % TEMPORARY
  35. -include("rebar.hrl").
  36. %% ===================================================================
  37. %% Public API
  38. %% ===================================================================
  39. is_app_dir() ->
  40. is_app_dir(rebar_utils:get_cwd()).
  41. is_app_dir(Dir) ->
  42. AppSrc = filename:join(Dir, "src/*.app.src"),
  43. case filelib:wildcard(AppSrc) of
  44. [AppSrcFile] ->
  45. {true, AppSrcFile};
  46. _ ->
  47. App = filename:join([Dir, "ebin/*.app"]),
  48. case filelib:wildcard(App) of
  49. [AppFile] ->
  50. {true, AppFile};
  51. _ ->
  52. false
  53. end
  54. end.
  55. is_app_src(Filename) ->
  56. %% If removing the extension .app.src yields a shorter name,
  57. %% this is an .app.src file.
  58. Filename =/= filename:rootname(Filename, ".app.src").
  59. app_src_to_app(Filename) ->
  60. filename:join("ebin", filename:basename(Filename, ".app.src") ++ ".app").
  61. app_name(AppFile) ->
  62. case load_app_file(AppFile) of
  63. {ok, AppName, _} ->
  64. AppName;
  65. {error, Reason} ->
  66. ?ABORT("Failed to extract name from ~s: ~p\n",
  67. [AppFile, Reason])
  68. end.
  69. app_applications(AppFile) ->
  70. case load_app_file(AppFile) of
  71. {ok, _, AppInfo} ->
  72. get_value(applications, AppInfo, AppFile);
  73. {error, Reason} ->
  74. ?ABORT("Failed to extract applications from ~s: ~p\n",
  75. [AppFile, Reason])
  76. end.
  77. app_vsn(AppFile) ->
  78. case load_app_file(AppFile) of
  79. {ok, _, AppInfo} ->
  80. get_value(vsn, AppInfo, AppFile);
  81. {error, Reason} ->
  82. ?ABORT("Failed to extract vsn from ~s: ~p\n",
  83. [AppFile, Reason])
  84. end.
  85. %% ===================================================================
  86. %% Internal functions
  87. %% ===================================================================
  88. load_app_file(Filename) ->
  89. AppFile = {app_file, Filename},
  90. case erlang:get(AppFile) of
  91. undefined ->
  92. case file:consult(Filename) of
  93. {ok, [{application, AppName, AppData}]} ->
  94. erlang:put(AppFile, {AppName, AppData}),
  95. {ok, AppName, AppData};
  96. {error, _} = Error ->
  97. Error;
  98. Other ->
  99. {error, {unexpected_terms, Other}}
  100. end;
  101. {AppName, AppData} ->
  102. {ok, AppName, AppData}
  103. end.
  104. get_value(Key, AppInfo, AppFile) ->
  105. case proplists:get_value(Key, AppInfo) of
  106. undefined ->
  107. ?ABORT("Failed to get app value '~p' from '~s'~n", [Key, AppFile]);
  108. Value ->
  109. Value
  110. end.