Skip to content

Commit 58810bd

Browse files
mobileoverlordJosé Valim
authored andcommitted
add filter option to deps.unlock (#4932)
Signed-off-by: José Valim <[email protected]>
1 parent 050cb45 commit 58810bd

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/mix/lib/mix/tasks/deps.unlock.ex

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ defmodule Mix.Tasks.Deps.Unlock do
1616
1717
"""
1818

19-
@switches [all: :boolean, unused: :boolean]
19+
@switches [all: :boolean, unused: :boolean, filter: :string]
2020

2121
@spec run(OptionParser.argv) :: :ok
2222
def run(args) do
@@ -29,6 +29,27 @@ defmodule Mix.Tasks.Deps.Unlock do
2929
opts[:unused] ->
3030
apps = Mix.Dep.loaded([]) |> Enum.map(& &1.app)
3131
Mix.Dep.Lock.read() |> Map.take(apps) |> Mix.Dep.Lock.write()
32+
filter = opts[:filter] ->
33+
lock = Mix.Dep.Lock.read
34+
apps = Map.keys(lock)
35+
36+
unlock =
37+
apps
38+
|> Enum.filter(&String.contains?("#{&1}", filter))
39+
40+
if unlock == [] do
41+
Mix.shell.error "warning: no dependencies were matched"
42+
else
43+
lock =
44+
Enum.reject(lock, fn({app, _}) ->
45+
app in unlock
46+
end)
47+
Mix.Dep.Lock.write(lock)
48+
Mix.shell.info """
49+
Unlocked deps:
50+
* #{Enum.join(unlock, "\n* ")}
51+
"""
52+
end
3253

3354
apps != [] ->
3455
lock =

lib/mix/test/mix/tasks/deps_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,21 @@ defmodule Mix.Tasks.DepsTest do
238238
end
239239
end
240240

241+
test "unlocks filtered deps", context do
242+
Mix.Project.push DepsApp
243+
in_tmp context.test, fn ->
244+
Mix.Dep.Lock.write %{git_repo: "abcdef", another: "hash", another_one: "hash"}
245+
Mix.Tasks.Deps.Unlock.run ["--filter", "another"]
246+
assert Mix.Dep.Lock.read == %{git_repo: "abcdef"}
247+
output = """
248+
Unlocked deps:
249+
* another
250+
* another_one
251+
"""
252+
assert_received {:mix_shell, :info, [^output]}
253+
end
254+
end
255+
241256
## Deps environment
242257

243258
defmodule DepsEnvApp do

0 commit comments

Comments
 (0)