Skip to content

Commit

Permalink
Do not use application:set_env
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuncer Ayaz committed Jul 23, 2012
1 parent 3c56fba commit 252757c
Show file tree
Hide file tree
Showing 18 changed files with 321 additions and 293 deletions.
3 changes: 0 additions & 3 deletions ebin/rebar.app
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@
%% Default log level
{log_level, error},

%% Default parallel jobs
{jobs, 3},

%% any_dir processing modules
{any_dir_modules, [
rebar_require_vsn,
Expand Down
193 changes: 106 additions & 87 deletions src/rebar.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
-export([main/1,
help/0,
parse_args/1,
version/0]).
version/0,
get_jobs/1]).

-include("rebar.hrl").

Expand All @@ -45,6 +46,8 @@
-define(OTP_INFO, "undefined").
-endif.

-define(DEFAULT_JOBS, 3).

%% ====================================================================
%% Public API
%% ====================================================================
Expand All @@ -66,55 +69,39 @@ main(Args) ->
%% Internal functions
%% ====================================================================

run(["help"]) ->
help();
run(["version"]) ->
ok = load_rebar_app(),
%% Display vsn and build time info
version();
run(RawArgs) ->
%% Pre-load the rebar app so that we get default configuration
ok = application:load(rebar),
ok = load_rebar_app(),
%% Parse out command line arguments -- what's left is a list of commands to
%% run -- and start running commands
Args = parse_args(RawArgs),
BaseConfig = init_config(Args),
{BaseConfig1, Cmds} = save_options(BaseConfig, Args),

case rebar_config:get_global(enable_profiling, false) of
case rebar_config:get_xconf(BaseConfig1, enable_profiling, false) of
true ->
io:format("Profiling!\n"),
try
fprof:apply(fun(A) -> run_aux(A) end, [Args])
fprof:apply(fun([C, A]) -> run_aux(C, A) end,
[BaseConfig1, Cmds])
after
fprof:profile(),
fprof:analyse([{dest, "fprof.analysis"}])
end;
_ ->
run_aux(Args)
false ->
run_aux(BaseConfig1, Cmds)
end.

run_aux(["help"]) ->
help(),
ok;
run_aux(["version"]) ->
%% Display vsn and build time info
version(),
ok;
run_aux(Commands) ->
%% Make sure crypto is running
case crypto:start() of
ok -> ok;
{error,{already_started,crypto}} -> ok
end,

%% Initialize logging system
rebar_log:init(),

%% Convert command strings to atoms
CommandAtoms = [list_to_atom(C) || C <- Commands],

%% Determine the location of the rebar executable; important for pulling
%% resources out of the escript
rebar_config:set_global(escript, filename:absname(escript:script_name())),
?DEBUG("Rebar location: ~p\n",
[rebar_config:get_global(escript, undefined)]),

%% Note the top-level directory for reference
rebar_config:set_global(base_dir, filename:absname(rebar_utils:get_cwd())),
load_rebar_app() ->
%% Pre-load the rebar app so that we get default configuration
ok = application:load(rebar).

init_config({Options, _NonOptArgs}) ->
%% If $HOME/.rebar/config exists load and use as global config
GlobalConfigFile = filename:join([os:getenv("HOME"), ".rebar", "config"]),
GlobalConfig = case filelib:is_regular(GlobalConfigFile) of
Expand All @@ -125,12 +112,45 @@ run_aux(Commands) ->
false ->
rebar_config:new()
end,
BaseConfig = rebar_config:base_config(GlobalConfig),

%% Set the rebar config to use
GlobalConfig1 = case proplists:get_value(config, Options) of
undefined ->
GlobalConfig;
Conf ->
rebar_config:set_global(GlobalConfig, config, Conf)
end,

GlobalConfig2 = set_log_level(GlobalConfig1, Options),
%% Initialize logging system
ok = rebar_log:init(GlobalConfig2),

BaseConfig = rebar_config:base_config(GlobalConfig2),

%% Keep track of how many operations we do, so we can detect bad commands
BaseConfig1 = rebar_config:set_xconf(BaseConfig, operations, 0),
%% Initialize vsn cache
BaseConfig2 = rebar_config:set_xconf(BaseConfig1, vsn_cache, dict:new()),
rebar_config:set_xconf(BaseConfig1, vsn_cache, dict:new()).

run_aux(BaseConfig, Commands) ->
%% Make sure crypto is running
case crypto:start() of
ok -> ok;
{error,{already_started,crypto}} -> ok
end,

%% Convert command strings to atoms
CommandAtoms = [list_to_atom(C) || C <- Commands],

%% Determine the location of the rebar executable; important for pulling
%% resources out of the escript
ScriptName = filename:absname(escript:script_name()),
BaseConfig1 = rebar_config:set_xconf(BaseConfig, escript, ScriptName),
?DEBUG("Rebar location: ~p\n", [ScriptName]),

%% Note the top-level directory for reference
AbsCwd = filename:absname(rebar_utils:get_cwd()),
BaseConfig2 = rebar_config:set_xconf(BaseConfig1, base_dir, AbsCwd),

%% Process each command, resetting any state between each one
rebar_core:process_commands(CommandAtoms, BaseConfig2).
Expand All @@ -149,63 +169,59 @@ help() ->
%% Parse command line arguments using getopt and also filtering out any
%% key=value pairs. What's left is the list of commands to run
%%
parse_args(Args) ->
parse_args(RawArgs) ->
%% Parse getopt options
OptSpecList = option_spec_list(),
case getopt:parse(OptSpecList, Args) of
{ok, {Options, NonOptArgs}} ->
%% Check options and maybe halt execution
ok = show_info_maybe_halt(Options, NonOptArgs),

GlobalDefines = proplists:get_all_values(defines, Options),
rebar_config:set_global(defines, GlobalDefines),

%% Setup profiling flag
rebar_config:set_global(enable_profiling,
proplists:get_bool(profile, Options)),

%% Setup flag to keep running after a single command fails
rebar_config:set_global(keep_going,
proplists:get_bool(keep_going, Options)),

%% Set global variables based on getopt options
set_log_level(Options),
set_global_flag(Options, force),
DefJobs = rebar_config:get_jobs(),
case proplists:get_value(jobs, Options, DefJobs) of
DefJobs ->
ok;
Jobs ->
rebar_config:set_global(jobs, Jobs)
end,

%% Set the rebar config to use
case proplists:get_value(config, Options) of
undefined -> ok;
Conf -> rebar_config:set_global(config, Conf)
end,

%% Filter all the flags (i.e. strings of form key=value) from the
%% command line arguments. What's left will be the commands to run.
unabbreviate_command_names(filter_flags(NonOptArgs, []));

case getopt:parse(OptSpecList, RawArgs) of
{ok, Args} ->
Args;
{error, {Reason, Data}} ->
?ERROR("~s ~p~n~n", [Reason, Data]),
help(),
rebar_utils:delayed_halt(1)
end.

save_options(Config, {Options, NonOptArgs}) ->
%% Check options and maybe halt execution
ok = show_info_maybe_halt(Options, NonOptArgs),

GlobalDefines = proplists:get_all_values(defines, Options),

Config1 = rebar_config:set_xconf(Config, defines, GlobalDefines),

%% Setup profiling flag
Config2 = rebar_config:set_xconf(Config1, enable_profiling,
proplists:get_bool(profile, Options)),

%% Setup flag to keep running after a single command fails
Config3 = rebar_config:set_xconf(Config2, keep_going,
proplists:get_bool(keep_going, Options)),

%% Set global variables based on getopt options
Config4 = set_global_flag(Config3, Options, force),
Config5 = case proplists:get_value(jobs, Options, ?DEFAULT_JOBS) of
?DEFAULT_JOBS ->
Config4;
Jobs ->
rebar_config:set_global(Config4, jobs, Jobs)
end,

%% Filter all the flags (i.e. strings of form key=value) from the
%% command line arguments. What's left will be the commands to run.
{Config6, RawCmds} = filter_flags(Config5, NonOptArgs, []),
{Config6, unabbreviate_command_names(RawCmds)}.

%%
%% set log level based on getopt option
%%
set_log_level(Options) ->
set_log_level(Config, Options) ->
LogLevel = case proplists:get_all_values(verbose, Options) of
[] ->
rebar_log:default_level();
Verbosities ->
lists:last(Verbosities)
end,
rebar_config:set_global(verbose, LogLevel).
rebar_config:set_global(Config, verbose, LogLevel).

%%
%% show version information and halt
Expand All @@ -219,14 +235,14 @@ version() ->
%%
%% set global flag based on getopt option boolean value
%%
set_global_flag(Options, Flag) ->
set_global_flag(Config, Options, Flag) ->
Value = case proplists:get_bool(Flag, Options) of
true ->
"1";
false ->
"0"
end,
rebar_config:set_global(Flag, Value).
rebar_config:set_global(Config, Flag, Value).

%%
%% show info and maybe halt execution
Expand Down Expand Up @@ -291,11 +307,14 @@ version Show version information
">>,
io:put_chars(S).

get_jobs(Config) ->
rebar_config:get_global(Config, jobs, ?DEFAULT_JOBS).

%%
%% options accepted via getopt
%%
option_spec_list() ->
Jobs = rebar_config:get_jobs(),
Jobs = ?DEFAULT_JOBS,
JobsHelp = io_lib:format(
"Number of concurrent workers a command may use. Default: ~B",
[Jobs]),
Expand All @@ -319,12 +338,12 @@ option_spec_list() ->
%% Seperate all commands (single-words) from flags (key=value) and store
%% values into the rebar_config global storage.
%%
filter_flags([], Commands) ->
lists:reverse(Commands);
filter_flags([Item | Rest], Commands) ->
filter_flags(Config, [], Commands) ->
{Config, lists:reverse(Commands)};
filter_flags(Config, [Item | Rest], Commands) ->
case string:tokens(Item, "=") of
[Command] ->
filter_flags(Rest, [Command | Commands]);
filter_flags(Config, Rest, [Command | Commands]);
[KeyStr, RawValue] ->
Key = list_to_atom(KeyStr),
Value = case Key of
Expand All @@ -333,11 +352,11 @@ filter_flags([Item | Rest], Commands) ->
_ ->
RawValue
end,
rebar_config:set_global(Key, Value),
filter_flags(Rest, Commands);
Config1 = rebar_config:set_global(Config, Key, Value),
filter_flags(Config1, Rest, Commands);
Other ->
?CONSOLE("Ignoring command line argument: ~p\n", [Other]),
filter_flags(Rest, Commands)
filter_flags(Config, Rest, Commands)
end.

command_names() ->
Expand Down
18 changes: 9 additions & 9 deletions src/rebar_app_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ is_skipped_app(Config, AppFile) ->
%% Check for apps global parameter; this is a comma-delimited list
%% of apps on which we want to run commands
Skipped =
case get_apps() of
case get_apps(Config) of
undefined ->
%% No apps parameter specified, check the skip_apps list..
case get_skip_apps() of
case get_skip_apps(Config) of
undefined ->
%% No skip_apps list, run everything..
false;
Expand All @@ -136,8 +136,8 @@ is_skipped_app(Config, AppFile) ->

load_app_file(Config, Filename) ->
AppFile = {app_file, Filename},
case rebar_config:get_xconf(Config, {appfile, AppFile}) of
error ->
case rebar_config:get_xconf(Config, {appfile, AppFile}, undefined) of
undefined ->
case file:consult(Filename) of
{ok, [{application, AppName, AppData}]} ->
Config1 = rebar_config:set_xconf(Config,
Expand All @@ -149,7 +149,7 @@ load_app_file(Config, Filename) ->
Other ->
{error, {unexpected_terms, Other}}
end;
{ok, {AppName, AppData}} ->
{AppName, AppData} ->
{ok, Config, AppName, AppData}
end.

Expand Down Expand Up @@ -179,8 +179,8 @@ is_skipped(ThisApp, TargetApps) ->
{true, ThisApp}
end.

get_apps() ->
rebar_utils:get_deprecated_global(app, apps, "soon").
get_apps(Config) ->
rebar_utils:get_deprecated_global(Config, app, apps, "soon").

get_skip_apps() ->
rebar_utils:get_deprecated_global(skip_app, skip_apps, "soon").
get_skip_apps(Config) ->
rebar_utils:get_deprecated_global(Config, skip_app, skip_apps, "soon").
7 changes: 4 additions & 3 deletions src/rebar_appups.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@
'generate-appups'(Config, ReltoolFile) ->
%% Get the old release path
{Config1, ReltoolConfig} = rebar_rel_utils:load_config(Config, ReltoolFile),
TargetParentDir = rebar_rel_utils:get_target_parent_dir(ReltoolConfig),
TargetParentDir = rebar_rel_utils:get_target_parent_dir(Config,
ReltoolConfig),

OldVerPath = filename:join([TargetParentDir,
rebar_rel_utils:get_previous_release_path()]),
PrevRelPath = rebar_rel_utils:get_previous_release_path(Config),
OldVerPath = filename:join([TargetParentDir, PrevRelPath]),

%% Get the new and old release name and versions
{Name, _Ver} = rebar_rel_utils:get_reltool_release_info(ReltoolConfig),
Expand Down
2 changes: 1 addition & 1 deletion src/rebar_base_compiler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ run(Config, FirstFiles, RestFiles, CompileFn) ->
_ ->
Self = self(),
F = fun() -> compile_worker(Self, Config, CompileFn) end,
Jobs = rebar_config:get_jobs(),
Jobs = rebar:get_jobs(Config),
?DEBUG("Starting ~B compile worker(s)~n", [Jobs]),
Pids = [spawn_monitor(F) || _I <- lists:seq(1,Jobs)],
compile_queue(Pids, RestFiles)
Expand Down
Loading

0 comments on commit 252757c

Please sign in to comment.