summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/rebar3.erl98
-rw-r--r--src/rebar_config.erl1
-rw-r--r--src/rebar_core.erl13
-rw-r--r--src/rebar_prv_compile.erl10
-rw-r--r--src/rebar_prv_upgrade.erl47
-rw-r--r--src/rebar_prv_version.erl1
-rw-r--r--src/rebar_state.erl12
7 files changed, 97 insertions, 85 deletions
diff --git a/src/rebar3.erl b/src/rebar3.erl
index 20edfa6..0a07c90 100644
--- a/src/rebar3.erl
+++ b/src/rebar3.erl
@@ -28,15 +28,16 @@
-export([main/1,
run/2,
- option_spec_list/0,
+ global_option_spec_list/0,
+ init_config/0,
+ init_config1/1,
+ set_options/2,
parse_args/1,
version/0,
- log_level/1]).
+ log_level/0]).
-include("rebar.hrl").
--define(DEFAULT_JOBS, 3).
-
%% ====================================================================
%% Public API
%% ====================================================================
@@ -49,7 +50,7 @@ main(Args) ->
rebar_abort ->
rebar_utils:delayed_halt(1);
{error, Error} ->
- ?ERROR(Error, []),
+ ?ERROR(Error++"~n", []),
rebar_utils:delayed_halt(1);
Error ->
%% Nothing should percolate up from rebar_core;
@@ -71,24 +72,21 @@ run(BaseState, Command) ->
run(RawArgs) ->
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, Args1} = set_options(BaseConfig, Args),
- run_aux(BaseConfig1, Args1).
+ BaseConfig = init_config(),
+ {BaseConfig1, _Args1} = set_options(BaseConfig, {[], []}),
+ run_aux(BaseConfig1, RawArgs).
load_rebar_app() ->
%% Pre-load the rebar app so that we get default configuration
ok = application:load(rebar).
-init_config({Options, _NonOptArgs}) ->
+init_config() ->
%% Initialize logging system
- Verbosity = log_level(Options),
+ Verbosity = log_level(),
ok = rebar_log:init(command_line, Verbosity),
- Config = case proplists:get_value(config, Options) of
- undefined ->
+ Config = case os:getenv("REBAR_CONFIG") of
+ false ->
rebar_config:consult_file(?DEFAULT_CONFIG_FILE);
ConfigFile ->
rebar_config:consult_file(ConfigFile)
@@ -127,7 +125,7 @@ init_config1(BaseConfig) ->
BaseConfig
end.
-run_aux(State, Args) ->
+run_aux(State, RawArgs) ->
%% Make sure crypto is running
case crypto:start() of
ok -> ok;
@@ -149,23 +147,24 @@ run_aux(State, Args) ->
rebar_core:update_code_path(State),
State4 = rebar_state:create_logic_providers(Providers++PluginProviders, State3),
- Task = rebar_state:get(State4, task, "help"),
+ {Task, Args} = parse_args(RawArgs),
+
rebar_core:process_command(rebar_state:command_args(State4, Args), list_to_atom(Task)).
%%
%% 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(RawArgs) ->
- %% Parse getopt options
- OptSpecList = option_spec_list(),
- case getopt:parse(OptSpecList, RawArgs) of
- {ok, Args} ->
- Args;
- {error, {Reason, Data}} ->
- ?ERROR("~s ~p~n~n", [Reason, Data]),
- rebar_utils:delayed_halt(1)
- end.
+parse_args([]) ->
+ parse_args(["help"]);
+parse_args([H | Rest]) when H =:= "-h"
+ ; H =:= "--help" ->
+ parse_args(["help" | Rest]);
+parse_args([H | Rest]) when H =:= "-v"
+ ; H =:= "--version" ->
+ parse_args(["version" | Rest]);
+parse_args([RawTask | RawRest]) ->
+ {RawTask, RawRest}.
set_options(State, {Options, NonOptArgs}) ->
GlobalDefines = proplists:get_all_values(defines, Options),
@@ -174,32 +173,26 @@ set_options(State, {Options, NonOptArgs}) ->
%% Set global variables based on getopt options
State2 = set_global_flag(State1, Options, force),
- State3 = case proplists:get_value(jobs, Options, ?DEFAULT_JOBS) of
- ?DEFAULT_JOBS ->
- State2;
- Jobs ->
- rebar_state:set(State2, jobs, Jobs)
- end,
Task = proplists:get_value(task, Options, "help"),
- {rebar_state:set(State3, task, Task), NonOptArgs}.
+ {rebar_state:set(State2, task, Task), NonOptArgs}.
%%
%% get log level based on getopt option
%%
-log_level(Options) ->
- case proplists:get_bool(quiet, Options) of
- true ->
- rebar_log:error_level();
+log_level() ->
+ case os:getenv("QUIET") of
false ->
DefaultLevel = rebar_log:default_level(),
- case proplists:get_all_values(verbose, Options) of
- [] ->
+ case os:getenv("DEBUG") of
+ false ->
DefaultLevel;
- Verbosities ->
- DefaultLevel + lists:last(Verbosities)
- end
+ _ ->
+ DefaultLevel + 3
+ end;
+ _ ->
+ rebar_log:error_level()
end.
%%
@@ -226,17 +219,12 @@ set_global_flag(State, Options, Flag) ->
%%
%% options accepted via getopt
%%
-option_spec_list() ->
- Jobs = ?DEFAULT_JOBS,
- JobsHelp = io_lib:format(
- "Number of concurrent workers a command may use. Default: ~B",
- [Jobs]),
+global_option_spec_list() ->
[
- %% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
- {help, $h, "help", undefined, "Print this help."},
- {verbose, $v, "verbose", integer, "Verbosity level (-v, -vv)."},
- {version, $V, "version", undefined, "Show version information."},
- {jobs, $j, "jobs", integer, JobsHelp},
- {config, $C, "config", string, "Rebar config file to use."},
- {task, undefined, undefined, string, "Task to run."}
+ %% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
+ {help, $h, "help", undefined, "Print this help."},
+ %{verbose, $v, "verbose", integer, "Verbosity level (-v, -vv)."},
+ {version, $V, "version", undefined, "Show version information."},
+ %{config, $C, "config", string, "Rebar config file to use."},
+ {task, undefined, undefined, string, "Task to run."}
].
diff --git a/src/rebar_config.erl b/src/rebar_config.erl
index b3003d6..7ef2488 100644
--- a/src/rebar_config.erl
+++ b/src/rebar_config.erl
@@ -73,7 +73,6 @@ remove_script_ext(F) ->
try_consult(File) ->
case file:consult(File) of
{ok, Terms} ->
- ?DEBUG("Consult config file ~p~n", [File]),
Terms;
{error, enoent} ->
[];
diff --git a/src/rebar_core.erl b/src/rebar_core.erl
index 17fc0e2..340f2ae 100644
--- a/src/rebar_core.erl
+++ b/src/rebar_core.erl
@@ -36,14 +36,23 @@ process_command(State, Command) ->
%% ? rebar_prv_install_deps:setup_env(State),
Providers = rebar_state:providers(State),
TargetProviders = providers:get_target_providers(Command, Providers),
- do(TargetProviders, State).
+ CommandProvider = providers:get_provider(Command
+ ,Providers),
+ Opts = providers:opts(CommandProvider)++rebar3:global_option_spec_list(),
+ case getopt:parse(Opts, rebar_state:command_args(State)) of
+ {ok, Args} ->
+ State2 = rebar_state:command_parsed_args(State, Args),
+ do(TargetProviders, State2);
+ {error, {invalid_option, Option}} ->
+ {error, io_lib:format("Invalid option ~s on task ~p", [Option, Command])}
+ end.
-spec do([atom()], rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do([], State) ->
{ok, State};
do([ProviderName | Rest], State) ->
Provider = providers:get_provider(ProviderName
- ,rebar_state:providers(State)),
+ ,rebar_state:providers(State)),
case providers:do(Provider, State) of
{ok, State1} ->
do(Rest, State1);
diff --git a/src/rebar_prv_compile.erl b/src/rebar_prv_compile.erl
index fdeafdd..773e8a9 100644
--- a/src/rebar_prv_compile.erl
+++ b/src/rebar_prv_compile.erl
@@ -11,12 +11,18 @@
-define(PROVIDER, compile).
-define(DEPS, [lock]).
+-define(DEFAULT_JOBS, 3).
+
%% ===================================================================
%% Public API
%% ===================================================================
-spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
init(State) ->
+ Jobs = ?DEFAULT_JOBS,
+ JobsHelp = io_lib:format(
+ "Number of concurrent workers a command may use. Default: ~B",
+ [Jobs]),
State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER},
{module, ?MODULE},
{bare, false},
@@ -24,7 +30,9 @@ init(State) ->
{example, "rebar compile"},
{short_desc, "Compile apps .app.src and .erl files."},
{desc, ""},
- {opts, []}])),
+ {opts, [
+ {jobs, $j, "jobs", integer, JobsHelp}
+ ]}])),
{ok, State1}.
-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
diff --git a/src/rebar_prv_upgrade.erl b/src/rebar_prv_upgrade.erl
index 1c6abf7..cbeec98 100644
--- a/src/rebar_prv_upgrade.erl
+++ b/src/rebar_prv_upgrade.erl
@@ -19,31 +19,32 @@
-spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
init(State) ->
- State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER},
- {module, ?MODULE},
- {bare, false},
- {deps, ?DEPS},
- {example, "rebar upgrade cowboy"},
- {short_desc, "Upgrade dependency."},
- {desc, ""},
- {opts, []}])),
+ State1 =
+ rebar_state:add_provider(State,
+ providers:create([{name, ?PROVIDER},
+ {module, ?MODULE},
+ {bare, false},
+ {deps, ?DEPS},
+ {example, "rebar upgrade cowboy"},
+ {short_desc, "Upgrade dependency."},
+ {desc, ""},
+ {opts, [
+ {package, undefined, undefined, string, "Package to upgrade."}
+ ]}])),
{ok, State1}.
-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do(State) ->
- case rebar_state:command_args(State) of
- [Name] ->
- ?INFO("Updating ~s~n", [Name]),
- Locks = rebar_state:get(State, locks, []),
- case lists:keyfind(ec_cnv:to_binary(Name), 1, Locks) of
- {_, _, _, Level} ->
- Deps = rebar_state:get(State, deps),
- Dep = lists:keyfind(list_to_atom(Name), 1, Deps),
- rebar_prv_install_deps:handle_deps(State, [Dep], {true, ec_cnv:to_binary(Name), Level}),
- {ok, State};
- false ->
- {error, io_lib:format("No such dependency ~s~n", [Name])}
- end;
- [] ->
- {error, "No package given to upgrade."}
+ {Args, _} = rebar_state:command_parsed_args(State),
+ Name = proplists:get_value(package, Args),
+ ?INFO("Updating ~s~n", [Name]),
+ Locks = rebar_state:get(State, locks, []),
+ case lists:keyfind(ec_cnv:to_binary(Name), 1, Locks) of
+ {_, _, _, Level} ->
+ Deps = rebar_state:get(State, deps),
+ Dep = lists:keyfind(list_to_atom(Name), 1, Deps),
+ rebar_prv_install_deps:handle_deps(State, [Dep], {true, ec_cnv:to_binary(Name), Level}),
+ {ok, State};
+ _ ->
+ {error, io_lib:format("No such dependency ~s~n", [Name])}
end.
diff --git a/src/rebar_prv_version.erl b/src/rebar_prv_version.erl
index f158b6d..c02f851 100644
--- a/src/rebar_prv_version.erl
+++ b/src/rebar_prv_version.erl
@@ -32,5 +32,6 @@ init(State) ->
-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
do(State) ->
+ io:format("REST ~p~n", [rebar_state:command_args(State)]),
rebar3:version(),
{ok, State}.
diff --git a/src/rebar_state.erl b/src/rebar_state.erl
index 14b55d2..20ec4a0 100644
--- a/src/rebar_state.erl
+++ b/src/rebar_state.erl
@@ -3,6 +3,7 @@
-export([new/0, new/1, new/2, new/3,
get/2, get/3, set/3,
command_args/1, command_args/2,
+ command_parsed_args/1, command_parsed_args/2,
dir/1, dir/2,
create_logic_providers/2,
@@ -23,15 +24,14 @@
opts = [],
command_args = [],
+ command_parsed_args = [],
src_deps = [],
src_apps = [],
pkg_deps = [],
project_apps = [],
- providers = [],
- hooks = []}).
-
+ providers = []}).
-export_type([t/0]).
@@ -83,6 +83,12 @@ command_args(#state_t{command_args=CmdArgs}) ->
command_args(State, CmdArgs) ->
State#state_t{command_args=CmdArgs}.
+command_parsed_args(#state_t{command_parsed_args=CmdArgs}) ->
+ CmdArgs.
+
+command_parsed_args(State, CmdArgs) ->
+ State#state_t{command_parsed_args=CmdArgs}.
+
dir(#state_t{dir=Dir}) ->
Dir.