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

82 行
2.7 KiB

  1. -module(rebar_prv_bare_compile).
  2. -behaviour(provider).
  3. -export([init/1,
  4. do/1,
  5. format_error/1]).
  6. -include_lib("providers/include/providers.hrl").
  7. -include("rebar.hrl").
  8. -define(PROVIDER, compile).
  9. -define(NAMESPACE, bare).
  10. -define(DEPS, [{default, app_discovery}]).
  11. %% ===================================================================
  12. %% Public API
  13. %% ===================================================================
  14. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  15. init(State) ->
  16. State1 = rebar_state:add_provider(
  17. State,
  18. providers:create([
  19. {name, ?PROVIDER},
  20. {module, ?MODULE},
  21. {namespace, ?NAMESPACE},
  22. {bare, false},
  23. {deps, ?DEPS},
  24. {example, ""},
  25. {short_desc, ""},
  26. {desc, ""},
  27. {opts, [
  28. {paths, $p, "paths", string,
  29. "Wildcard paths of ebin directories to add to code path, "
  30. "separated by a colon"},
  31. {separator, $s, "separator", string,
  32. "In case of multiple return paths, the separator character "
  33. "to use to join them."},
  34. {outdir, $o, "outdir", string,
  35. "Path where build artifacts are located. Defaults to the "
  36. "current directory."}
  37. ]}
  38. ])
  39. ),
  40. {ok, State1}.
  41. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  42. do(State) ->
  43. OrigPath = code:get_path(),
  44. %% Add code paths from --paths to the beginning of the code path
  45. {RawOpts, _} = rebar_state:command_parsed_args(State),
  46. Paths = proplists:get_value(paths, RawOpts),
  47. Sep = proplists:get_value(separator, RawOpts, " "),
  48. OutDir = proplists:get_value(outdir, RawOpts, rebar_dir:get_cwd()),
  49. [ code:add_pathsa(filelib:wildcard(PathWildcard))
  50. || PathWildcard <- rebar_string:lexemes(Paths, Sep) ],
  51. case rebar_state:project_apps(State) of
  52. [] ->
  53. {error, {?MODULE, {not_an_application_structure, OutDir}}};
  54. [AppInfo] ->
  55. AppInfo1 = rebar_app_info:out_dir(AppInfo, OutDir),
  56. %% run compile in the default namespace
  57. rebar_prv_compile:compile(rebar_state:namespace(State, default), AppInfo1),
  58. rebar_utils:cleanup_code_path(OrigPath),
  59. {ok, State}
  60. end.
  61. -spec format_error(any()) -> iolist().
  62. format_error({not_an_application_structure, Path}) ->
  63. io_lib:format(
  64. "Compilation failed: there is no code in this directory (~ts), " ++
  65. "it is unreadable or for some other reason " ++
  66. "is not a recognizable application structure.",
  67. [Path]);
  68. format_error(Reason) ->
  69. io_lib:format("~p", [Reason]).