您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

165 行
5.4 KiB

  1. #!/usr/bin/env escript
  2. %% -*- erlang -*-
  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. -include_lib("rebar/include/rebar.hrl").
  28. main(Commands) ->
  29. %% Pre-load the rebar app so that we get default configuration
  30. application:load(rebar),
  31. %% From the current working directory, search recursively and find
  32. %% all the application and release directories. We always terminate the
  33. %% recursion at an application or release directory.
  34. Cwd = rebar_utils:get_cwd(),
  35. case target_type(Cwd) of
  36. undefined ->
  37. Targets = find_targets(Cwd);
  38. {Type, Filename} ->
  39. Targets = [{Type, Cwd, Filename}]
  40. end,
  41. %% Prefix all the app targets to the code path so that inter-app compilation
  42. %% works properly
  43. update_code_path(Targets),
  44. %% Finally, apply the specified command to each target
  45. apply_commands(Targets, Commands);
  46. main(_) ->
  47. io:format("usage: rebar <command>...\n").
  48. %%
  49. %% Recursively find all the targets starting at a root directory
  50. %%
  51. find_targets(Root) ->
  52. {ok, Files} = file:list_dir(Root),
  53. find_targets(Files, Root, [], 1).
  54. find_targets([], _Root, Acc, _Depth) ->
  55. Acc;
  56. find_targets(_Files, _Root, Acc, 10) ->
  57. Acc;
  58. find_targets([F | Rest], Root, Acc, Depth) ->
  59. AbsName = filename:join([Root, F]),
  60. case target_type(AbsName) of
  61. undefined ->
  62. case filelib:is_dir(AbsName) of
  63. true ->
  64. {ok, SubFiles} = file:list_dir(AbsName),
  65. Acc2 = find_targets(SubFiles, AbsName, Acc, Depth+1);
  66. false ->
  67. Acc2 = Acc
  68. end;
  69. {Type, Filename} ->
  70. Acc2 = [{Type, AbsName, Filename} | Acc]
  71. end,
  72. find_targets(Rest, Root, Acc2, Depth).
  73. %%
  74. %% Determine the target type of a given file: app, rel or undefined
  75. %%
  76. target_type(AbsName) ->
  77. case rebar_app_utils:is_app_dir(AbsName) of
  78. {true, AppFile} ->
  79. {app, AppFile};
  80. false ->
  81. case rebar_rel_utils:is_rel_dir(AbsName) of
  82. true ->
  83. {rel, ""};
  84. false ->
  85. undefined
  86. end
  87. end.
  88. %%
  89. %% Given a command and target type, determine if the command is applicable
  90. %%
  91. valid_command(compile, app) -> true;
  92. valid_command(clean, _Any) -> true;
  93. valid_command(_, _) -> false.
  94. %%
  95. %% Add all application targets to the front of the code path
  96. %%
  97. update_code_path([]) ->
  98. ok;
  99. update_code_path([{app, Dir, _} | Rest]) ->
  100. EbinDir = filename:join([Dir, "ebin"]),
  101. true = code:add_patha(EbinDir),
  102. update_code_path(Rest);
  103. update_code_path([_ | Rest]) ->
  104. update_code_path(Rest).
  105. apply_commands(_Targets, []) ->
  106. ok;
  107. apply_commands(Targets, [CommandStr | Rest]) ->
  108. %% Convert the command into an atom for convenience
  109. Command = list_to_atom(CommandStr),
  110. %% Filter out all the targets, based on the specified command.
  111. FilteredTargets = [{Type, Dir, Filename} || {Type, Dir, Filename} <- Targets,
  112. valid_command(Command, Type) == true],
  113. case apply_command(FilteredTargets, Command) of
  114. ok ->
  115. apply_commands(Targets, Rest);
  116. Other ->
  117. Other
  118. end.
  119. apply_command([], _Command) ->
  120. ok;
  121. apply_command([{Type, Dir, File} | Rest], Command) ->
  122. ok = file:set_cwd(Dir),
  123. Config = rebar_config:new(Dir),
  124. %% Provide some info on where we are
  125. ?CONSOLE("==> ~s (~s)\n", [filename:basename(Dir), Command]),
  126. %% Pull the list of modules that are associated with Type operations. Each module
  127. %% will be inspected for a function matching Command and if found, will execute that.
  128. Modules = rebar_config:get_modules(Config, Type),
  129. case catch(run_modules(Modules, Command, Config, File)) of
  130. ok ->
  131. apply_command(Rest, Command);
  132. Other ->
  133. ?CONSOLE("~p failed while processing ~s: ~p", [Command, Dir, Other])
  134. end.
  135. run_modules([], _Command, _Config, _File) ->
  136. ok;
  137. run_modules([Module | Rest], Command, Config, File) ->
  138. case Module:Command(Config, File) of
  139. ok ->
  140. run_modules(Rest, Command, Config, File);
  141. {error, Reason} ->
  142. {error, Reason}
  143. end.