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.

101 lines
4.2 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 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_core).
  28. -export([process_command/2
  29. ,update_code_path/1]).
  30. -include("rebar.hrl").
  31. -spec process_command(rebar_state:t(), atom()) -> {ok, rebar_state:t()} | {error, string()}.
  32. process_command(State, Command) ->
  33. %% ? rebar_prv_install_deps:setup_env(State),
  34. Providers = rebar_state:providers(State),
  35. TargetProviders = providers:get_target_providers(Command, Providers),
  36. case providers:get_provider(Command, Providers) of
  37. not_found ->
  38. {error, io_lib:format("Command ~p not found", [Command])};
  39. CommandProvider ->
  40. Profile = providers:profile(CommandProvider),
  41. State1 = rebar_state:current_profile(State, Profile),
  42. Opts = providers:opts(CommandProvider)++rebar3:global_option_spec_list(),
  43. case Command of
  44. do ->
  45. do(TargetProviders, State1);
  46. _ ->
  47. case getopt:parse(Opts, rebar_state:command_args(State1)) of
  48. {ok, Args} ->
  49. State3 = rebar_state:command_parsed_args(State1, Args),
  50. do(TargetProviders, State3);
  51. {error, {invalid_option, Option}} ->
  52. {error, io_lib:format("Invalid option ~s on task ~p", [Option, Command])}
  53. end
  54. end
  55. end.
  56. -spec do([{atom(), atom()}], rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  57. do([], State) ->
  58. {ok, State};
  59. do([{ProviderName, Profile} | Rest], State) ->
  60. State1 = rebar_state:current_profile(State, Profile),
  61. Provider = providers:get_provider(ProviderName
  62. ,rebar_state:providers(State1)),
  63. case providers:do(Provider, State1) of
  64. {ok, State2} ->
  65. do(Rest, State2);
  66. {error, Error} ->
  67. {error, Error}
  68. end.
  69. update_code_path(State) ->
  70. true = rebar_utils:expand_code_path(),
  71. LibDirs = rebar_dir:lib_dirs(State),
  72. DepsDir = rebar_dir:deps_dir(State),
  73. PluginsDir = rebar_dir:plugins_dir(State),
  74. _UpdatedCodePaths = update_code_path_([DepsDir, PluginsDir | LibDirs]).
  75. %% ===================================================================
  76. %% Internal functions
  77. %% ===================================================================
  78. update_code_path_(Paths) ->
  79. LibPaths = expand_lib_dirs(Paths, rebar_dir:get_cwd(), []),
  80. ok = code:add_pathsa(LibPaths),
  81. %% track just the paths we added, so we can remove them without
  82. %% removing other paths added by this dep
  83. {added, LibPaths}.
  84. expand_lib_dirs([], _Root, Acc) ->
  85. Acc;
  86. expand_lib_dirs([Dir | Rest], Root, Acc) ->
  87. Apps = filelib:wildcard(filename:join([Dir, "*", "ebin"])),
  88. FqApps = case filename:pathtype(Dir) of
  89. absolute -> Apps;
  90. _ -> [filename:join([Root, A]) || A <- Apps]
  91. end,
  92. expand_lib_dirs(Rest, Root, Acc ++ FqApps).