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.

118 line
4.3 KiB

  1. -module(rebar_agent).
  2. -export([start_link/1, do/1, do/2]).
  3. -export([init/1,
  4. handle_call/3, handle_cast/2, handle_info/2,
  5. code_change/3, terminate/2]).
  6. -include("rebar.hrl").
  7. -record(state, {state,
  8. cwd,
  9. show_warning=true}).
  10. start_link(State) ->
  11. gen_server:start_link({local, ?MODULE}, ?MODULE, State, []).
  12. do(Command) when is_atom(Command) ->
  13. gen_server:call(?MODULE, {cmd, Command}, infinity).
  14. do(Namespace, Command) when is_atom(Namespace), is_atom(Command) ->
  15. gen_server:call(?MODULE, {cmd, Namespace, Command}, infinity).
  16. init(State) ->
  17. Cwd = rebar_dir:get_cwd(),
  18. {ok, #state{state=State, cwd=Cwd}}.
  19. handle_call({cmd, Command}, _From, State=#state{state=RState, cwd=Cwd}) ->
  20. MidState = maybe_show_warning(State),
  21. {Res, NewRState} = run(default, Command, RState, Cwd),
  22. {reply, Res, MidState#state{state=NewRState}, hibernate};
  23. handle_call({cmd, Namespace, Command}, _From, State = #state{state=RState, cwd=Cwd}) ->
  24. MidState = maybe_show_warning(State),
  25. {Res, NewRState} = run(Namespace, Command, RState, Cwd),
  26. {reply, Res, MidState#state{state=NewRState}, hibernate};
  27. handle_call(_Call, _From, State) ->
  28. {noreply, State}.
  29. handle_cast(_Cast, State) ->
  30. {noreply, State}.
  31. handle_info(_Info, State) ->
  32. {noreply, State}.
  33. code_change(_OldVsn, State, _Extra) ->
  34. {ok, State}.
  35. terminate(_Reason, _State) ->
  36. ok.
  37. run(Namespace, Command, RState, Cwd) ->
  38. try
  39. case rebar_dir:get_cwd() of
  40. Cwd ->
  41. Args = [atom_to_list(Namespace), atom_to_list(Command)],
  42. CmdState0 = refresh_state(RState, Cwd),
  43. CmdState1 = rebar_state:set(CmdState0, task, atom_to_list(Command)),
  44. CmdState = rebar_state:set(CmdState1, caller, api),
  45. case rebar3:run(CmdState, Args) of
  46. {ok, TmpState} ->
  47. refresh_paths(TmpState),
  48. {ok, CmdState};
  49. {error, Err} when is_list(Err) ->
  50. refresh_paths(CmdState),
  51. {{error, lists:flatten(Err)}, CmdState};
  52. {error, Err} ->
  53. refresh_paths(CmdState),
  54. {{error, Err}, CmdState}
  55. end;
  56. _ ->
  57. {{error, cwd_changed}, RState}
  58. end
  59. catch
  60. Type:Reason ->
  61. ?DEBUG("Agent Stacktrace: ~p", [erlang:get_stacktrace()]),
  62. {{error, {Type, Reason}}, RState}
  63. end.
  64. maybe_show_warning(S=#state{show_warning=true}) ->
  65. ?WARN("This feature is experimental and may be modified or removed at any time.", []),
  66. S#state{show_warning=false};
  67. maybe_show_warning(State) ->
  68. State.
  69. refresh_paths(RState) ->
  70. ToRefresh = (rebar_state:code_paths(RState, all_deps)
  71. ++ [filename:join([rebar_app_info:out_dir(App), "test"])
  72. || App <- rebar_state:project_apps(RState)]
  73. %% make sure to never reload self; halt()s the VM
  74. ) -- [filename:dirname(code:which(?MODULE))],
  75. %% Similar to rebar_utils:update_code/1, but also forces a reload
  76. %% of used modules. Also forces to reload all of ebin/ instead
  77. %% of just the modules in the .app file, because 'extra_src_dirs'
  78. %% allows to load and compile files that are not to be kept
  79. %% in the app file.
  80. lists:foreach(fun(Path) ->
  81. Name = filename:basename(Path, "/ebin"),
  82. Files = filelib:wildcard(filename:join([Path, "*.beam"])),
  83. Modules = [list_to_atom(filename:basename(F, ".beam"))
  84. || F <- Files],
  85. App = list_to_atom(Name),
  86. application:load(App),
  87. case application:get_key(App, modules) of
  88. undefined ->
  89. code:add_patha(Path),
  90. ok;
  91. {ok, _} ->
  92. ?DEBUG("reloading ~p from ~s", [Modules, Path]),
  93. code:replace_path(App, Path),
  94. [begin code:purge(M), code:delete(M), code:load_file(M) end
  95. || M <- Modules]
  96. end
  97. end, ToRefresh).
  98. refresh_state(RState, _Dir) ->
  99. lists:foldl(
  100. fun(F, State) -> F(State) end,
  101. rebar3:init_config(),
  102. [fun(S) -> rebar_state:apply_profiles(S, rebar_state:current_profiles(RState)) end]
  103. ).