浏览代码

Add support for an unlock command

Allows to remove entries from the lock file, or the lock file as a
whole.
pull/345/head
Fred Hebert 10 年前
父节点
当前提交
326f840123
共有 5 个文件被更改,包括 166 次插入5 次删除
  1. +1
    -0
      src/rebar.app.src
  2. +1
    -5
      src/rebar_prv_lock.erl
  3. +84
    -0
      src/rebar_prv_unlock.erl
  4. +40
    -0
      test/rebar_unlock_SUITE.erl
  5. +40
    -0
      test/rebar_unlock_SUITE_data/rebar.lock

+ 1
- 0
src/rebar.app.src 查看文件

@ -49,6 +49,7 @@
rebar_prv_report,
rebar_prv_shell,
rebar_prv_tar,
rebar_prv_unlock,
rebar_prv_update,
rebar_prv_upgrade,
rebar_prv_version,

+ 1
- 5
src/rebar_prv_lock.erl 查看文件

@ -23,7 +23,7 @@ init(State) ->
{deps, ?DEPS},
{example, ""},
{short_desc, "Locks dependencies."},
{desc, info("Locks dependencies")},
{desc, "Locks dependencies"},
{opts, []}])),
{ok, State1}.
@ -39,9 +39,6 @@ do(State) ->
format_error(Reason) ->
io_lib:format("~p", [Reason]).
info(_) ->
"".
build_locks(State) ->
AllDeps = rebar_state:lock(State),
[begin
@ -54,4 +51,3 @@ build_locks(State) ->
,rebar_fetch:lock_source(Dir, Source, State)
,rebar_app_info:dep_level(Dep)}
end || Dep <- AllDeps, not(rebar_app_info:is_checkout(Dep))].

+ 84
- 0
src/rebar_prv_unlock.erl 查看文件

@ -0,0 +1,84 @@
-module(rebar_prv_unlock).
-behaviour(provider).
-export([init/1,
do/1,
format_error/1]).
-include("rebar.hrl").
-include_lib("providers/include/providers.hrl").
-define(PROVIDER, unlock).
-define(DEPS, []).
%% ===================================================================
%% Public API
%% ===================================================================
-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, ""},
{short_desc, "Unlock dependencies."},
{desc, "Unlock project dependencies. Mentioning no application "
"will unlock all dependencies. To unlock specific dependencies, "
"their name can be listed in the command."},
{opts, [
{package, undefined, undefined, string,
"List of packages to upgrade. If not specified, all dependencies are upgraded."}
]}
])
),
{ok, State1}.
do(State) ->
Dir = rebar_state:dir(State),
LockFile = filename:join(Dir, ?LOCK_FILE),
case file:consult(LockFile) of
{error, enoent} ->
%% Our work is done.
{ok, State};
{error, Reason} ->
?PRV_ERROR({file,Reason});
{ok, [Locks]} ->
case handle_unlocks(State, Locks, LockFile) of
ok ->
{ok, State};
{error, Reason} ->
?PRV_ERROR({file,Reason})
end;
{ok, _Other} ->
?PRV_ERROR(unknown_lock_format)
end.
-spec format_error(any()) -> iolist().
format_error({file, Reason}) ->
io_lib:format("Lock file editing failed for reason ~p", [Reason]);
format_error(unknown_lock_format) ->
"Lock file format unknown";
format_error(Reason) ->
io_lib:format("~p", [Reason]).
handle_unlocks(State, Locks, LockFile) ->
{Args, _} = rebar_state:command_parsed_args(State),
Names = parse_names(ec_cnv:to_binary(proplists:get_value(package, Args, <<"">>))),
case [Lock || Lock = {Name, _, _} <- Locks, not lists:member(Name, Names)] of
[] ->
file:delete(LockFile);
_ when Names =:= [] -> % implicitly all locks
file:delete(LockFile);
NewLocks ->
file:write_file(LockFile, io_lib:format("~p.~n", [NewLocks]))
end.
parse_names(Bin) ->
case lists:usort(re:split(Bin, <<" *, *">>, [trim])) of
[<<"">>] -> []; % nothing submitted
Other -> Other
end.

+ 40
- 0
test/rebar_unlock_SUITE.erl 查看文件

@ -0,0 +1,40 @@
-module(rebar_unlock_SUITE).
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
-compile(export_all).
all() -> [unlock, unlock_all].
init_per_testcase(Case, Config0) ->
Config = rebar_test_utils:init_rebar_state(Config0, atom_to_list(Case)),
Lockfile = filename:join(?config(apps, Config), "rebar.lock"),
ec_file:copy(filename:join(?config(data_dir, Config), "rebar.lock"),
Lockfile),
[{lockfile, Lockfile} | Config].
end_per_testcase(_, Config) ->
Config.
unlock(Config) ->
Locks = read_locks(Config),
rebar_test_utils:run_and_check(Config, [], ["unlock", "fakeapp"], {ok, []}),
Locks = read_locks(Config),
rebar_test_utils:run_and_check(Config, [], ["unlock", "uuid"], {ok, []}),
?assertEqual(Locks -- ["uuid"], read_locks(Config)),
rebar_test_utils:run_and_check(Config, [], ["unlock", "gproc,itc"], {ok, []}),
?assertEqual(Locks -- ["uuid","gproc","itc"], read_locks(Config)),
rebar_test_utils:run_and_check(Config, [], ["unlock", string:join(Locks,",")], {ok, []}),
?assertEqual({error, enoent}, read_locks(Config)),
ok.
unlock_all(Config) ->
[_|_] = read_locks(Config),
rebar_test_utils:run_and_check(Config, [], ["unlock"], {ok, []}),
?assertEqual({error, enoent}, read_locks(Config)),
ok.
read_locks(Config) ->
case file:consult(?config(lockfile, Config)) of
{ok, [Locks]} -> [binary_to_list(element(1,Lock)) || Lock <- Locks];
Other -> Other
end.

+ 40
- 0
test/rebar_unlock_SUITE_data/rebar.lock 查看文件

@ -0,0 +1,40 @@
[{<<"meck">>,
{git,"git://github.com/eproxus/meck",
{ref,"69664df5d0e01a0ae27efb62831d6eea45cc1cd4"}},
3},
{<<"itc">>,
{git,"git://github.com/ferd/Interval-Tree-Clocks.git",
{ref,"168449da42871892f6047733edddf33936dd660f"}},
2},
{<<"bitcask">>,
{git,"https://github.com/basho/bitcask.git",
{ref,"c3e9cc5e64bcfd6e342a85932a90428cfdc1ee2d"}},
2},
{<<"quickrand">>,
{git,"https://github.com/okeuday/quickrand.git",
{ref,"0395a10b94472ccbe38b62bbfa9d0fc1ddac1dd7"}},
1},
{<<"merklet">>,
{git,"git://github.com/ferd/merklet.git",
{ref,"21b5797a21ab6aa1ddb6740799372d2d4b6a6054"}},
1},
{<<"interclock">>,
{git,"git://github.com/ferd/interclock.git",
{ref,"6a8b7443fa200da16d819f058e4dc8a0a75eca1e"}},
1},
{<<"gproc">>,
{git,"git://github.com/uwiger/gproc.git",
{ref,"7655bddc4397d0cc3f7c4d95e7b96790605946fe"}},
1},
{<<"uuid">>,
{git,"https://github.com/okeuday/uuid.git",
{ref,"f7c141c8359cd690faba0d2684b449a07db8e915"}},
0},
{<<"peeranha">>,
{git,"https://github.com/ferd/peeranha.git",
{ref,"3bdbdadf6e34a1705bf2669eb28acd32a4fc9166"}},
0},
{<<"file_monitor">>,
{git,"https://github.com/richcarl/file_monitor.git",
{ref,"4910b991bf3166377985d7ed961665c4b24e3de6"}},
0}].

正在加载...
取消
保存