Skip to content

Commit e6420c8

Browse files
author
José Valim
committed
Merge pull request #2715 from nurugger07/remove_unused_deps
Add an unused flag to deps.clean & deps.unlock
2 parents 2af00f4 + 5f853f8 commit e6420c8

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

lib/mix/lib/mix/tasks/deps.clean.ex

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ defmodule Mix.Tasks.Deps.Clean do
1111
also works accross all environments, unless `--only` is given.
1212
1313
Clean does not unlock the dependencies, unless `--unlock` is given.
14+
The `--unused` flag removes unused the dependencies.
1415
"""
15-
@switches [unlock: :boolean, all: :boolean, only: :string]
16+
@switches [unlock: :boolean, all: :boolean, only: :string, unused: :boolean]
1617

1718
def run(args) do
1819
Mix.Project.get!
@@ -24,11 +25,24 @@ defmodule Mix.Tasks.Deps.Clean do
2425
clean_opts = if only = opts[:only], do: [env: :"#{only}"], else: []
2526
apps = Mix.Dep.loaded(clean_opts) |> Enum.map(&(&1.app))
2627
do_clean apps, opts
28+
opts[:unused] ->
29+
Mix.Dep.loaded([]) |> Enum.map(&(&1.app)) |> unused_apps |> do_clean opts
2730
args != [] ->
2831
do_clean args, opts
2932
true ->
3033
Mix.raise "mix deps.clean expects dependencies as arguments or " <>
31-
"the --all option to clean all dependencies"
34+
"a flag indicating which dependencies to clean " <>
35+
"The --all option will clean all dependencies while"
36+
"the --unused option cleans unused dependencies."
37+
end
38+
end
39+
40+
defp unused_apps(loaded_apps) do
41+
case File.ls(Mix.Project.deps_path) do
42+
{:ok, deps} ->
43+
Enum.reject deps,
44+
fn(x) -> not File.dir?(x) and Enum.member?(loaded_apps, String.to_atom(x)) end
45+
{_, _} -> []
3246
end
3347
end
3448

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,17 +539,37 @@ defmodule Mix.Tasks.DepsTest do
539539
File.mkdir_p!("_build/dev/lib/git_repo")
540540

541541
message = "mix deps.clean expects dependencies as arguments or " <>
542-
"the --all option to clean all dependencies"
542+
"a flag indicating which dependencies to clean " <>
543+
"The --all option will clean all dependencies while"
544+
"the --unused option cleans unused dependencies."
543545

544546
assert_raise Mix.Error, message, fn ->
545547
Mix.Tasks.Deps.Clean.run []
546548
end
547549

548550
Mix.Tasks.Deps.Clean.run ["--all"]
551+
refute File.exists?("_build/dev/lib/git_repo")
549552
refute File.exists?("deps/git_repo")
550553
end
551554
end
552555

556+
test "clean unused" do
557+
Mix.Project.push CleanDepsApp
558+
559+
in_fixture "deps_status", fn ->
560+
File.mkdir_p!("deps/git_repo")
561+
File.mkdir_p!("_build/dev/lib/git_repo")
562+
File.mkdir_p!("deps/git_repo_unused")
563+
File.mkdir_p!("_build/dev/lib/git_repo_unused")
564+
565+
Mix.Tasks.Deps.Clean.run ["--unused"]
566+
assert File.exists?("deps/git_repo")
567+
assert File.exists?("_build/dev/lib/git_repo")
568+
refute File.exists?("deps/git_repo_unused")
569+
refute File.exists?("_build/dev/lib/git_repo_unused")
570+
end
571+
end
572+
553573
test "cleans dependencies" do
554574
Mix.Project.push CleanDepsApp
555575

0 commit comments

Comments
 (0)