From daf276de9861001a39eb8f35bebd1d683f7a6933 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 5 Feb 2025 00:15:33 +0800 Subject: [PATCH] feat(nvim-plug): support `build` option --- bundle/nvim-plug/README.md | 15 +++--- bundle/nvim-plug/lua/plug/installer.lua | 69 +++++++++++++++++-------- bundle/nvim-plug/lua/plug/loader.lua | 6 +++ 3 files changed, 62 insertions(+), 28 deletions(-) diff --git a/bundle/nvim-plug/README.md b/bundle/nvim-plug/README.md index 58cc73b5a..eeb27accd 100644 --- a/bundle/nvim-plug/README.md +++ b/bundle/nvim-plug/README.md @@ -32,13 +32,14 @@ require('plug').add({ ## Plugin Spec -| name | description | -| -------- | ------------------------------------------------------- | -| `[1]` | `string`, plugin repo short name, `wsdjeg/flygrep.nvim` | -| `cmds` | `table`, commands lazy loading | -| `events` | `table`, events lazy loading | -| `on_ft` | `table`, filetypes lazy loading | -| `type` | `string`, plugin type including `color`, `plugin` | +| name | description | +| -------- | --------------------------------------------------------------------------------- | +| `[1]` | `string`, plugin repo short name, `wsdjeg/flygrep.nvim` | +| `cmds` | `table`, commands lazy loading | +| `events` | `table`, events lazy loading | +| `on_ft` | `table`, filetypes lazy loading | +| `type` | `string`, plugin type including `color`, `plugin` | +| `build` | `string` or `table`, executed by [job](https://spacevim.org/api/job/) api | ## Commands diff --git a/bundle/nvim-plug/lua/plug/installer.lua b/bundle/nvim-plug/lua/plug/installer.lua index be0a3fb46..94b66e0bb 100644 --- a/bundle/nvim-plug/lua/plug/installer.lua +++ b/bundle/nvim-plug/lua/plug/installer.lua @@ -7,24 +7,13 @@ local M = {} -local job = require("spacevim.api.job") -local notify = require("spacevim.api.notify") +local job = require('spacevim.api.job') +local notify = require('spacevim.api.notify') local jobs = {} -local function install_plugin(plugSpec) - local cmd = { "git", "clone", "--depth", "1" } - if plugSpec.branch then - table.insert(cmd, "--branch") - table.insert(cmd, plugSpec.branch) - elseif plugSpec.tag then - table.insert(cmd, "--branch") - table.insert(cmd, plugSpec.tag) - end - - table.insert(cmd, plugSpec.url) - table.insert(cmd, plugSpec.path) - vim.print(plugSpec) - local jobid = job.start(cmd, { +--- @param plugSpec PluginSpec +local function build(plugSpec) + local jobid = job.start(plugSpec.build, { on_stdout = function(id, data) for _, v in ipairs(data) do notify.notify(jobs['jobid_' .. id .. ':' .. v]) @@ -35,14 +24,52 @@ local function install_plugin(plugSpec) notify.notify(jobs['jobid_' .. id .. ':' .. v]) end end, - on_exit = function(id, data, single) - if data == 0 and single == 0 then + on_exit = function(id, data, single) + if data == 0 and single == 0 then + notify.notify('Successfully build ' .. jobs['jobid_' .. id]) + else + notify.notify('failed to build ' .. jobs['jobid_' .. id]) + end + end, + }) + jobs['jobid_' .. jobid] = plugSpec.name +end + +--- @param plugSpec PluginSpec +local function install_plugin(plugSpec) + local cmd = { 'git', 'clone', '--depth', '1' } + if plugSpec.branch then + table.insert(cmd, '--branch') + table.insert(cmd, plugSpec.branch) + elseif plugSpec.tag then + table.insert(cmd, '--branch') + table.insert(cmd, plugSpec.tag) + end + + table.insert(cmd, plugSpec.url) + table.insert(cmd, plugSpec.path) + local jobid = job.start(cmd, { + on_stdout = function(id, data) + for _, v in ipairs(data) do + notify.notify(jobs['jobid_' .. id .. ':' .. v]) + end + end, + on_stderr = function(id, data) + for _, v in ipairs(data) do + notify.notify(jobs['jobid_' .. id .. ':' .. v]) + end + end, + on_exit = function(id, data, single) + if data == 0 and single == 0 then notify.notify('Successfully installed ' .. jobs['jobid_' .. id]) + if plugSpec.build then + build(plugSpec) + end else notify.notify('failed to install ' .. jobs['jobid_' .. id]) - end - end, - }) + end + end, + }) jobs['jobid_' .. jobid] = plugSpec.name end diff --git a/bundle/nvim-plug/lua/plug/loader.lua b/bundle/nvim-plug/lua/plug/loader.lua index f73e9d318..6767a977e 100644 --- a/bundle/nvim-plug/lua/plug/loader.lua +++ b/bundle/nvim-plug/lua/plug/loader.lua @@ -14,6 +14,12 @@ local config = require("plug.config") --- @field events table --- @field cmds table --- @field config function +--- @field name string +--- @field branch string +--- @field tag string +--- @field url string +--- @field path string +--- @field build string|table function M.parser(plugSpec) plugSpec.name = vim.split(plugSpec[1], '/')[2]