-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: should download latest release on install (#68)
- Loading branch information
Showing
19 changed files
with
441 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ data.db | |
data.db* | ||
|
||
.luarc.json | ||
dist/ |
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 |
---|---|---|
@@ -1,51 +1,71 @@ | ||
-- TODO: Can switch to vim.system later | ||
local system = function(cmd, opts) | ||
local status = {} | ||
opts = opts or {} | ||
|
||
opts.on_stdout = function(_, data) | ||
if data then | ||
print(table.concat(data, "")) | ||
local sourced_filename = (function() | ||
return vim.fn.fnamemodify(vim.fs.normalize(debug.getinfo(2, "S").source:sub(2)), ":p") | ||
end)() | ||
|
||
-- Add sourcegraph plugin to runtimepath | ||
-- This let's us require "sg.config" and "sg.build" | ||
vim.opt.rtp:prepend(vim.fn.fnamemodify(sourced_filename, ":h:h")) | ||
|
||
local ok, config = pcall(require, "sg.config") | ||
if not ok then | ||
config = {} | ||
end | ||
|
||
-- This is the default path of downloading binaries | ||
if config.download_binaries or config.download_binaries == nil then | ||
return require("sg.build").download() | ||
else | ||
-- This is the path to build these manually. | ||
|
||
-- TODO: Can switch to vim.system later | ||
local system = function(cmd, opts) | ||
local status = {} | ||
opts = opts or {} | ||
|
||
opts.on_stdout = function(_, data) | ||
if data then | ||
print(table.concat(data, "")) | ||
end | ||
end | ||
end | ||
|
||
opts.on_stderr = function(_, data) | ||
if data then | ||
print(table.concat(data, "")) | ||
opts.on_stderr = function(_, data) | ||
if data then | ||
print(table.concat(data, "")) | ||
end | ||
end | ||
end | ||
|
||
opts.on_exit = function(_, code) | ||
if code ~= 0 then | ||
status.errored = true | ||
return | ||
opts.on_exit = function(_, code) | ||
if code ~= 0 then | ||
status.errored = true | ||
return | ||
end | ||
|
||
status.done = true | ||
print "" | ||
end | ||
|
||
status.done = true | ||
print "" | ||
vim.fn.jobstart(cmd, opts) | ||
return status | ||
end | ||
|
||
vim.fn.jobstart(cmd, opts) | ||
return status | ||
end | ||
print "=====================" | ||
print "installing sg.nvim..." | ||
print "=====================" | ||
|
||
print "=====================" | ||
print "installing sg.nvim..." | ||
print "=====================" | ||
-- Wait for up to ten minutes...? Idk, maybe that's too long | ||
-- or short haha. I don't know what build times are for other people | ||
local wait_for_status = function(status) | ||
vim.wait(10 * 60 * 1000, function() | ||
return status.done or status.errored | ||
end, 200) | ||
end | ||
|
||
-- Wait for up to ten minutes...? Idk, maybe that's too long | ||
-- or short haha. I don't know what build times are for other people | ||
local wait_for_status = function(status) | ||
vim.wait(10 * 60 * 1000, function() | ||
return status.done or status.errored | ||
end, 200) | ||
end | ||
local status_bins = system { "cargo", "build", "--bins" } | ||
wait_for_status(status_bins) | ||
|
||
local status_bins = system { "cargo", "build", "--bins" } | ||
wait_for_status(status_bins) | ||
if status_bins.errored then | ||
error "failed to build the binaries" | ||
end | ||
|
||
if status_bins.errored then | ||
error "failed to build the binaries" | ||
print "success\n" | ||
end | ||
|
||
print "success\n" |
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,100 @@ | ||
--[[ | ||
NOTE!! This file cannot depend on anything besides sg.* | ||
- RTP is not always set up during build time. So don't add anything other items. | ||
- Preferably, this only uses sg.utils | ||
--]] | ||
|
||
local sourced_filename = (function() | ||
return vim.fn.fnamemodify(vim.fs.normalize(debug.getinfo(2, "S").source:sub(2)), ":p") | ||
end)() | ||
|
||
local plugin_root = vim.fn.fnamemodify(sourced_filename, ":h:h:h") | ||
|
||
local utils = require "sg.utils" | ||
local system = utils.system | ||
local joinpath = utils.joinpath | ||
|
||
local os_uname = vim.loop.os_uname() | ||
local sysname = os_uname.sysname:lower() | ||
local machine = os_uname.machine | ||
|
||
local basename = (function() | ||
if sysname == "linux" then | ||
return "sg-x86_64-unknown-linux-gnu" | ||
end | ||
|
||
if sysname == "windows_nt" then | ||
return "sg-x86_64-pc-windows-msvc" | ||
end | ||
|
||
if sysname == "darwin" then | ||
if machine == "arm64" then | ||
return "sg-aarch64-apple-darwin" | ||
else | ||
return "sg-x86_64-apple-darwin" | ||
end | ||
end | ||
|
||
error "Must have a valid basename" | ||
end)() | ||
|
||
local fullname = (function() | ||
if sysname == "windows_nt" then | ||
return basename .. ".zip" | ||
end | ||
|
||
return basename .. ".tar.xz" | ||
end)() | ||
|
||
local link = "https://github.com/sourcegraph/sg.nvim/releases/latest/download/" .. fullname | ||
|
||
local M = {} | ||
|
||
local tarfile = joinpath(plugin_root, "dist", fullname) | ||
local move_to_dist = function(bin) | ||
return vim.loop.fs_rename(joinpath(plugin_root, "dist", basename, bin), joinpath(plugin_root, "dist", bin)) | ||
end | ||
|
||
M.download = function() | ||
-- TODO: Proper error handling here. | ||
-- Right now, nvim won't exit with a non-zero exit code | ||
-- if you run this with nvim -l build/init.lua | ||
-- because we don't force the error in the main thread. | ||
-- | ||
-- so we need to vim.wait for them. | ||
vim.notify "[sg] Starting to download binaries..." | ||
|
||
-- TODO: Windows | ||
-- Check that we have curl | ||
-- Check what to do to zip | ||
|
||
local curl = system({ "curl", link, "-L", "-o", tarfile }):wait() | ||
if curl.code ~= 0 then | ||
error("Failed to execute downloading release" .. vim.inspect(curl)) | ||
end | ||
print "[sg] Done downloading" | ||
|
||
local tar = system({ "tar", "-xvf", tarfile, "-C", joinpath(plugin_root, "dist/") }):wait() | ||
if tar.code ~= 0 then | ||
error("Failed to untar release" .. tar) | ||
end | ||
print "[sg] Done extracting" | ||
|
||
local lsp_rename = move_to_dist "sg-lsp" | ||
if not lsp_rename then | ||
error("Failed to rename sg-lsp: " .. vim.inspect(lsp_rename)) | ||
return | ||
end | ||
|
||
local agent_rename = move_to_dist "sg-nvim-agent" | ||
if not agent_rename then | ||
error("Failed to rename sg-nvim-agent" .. vim.inspect(agent_rename)) | ||
return | ||
end | ||
|
||
vim.notify "[sg] Download complete" | ||
end | ||
|
||
return M |
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
Oops, something went wrong.