1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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, true},
{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 unlock. If not specified, all dependencies are unlocked."}
]}
])
),
{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.
|