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.

66 line
2.7 KiB

  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. -module(rebar_relx).
  4. -export([do/4,
  5. format_error/1]).
  6. -include("rebar.hrl").
  7. %% ===================================================================
  8. %% Public API
  9. %% ===================================================================
  10. -spec do(atom(), string(), atom(), rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  11. do(Module, Command, Provider, State) ->
  12. %% We set the color mode for relx as a application env
  13. application:set_env(relx, color_intensity, rebar_log:intensity()),
  14. LogLevel = rebar_log:get_level(),
  15. Options = rebar_state:command_args(State),
  16. DepsDir = rebar_dir:deps_dir(State),
  17. ProjectAppDirs = lists:delete(".", ?DEFAULT_PROJECT_APP_DIRS),
  18. LibDirs = rebar_utils:filtermap(fun ec_file:exists/1,
  19. [rebar_dir:checkouts_dir(State), DepsDir | ProjectAppDirs]),
  20. OutputDir = filename:join(rebar_dir:base_dir(State), ?DEFAULT_RELEASE_DIR),
  21. AllOptions = string:join([Command | Options], " "),
  22. Cwd = rebar_state:dir(State),
  23. Providers = rebar_state:providers(State),
  24. rebar_hooks:run_project_and_app_hooks(Cwd, pre, Provider, Providers, State),
  25. try
  26. case rebar_state:get(State, relx, []) of
  27. [] ->
  28. relx:main([{lib_dirs, LibDirs}
  29. ,{caller, api}
  30. ,{log_level, LogLevel} | output_dir(OutputDir, Options)], AllOptions);
  31. Config ->
  32. Config1 = merge_overlays(Config),
  33. relx:main([{lib_dirs, LibDirs}
  34. ,{config, Config1}
  35. ,{caller, api}
  36. ,{log_level, LogLevel} | output_dir(OutputDir, Options)], AllOptions)
  37. end,
  38. rebar_hooks:run_project_and_app_hooks(Cwd, post, Provider, Providers, State),
  39. {ok, State}
  40. catch
  41. throw:T ->
  42. {error, {Module, T}}
  43. end.
  44. -spec format_error(any()) -> iolist().
  45. format_error(Reason) ->
  46. io_lib:format("~p", [Reason]).
  47. %% Don't override output_dir if the user passed one on the command line
  48. output_dir(OutputDir, Options) ->
  49. [{output_dir, OutputDir} || not(lists:member("-o", Options))
  50. andalso not(lists:member("--output-dir", Options))].
  51. merge_overlays(Config) ->
  52. {Overlays, Others} =
  53. lists:partition(fun(C) when element(1, C) =:= overlay -> true;
  54. (_) -> false
  55. end, Config),
  56. %% Have profile overlay entries come before others to match how profiles work elsewhere
  57. NewOverlay = lists:reverse(lists:flatmap(fun({overlay, Overlay}) -> Overlay end, Overlays)),
  58. [{overlay, NewOverlay} | Others].