diff options
author | Alexey Romanov <alexey.v.romanov@gmail.com> | 2011-02-21 12:41:20 +0300 |
---|---|---|
committer | Alexey Romanov <alexey.v.romanov@gmail.com> | 2011-02-21 12:41:20 +0300 |
commit | 7dc371d8a3b9a6e2ab9f814c0d2536cf638ceb99 (patch) | |
tree | 174193ae08d2824164ba99e63f696a6593ee63c6 /src/rebar_core.erl | |
parent | 2ceeb3272139b7569c8dabc215ca1e7063b0d385 (diff) | |
parent | 6056c63eed288736c912c82d6f36aa7dd055f9ca (diff) |
Merge branch 'master' of https://github.com/basho/rebar
Diffstat (limited to 'src/rebar_core.erl')
-rw-r--r-- | src/rebar_core.erl | 274 |
1 files changed, 31 insertions, 243 deletions
diff --git a/src/rebar_core.erl b/src/rebar_core.erl index 3c64f8d..db3e0b4 100644 --- a/src/rebar_core.erl +++ b/src/rebar_core.erl @@ -1,4 +1,4 @@ -%% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*- +%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*- %% ex: ts=4 sw=4 et %% ------------------------------------------------------------------- %% @@ -26,7 +26,7 @@ %% ------------------------------------------------------------------- -module(rebar_core). --export([run/1, +-export([process_commands/1, skip_dir/1, is_skip_dir/1, skip_dirs/0]). @@ -34,57 +34,10 @@ -include("rebar.hrl"). --ifndef(BUILD_TIME). --define(BUILD_TIME, "undefined"). --endif. - --ifndef(VCS_INFO). --define(VCS_INFO, "undefined"). --endif. - %% =================================================================== %% Public API %% =================================================================== -run(["help"]) -> - help(), - ok; -run(["version"]) -> - %% Load application spec and display vsn and build time info - ok = application:load(rebar), - version(), - ok; -run(RawArgs) -> - %% Pre-load the rebar app so that we get default configuration - ok = application:load(rebar), - - %% Parse out command line arguments -- what's left is a list of commands to - %% run - Commands = parse_args(RawArgs), - - %% Make sure crypto is running - ok = crypto:start(), - - %% 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())), - - %% Keep track of how many operations we do, so we can detect bad commands - erlang:put(operations, 0), - - %% Process each command, resetting any state between each one - process_commands(CommandAtoms). - skip_dir(Dir) -> SkipDir = {skip_dir, Dir}, case erlang:get(SkipDir) of @@ -97,10 +50,10 @@ skip_dir(Dir) -> is_skip_dir(Dir) -> case erlang:get({skip_dir, Dir}) of - undefined -> - false; - true -> - true + undefined -> + false; + true -> + true end. skip_dirs() -> @@ -110,180 +63,6 @@ skip_dirs() -> %% Internal functions %% =================================================================== -%% -%% 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 getopt options - OptSpecList = option_spec_list(), - case getopt:parse(OptSpecList, Args) of - {ok, {Options, NonOptArgs}} -> - %% Check options and maybe halt execution - {ok, continue} = show_info_maybe_halt(Options, NonOptArgs), - - %% Set global variables based on getopt options - set_global_flag(Options, verbose), - 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. - filter_flags(NonOptArgs, []); - - {error, {Reason, Data}} -> - ?ERROR("Error: ~s ~p~n~n", [Reason, Data]), - help(), - halt(1) - end. - -%% -%% set global flag based on getopt option boolean value -%% -set_global_flag(Options, Flag) -> - Value = case proplists:get_bool(Flag, Options) of - true -> - "1"; - false -> - "0" - end, - rebar_config:set_global(Flag, Value). - -%% -%% show info and maybe halt execution -%% -show_info_maybe_halt(Opts, NonOptArgs) -> - case proplists:get_bool(help, Opts) of - true -> - help(), - halt(0); - false -> - case proplists:get_bool(commands, Opts) of - true -> - commands(), - halt(0); - false -> - case proplists:get_bool(version, Opts) of - true -> - version(), - halt(0); - false -> - case NonOptArgs of - [] -> - ?CONSOLE("No command to run specified!~n",[]), - help(), - halt(1); - _ -> - {ok, continue} - end - end - end - end. - -%% -%% print help/usage string -%% -help() -> - OptSpecList = option_spec_list(), - getopt:usage(OptSpecList, "rebar", - "[var=value,...] <command,...>", - [{"var=value", "rebar global variables (e.g. force=1)"}, - {"command", "Command to run (e.g. compile)"}]). - -%% -%% print known commands -%% -commands() -> - S = <<" -dialyze Analyze with Dialyzer -build-plt Build Dialyzer PLT -check-plt Check Dialyzer PLT - -clean Clean -compile Compile sources - -create template= [var=foo,...] Create skel based on template and vars -create-app [appid=myapp] Create simple app skel -create-node [nodeid=mynode] Create simple node skel -list-templates List available templates - -doc Generate Erlang program documentation - -check-deps Display to be fetched dependencies -get-deps Fetch dependencies -delete-deps Delete fetched dependencies - -generate [dump_spec=0/1] Build release with reltool - -eunit [suite=foo] Run eunit [test/foo_tests.erl] tests -ct [suite=] [case=] Run common_test suites in ./test - -xref Run cross reference analysis - -help Show the program options -version Show version information -">>, - io:put_chars(S), - %% workaround to delay exit until all output is written - timer:sleep(300). - -%% -%% show version information and halt -%% -version() -> - {ok, Vsn} = application:get_key(rebar, vsn), - ?CONSOLE("rebar version: ~s date: ~s vcs: ~s\n", [Vsn, ?BUILD_TIME, ?VCS_INFO]). - -%% -%% options accepted via getopt -%% -option_spec_list() -> - Jobs = rebar_config:get_jobs(), - JobsHelp = io_lib:format( - "Number of concurrent workers a command may use. Default: ~B", - [Jobs]), - [ - %% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg} - {help, $h, "help", undefined, "Show the program options"}, - {commands, $c, "commands", undefined, "Show available commands"}, - {verbose, $v, "verbose", undefined, "Be verbose about what gets done"}, - {version, $V, "version", undefined, "Show version information"}, - {force, $f, "force", undefined, "Force"}, - {jobs, $j, "jobs", integer, JobsHelp}, - {config, $C, "config", string, "Rebar config file to use"} - ]. - -%% -%% 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) -> - case string:tokens(Item, "=") of - [Command] -> - filter_flags(Rest, [Command | Commands]); - [KeyStr, Value] -> - Key = list_to_atom(KeyStr), - rebar_config:set_global(Key, Value), - filter_flags(Rest, Commands); - Other -> - ?CONSOLE("Ignoring command line argument: ~p\n", [Other]), - filter_flags(Rest, Commands) - end. - process_commands([]) -> case erlang:get(operations) of 0 -> @@ -297,7 +76,8 @@ process_commands([Command | Rest]) -> lists:foreach(fun (D) -> erlang:erase({skip_dir, D}) end, skip_dirs()), Operations = erlang:get(operations), - _ = process_dir(rebar_utils:get_cwd(), rebar_config:new(), Command, sets:new()), + _ = process_dir(rebar_utils:get_cwd(), rebar_config:new(), + Command, sets:new()), case erlang:get(operations) of Operations -> %% This command didn't do anything @@ -329,7 +109,8 @@ process_dir(Dir, ParentConfig, Command, DirSet) -> %% CWD to see if it's a fit -- if it is, use that set of modules %% to process this dir. {ok, AvailModuleSets} = application:get_env(rebar, modules), - {DirModules, ModuleSetFile} = choose_module_set(AvailModuleSets, Dir), + {DirModules, ModuleSetFile} = choose_module_set(AvailModuleSets, + Dir), %% Get the list of modules for "any dir". This is a catch-all list %% of modules that are processed in addition to modules associated @@ -343,7 +124,8 @@ process_dir(Dir, ParentConfig, Command, DirSet) -> %% directories that should be processed _before_ the current one. Predirs = acc_modules(Modules, preprocess, Config, ModuleSetFile), ?DEBUG("Predirs: ~p\n", [Predirs]), - DirSet2 = process_each(Predirs, Command, Config, ModuleSetFile, DirSet), + DirSet2 = process_each(Predirs, Command, Config, + ModuleSetFile, DirSet), %% Make sure the CWD is reset properly; processing the dirs may have %% caused it to change @@ -352,27 +134,30 @@ process_dir(Dir, ParentConfig, Command, DirSet) -> %% Check that this directory is not on the skip list case is_skip_dir(Dir) of true -> - %% Do not execute the command on the directory, as some module - %% as requested a skip on it. + %% Do not execute the command on the directory, as some + %% module as requested a skip on it. ?INFO("Skipping ~s in ~s\n", [Command, Dir]); false -> - %% Get the list of plug-in modules from rebar.config. These modules are - %% processed LAST and do not participate in preprocess. + %% Get the list of plug-in modules from rebar.config. These + %% modules are processed LAST and do not participate + %% in preprocess. {ok, PluginModules} = plugin_modules(Config), %% Execute the current command on this directory - execute(Command, Modules ++ PluginModules, Config, ModuleSetFile) + execute(Command, Modules ++ PluginModules, + Config, ModuleSetFile) end, %% Mark the current directory as processed DirSet3 = sets:add_element(Dir, DirSet2), - %% Invoke 'postprocess' on the modules -- this yields a list of other + %% Invoke 'postprocess' on the modules. This yields a list of other %% directories that should be processed _after_ the current one. Postdirs = acc_modules(Modules, postprocess, Config, ModuleSetFile), ?DEBUG("Postdirs: ~p\n", [Postdirs]), - DirSet4 = process_each(Postdirs, Command, Config, ModuleSetFile, DirSet3), + DirSet4 = process_each(Postdirs, Command, Config, + ModuleSetFile, DirSet3), %% Make sure the CWD is reset properly; processing the dirs may have %% caused it to change @@ -441,21 +226,24 @@ execute(Command, Modules, Config, ModuleFile) -> Dir = rebar_utils:get_cwd(), ?CONSOLE("==> ~s (~s)\n", [filename:basename(Dir), Command]), - %% Increment the count of operations, since some module responds to this command + %% Increment the count of operations, since some module + %% responds to this command erlang:put(operations, erlang:get(operations) + 1), %% Run the available modules - case catch(run_modules(TargetModules, Command, Config, ModuleFile)) of + case catch(run_modules(TargetModules, Command, + Config, ModuleFile)) of ok -> ok; {error, failed} -> ?FAIL; {Module, {error, _} = Other} -> ?ABORT("~p failed while processing ~s in module ~s: ~s\n", - [Command, Dir, Module, io_lib:print(Other, 1,80,-1)]); + [Command, Dir, Module, + io_lib:print(Other, 1, 80, -1)]); Other -> ?ABORT("~p failed while processing ~s: ~s\n", - [Command, Dir, io_lib:print(Other, 1,80,-1)]) + [Command, Dir, io_lib:print(Other, 1, 80, -1)]) end end. @@ -474,8 +262,8 @@ update_code_path(Config) -> restore_code_path(no_change) -> ok; restore_code_path({old, Path}) -> - %% Verify that all of the paths still exist -- some dynamically add paths - %% can get blown away during clean. + %% Verify that all of the paths still exist -- some dynamically + %% added paths can get blown away during clean. true = code:set_path([F || F <- Path, filelib:is_file(F)]), ok. |