summaryrefslogtreecommitdiff
path: root/lib/mix/lib/mix/hex.ex
blob: d43c60f52c4d7ec6c9044c057d99bcd216b13e82 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
defmodule Mix.Hex do
  @moduledoc false
  @compile {:no_warn_undefined, Hex}
  @hex_requirement ">= 0.19.0"
  @hex_mirror "https://repo.hex.pm"

  @doc """
  Returns `true` if `Hex` is loaded or installed.

  Otherwise returns `false`.
  """
  @spec ensure_installed?(boolean) :: boolean
  def ensure_installed?(force?) do
    cond do
      Code.ensure_loaded?(Hex) -> true
      force? or install_hex?() -> Mix.Tasks.Local.Hex.run(["--force"])
      true -> false
    end
  end

  defp install_hex? do
    shell = Mix.shell()
    shell.info("Mix requires the Hex package manager to fetch dependencies")

    shell.yes?(
      "Shall I install Hex? (if running non-interactively, use \"mix local.hex --force\")"
    )
  end

  @doc """
  Returns `true` if it has the required `Hex`. If an update is performed, it then exits.
  Otherwise returns `false` without updating anything.
  """
  @spec ensure_updated?() :: boolean
  def ensure_updated?() do
    cond do
      not Code.ensure_loaded?(Hex) ->
        false

      not Version.match?(Hex.version(), @hex_requirement) ->
        Mix.shell().info("Mix requires Hex #{@hex_requirement} but you have #{Hex.version()}")

        if Mix.shell().yes?("Shall I abort the current command and update Hex?") do
          Mix.Tasks.Local.Hex.run(["--force"])
          exit({:shutdown, 0})
        end

        false

      true ->
        true
    end
  end

  @doc """
  Ensures `Hex` is started.
  """
  def start do
    try do
      Hex.start()
    catch
      kind, reason ->
        Mix.shell().error(
          "Could not start Hex. Try fetching a new version with " <>
            "\"mix local.hex\" or uninstalling it with \"mix archive.uninstall hex.ez\""
        )

        :erlang.raise(kind, reason, __STACKTRACE__)
    end
  end

  @doc """
  Returns the URL to the Hex mirror.
  """
  def mirror do
    System.get_env("HEX_MIRROR") || @hex_mirror
  end
end