-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #170
- Loading branch information
Showing
7 changed files
with
285 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
defmodule NextLS.Updater do | ||
@moduledoc false | ||
use Task | ||
|
||
def start_link(arg \\ []) do | ||
Task.start_link(__MODULE__, :run, [arg]) | ||
end | ||
|
||
def run(opts) do | ||
Logger.put_module_level(Req.Steps, :none) | ||
|
||
binpath = Keyword.get(opts, :binpath, Path.expand("~/.cache/elixir-tools/nextls/bin/nextls")) | ||
api_host = Keyword.get(opts, :api_host, "https://api.github.com") | ||
github_host = Keyword.get(opts, :github_host, "https://github.com") | ||
logger = Keyword.fetch!(opts, :logger) | ||
current_version = Keyword.fetch!(opts, :current_version) | ||
retry = Keyword.get(opts, :retry, :safe) | ||
|
||
case Req.get("/repos/elixir-tools/next-ls/releases/latest", base_url: api_host, retry: retry) do | ||
{:ok, %{body: %{"tag_name" => "v" <> version = tag}}} -> | ||
with {:ok, latest_version} <- Version.parse(version), | ||
:gt <- Version.compare(latest_version, current_version) do | ||
with :ok <- File.rename(binpath, binpath <> "-#{Version.to_string(current_version)}"), | ||
{:ok, _} <- | ||
File.open(binpath, [:write], fn file -> | ||
fun = fn request, finch_request, finch_name, finch_options -> | ||
fun = fn | ||
{:status, status}, response -> | ||
%{response | status: status} | ||
|
||
{:headers, headers}, response -> | ||
%{response | headers: headers} | ||
|
||
{:data, data}, response -> | ||
IO.binwrite(file, data) | ||
response | ||
end | ||
|
||
case Finch.stream(finch_request, finch_name, Req.Response.new(), fun, finch_options) do | ||
{:ok, response} -> {request, response} | ||
{:error, exception} -> {request, exception} | ||
end | ||
end | ||
|
||
with {:error, error} <- | ||
Req.get("/elixir-tools/next-ls/releases/download/#{tag}/next_ls_#{os()}_#{arch()}", | ||
finch_request: fun, | ||
base_url: github_host, | ||
retry: retry | ||
) do | ||
NextLS.Logger.show_message(logger, :error, "Failed to download version #{version} of Next LS!") | ||
NextLS.Logger.error(logger, "Failed to download Next LS: #{inspect(error)}") | ||
:error | ||
end | ||
end) do | ||
File.chmod(binpath, 0o755) | ||
|
||
NextLS.Logger.show_message( | ||
logger, | ||
:info, | ||
"[Next LS] Downloaded v#{version}, please restart your editor for it to take effect." | ||
) | ||
|
||
NextLS.Logger.info(logger, "Downloaded #{version} of Next LS") | ||
end | ||
end | ||
|
||
{:error, error} -> | ||
NextLS.Logger.error( | ||
logger, | ||
"Failed to retrieve the latest version number of Next LS from the GitHub API: #{inspect(error)}" | ||
) | ||
end | ||
end | ||
|
||
defp arch do | ||
arch_str = :erlang.system_info(:system_architecture) | ||
[arch | _] = arch_str |> List.to_string() |> String.split("-") | ||
|
||
case {:os.type(), arch, :erlang.system_info(:wordsize) * 8} do | ||
{{:win32, _}, _arch, 64} -> :amd64 | ||
{_os, arch, 64} when arch in ~w(arm aarch64) -> :arm64 | ||
{_os, arch, 64} when arch in ~w(amd64 x86_64) -> :amd64 | ||
{os, arch, _wordsize} -> raise "Unsupported system: os=#{inspect(os)}, arch=#{inspect(arch)}" | ||
end | ||
end | ||
|
||
defp os do | ||
case :os.type() do | ||
{:win32, _} -> :windows | ||
{:unix, :darwin} -> :darwin | ||
{:unix, :linux} -> :linux | ||
unknown_os -> raise "Unsupported system: os=#{inspect(unknown_os)}}" | ||
end | ||
end | ||
end |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
defmodule NextLS.UpdaterTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias NextLS.Updater | ||
|
||
@moduletag :tmp_dir | ||
|
||
setup do | ||
me = self() | ||
|
||
{:ok, logger} = | ||
Task.start_link(fn -> | ||
recv = fn recv -> | ||
receive do | ||
{:"$gen_cast", msg} -> | ||
# dbg(msg) | ||
send(me, msg) | ||
end | ||
|
||
recv.(recv) | ||
end | ||
|
||
recv.(recv) | ||
end) | ||
|
||
[logger: logger] | ||
end | ||
|
||
test "downloads the exe", %{tmp_dir: tmp_dir, logger: logger} do | ||
api = Bypass.open(port: 8000) | ||
github = Bypass.open(port: 8001) | ||
|
||
Bypass.expect(api, "GET", "/repos/elixir-tools/next-ls/releases/latest", fn conn -> | ||
conn | ||
|> Plug.Conn.put_resp_header("content-type", "application/json") | ||
|> Plug.Conn.resp(200, Jason.encode!(%{tag_name: "v1.0.0"})) | ||
end) | ||
|
||
exe = String.duplicate("time to hack\n", 1000) | ||
|
||
Bypass.expect(github, fn conn -> | ||
assert "GET" == conn.method | ||
assert "/elixir-tools/next-ls/releases/download/v1.0.0/next_ls_" <> rest = conn.request_path | ||
|
||
assert rest in [ | ||
"darwin_arm64", | ||
"darwin_amd64", | ||
"linux_arm64", | ||
"linux_amd64", | ||
"windows_amd64" | ||
] | ||
|
||
Plug.Conn.resp(conn, 200, exe) | ||
end) | ||
|
||
binpath = Path.join(tmp_dir, "nextls") | ||
File.write(binpath, "yoyoyo") | ||
|
||
Updater.run( | ||
current_version: Version.parse!("0.9.0"), | ||
binpath: binpath, | ||
api_host: "http://localhost:8000", | ||
github_host: "http://localhost:8001", | ||
logger: logger | ||
) | ||
|
||
assert File.read!(binpath) == exe | ||
assert File.stat!(binpath).mode == 33_261 | ||
assert File.exists?(binpath <> "-0.9.0") | ||
end | ||
|
||
test "doesn't download when the version is at the latest", %{tmp_dir: tmp_dir, logger: logger} do | ||
api = Bypass.open(port: 8000) | ||
|
||
Bypass.expect(api, "GET", "/repos/elixir-tools/next-ls/releases/latest", fn conn -> | ||
conn | ||
|> Plug.Conn.put_resp_header("content-type", "application/json") | ||
|> Plug.Conn.resp(200, Jason.encode!(%{tag_name: "v1.0.0"})) | ||
end) | ||
|
||
binpath = Path.join(tmp_dir, "nextls") | ||
|
||
Updater.run( | ||
current_version: Version.parse!("1.0.0"), | ||
binpath: binpath, | ||
api_host: "http://localhost:8000", | ||
github_host: "http://localhost:8001", | ||
logger: logger | ||
) | ||
|
||
refute File.exists?(binpath) | ||
end | ||
|
||
test "logs that it failed when api call fails", %{tmp_dir: tmp_dir, logger: logger} do | ||
binpath = Path.join(tmp_dir, "nextls") | ||
File.write(binpath, "yoyoyo") | ||
|
||
Updater.run( | ||
current_version: Version.parse!("1.0.0"), | ||
binpath: binpath, | ||
api_host: "http://localhost:8000", | ||
github_host: "http://localhost:8001", | ||
logger: logger, | ||
retry: false | ||
) | ||
|
||
assert_receive {:log, :error, "Failed to retrieve the latest version number of Next LS from the GitHub API: " <> _} | ||
end | ||
|
||
test "logs that it failed when download fails", %{tmp_dir: tmp_dir, logger: logger} do | ||
api = Bypass.open(port: 8000) | ||
|
||
Bypass.expect(api, "GET", "/repos/elixir-tools/next-ls/releases/latest", fn conn -> | ||
conn | ||
|> Plug.Conn.put_resp_header("content-type", "application/json") | ||
|> Plug.Conn.resp(200, Jason.encode!(%{tag_name: "v1.0.0"})) | ||
end) | ||
|
||
binpath = Path.join(tmp_dir, "nextls") | ||
File.write(binpath, "yoyoyo") | ||
|
||
Updater.run( | ||
current_version: Version.parse!("0.9.0"), | ||
binpath: binpath, | ||
api_host: "http://localhost:8000", | ||
github_host: "http://localhost:8001", | ||
logger: logger, | ||
retry: false | ||
) | ||
|
||
assert_receive {:show_message, :error, "Failed to download version 1.0.0 of Next LS!"} | ||
assert_receive {:log, :error, "Failed to download Next LS: " <> _} | ||
end | ||
end |