|
| 1 | +defmodule ElixirLS.Shell.Quiet do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + @behaviour Mix.Shell |
| 5 | + |
| 6 | + @impl true |
| 7 | + def print_app() do |
| 8 | + if name = Mix.Shell.printable_app_name() do |
| 9 | + IO.puts(:stderr, "==> #{name}") |
| 10 | + end |
| 11 | + |
| 12 | + :ok |
| 13 | + end |
| 14 | + |
| 15 | + @impl true |
| 16 | + def info(message) do |
| 17 | + print_app() |
| 18 | + IO.puts(:stderr, IO.ANSI.format(message)) |
| 19 | + end |
| 20 | + |
| 21 | + @impl true |
| 22 | + def error(message) do |
| 23 | + print_app() |
| 24 | + IO.puts(:stderr, IO.ANSI.format(message)) |
| 25 | + end |
| 26 | + |
| 27 | + @impl true |
| 28 | + def prompt(message) do |
| 29 | + print_app() |
| 30 | + IO.puts(:stderr, IO.ANSI.format(message)) |
| 31 | + raise "Mix.Shell.prompt is not supported at this time" |
| 32 | + end |
| 33 | + |
| 34 | + @impl true |
| 35 | + def yes?(message, options \\ []) do |
| 36 | + default = Keyword.get(options, :default, :yes) |
| 37 | + |
| 38 | + unless default in [:yes, :no] do |
| 39 | + raise ArgumentError, |
| 40 | + "expected :default to be either :yes or :no, got: #{inspect(default)}" |
| 41 | + end |
| 42 | + |
| 43 | + IO.puts(:stderr, IO.ANSI.format(message)) |
| 44 | + |
| 45 | + default == :yes |
| 46 | + end |
| 47 | + |
| 48 | + @impl true |
| 49 | + def cmd(command, opts \\ []) do |
| 50 | + print_app? = Keyword.get(opts, :print_app, true) |
| 51 | + |
| 52 | + Mix.Shell.cmd(command, opts, fn data -> |
| 53 | + if print_app?, do: print_app() |
| 54 | + IO.write(:stderr, data) |
| 55 | + end) |
| 56 | + end |
| 57 | +end |
| 58 | + |
| 59 | +defmodule ElixirLS.Installer do |
| 60 | + defp local_dir, do: Path.expand("#{__DIR__}/..") |
| 61 | + |
| 62 | + defp run_mix_install({:local, dir}, force?) do |
| 63 | + Mix.install( |
| 64 | + [ |
| 65 | + {:elixir_ls, path: dir}, |
| 66 | + ], |
| 67 | + force: force?, |
| 68 | + consolidate_protocols: false, |
| 69 | + config_path: Path.join(dir, "config/config.exs"), |
| 70 | + lockfile: Path.join(dir, "mix.lock") |
| 71 | + ) |
| 72 | + end |
| 73 | + |
| 74 | + defp run_mix_install({:tag, tag}, force?) do |
| 75 | + Mix.install([ |
| 76 | + {:elixir_ls, github: "elixir-lsp/elixir-ls", tag: tag} |
| 77 | + ], |
| 78 | + force: force?, |
| 79 | + consolidate_protocols: false |
| 80 | + ) |
| 81 | + end |
| 82 | + |
| 83 | + defp local? do |
| 84 | + System.get_env("ELS_LOCAL") == "1" |
| 85 | + end |
| 86 | + |
| 87 | + defp get_release do |
| 88 | + version = Path.expand("#{__DIR__}/VERSION") |
| 89 | + |> File.read!() |
| 90 | + |> String.trim() |
| 91 | + {:tag, "v#{version}"} |
| 92 | + end |
| 93 | + |
| 94 | + def install(force?) do |
| 95 | + if local?() do |
| 96 | + dir = local_dir() |
| 97 | + IO.puts(:stderr, "Installing local ElixirLS from #{dir}") |
| 98 | + IO.puts(:stderr, "Running in #{File.cwd!}") |
| 99 | + |
| 100 | + run_mix_install({:local, dir}, force?) |
| 101 | + else |
| 102 | + {:tag, tag} = get_release() |
| 103 | + IO.puts(:stderr, "Installing ElixirLS release #{tag}") |
| 104 | + IO.puts(:stderr, "Running in #{File.cwd!}") |
| 105 | + |
| 106 | + run_mix_install({:tag, tag}, force?) |
| 107 | + end |
| 108 | + IO.puts(:stderr, "Install complete") |
| 109 | + end |
| 110 | + |
| 111 | + def install_for_launch do |
| 112 | + if local?() do |
| 113 | + dir = Path.expand("#{__DIR__}/..") |
| 114 | + run_mix_install({:local, dir}, false) |
| 115 | + else |
| 116 | + run_mix_install(get_release(), false) |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + def install_with_retry do |
| 121 | + try do |
| 122 | + install(false) |
| 123 | + catch |
| 124 | + kind, error -> |
| 125 | + IO.puts(:stderr, "Mix.install failed with #{Exception.format(kind, error, __STACKTRACE__)}") |
| 126 | + IO.puts(:stderr, "Retrying Mix.install with force: true") |
| 127 | + install(true) |
| 128 | + end |
| 129 | + end |
| 130 | +end |
0 commit comments