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.

151 lines
5.0 KiB

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