Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 22 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,22 @@
# Blink Download (blink.download)

Neovim libary for downloading pre-built binaries for Rust based plugins. For a quick start, see the [neovim-lua-rust-template](https://github.com/Saghen/neovim-lua-rust-template).

## Usage

Add the following at the top level of your plugin:

```lua
local my_plugin = {}

function my_plugin.setup()
-- get the root directory of the plugin, by getting the relative path to this file
-- for example, if this file is in `/lua/my_plugin/init.lua`, use `../../`
local root_dir = vim.fn.resolve(debug.getinfo(1).source:match('@?(.*/)') .. '../../')

require('blink.download').ensure_downloaded({
-- omit this property to disable downloading
-- i.e. https://github.com/Saghen/blink.delimiters/releases/download/v0.1.0/x86_64-unknown-linux-gnu.so
download_url = function(version, system_triple, extension)
return 'https://github.com/saghen/blink.delimiters/releases/download/' .. version .. '/' .. system_triple .. extension
end,

root_dir,
output_dir = '/target/release',
binary_name = 'blink_delimiters' -- excluding `lib` prefix
}, function(err)
if err then error(err) end

local rust_module = require('blink_delimiters')
end)
end
```


Add the following to your `build.rs`. This deletes the `version` file created by the downloader, such that the downloader will accept the binary as-is.

```rust
fn main() {
// delete existing version file created by downloader
let _ = std::fs::remove_file("target/release/version");
}
```
<p align="center">
<h2 align="center">Blink Lib (blink.lib)</h2>
</p>

> [!WARNING]
> Not ready for use

**blink.lib** provides generic utilities for all other blink plugins, aka all the code I don't want to copy between my plugins :)

## Roadmap

- [x] `blink.lib.task`: Async
- [x] `blink.lib.fs`: Filesystem APIs
- [ ] `blink.lib.config`: Config module with validation (merge `vim.g/vim.b/setup()`, `enable()`, `is_enabled()`)
- [ ] `blink.lib`: Utils (lazy_require, dedup, debounce, truncate, dedent, copy, slice, ...) with all other modules exported (lazily)
- [ ] `blink.lib.log`: Logging to file and/or notifications
- [ ] `blink.lib.download`: Binary downloader (e.g. downloading rust binaries)
- [ ] `blink.lib.build`: Build system (e.g. building rust binaries)
- [ ] `blink.lib.regex`: Regex
- [ ] `blink.lib.git`: Git APIs using FFI
- [ ] `blink.lib.http`: HTTP APIs using [`reqwest`](https://github.com/seanmonstar/reqwest)
- [ ] `blink.lib.lsp`: In-process LSP client wrapper
1 change: 1 addition & 0 deletions lua/blink/download.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require('blink.lib.download')
149 changes: 0 additions & 149 deletions lua/blink/download/files.lua

This file was deleted.

54 changes: 0 additions & 54 deletions lua/blink/download/init.lua

This file was deleted.

116 changes: 116 additions & 0 deletions lua/blink/lib/build/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
local async = require('blink.cmp.lib.async')
local utils = require('blink.cmp.lib.utils')
local log_file = require('blink.cmp.fuzzy.build.log')

--- @class blink.lib.build
local build = {}

--- Gets the path to the blink.cmp root directory (parent of lua/)
--- @return string
local function get_project_root()
local current_file = debug.getinfo(1, 'S').source:sub(2)
-- Go up from lua/blink.cmp/fuzzy/build/init.lua to the project root
return vim.fn.fnamemodify(current_file, ':p:h:h:h:h:h:h')
end

--- @param cmd string[]
--- @return blink.cmp.Task<vim.SystemCompleted>
local async_system = function(cmd, opts)
return async.task.new(function(resolve, reject)
local proc = vim.system(
cmd,
vim.tbl_extend('force', {
cwd = get_project_root(),
text = true,
}, opts or {}),
vim.schedule_wrap(function(out)
if out.code == 0 then
resolve(out)
else
reject(out)
end
end)
)

return function() return proc:kill('TERM') end
end)
end

--- Detects if cargo supports +nightly
--- @return blink.cmp.Task<boolean>
local function supports_rustup()
return async_system({ 'cargo', '+nightly', '--version' })
:map(function() return true end)
:catch(function() return false end)
end

--- Detect if cargo supports nightly
--- (defaulted to nightly in rustup or globally installed without rustup)
--- @return blink.cmp.Task<boolean>
local function supports_nightly()
return async_system({ 'cargo', '--version' })
:map(function(out) return out.stdout:match('nightly') end)
:catch(function() return false end)
end

local function get_cargo_cmd()
return async.task.all({ supports_rustup(), supports_nightly() }):map(function(results)
local rustup = results[1]
local nightly = results[2]

if rustup then return { 'cargo', '+nightly', 'build', '--release' } end
if nightly then return { 'cargo', 'build', '--release' } end

utils.notify({
{ 'Rust ' },
{ 'nightly', 'DiagnosticInfo' },
{ ' not available via ' },
{ 'cargo --version', 'DiagnosticInfo' },
{ ' and rustup not detected via ' },
{ 'cargo +nightly --version', 'DiagnosticInfo' },
{ '. Cannot build fuzzy matching library' },
}, vim.log.levels.ERROR)
end)
end

--- Builds the rust binary from source
--- @return blink.cmp.Task
function build.build()
utils.notify({ { 'Building fuzzy matching library from source...' } }, vim.log.levels.INFO)

local log = log_file.create()
log.write('Working Directory: ' .. get_project_root())

return get_cargo_cmd()
--- @param cmd string[]
:map(function(cmd)
log.write('Command: ' .. table.concat(cmd, ' ') .. '\n')
log.write('\n\n---\n\n')

return async_system(cmd, {
stdout = function(_, data) log.write(data or '') end,
stderr = function(_, data) log.write(data or '') end,
})
end)
:map(
function()
utils.notify({
{ 'Successfully built fuzzy matching library. ' },
{ ':BlinkCmp build-log', 'DiagnosticInfo' },
}, vim.log.levels.INFO)
end
)
:catch(
function()
utils.notify({
{ 'Failed to build fuzzy matching library! ', 'DiagnosticError' },
{ ':BlinkCmp build-log', 'DiagnosticInfo' },
}, vim.log.levels.ERROR)
end
)
:map(function() log.close() end)
end

function build.build_log() log_file.open() end

return build
Loading