From 1ef5ec6483f9d2280646357781edfd95962fcfe7 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Sat, 2 Oct 2021 21:14:48 -0700 Subject: [PATCH 1/6] initial support for all in unlock --- src/rebar_prv_unlock.erl | 42 ++++++++++++++++++++++++++----------- test/rebar_unlock_SUITE.erl | 2 +- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/rebar_prv_unlock.erl b/src/rebar_prv_unlock.erl index 6fe8bd800..2f888e4b9 100644 --- a/src/rebar_prv_unlock.erl +++ b/src/rebar_prv_unlock.erl @@ -29,9 +29,9 @@ init(State) -> {desc, "Unlock project dependencies. Mentioning no application " "will unlock all dependencies. To unlock specific dependencies, " "their name can be listed in the command."}, - {opts, [ + {opts, [{all, $a, "all", undefined, "Unlock all dependencies and remove the lock file."}, {package, undefined, undefined, string, - "List of packages to unlock. If not specified, all dependencies are unlocked."} + "List of packages to unlock."} ]} ]) ), @@ -57,24 +57,42 @@ 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(no_arg) -> + "Specify a list of dependencies to unlock, or --all to unlock them all"; format_error(Reason) -> io_lib:format("~p", [Reason]). handle_unlocks(State, Locks, LockFile) -> - {Args, _} = rebar_state:command_parsed_args(State), - Names = parse_names(rebar_utils:to_binary(proplists:get_value(package, Args, <<"">>))), - case [Lock || Lock = {Name, _, _} <- Locks, not lists:member(Name, Names)] of - [] -> - file:delete(LockFile), - {ok, []}; - _ when Names =:= [] -> % implicitly all locks + {_All, Names} = handle_args(State), + case handle_args(State) of + %% a list of dependencies or --all is required + {false, []} -> + throw(?PRV_ERROR(no_arg)); + %% if --all is specified, delete the lock file + {true, _} -> file:delete(LockFile), {ok, []}; - NewLocks -> - rebar_config:write_lock_file(LockFile, NewLocks), - {ok, NewLocks} + %% otherwise, unlock the given list of dependency names. if none are left, delete the lock file + {false, Names} -> + case [Lock || Lock = {Name, _, _} <- Locks, not lists:member(Name, Names)] of + [] -> + file:delete(LockFile), + {ok, []}; + _ when Names =:= [] -> % implicitly all locks + file:delete(LockFile), + {ok, []}; + NewLocks -> + rebar_config:write_lock_file(LockFile, NewLocks), + {ok, NewLocks} + end end. +handle_args(State) -> + {Args, _} = rebar_state:command_parsed_args(State), + All = proplists:get_value(all, Args, false), + Names = parse_names(rebar_utils:to_binary(proplists:get_value(package, Args, <<"">>))), + {All, Names}. + parse_names(Bin) -> case lists:usort(re:split(Bin, <<" *, *">>, [trim, unicode])) of [<<"">>] -> []; % nothing submitted diff --git a/test/rebar_unlock_SUITE.erl b/test/rebar_unlock_SUITE.erl index a8d14000f..5d5a20126 100644 --- a/test/rebar_unlock_SUITE.erl +++ b/test/rebar_unlock_SUITE.erl @@ -54,7 +54,7 @@ unlock(Config) -> unlock_all(Config) -> [_|_] = read_locks(Config), - {ok, State} = rebar_test_utils:run_and_check(Config, [], ["unlock"], return), + {ok, State} = rebar_test_utils:run_and_check(Config, [], ["unlock", "--all"], return), ?assertEqual({error, enoent}, read_locks(Config)), ?assertEqual([], rebar_state:get(State, {locks, default})), ok. From 412574f001b2b02867ee4e6fdd964738f63e51e7 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Tue, 12 Oct 2021 20:32:55 -0700 Subject: [PATCH 2/6] no longer used --- src/rebar_prv_unlock.erl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/rebar_prv_unlock.erl b/src/rebar_prv_unlock.erl index 2f888e4b9..7c11cbbc3 100644 --- a/src/rebar_prv_unlock.erl +++ b/src/rebar_prv_unlock.erl @@ -63,7 +63,6 @@ format_error(Reason) -> io_lib:format("~p", [Reason]). handle_unlocks(State, Locks, LockFile) -> - {_All, Names} = handle_args(State), case handle_args(State) of %% a list of dependencies or --all is required {false, []} -> From 8e66731f15920c3b603919fc02a3a61aaaabe67d Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Tue, 12 Oct 2021 21:35:57 -0700 Subject: [PATCH 3/6] fix docs for command --- src/rebar_prv_unlock.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rebar_prv_unlock.erl b/src/rebar_prv_unlock.erl index 7c11cbbc3..fe2812d19 100644 --- a/src/rebar_prv_unlock.erl +++ b/src/rebar_prv_unlock.erl @@ -26,8 +26,8 @@ init(State) -> {deps, ?DEPS}, {example, ""}, {short_desc, "Unlock dependencies."}, - {desc, "Unlock project dependencies. Mentioning no application " - "will unlock all dependencies. To unlock specific dependencies, " + {desc, "Unlock project dependencies. Use the -all option " + "to unlock all dependencies. To unlock specific dependencies, " "their name can be listed in the command."}, {opts, [{all, $a, "all", undefined, "Unlock all dependencies and remove the lock file."}, {package, undefined, undefined, string, From 933abc3bf0ea41cf2280bf194c1732a2296e9965 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Tue, 12 Oct 2021 21:36:25 -0700 Subject: [PATCH 4/6] add missing dash --- src/rebar_prv_unlock.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rebar_prv_unlock.erl b/src/rebar_prv_unlock.erl index fe2812d19..f0b2298d7 100644 --- a/src/rebar_prv_unlock.erl +++ b/src/rebar_prv_unlock.erl @@ -26,7 +26,7 @@ init(State) -> {deps, ?DEPS}, {example, ""}, {short_desc, "Unlock dependencies."}, - {desc, "Unlock project dependencies. Use the -all option " + {desc, "Unlock project dependencies. Use the --all option " "to unlock all dependencies. To unlock specific dependencies, " "their name can be listed in the command."}, {opts, [{all, $a, "all", undefined, "Unlock all dependencies and remove the lock file."}, From cb22f138bcceb1621c29e90b6f095674c3b0c982 Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Tue, 26 Oct 2021 11:02:15 -0700 Subject: [PATCH 5/6] test no_arg error --- test/rebar_unlock_SUITE.erl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/rebar_unlock_SUITE.erl b/test/rebar_unlock_SUITE.erl index 5d5a20126..67692d249 100644 --- a/test/rebar_unlock_SUITE.erl +++ b/test/rebar_unlock_SUITE.erl @@ -3,7 +3,7 @@ -include_lib("eunit/include/eunit.hrl"). -compile(export_all). -all() -> [pkgunlock, unlock, unlock_all]. +all() -> [pkgunlock, unlock, unlock_all, unlock_no_args]. init_per_testcase(pkgunlock, Config0) -> Config = rebar_test_utils:init_rebar_state(Config0, "pkgunlock"), @@ -59,6 +59,13 @@ unlock_all(Config) -> ?assertEqual([], rebar_state:get(State, {locks, default})), ok. +unlock_no_args(Config) -> + try rebar_test_utils:run_and_check(Config, [], ["unlock"], return) + catch {error, {rebar_prv_unlock, no_arg}} -> + ok + end, + ok. + read_locks(Config) -> case file:consult(?config(lockfile, Config)) of {ok, _} -> From 0698db783170a377f01f3cd779c56672b1e1e1df Mon Sep 17 00:00:00 2001 From: josh rotenberg Date: Tue, 26 Oct 2021 11:02:33 -0700 Subject: [PATCH 6/6] update alias tests with unlock argument --- test/rebar_alias_SUITE.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/rebar_alias_SUITE.erl b/test/rebar_alias_SUITE.erl index d6e27d95c..4356496ad 100644 --- a/test/rebar_alias_SUITE.erl +++ b/test/rebar_alias_SUITE.erl @@ -24,7 +24,7 @@ command(Config) -> Name = rebar_test_utils:create_random_name("alias_command_"), Vsn = rebar_test_utils:create_random_vsn(), rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), - RebarConfig = [{alias, [{test, [compile, unlock]}]}], + RebarConfig = [{alias, [{test, [compile, {unlock,"-a"}]}]}], %% compile job ran rebar_test_utils:run_and_check(Config, RebarConfig, @@ -64,7 +64,7 @@ many(Config) -> Vsn = rebar_test_utils:create_random_vsn(), rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), RebarConfig = [{alias, [{test, [{eunit,"-c"}, cover]}, - {nolock, [compile, unlock]}]}], + {nolock, [compile, {unlock,"-a"}]}]}], %% test job ran (compiled and succeeded) rebar_test_utils:run_and_check(Config, RebarConfig,