|
| 1 | +defmodule Desktop.Toolchain do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + alias Desktop.ToolVersions |
| 5 | + |
| 6 | + @doc """ |
| 7 | + Verifies that running OTP major and Elixir version match entries parsed from `.tool-versions`. |
| 8 | +
|
| 9 | + `requirements` is the map returned by `Desktop.ToolVersions.parse/1`. |
| 10 | +
|
| 11 | + Optional `otp_release` and `elixir_version` override `System` values (for testing). |
| 12 | +
|
| 13 | + Returns `:ok` or `{:error, message}` where `message` is human-readable. |
| 14 | + """ |
| 15 | + @spec verify(map(), keyword()) :: :ok | {:error, String.t()} |
| 16 | + def verify(requirements, opts \\ []) when is_map(requirements) do |
| 17 | + otp_actual = Keyword.get_lazy(opts, :otp_release, fn -> System.otp_release() end) |
| 18 | + elixir_actual = Keyword.get_lazy(opts, :elixir_version, fn -> System.version() end) |
| 19 | + |
| 20 | + errors = |
| 21 | + [] |
| 22 | + |> maybe_check_erlang(Map.get(requirements, :erlang), otp_actual) |
| 23 | + |> maybe_check_elixir(Map.get(requirements, :elixir), elixir_actual) |
| 24 | + |
| 25 | + case errors do |
| 26 | + [] -> :ok |
| 27 | + msgs -> {:error, Enum.join(msgs, "\n")} |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + defp maybe_check_erlang(acc, nil, _otp_actual), do: acc |
| 32 | + |
| 33 | + defp maybe_check_erlang(acc, raw, otp_actual) when is_binary(raw) do |
| 34 | + expected_major = erlang_major(raw) |
| 35 | + |
| 36 | + case Integer.parse(to_string(otp_actual)) do |
| 37 | + {actual_major, _} when actual_major == expected_major -> |
| 38 | + acc |
| 39 | + |
| 40 | + {actual_major, _} -> |
| 41 | + [ |
| 42 | + "Erlang/OTP major mismatch: running OTP #{actual_major}, `.tool-versions` expects OTP #{expected_major} (from erlang #{inspect(raw)})." |
| 43 | + | acc |
| 44 | + ] |
| 45 | + |
| 46 | + :error -> |
| 47 | + ["Could not parse running OTP release #{inspect(otp_actual)}." | acc] |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + defp maybe_check_elixir(acc, nil, _elixir_actual), do: acc |
| 52 | + |
| 53 | + defp maybe_check_elixir(acc, raw, elixir_actual) when is_binary(raw) do |
| 54 | + expected_base = elixir_base_version(raw) |
| 55 | + |
| 56 | + case Version.parse(expected_base) do |
| 57 | + {:ok, expected_ver} -> |
| 58 | + case Version.parse(elixir_actual) do |
| 59 | + {:ok, actual_ver} -> |
| 60 | + if Version.compare(actual_ver, expected_ver) == :eq do |
| 61 | + acc |
| 62 | + else |
| 63 | + [ |
| 64 | + "Elixir version mismatch: running #{elixir_actual}, `.tool-versions` expects #{expected_base} (from elixir #{inspect(raw)})." |
| 65 | + | acc |
| 66 | + ] |
| 67 | + end |
| 68 | + |
| 69 | + :error -> |
| 70 | + ["Could not parse running Elixir version #{inspect(elixir_actual)}." | acc] |
| 71 | + end |
| 72 | + |
| 73 | + :error -> |
| 74 | + ["Could not parse Elixir version in `.tool-versions`: #{inspect(raw)}." | acc] |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + @doc false |
| 79 | + def erlang_major(raw) when is_binary(raw) do |
| 80 | + raw |
| 81 | + |> String.split() |
| 82 | + |> hd() |
| 83 | + |> String.split(".") |
| 84 | + |> hd() |
| 85 | + |> String.to_integer() |
| 86 | + end |
| 87 | + |
| 88 | + @doc false |
| 89 | + def elixir_base_version(raw) when is_binary(raw) do |
| 90 | + case Regex.run(~r/^(\d+\.\d+\.\d+)/, raw) do |
| 91 | + [_, base] -> |
| 92 | + base |
| 93 | + |
| 94 | + nil -> |
| 95 | + raw |
| 96 | + |> String.split("-") |
| 97 | + |> hd() |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + @doc """ |
| 102 | + Loads `.tool-versions` from `path`, parses it, and verifies the toolchain. |
| 103 | + """ |
| 104 | + @spec verify_file(String.t(), keyword()) :: :ok | {:error, String.t()} |
| 105 | + def verify_file(path, opts \\ []) do |
| 106 | + case File.read(path) do |
| 107 | + {:ok, content} -> |
| 108 | + content |> ToolVersions.parse() |> verify(opts) |
| 109 | + |
| 110 | + {:error, reason} -> |
| 111 | + {:error, "Could not read #{inspect(path)}: #{inspect(reason)}"} |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments