Skip to content

Instantly share code, notes, and snippets.

@stefanluptak
Created June 24, 2021 19:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanluptak/533d3069b63ac89213e9d09dd1bd281a to your computer and use it in GitHub Desktop.
Save stefanluptak/533d3069b63ac89213e9d09dd1bd281a to your computer and use it in GitHub Desktop.
lib/mix/tasks/hex_latest.ex
defmodule Mix.Tasks.Hex.Latest do
@doc """
## Example usage:
`mix hex.latest jason`
Or you can copy the output with `pbcopy` like this:
`mix hex.latest jason | pbcopy`
"""
use Mix.Task
@shortdoc "Get the latest version of hex package"
@impl Mix.Task
def run([package]) do
with {output, 0} <- System.cmd("mix", ["hex.info", package], stderr_to_stdout: true),
[_all, match] <- Regex.run(~r/Config: ({.*})/, output) do
Mix.Shell.IO.info(match <> ",")
else
{error_message, error_code} when is_integer(error_code) ->
error_message
|> String.trim()
|> Mix.Shell.IO.error()
end
end
def run(_) do
Mix.raise("""
Invalid arguments. Expected:
mix hex.latest [PACKAGE]
""")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment