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) 2011 Trifork
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
28-module(rebar_shell).
29-author("Kresten Krab Thorup <krab@trifork.com>").
30
31-include("rebar.hrl").
32
33-export([shell/2, info/2]).
34
35%% NOTE:
36%% this is an attempt to replicate `erl -pa ./ebin -pa deps/*/ebin`. it is
37%% mostly successful but does stop and then restart the user io system to get
38%% around issues with rebar being an escript and starting in `noshell` mode.
39%% it also lacks the ctrl-c interrupt handler that `erl` features. ctrl-c will
40%% immediately kill the script. ctrl-g, however, works fine
41
42shell(_Config, _AppFile) ->
43    true = code:add_pathz(rebar_utils:ebin_dir()),
44    %% scan all processes for any with references to the old user and save them to
45    %% update later
46    NeedsUpdate = [Pid || Pid <- erlang:processes(),
47        proplists:get_value(group_leader, erlang:process_info(Pid)) == whereis(user)
48    ],
49    %% terminate the current user
50    ok = supervisor:terminate_child(kernel_sup, user),
51    %% start a new shell (this also starts a new user under the correct group)
52    _ = user_drv:start(),
53    %% wait until user_drv and user have been registered (max 3 seconds)
54    ok = wait_until_user_started(3000),
55    %% set any process that had a reference to the old user's group leader to the
56    %% new user process
57    _ = [erlang:group_leader(whereis(user), Pid) || Pid <- NeedsUpdate],
58    %% enable error_logger's tty output
59    ok = error_logger:swap_handler(tty),
60    %% disable the simple error_logger (which may have been added multiple
61    %% times). removes at most the error_logger added by init and the
62    %% error_logger added by the tty handler
63    ok = remove_error_handler(3),
64    %% this call never returns (until user quits shell)
65    timer:sleep(infinity).
66
67info(help, shell) ->
68    ?CONSOLE(
69        "Start a shell with project and deps preloaded similar to~n"
70        "'erl -pa ebin -pa deps/*/ebin'.~n",
71        []
72    ).
73
74remove_error_handler(0) ->
75    ?WARN("Unable to remove simple error_logger handler~n", []);
76remove_error_handler(N) ->
77    case gen_event:delete_handler(error_logger, error_logger, []) of
78        {error, module_not_found} -> ok;
79        {error_logger, _} -> remove_error_handler(N-1)
80    end.
81
82%% Timeout is a period to wait before giving up
83wait_until_user_started(0) ->
84    ?ABORT("Timeout exceeded waiting for `user` to register itself~n", []),
85    erlang:error(timeout);
86wait_until_user_started(Timeout) ->
87    case whereis(user) of
88        %% if user is not yet registered wait a tenth of a second and try again
89        undefined -> timer:sleep(100), wait_until_user_started(Timeout - 100);
90        _ -> ok
91    end.