Skip to content

Commit

Permalink
fix: updater
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Feb 28, 2024
1 parent 55e91ac commit 69db3b2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 39 deletions.
56 changes: 17 additions & 39 deletions lib/next_ls/updater.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,26 @@ defmodule NextLS.Updater do
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
{:error, error} <-
Req.get("/elixir-tools/next-ls/releases/download/#{tag}/next_ls_#{os()}_#{arch()}",
into: File.stream!(binpath),
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

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)
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.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
NextLS.Logger.info(logger, "Downloaded #{version} of Next LS")
end

{_, error} ->
Expand Down
1 change: 1 addition & 0 deletions test/next_ls/updater_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ defmodule NextLS.UpdaterTest do

assert File.read!(binpath) == exe
assert File.stat!(binpath).mode == 33_261
assert File.stat!(binpath).size > 10_000
assert File.exists?(binpath <> "-0.9.0")
end

Expand Down

0 comments on commit 69db3b2

Please sign in to comment.