From 252757c753e9ec5c5247a86eb635aee385d05ee1 Mon Sep 17 00:00:00 2001 From: Tuncer Ayaz Date: Sat, 14 Jul 2012 23:44:47 +0200 Subject: Do not use application:set_env --- src/rebar.erl | 193 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 106 insertions(+), 87 deletions(-) (limited to 'src/rebar.erl') diff --git a/src/rebar.erl b/src/rebar.erl index 5812170..21e662b 100644 --- a/src/rebar.erl +++ b/src/rebar.erl @@ -29,7 +29,8 @@ -export([main/1, help/0, parse_args/1, - version/0]). + version/0, + get_jobs/1]). -include("rebar.hrl"). @@ -45,6 +46,8 @@ -define(OTP_INFO, "undefined"). -endif. +-define(DEFAULT_JOBS, 3). + %% ==================================================================== %% Public API %% ==================================================================== @@ -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 @@ -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). @@ -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 @@ -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 @@ -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]), @@ -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 @@ -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() -> -- cgit v1.1