feat(crates): prebuilt binaries (#473)

* feat(crates): prebuilt binaries

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>

* chore: update name

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>

* chore: build on PR

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>

* chore: only build for lua51 and luajit

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>

* feat: build stuff

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>

* chore: only build if changes in Rust

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>

* fix: remove deadcode

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>

---------

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
Aaron Pham
2024-09-03 06:20:53 -04:00
committed by GitHub
parent 9765eaca21
commit a5726bd2bf
8 changed files with 223 additions and 62 deletions

View File

@@ -10,7 +10,7 @@ local Utils = require("avante.utils")
---@field ask fun(question:string?): boolean
---@field edit fun(question:string?): nil
---@field refresh fun(): nil
---@field build fun(): boolean
---@field build fun(opts: {source: boolean}): boolean
---@field switch_provider fun(target: string): nil
---@field toggle avante.ApiToggle
---@field get_suggestion fun(): avante.Suggestion | nil
@@ -20,6 +20,57 @@ local M = {}
---@param target Provider
M.switch_provider = function(target) require("avante.providers").refresh(target) end
---@param path string
local function to_windows_path(path)
local winpath = path:gsub("/", "\\")
if winpath:match("^%a:") then winpath = winpath:sub(1, 2):upper() .. winpath:sub(3) end
winpath = winpath:gsub("\\$", "")
return winpath
end
---@param opts {source: boolean}
M.build = function(opts)
local dirname = Utils.trim(string.sub(debug.getinfo(1).source, 2, #"/init.lua" * -1), { suffix = "/" })
local git_root = vim.fs.find(".git", { path = dirname, upward = true })[1]
local build_directory = git_root and vim.fn.fnamemodify(git_root, ":h") or (dirname .. "/../../")
if opts.source and not vim.fn.executable("cargo") then
error("Building avante.nvim requires cargo to be installed.", 2)
end
---@type string[]
local cmd
local os_name = Utils.get_os_name()
if vim.tbl_contains({ "linux", "darwin" }, os_name) then
cmd = {
"sh",
"-c",
string.format("make BUILD_FROM_SOURCE=%s -C %s", opts.source == true and "true" or "false", build_directory),
}
elseif os_name == "windows" then
build_directory = to_windows_path(build_directory)
cmd = {
"powershell",
"-ExecutionPolicy",
"Bypass",
"-File",
string.format("%s\\Build.ps1", build_directory),
"-WorkingDirectory",
build_directory,
}
else
error("Unsupported operating system: " .. os_name, 2)
end
local job = vim.system(cmd, { text = true }):wait()
return vim.tbl_contains({ 0 }, job.code) and true or false
end
---@param question? string
M.ask = function(question)
if not require("avante").toggle() then return false end

View File

@@ -44,7 +44,20 @@ H.commands = function()
{ desc = "avante: edit selected block", nargs = "*" }
)
cmd("Refresh", function() require("avante.api").refresh() end, { desc = "avante: refresh windows" })
cmd("Build", function() M.build() end, { desc = "avante: build dependencies" })
cmd("Build", function(opts)
local args = {}
for _, arg in ipairs(opts.fargs) do
local key, value = arg:match("(%w+)=(%w+)")
if key and value then args[key] = value == "true" end
end
if args.source == nil then args.source = Config.debug and true or false end
require("avante.api").build(args)
end, {
desc = "avante: build dependencies",
nargs = "*",
complete = function(_, _, _) return { "source=true", "source=false" } end,
})
cmd("SwitchProvider", function(opts) require("avante.api").switch_provider(vim.trim(opts.args or "")) end, {
nargs = 1,
desc = "avante: switch provider",
@@ -269,50 +282,6 @@ setmetatable(M.toggle, {
end,
})
---@param path string
local function to_windows_path(path)
local winpath = path:gsub("/", "\\")
if winpath:match("^%a:") then winpath = winpath:sub(1, 2):upper() .. winpath:sub(3) end
winpath = winpath:gsub("\\$", "")
return winpath
end
M.build = H.api(function()
local dirname = Utils.trim(string.sub(debug.getinfo(1).source, 2, #"/init.lua" * -1), { suffix = "/" })
local git_root = vim.fs.find(".git", { path = dirname, upward = true })[1]
local build_directory = git_root and vim.fn.fnamemodify(git_root, ":h") or (dirname .. "/../../")
if not vim.fn.executable("cargo") then error("Building avante.nvim requires cargo to be installed.", 2) end
---@type string[]
local cmd
local os_name = Utils.get_os_name()
if vim.tbl_contains({ "linux", "darwin" }, os_name) then
cmd = { "sh", "-c", string.format("make -C %s", build_directory) }
elseif os_name == "windows" then
build_directory = to_windows_path(build_directory)
cmd = {
"powershell",
"-ExecutionPolicy",
"Bypass",
"-File",
string.format("%s\\Build.ps1", build_directory),
"-WorkingDirectory",
build_directory,
}
else
error("Unsupported operating system: " .. os_name, 2)
end
local job = vim.system(cmd, { text = true }):wait()
return vim.tbl_contains({ 0 }, job.code) and true or false
end)
---@param opts? avante.Config
function M.setup(opts)
---PERF: we can still allow running require("avante").setup() multiple times to override config if users wish to