Skip to content

Commit

Permalink
feat: should download latest release on install (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdevries authored Aug 11, 2023
1 parent 6138d65 commit d8ddfc2
Show file tree
Hide file tree
Showing 19 changed files with 441 additions and 125 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ jobs:
- name: Run build scripts
run: |
nvim -l build/init.lua
nvim -u scripts/init.lua -l build/init.lua
- name: Run tests
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ data.db
data.db*

.luarc.json
dist/
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ You can check that you're logged in by then running `:checkhealth sg`
Requires:

- nvim 0.9 or nvim nightly
- Cargo to build (pre-built binary/dylibs options to come)
- Node.js >= 18.17.0 (LTS) at runtime for [`cody-agent.js` - sourcegraph](./dist/cody-agent.js)
- Node.js >= 18.17.0 (LTS) at runtime for [`cody-agent.js`](github.com/sourcegraph/cody)

(By default, sg.nvim downloads released binaries from Github. If you prefer to build the plugin yourself, you'll need `cargo` to build)


### Install

Expand Down
96 changes: 58 additions & 38 deletions build/init.lua
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"
29 changes: 19 additions & 10 deletions doc/sg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ sg.config *sg.config*


Configuration Options: ~
{download_binaries} (boolean) Default true, download
latest release from
Github
{node_executable} (string) path to node executable
{cody_agent} (string) path to the cody-agent
js bundle
Expand Down Expand Up @@ -200,16 +203,6 @@ COMMANDS *sg.commands*

Default commands for interacting with Sourcegraph

*:SourcegraphLink*
:SourcegraphLink ~
Get a sourcegraph link to the current repo + file + line. Automatically
adds it to your '+' register

*:SourcegraphSearch*
:SourcegraphSearch ~
Run a search. For more sourcegraph search syntax, refer to online
documentation

*:SourcegraphLogin*
:SourcegraphLogin ~
Get prompted for endpoint and access_token if you don't want to set them
Expand All @@ -220,6 +213,22 @@ Default commands for interacting with Sourcegraph
Rebuild the Sourcegraph crates and required dependencies (in case build
failed during installation)

*:SourcegraphDownloadBinaries*
:SourcegraphDownloadBinaries ~
(Re-)Download the sourcegraph binaries. This should happen during
installation but you can force redownloading the binaries this way to
ensure that sg.nvim is properly installed.

*:SourcegraphLink*
:SourcegraphLink ~
Get a sourcegraph link to the current repo + file + line. Automatically
adds it to your '+' register

*:SourcegraphSearch*
:SourcegraphSearch ~
Run a search. For more sourcegraph search syntax, refer to online
documentation




Expand Down
18 changes: 16 additions & 2 deletions lua/sg/auth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ M.get = function(ordering)
return nil, nil
end

M.get_all_valid = function(ordering)
ordering = ordering or config.auth_strategy

local results = {}
for _, order in ipairs(ordering) do
local res = M.get { order }
if res then
table.insert(results, { res, order })
end
end

return results
end

M.valid = function()
return M.get() ~= nil
end
Expand All @@ -116,8 +130,8 @@ M.set_nvim_auth = function(opts)
assert(opts.endpoint, "[sg-cody] Nvim auth must have an endpoint")

local cody_data = data.get_cody_data()
cody_data.token = data.token
cody_data.endpoint = data.endpoint
cody_data.token = opts.token
cody_data.endpoint = opts.endpoint
data.write_cody_data(cody_data)
end

Expand Down
100 changes: 100 additions & 0 deletions lua/sg/build.lua
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
2 changes: 2 additions & 0 deletions lua/sg/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
local types = require "sg.types"

---@class sg.config
---@field download_binaries boolean: Default true, download latest release from Github
---@field node_executable string: path to node executable
---@field cody_agent string: path to the cody-agent js bundle
---@field did_change_debounce number: Number of ms to debounce changes
Expand All @@ -25,6 +26,7 @@ local types = require "sg.types"
---@type sg.config
local config = {}

config.download_binaries = true
config.node_executable = "node"
config.cody_agent = vim.api.nvim_get_runtime_file("dist/cody-agent.js", false)[1]

Expand Down
Loading

0 comments on commit d8ddfc2

Please sign in to comment.