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

125 行
4.7 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. %% Modules from apps we can't reload without breaking functionality
  76. Blacklist = [ec_cmd_log, providers, cf, cth_readable],
  77. %% Similar to rebar_utils:update_code/1, but also forces a reload
  78. %% of used modules. Also forces to reload all of ebin/ instead
  79. %% of just the modules in the .app file, because 'extra_src_dirs'
  80. %% allows to load and compile files that are not to be kept
  81. %% in the app file.
  82. lists:foreach(fun(Path) ->
  83. Name = filename:basename(Path, "/ebin"),
  84. Files = filelib:wildcard(filename:join([Path, "*.beam"])),
  85. Modules = [list_to_atom(filename:basename(F, ".beam"))
  86. || F <- Files],
  87. App = list_to_atom(Name),
  88. application:load(App),
  89. case application:get_key(App, modules) of
  90. undefined ->
  91. code:add_patha(Path),
  92. ok;
  93. {ok, Mods} ->
  94. case {length(Mods), length(Mods -- Blacklist)} of
  95. {X,X} ->
  96. ?DEBUG("reloading ~p from ~s", [Modules, Path]),
  97. code:replace_path(App, Path),
  98. [begin code:purge(M), code:delete(M), code:load_file(M) end
  99. || M <- Modules];
  100. {_,_} ->
  101. ?DEBUG("skipping app ~p, stable copy required", [App])
  102. end
  103. end
  104. end, ToRefresh).
  105. refresh_state(RState, _Dir) ->
  106. lists:foldl(
  107. fun(F, State) -> F(State) end,
  108. rebar3:init_config(),
  109. [fun(S) -> rebar_state:apply_profiles(S, rebar_state:current_profiles(RState)) end]
  110. ).