From 5735c538b976d856606f857107e7a931fdcce1d2 Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Sat, 16 Aug 2014 10:47:34 -0500 Subject: add rebar update provider --- src/rebar.erl | 167 +++------------------------------------------- src/rebar_app_info.erl | 5 ++ src/rebar_config.erl | 10 ++- src/rebar_core.erl | 4 +- src/rebar_deps.erl | 6 +- src/rebar_otp_app.erl | 5 +- src/rebar_prv_release.erl | 3 +- src/rebar_prv_tar.erl | 1 - src/rebar_prv_update.erl | 56 ++++++++++++++++ src/rebar_shell.erl | 1 - 10 files changed, 88 insertions(+), 170 deletions(-) create mode 100644 src/rebar_prv_update.erl (limited to 'src') diff --git a/src/rebar.erl b/src/rebar.erl index 2284afe..dd6d6d0 100644 --- a/src/rebar.erl +++ b/src/rebar.erl @@ -79,7 +79,7 @@ run(BaseConfig, Commands) -> run(["help"|RawCmds]) when RawCmds =/= [] -> ok = load_rebar_app(), - Cmds = unabbreviate_command_names(RawCmds), + Cmds = RawCmds, Args = parse_args(Cmds), BaseConfig = init_config(Args), {BaseConfig1, _} = save_options(BaseConfig, Args), @@ -174,7 +174,7 @@ run_aux(BaseConfig, Commands) -> %% Process each command, resetting any state between each one {ok, Providers} = application:get_env(rebar, providers), BaseConfig2 = rebar_config:create_logic_providers(Providers, BaseConfig1), - rebar_core:process_commands(CommandAtom, Args, BaseConfig2), + rebar_core:process_commands(CommandAtom, rebar_config:command_args(BaseConfig2, Args)), ok. %% @@ -190,50 +190,7 @@ help() -> ?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", []), - ?CONSOLE( - "rebar allows you to abbreviate the command to run:~n" - "$ rebar co # same as rebar compile~n" - "$ rebar eu # same as rebar eunit~n" - "$ rebar g-d # same as rebar get-deps~n" - "$ rebar x eu # same as rebar xref eunit~n" - "$ rebar l-d # same as rebar list-deps~n" - "$ rebar l-d l-t # same as rebar list-deps list-templates~n" - "$ rebar list-d l-te # same as rebar list-deps list-templates~n" - "~n", []), - ?CONSOLE( - "Core rebar.config options:~n" - " ~p~n" - " ~p~n" - " ~p~n" - " ~p~n" - " ~p~n" - " ~p~n" - " ~p~n" - " ~p~n" - " ~p~n" - " ~p~n" - "Core command line options:~n" - " apps=app1,app2 (specify apps to process)~n" - " skip_apps=app1,app2 (specify apps to skip)~n", - [ - {recursive_cmds, []}, - {require_erts_vsn, ".*"}, - {require_otp_vsn, ".*"}, - {require_min_otp_vsn, ".*"}, - {lib_dirs, []}, - {sub_dirs, ["dir1", "dir2"]}, - {plugins, [plugin1, plugin2]}, - {plugin_dir, "some_other_directory"}, - {pre_hooks, [{clean, "./prepare_package_files.sh"}, - {"linux", compile, "c_src/build_linux.sh"}, - {compile, "escript generate_headers"}, - {compile, "escript check_headers"}]}, - {post_hooks, [{clean, "touch file1.out"}, - {"freebsd", compile, "c_src/freebsd_tweaks.sh"}, - {eunit, "touch file2.out"}, - {compile, "touch postcompile.out"}]} - ]). + "~n~n", []). %% %% Parse command line arguments using getopt and also filtering out any @@ -283,7 +240,7 @@ save_options(Config, {Options, NonOptArgs}) -> %% 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. {Config7, RawCmds} = filter_flags(Config6, NonOptArgs, []), - {Config7, unabbreviate_command_names(RawCmds)}. + {Config7, RawCmds}. %% %% set log level based on getopt option @@ -367,46 +324,11 @@ compile Compile sources escriptize Generate escript archive -create template= [var=foo,...] Create skel based on template and vars -create-app [appid=myapp] Create simple app skel -create-lib [libid=mylib] Create simple lib skel -create-node [nodeid=mynode] Create simple node skel -list-templates List available templates - -doc Generate Erlang program documentation - -prepare-deps Run 'rebar -r get-deps compile' -refresh-deps Run 'rebar -r update-deps compile' - -check-deps Display to be fetched dependencies -get-deps Fetch dependencies -update-deps Update fetched dependencies -delete-deps Delete fetched dependencies -list-deps List dependencies - -eunit [suite[s]=foo] Run EUnit tests in foo.erl and - test/foo_tests.erl - [suite[s]=foo] [test[s]=bar] Run specific EUnit tests [first test - name starting with 'bar' in foo.erl - and test/foo_tests.erl] - [test[s]=bar] For every existing suite, run the first - test whose name starts with bar and, if - no such test exists, run the test whose - name starts with bar in the suite's - _tests module. - [random_suite_order=true] Run tests in a random order, either - [random_suite_order=Seed] with a random seed for the PRNG, or a - specific one. - -ct [suite[s]=] [case=] Run common_test suites - -qc Test QuickCheck properties - -xref Run cross reference analysis - shell Start a shell similar to 'erl -pa ebin -pa deps/*/ebin' +update [dep] Update source dep + help Show the program options version Show version information ">>, @@ -468,85 +390,12 @@ filter_flags(Config, [Item | Rest], Commands) -> command_names() -> [ - "check-deps", "clean", "compile", "release", - "create", - "create-app", - "create-lib", - "create-node", - "ct", - "delete-deps", - "doc", - "eunit", + "update", "escriptize", - "get-deps", "help", - "list-deps", - "list-templates", - "prepare-deps", - "qc", - "refresh-deps", - "update-deps", - "overlay", "shell", - "version", - "xref" + "version" ]. - -unabbreviate_command_names([]) -> - []; -unabbreviate_command_names([Command | Commands]) -> - case get_command_name_candidates(Command) of - [] -> - %% let the rest of the code detect that the command doesn't exist - %% (this would perhaps be a good place to fail) - [Command | unabbreviate_command_names(Commands)]; - [FullCommand] -> - [FullCommand | unabbreviate_command_names(Commands)]; - Candidates -> - ?ABORT("Found more than one match for abbreviated command name " - " '~s',~nplease be more specific. Possible candidates:~n" - " ~s~n", - [Command, string:join(Candidates, ", ")]) - end. - -get_command_name_candidates(Command) -> - %% Get the command names which match the given (abbreviated) command name. - %% * "c" matches commands like compile, clean and create-app - %% * "create" matches command create only, since it's unique - %% * "create-" matches commands starting with create- - %% * "c-a" matches create-app - %% * "create-a" matches create-app - %% * "c-app" matches create-app - Candidates = [Candidate || Candidate <- command_names(), - is_command_name_candidate(Command, Candidate)], - %% Is there a complete match? If so return only that, return a - %% list of candidates otherwise - case lists:member(Command, Candidates) of - true -> [Command]; - false -> Candidates - end. - -is_command_name_candidate(Command, Candidate) -> - lists:prefix(Command, Candidate) - orelse is_command_name_sub_word_candidate(Command, Candidate). - -is_command_name_sub_word_candidate(Command, Candidate) -> - %% Allow for parts of commands to be abbreviated, i.e. create-app - %% can be shortened to "create-a", "c-a" or "c-app" (but not - %% "create-" since that would be ambiguous). - ReOpts = [{return, list}], - CommandSubWords = re:split(Command, "-", ReOpts), - CandidateSubWords = re:split(Candidate, "-", ReOpts), - is_command_name_sub_word_candidate_aux(CommandSubWords, CandidateSubWords). - -is_command_name_sub_word_candidate_aux([CmdSW | CmdSWs], - [CandSW | CandSWs]) -> - lists:prefix(CmdSW, CandSW) andalso - is_command_name_sub_word_candidate_aux(CmdSWs, CandSWs); -is_command_name_sub_word_candidate_aux([], []) -> - true; -is_command_name_sub_word_candidate_aux(_CmdSWs, _CandSWs) -> - false. diff --git a/src/rebar_app_info.erl b/src/rebar_app_info.erl index 503d3bc..89307ad 100644 --- a/src/rebar_app_info.erl +++ b/src/rebar_app_info.erl @@ -11,6 +11,7 @@ app_file/1, app_file/2, original_vsn/1, + original_vsn/2, dir/1, dir/2]). @@ -84,6 +85,10 @@ app_file(AppInfo=#app_info_t{}, AppFile) -> original_vsn(#app_info_t{original_vsn=Vsn}) -> Vsn. +-spec original_vsn(t(), string()) -> string(). +original_vsn(AppInfo=#app_info_t{}, Vsn) -> + AppInfo#app_info_t{original_vsn=Vsn}. + -spec dir(t()) -> file:name(). dir(#app_info_t{dir=Dir}) -> Dir. diff --git a/src/rebar_config.erl b/src/rebar_config.erl index 079801a..50682e0 100644 --- a/src/rebar_config.erl +++ b/src/rebar_config.erl @@ -30,6 +30,7 @@ get/3, get_local/3, get_list/3, get_all/2, set/3, + command_args/1, command_args/2, set_global/3, get_global/3, is_recursive/1, save_env/3, get_env/2, reset_envs/1, @@ -56,6 +57,7 @@ local_opts = [] :: list(), globals = new_globals() :: rebar_dict(), envs = new_env() :: rebar_dict(), + command_args = [] :: list(), %% cross-directory/-command config goals = [], providers = [], @@ -119,7 +121,7 @@ new(ParentConfig, ConfName, Dir) -> Opts1 = case consult_file(?LOCK_FILE) of {ok, [D]} -> - lists:keyreplace(deps, 1, Opts, {deps, D}); + [{lock_deps, D} | Opts]; _ -> Opts end, @@ -230,6 +232,12 @@ erase_xconf(Config, Key) -> NewXconf = dict:erase(Key, Config#config.xconf), Config#config{xconf = NewXconf}. +command_args(#config{command_args=CmdArgs}) -> + CmdArgs. + +command_args(Config, CmdArgs) -> + Config#config{command_args=CmdArgs}. + get_dep(#config{deps=Apps}, Name) -> lists:keyfind(Name, 2, Apps). diff --git a/src/rebar_core.erl b/src/rebar_core.erl index 66af875..5dbdefb 100644 --- a/src/rebar_core.erl +++ b/src/rebar_core.erl @@ -26,7 +26,7 @@ %% ------------------------------------------------------------------- -module(rebar_core). --export([process_commands/3, help/2]). +-export([process_commands/2, help/2]). -include("rebar.hrl"). @@ -53,7 +53,7 @@ help(ParentConfig, Commands) -> end, Providers) end, Commands). -process_commands(Command, Args, ParentConfig) -> +process_commands(Command, ParentConfig) -> true = rebar_utils:expand_code_path(), LibDirs = rebar_config:get_local(ParentConfig, lib_dirs, ["apps", "libs", "."]), DepsDir = rebar_deps:get_deps_dir(ParentConfig), diff --git a/src/rebar_deps.erl b/src/rebar_deps.erl index 02195a2..69dce46 100644 --- a/src/rebar_deps.erl +++ b/src/rebar_deps.erl @@ -38,6 +38,7 @@ %% for internal use only -export([info/2]). -export([get_deps_dir/1]). +-export([get_deps_dir/2]). -define(PROVIDER, deps). -define(DEPS, []). @@ -152,12 +153,13 @@ get_deps_dir(Config) -> BaseDir = rebar_utils:base_dir(Config), get_deps_dir(BaseDir, "deps"). +get_deps_dir(DepsDir, App) -> + filename:join(DepsDir, App). + %% =================================================================== %% Internal functions %% =================================================================== -get_deps_dir(DepsDir, App) -> - filename:join(DepsDir, App). -spec gather_application_info(file:name(), file:filename()) -> {ok, rebar_app_info:t()} | diff --git a/src/rebar_otp_app.erl b/src/rebar_otp_app.erl index 6dfb6b0..1779217 100644 --- a/src/rebar_otp_app.erl +++ b/src/rebar_otp_app.erl @@ -55,6 +55,7 @@ compile(Config, App) -> AppFile = rebar_app_info:app_file(App1), case rebar_app_utils:load_app_file(Config2, AppFile) of {ok, Config3, AppName, AppData} -> + AppVsn = proplists:get_value(vsn, AppData), validate_name(AppName, AppFile), %% In general, the list of modules is an important thing to validate %% for compliance with OTP guidelines and upgrade procedures. @@ -62,9 +63,9 @@ compile(Config, App) -> case rebar_config:get_local(Config3, validate_app_modules, true) of true -> Modules = proplists:get_value(modules, AppData), - {validate_modules(Dir, AppName, Modules), App1}; + {validate_modules(Dir, AppName, Modules), rebar_app_info:original_vsn(App1, AppVsn)}; false -> - {ok, App1} + {ok, rebar_app_info:original_vsn(App1, AppVsn)} end; {error, Reason} -> ?ABORT("Failed to load app file ~s: ~p\n", [AppFile, Reason]) diff --git a/src/rebar_prv_release.erl b/src/rebar_prv_release.erl index 3edf2eb..77a2353 100644 --- a/src/rebar_prv_release.erl +++ b/src/rebar_prv_release.erl @@ -11,7 +11,7 @@ -include("rebar.hrl"). -define(PROVIDER, release). --define(DEPS, [app_builder]). +-define(DEPS, [compile]). %% =================================================================== %% Public API @@ -21,7 +21,6 @@ init(State) -> State1 = rebar_config:add_provider(State, #provider{name = ?PROVIDER, provider_impl = ?MODULE, - provides = release, bare = false, deps = ?DEPS, example = "rebar release", diff --git a/src/rebar_prv_tar.erl b/src/rebar_prv_tar.erl index d45c70d..775e3b9 100644 --- a/src/rebar_prv_tar.erl +++ b/src/rebar_prv_tar.erl @@ -21,7 +21,6 @@ init(State) -> State1 = rebar_config:add_provider(State, #provider{name = ?PROVIDER, provider_impl = ?MODULE, - provides = tar, bare = false, deps = ?DEPS, example = "rebar tar", diff --git a/src/rebar_prv_update.erl b/src/rebar_prv_update.erl new file mode 100644 index 0000000..ef6270a --- /dev/null +++ b/src/rebar_prv_update.erl @@ -0,0 +1,56 @@ +%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*- +%% ex: ts=4 sw=4 et + +-module(rebar_prv_update). + +-behaviour(rebar_provider). + +-export([init/1, + do/1]). + +-include("rebar.hrl"). + +-define(PROVIDER, update). +-define(DEPS, []). + +%% =================================================================== +%% Public API +%% =================================================================== + +-spec init(rebar_config:config()) -> {ok, rebar_config:config()}. +init(State) -> + State1 = rebar_config:add_provider(State, #provider{name = ?PROVIDER, + provider_impl = ?MODULE, + bare = false, + deps = ?DEPS, + example = "rebar update cowboy", + short_desc = "", + desc = "", + opts = []}), + {ok, State1}. + +-spec do(rebar_config:config()) -> {ok, rebar_config:config()} | relx:error(). +do(Config) -> + [Name] = rebar_config:command_args(Config), + ?INFO("Updating ~s~n", [Name]), + + DepsDir = rebar_deps:get_deps_dir(Config), + Deps = rebar_config:get_local(Config, deps, []), + {_, _, Source} = lists:keyfind(list_to_atom(Name), 1, Deps), + TargetDir = rebar_deps:get_deps_dir(DepsDir, Name), + rebar_fetch:update_source1(TargetDir, Source), + + [App] = rebar_app_discover:find_apps([TargetDir]), + + {ok, AppInfo1} = rebar_otp_app:compile(Config, App), + Config1 = rebar_config:replace_app(Config, rebar_app_info:name(AppInfo1), AppInfo1), + rebar_erlc_compiler:compile(Config, rebar_app_info:dir(AppInfo1)), + update_lock_file(Config, AppInfo1, Source), + + {ok, Config}. + +update_lock_file(Config, App, Source) -> + New = rebar_fetch:new(rebar_app_info:dir(App), rebar_app_info:name(App), rebar_app_info:original_vsn(App), Source), + {ok, [Terms]} = file:consult("./rebar.lock"), + LockDeps = lists:keyreplace(rebar_app_info:name(App), 1, Terms, New), + ok = file:write_file("./rebar.lock", io_lib:format("~p.~n", [LockDeps])). diff --git a/src/rebar_shell.erl b/src/rebar_shell.erl index 6b4bb1f..97ac23e 100644 --- a/src/rebar_shell.erl +++ b/src/rebar_shell.erl @@ -46,7 +46,6 @@ init(State) -> State1 = rebar_config:add_provider(State, #provider{name = ?PROVIDER, provider_impl = ?MODULE, - provides = shell, bare = false, deps = ?DEPS, example = "rebar shell", -- cgit v1.1