~cosmicrose/hex_licenses

ref: a4b469780dde0cab6811fb78a23195eb60cfc5d6 hex_licenses/lib/hex_licenses.ex -rw-r--r-- 1.4 KiB
a4b46978Rosa Richter Reduce complexity of lint function 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# SPDX-FileCopyrightText: 2021 Rosa Richter
#
# SPDX-License-Identifier: MIT

defmodule HexLicenses do
  @moduledoc """
  Documentation for `HexLicenses`.
  """

  alias HexLicenses.Hex

  def lint(package, license_list) do
    if is_nil(package) do
      {:error, :package_not_defined}
    else
      results =
        Map.new(package[:licenses], fn license ->
          {license, license_status(license, license_list)}
        end)

      {:ok, results}
    end
  end

  def license_check(spdx_licenses) when is_map(spdx_licenses) do
    app_deps()
    |> Task.async_stream(fn dep ->
      licenses = Hex.license_for_package(to_string(dep))
      {dep, licenses}
    end)
    |> Stream.map(fn {:ok, val} -> val end)
    |> Enum.map(fn
      {dep, {:ok, licenses}} ->
        license_statuses =
          Map.new(licenses, fn license ->
            {license, license_status(license, spdx_licenses)}
          end)

        {dep, license_statuses}

      {dep, {:error, :enoent}} ->
        {dep, :not_in_hex}
    end)
    |> Map.new()
  end

  def license_status(license, license_map) when is_map(license_map) do
    cond do
      not Map.has_key?(license_map, license) -> :not_recognized
      license_map[license].deprecated? -> :deprecated
      license_map[license].osi_approved? -> :osi_approved
      true -> :not_approved
    end
  end

  defp app_deps do
    Mix.Project.get!().project()[:deps] |> Enum.map(&elem(&1, 0))
  end
end