Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide --sort flag sorting outdated results by status #1017

Merged
merged 7 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/mix/tasks/hex.outdated.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ defmodule Mix.Tasks.Hex.Outdated do
* `--all` - shows all outdated packages, including children of packages defined in `mix.exs`
* `--pre` - include pre-releases when checking for newer versions
* `--within-requirements` - exit with non-zero code only if requirements specified in `mix.exs` is met.
* `--sort <column>` - sort results by the given column. Currently supports `status`.
"""
@behaviour Hex.Mix.TaskDescription

@switches [all: :boolean, pre: :boolean, within_requirements: :boolean]
@switches [all: :boolean, pre: :boolean, within_requirements: :boolean, sort: :string]

@impl true
def run(args) do
Expand Down Expand Up @@ -175,7 +176,8 @@ defmodule Mix.Tasks.Hex.Outdated do
|> Enum.sort()
|> get_versions(deps, lock, opts[:pre])

values = Enum.map(versions, &format_all_row/1)
values = versions |> Enum.map(&format_all_row/1) |> maybe_sort_by(opts[:sort])
juhalehtonen marked this conversation as resolved.
Show resolved Hide resolved

diff_links = Enum.map(versions, &build_diff_link/1) |> Enum.reject(&is_nil/1)

if Enum.empty?(values) do
Expand Down Expand Up @@ -207,6 +209,20 @@ defmodule Mix.Tasks.Hex.Outdated do
end
end

defp maybe_sort_by(values, "status") do
status_order = %{
"Up-to-date" => 1,
"Update not possible" => 2,
"Update possible" => 3
}

Enum.sort_by(values, fn [_package, _lock, _latest, [_color, status]] ->
Map.fetch!(status_order, status)
end)
end

defp maybe_sort_by(values, _), do: values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have been defp maybe_sort_by(values, nil), in other words, we shouldn't silently ignore incorrect sort-by value. I fixed it on main: f4e76b6.

Thank you for the PR, this is a really nice feature!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks Wojtek!


defp get_versions(dep_names, deps, lock, pre?) do
Enum.flat_map(dep_names, fn name ->
case Hex.Utils.lock(lock[name]) do
Expand Down
67 changes: 67 additions & 0 deletions test/mix/tasks/hex.outdated_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,65 @@ defmodule Mix.Tasks.Hex.OutdatedTest do
end)
end

test "outdated --all --sort status" do
Mix.Project.push(OutdatedApp.MixProject)

in_tmp(fn ->
set_home_tmp()
Mix.Dep.Lock.write(%{bar: {:hex, :bar, "0.1.0"}, foo: {:hex, :foo, "0.1.0"}})

Mix.Task.run("deps.get")
flush()

assert catch_throw(Mix.Task.run("hex.outdated", ["--all", "--sort", "status"])) ==
{:exit_code, 1}

_bar =
[
[:bright, "bar", :reset],
[" ", "0.1.0", :reset],
[" ", :green, "0.1.0", :reset],
[" ", :green, "Up-to-date", :reset],
" "
]
|> IO.ANSI.format()
|> List.to_string()

_foo =
[
[:bright, "foo", :reset],
[" ", "0.1.0", :reset],
[" ", :red, "0.1.1", :reset],
[" ", :yellow, "Update possible", :reset],
" "
]
|> IO.ANSI.format()
|> List.to_string()

_ex_doc =
[
[:bright, "ex_doc", :reset],
[" ", "0.0.1", :reset],
[" ", :red, "0.1.0", :reset],
[" ", :red, "Update not possible", :reset],
" "
]
|> IO.ANSI.format()
|> List.to_string()

lines = flush()
extracted_statuses = extract_statuses(lines)

assert extracted_statuses == [
"Up-to-date",
"Update not possible",
"Update not possible",
"Update not possible",
"Update possible"
]
end)
end

test "outdated --all with multiple dependent packages" do
Mix.Project.push(OutdatedMultiDeps.MixProject)

Expand Down Expand Up @@ -509,4 +568,12 @@ defmodule Mix.Tasks.Hex.OutdatedTest do
end)
end)
end

defp extract_statuses(lines) do
Enum.flat_map(lines, fn {_, _, [line]} ->
~r/Up-to-date|Update not possible|Update possible/
|> Regex.scan(line)
|> List.flatten()
end)
end
end
Loading