Created
March 29, 2018 22:40
-
-
Save jwietelmann/96f46f8d74091a69be75d1e9baf0fd32 to your computer and use it in GitHub Desktop.
A mix task to list all your dependencies' licenses.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Mix.Tasks.Deps.Licenses do | |
alias Mix.Dep | |
use Mix.Task | |
@shell Mix.shell() | |
def run(_) do | |
Mix.Project.get!() | |
{with_licenses, without_licenses} = | |
[] | |
|> Mix.Dep.loaded() | |
|> Enum.map(&add_licenses/1) | |
|> Enum.split_with(&has_license?/1) | |
@shell.info("Found licenses for #{length(with_licenses)} packages:\n") | |
print_all(with_licenses) | |
case without_licenses do | |
[] -> | |
nil | |
_ -> | |
@shell.info("\n#{length(without_licenses)} packages have unknown licenses:\n") | |
print_all(without_licenses) | |
end | |
end | |
defp add_licenses(%Dep{} = dep) do | |
{dep, get_licenses(dep)} | |
end | |
defp has_license?({_, nil}), do: false | |
defp has_license?({_, []}), do: false | |
defp has_license?({_, _}), do: true | |
defp get_licenses(%Dep{} = dep) do | |
Dep.in_dependency(dep, fn _ -> | |
get_in(Mix.Project.config(), [:package, :licenses]) | |
end) | |
end | |
defp print_all(items), do: Enum.each(items, &print_item/1) | |
defp print_item(item), do: item |> format() |> @shell.info() | |
defp format({%Dep{} = dep, licenses}) do | |
"* #{Dep.format_dep(dep)}#{format_licenses(licenses)}" | |
end | |
defp format_licenses(nil), do: "" | |
defp format_licenses(licenses), do: ": #{Enum.join(licenses, ", ")}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment