From 2357bdf67f2dd95468f07dea7acfa218e7d0d039 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Wed, 17 Sep 2014 21:41:49 -0500 Subject: reworking of help display and parse of commands --- src/rebar.app.src | 4 +- src/rebar3.erl | 223 ++++++---------------------------------- src/rebar_erlydtl_compiler.erl | 16 +-- src/rebar_provider.erl | 18 +++- src/rebar_prv_app_discovery.erl | 2 +- src/rebar_prv_compile.erl | 2 +- src/rebar_prv_deps.erl | 2 +- src/rebar_prv_escripter.erl | 4 +- src/rebar_prv_help.erl | 48 +++++++++ src/rebar_prv_install_deps.erl | 2 +- src/rebar_prv_lock.erl | 4 +- src/rebar_prv_new.erl | 2 +- src/rebar_prv_packages.erl | 2 +- src/rebar_prv_release.erl | 14 +-- src/rebar_prv_shell.erl | 2 +- src/rebar_prv_tar.erl | 2 +- src/rebar_prv_update.erl | 2 +- src/rebar_prv_version.erl | 35 +++++++ 18 files changed, 160 insertions(+), 224 deletions(-) create mode 100644 src/rebar_prv_help.erl create mode 100644 src/rebar_prv_version.erl (limited to 'src') diff --git a/src/rebar.app.src b/src/rebar.app.src index 818a0c7..59f35ca 100644 --- a/src/rebar.app.src +++ b/src/rebar.app.src @@ -33,6 +33,8 @@ rebar_prv_tar, rebar_prv_new, rebar_prv_update, - rebar_prv_release]} + rebar_prv_release, + rebar_prv_version, + rebar_prv_help]} ]} ]}. diff --git a/src/rebar3.erl b/src/rebar3.erl index d0a6baa..f29eaec 100644 --- a/src/rebar3.erl +++ b/src/rebar3.erl @@ -28,25 +28,13 @@ -export([main/1, run/2, - help/0, + option_spec_list/0, parse_args/1, version/0, log_level/1]). -include("rebar.hrl"). --ifndef(BUILD_TIME). --define(BUILD_TIME, "undefined"). --endif. - --ifndef(VCS_INFO). --define(VCS_INFO, "undefined"). --endif. - --ifndef(OTP_INFO). --define(OTP_INFO, "undefined"). --endif. - -define(DEFAULT_JOBS, 3). %% ==================================================================== @@ -68,52 +56,23 @@ main(Args) -> end. %% Erlang-API entry point -run(BaseConfig, Commands) -> +run(BaseState, Command) -> _ = application:load(rebar), - run_aux(BaseConfig, Commands). + BaseState1 = rebar_state:set(BaseState, task, Command), + run_aux(BaseState1, []). %% ==================================================================== %% Internal functions %% ==================================================================== -run(["help"|RawCmds]) when RawCmds =/= [] -> - ok = load_rebar_app(), - Cmds = RawCmds, - Args = parse_args(Cmds), - BaseConfig = init_config(Args), - {BaseConfig1, _} = save_options(BaseConfig, Args), - BaseConfig2 = init_config1(BaseConfig1), - rebar_core:help(BaseConfig2, [list_to_atom(C) || C <- Cmds]); -run(["help"]) -> - help(); -run(["info"|_]) -> - %% Catch calls to 'rebar info' to avoid treating plugins' info/2 functions - %% as commands. - ?CONSOLE("Command 'info' not understood or not applicable~n", []); -run(["version"]) -> - ok = load_rebar_app(), - %% Display vsn and build time info - version(); 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, Cmds} = save_options(BaseConfig, Args), - - case rebar_state:get(BaseConfig1, enable_profiling, false) of - true -> - io:format("Profiling!\n"), - try - fprof:apply(fun run_aux/2, [BaseConfig1, Cmds]) - after - ok = fprof:profile(), - ok = fprof:analyse([{dest, "fprof.analysis"}]) - end; - false -> - run_aux(BaseConfig1, Cmds) - end. + {BaseConfig1, Args1} = set_options(BaseConfig, Args), + run_aux(BaseConfig1, Args1). load_rebar_app() -> %% Pre-load the rebar app so that we get default configuration @@ -164,7 +123,7 @@ init_config1(BaseConfig) -> BaseConfig end. -run_aux(BaseConfig, Commands) -> +run_aux(State, Args) -> %% Make sure crypto is running case crypto:start() of ok -> ok; @@ -174,34 +133,18 @@ run_aux(BaseConfig, Commands) -> application:start(public_key), application:start(ssl), inets:start(), - [Command | Args] = Commands, - CommandAtom = list_to_atom(Command), - BaseConfig1 = init_config1(BaseConfig), + State1 = init_config1(State), %% Process each command, resetting any state between each one - BaseConfig2 = rebar_state:set(BaseConfig1, base_dir, filename:absname(rebar_state:dir(BaseConfig1))), + State2 = rebar_state:set(State1, base_dir, filename:absname(rebar_state:dir(State1))), {ok, Providers} = application:get_env(rebar, providers), - BaseConfig3 = rebar_state:create_logic_providers(Providers, BaseConfig2), - rebar_core:process_command(rebar_state:command_args(BaseConfig3, Args), CommandAtom), + State3 = rebar_state:create_logic_providers(Providers, State2), + Task = rebar_state:get(State3, task, "help"), + rebar_core:process_command(rebar_state:command_args(State3, Args), ec_cnv:to_atom(Task)), ok. %% -%% print help/usage string -%% -help() -> - OptSpecList = option_spec_list(), - getopt:usage(OptSpecList, "rebar", - "[var=value,...] ", - [{"var=value", "rebar global variables (e.g. force=1)"}, - {"command", "Command to run (e.g. compile)"}]), - - ?CONSOLE("To see a list of built-in commands, execute rebar -c.~n~n", []), - ?CONSOLE( - "Type 'rebar help ' for help on specific commands." - "~n~n", []). - -%% %% Parse command line arguments using getopt and also filtering out any %% key=value pairs. What's left is the list of commands to run %% @@ -213,35 +156,26 @@ parse_args(RawArgs) -> Args; {error, {Reason, Data}} -> ?ERROR("~s ~p~n~n", [Reason, Data]), - help(), rebar_utils:delayed_halt(1) end. -save_options(State, {Options, NonOptArgs}) -> - %% Check options and maybe halt execution - ok = show_info_maybe_halt(Options, NonOptArgs), - +set_options(State, {Options, NonOptArgs}) -> GlobalDefines = proplists:get_all_values(defines, Options), State1 = rebar_state:set(State, defines, GlobalDefines), - %% Setup profiling flag - State2 = rebar_state:set(State1, enable_profiling, - proplists:get_bool(profile, Options)), - %% Set global variables based on getopt options - State3 = set_global_flag(State2, Options, force), - State4 = case proplists:get_value(jobs, Options, ?DEFAULT_JOBS) of - ?DEFAULT_JOBS -> - State3; - Jobs -> - rebar_state:set(State3, jobs, Jobs) - end, + 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"), - %% 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. - {State5, RawCmds} = filter_flags(State4, NonOptArgs, []), - {State5, RawCmds}. + {rebar_state:set(State3, task, Task), NonOptArgs}. %% %% get log level based on getopt option @@ -265,8 +199,8 @@ log_level(Options) -> %% version() -> {ok, Vsn} = application:get_key(rebar, vsn), - ?CONSOLE("rebar ~s ~s ~s ~s\n", - [Vsn, ?OTP_INFO, ?BUILD_TIME, ?VCS_INFO]). + ?CONSOLE("rebar ~s on Erlang/OTP ~s Erts ~s~n", + [Vsn, erlang:system_info(otp_release), erlang:system_info(version)]). %% @@ -282,58 +216,6 @@ set_global_flag(State, Options, Flag) -> rebar_state:set(State, Flag, Value). %% -%% show info and maybe halt execution -%% -show_info_maybe_halt(Opts, NonOptArgs) -> - false = show_info_maybe_halt(help, Opts, fun help/0), - false = show_info_maybe_halt(commands, Opts, fun commands/0), - false = show_info_maybe_halt(version, Opts, fun version/0), - case NonOptArgs of - [] -> - ?CONSOLE("No command to run specified!~n",[]), - help(), - rebar_utils:delayed_halt(1); - _ -> - ok - end. - -show_info_maybe_halt(O, Opts, F) -> - case proplists:get_bool(O, Opts) of - true -> - F(), - rebar_utils:delayed_halt(0); - false -> - false - end. - -%% -%% print known commands -%% -commands() -> - S = <<" -clean Clean -compile Compile sources - -do - -escriptize Generate escript archive - -shell Start a shell similar to - 'erl -pa ebin -pa deps/*/ebin' - -update [dep] Update source dep - -ct -eunit - -new - -help Show the program options -version Show version information -">>, - io:put_chars(S). - -%% %% options accepted via getopt %% option_spec_list() -> @@ -343,55 +225,10 @@ option_spec_list() -> [Jobs]), [ %% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg} - {help, $h, "help", undefined, "Show the program options"}, - {commands, $c, "commands", undefined, "Show available commands"}, - {verbose, $v, "verbose", integer, "Verbosity level (-v, -vv)"}, - {quiet, $q, "quiet", boolean, "Quiet, only print error messages"}, - {version, $V, "version", undefined, "Show version information"}, - {force, $f, "force", undefined, "Force"}, - {defines, $D, undefined, string, "Define compiler macro"}, + {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"}, - {profile, $p, "profile", undefined, "Profile this run of rebar"}, - {keep_going, $k, "keep-going", undefined, - "Keep running after a command fails"}, - {recursive, $r, "recursive", boolean, - "Apply commands to subdirs and dependencies"} - ]. - -%% -%% Seperate all commands (single-words) from flags (key=value) and store -%% values into the rebar_state global storage. -%% -filter_flags(State, [], Commands) -> - {State, lists:reverse(Commands)}; -filter_flags(State, [Item | Rest], Commands) -> - case string:tokens(Item, "=") of - [Command] -> - filter_flags(State, Rest, [Command | Commands]); - [KeyStr, RawValue] -> - Key = list_to_atom(KeyStr), - Value = case Key of - verbose -> - list_to_integer(RawValue); - _ -> - RawValue - end, - State1 = rebar_state:set(State, Key, Value), - filter_flags(State1, Rest, Commands); - Other -> - ?CONSOLE("Ignoring command line argument: ~p\n", [Other]), - filter_flags(State, Rest, Commands) - end. - -command_names() -> - [ - "clean", - "compile", - "release", - "update", - "escriptize", - "help", - "shell", - "version" + {config, $C, "config", string, "Rebar config file to use."}, + {task, undefined, undefined, string, "Task to run."} ]. diff --git a/src/rebar_erlydtl_compiler.erl b/src/rebar_erlydtl_compiler.erl index b90ef37..88ee20f 100644 --- a/src/rebar_erlydtl_compiler.erl +++ b/src/rebar_erlydtl_compiler.erl @@ -104,7 +104,7 @@ -include("rebar.hrl"). --define(PROVIDER, compile). +-define(PROVIDER, erlydtl). -define(DEPS, []). %% =================================================================== @@ -114,13 +114,13 @@ -spec init(rebar_state:t()) -> {ok, rebar_state:t()}. init(State) -> State1 = rebar_state:add_provider(State, #provider{name = ?PROVIDER, - provider_impl = ?MODULE, - bare = false, - deps = ?DEPS, - example = "compile", - short_desc = "", - desc = "", - opts = []}), + provider_impl = ?MODULE, + bare = false, + deps = ?DEPS, + example = "rebar erlydtl compile", + short_desc = "Compile erlydtl templates.", + desc = "", + opts = []}), {ok, State1}. do(Config) -> diff --git a/src/rebar_provider.erl b/src/rebar_provider.erl index 30f7d80..3c80a80 100644 --- a/src/rebar_provider.erl +++ b/src/rebar_provider.erl @@ -6,6 +6,7 @@ impl/1, get_provider/2, get_target_providers/2, + help/1, format/1]). -export_type([t/0]). @@ -73,13 +74,26 @@ do(Provider, State) -> impl(Provider) -> Provider#provider.name. +help(State) -> + Providers = rebar_state:providers(State), + Help = lists:sort([{ec_cnv:to_list(P#provider.name), P#provider.short_desc} || P <- Providers, + P#provider.bare =/= true]), + Longest = lists:max([length(X) || {X, _} <- Help]), + + lists:foreach(fun({Name, ShortDesc}) -> + Length = length(Name), + Spacing = lists:duplicate(Longest - Length + 8, " "), + io:format("~s~s~s~n", [Name, Spacing, ShortDesc]) + end, Help). + + %% @doc print the provider module name %% %% @param T - The provider %% @return An iolist describing the provider -spec format(t()) -> iolist(). -format(#provider{provider_impl=Mod}) -> - erlang:atom_to_list(Mod). +format(#provider{name=Name}) -> + atom_to_list(Name). get_target_providers(Target, State) -> Providers = rebar_state:providers(State), diff --git a/src/rebar_prv_app_discovery.erl b/src/rebar_prv_app_discovery.erl index c1f5708..897a38d 100644 --- a/src/rebar_prv_app_discovery.erl +++ b/src/rebar_prv_app_discovery.erl @@ -21,7 +21,7 @@ init(State) -> State1 = rebar_state:add_provider(State, #provider{name = ?PROVIDER, provider_impl = ?MODULE, - bare = false, + bare = true, deps = ?DEPS, example = "", short_desc = "", diff --git a/src/rebar_prv_compile.erl b/src/rebar_prv_compile.erl index 5e7bd82..d89a4b0 100644 --- a/src/rebar_prv_compile.erl +++ b/src/rebar_prv_compile.erl @@ -22,7 +22,7 @@ init(State) -> bare = false, deps = ?DEPS, example = "rebar compile", - short_desc = "", + short_desc = "Compile apps .app.src and .erl files.", desc = "", opts = []}), {ok, State1}. diff --git a/src/rebar_prv_deps.erl b/src/rebar_prv_deps.erl index 834e7dc..111f32f 100644 --- a/src/rebar_prv_deps.erl +++ b/src/rebar_prv_deps.erl @@ -14,7 +14,7 @@ init(State) -> State1 = rebar_state:add_provider(State, #provider{name = ?PROVIDER, provider_impl = ?MODULE, - bare = false, + bare = true, deps = ?DEPS, example = "rebar deps", short_desc = "List dependencies", diff --git a/src/rebar_prv_escripter.erl b/src/rebar_prv_escripter.erl index f153564..418ac6c 100644 --- a/src/rebar_prv_escripter.erl +++ b/src/rebar_prv_escripter.erl @@ -53,8 +53,8 @@ init(State) -> provider_impl = ?MODULE, bare = false, deps = ?DEPS, - example = "escriptize", - short_desc = "", + example = "rebar escriptize", + short_desc = "Build escript from project.", desc = "", opts = []}), {ok, State1}. diff --git a/src/rebar_prv_help.erl b/src/rebar_prv_help.erl new file mode 100644 index 0000000..a9949e7 --- /dev/null +++ b/src/rebar_prv_help.erl @@ -0,0 +1,48 @@ +%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*- +%% ex: ts=4 sw=4 et + +-module(rebar_prv_help). + +-behaviour(rebar_provider). + +-export([init/1, + do/1]). + +-include("rebar.hrl"). + +-define(PROVIDER, help). +-define(DEPS, []). + +%% =================================================================== +%% Public API +%% =================================================================== + +-spec init(rebar_state:t()) -> {ok, rebar_state:t()}. +init(State) -> + State1 = rebar_state:add_provider(State, #provider{name = ?PROVIDER, + provider_impl = ?MODULE, + bare = false, + deps = ?DEPS, + example = "rebar help ", + short_desc = "Display a list of tasks or help for a given task or subtask.", + desc = "", + opts = []}), + {ok, State1}. + +-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | relx:error(). +do(State) -> + help(State), + {ok, State}. + +%% +%% print help/usage string +%% +help(State) -> + ?CONSOLE("Rebar is a tool for working with Erlang projects.~n~n", []), + OptSpecList = rebar3:option_spec_list(), + getopt:usage(OptSpecList, "rebar", "", []), + ?CONSOLE("~nSeveral tasks are available:~n", []), + + rebar_provider:help(State), + + ?CONSOLE("~nRun 'rebar help ' for details.~n~n", []). diff --git a/src/rebar_prv_install_deps.erl b/src/rebar_prv_install_deps.erl index 87db8b0..d20f2b5 100644 --- a/src/rebar_prv_install_deps.erl +++ b/src/rebar_prv_install_deps.erl @@ -55,7 +55,7 @@ init(State) -> State1 = rebar_state:add_provider(State, #provider{name = ?PROVIDER, provider_impl = ?MODULE, - bare = false, + bare = true, deps = ?DEPS, example = undefined, short_desc = "Install dependencies", diff --git a/src/rebar_prv_lock.erl b/src/rebar_prv_lock.erl index db6e753..b4a7d8d 100644 --- a/src/rebar_prv_lock.erl +++ b/src/rebar_prv_lock.erl @@ -18,10 +18,10 @@ init(State) -> State1 = rebar_state:add_provider(State, #provider{name = ?PROVIDER, provider_impl = ?MODULE, - bare = false, + bare = true, deps = ?DEPS, example = "", - short_desc = "Locks dependencies", + short_desc = "Locks dependencies.", desc = info("Locks dependencies"), opts = []}), {ok, State1}. diff --git a/src/rebar_prv_new.erl b/src/rebar_prv_new.erl index 528c95e..ed3c1b4 100644 --- a/src/rebar_prv_new.erl +++ b/src/rebar_prv_new.erl @@ -21,7 +21,7 @@ init(State) -> bare = false, deps = ?DEPS, example = "rebar new