1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-19 08:13:44 +08:00

feat(nvim-plug): support raw plugin

This commit is contained in:
Eric Wong 2025-02-08 22:47:33 +08:00
parent 2fe9e8fc11
commit 68eaa0ec0f
No known key found for this signature in database
GPG Key ID: 41BB7053E835C848
6 changed files with 384 additions and 283 deletions

View File

@ -39,20 +39,27 @@ setup nvim-plug:
```lua ```lua
require('plug').setup({ require('plug').setup({
-- set the bundle dir
bundle_dir = 'D:/bundle_dir', bundle_dir = 'D:/bundle_dir',
max_processes = 5, -- max number of processes used for nvim-plug job -- set the path where raw plugin is download to
raw_plugin_dir = 'D:/bundle_dir/raw_plugin',
-- max number of processes used for nvim-plug job
max_processes = 5,
base_url = 'https://github.com', base_url = 'https://github.com',
ui = 'notify', -- default ui is `notify`, use `default` for split window UI -- default ui is `notify`, use `default` for split window UI
http_proxy = 'http://127.0.0.1:7890', -- default is nil ui = 'notify',
https_proxy = 'http://127.0.0.1:7890', -- default is nil -- default is nil
clone_depth = 1 -- default history depth for `git clone` http_proxy = 'http://127.0.0.1:7890',
-- default is nil
https_proxy = 'http://127.0.0.1:7890',
-- default history depth for `git clone`
clone_depth = 1,
}) })
``` ```
add plugins: add plugins:
```lua ```lua
require('plug').add({ require('plug').add({
{ {
'wsdjeg/scrollbar.vim', 'wsdjeg/scrollbar.vim',
@ -72,6 +79,11 @@ require('plug').add({
require('flygrep').setup() require('flygrep').setup()
end, end,
}, },
{
type = 'raw',
url = 'https://gist.githubusercontent.com/wsdjeg/4ac99019c5ca156d35704550648ba321/raw/4e8c202c74e98b5d56616c784bfbf9b873dc8868/markdown.vim',
script_type = 'after/syntax'
},
{ {
'D:/wsdjeg/winbar.nvim', 'D:/wsdjeg/winbar.nvim',
events = { 'VimEnter' }, events = { 'VimEnter' },
@ -85,19 +97,24 @@ require('plug').add({
## Plugin Spec ## Plugin Spec
The plugin spec is inspired by dein.nvim.
| name | description | | name | description |
| --------- | --------------------------------------------------------------------------------------- | | ------------- | ---------------------------------------------------------------------------------------------------- |
| `[1]` | `string`, plugin repo short name, `wsdjeg/flygrep.nvim` | | `[1]` | `string`, plugin repo short name, `wsdjeg/flygrep.nvim` |
| `cmds` | `table<string>`, commands lazy loading | | `cmds` | `table<string>`, commands lazy loading |
| `events` | `table<string>`, events lazy loading | | `events` | `table<string>`, events lazy loading |
| `on_ft` | `table<string>`, filetypes lazy loading | | `on_ft` | `table<string>`, filetypes lazy loading |
| `on_map` | `table<string>`, key bindings lazy loading | | `on_map` | `table<string>`, key bindings lazy loading |
| `on_func` | `string` or `table<string>`, vim function lazy loading | | `on_func` | `string` or `table<string>`, vim function lazy loading |
| `type` | `string`, plugin type including `color`, `plugin` | | `script_type` | `string`, plugin type including `color`, `plugin`, etc.. |
| `build` | `string` or `table<string>`, executed by [job](https://spacevim.org/api/job/) api | | `build` | `string` or `table<string>`, executed by [job](https://spacevim.org/api/job/) api |
| `enabled` | `boolean` or `function` evaluated when startup, when it is false, plugin will be skiped | | `enabled` | `boolean` or `function` evaluated when startup, when it is false, plugin will be skiped |
| `frozen` | update only when specific with `PlugUpdate name` | | `frozen` | update only when specific with `PlugUpdate name` |
| `depends` | `table<PluginSpec>` a list of plugins | | `depends` | `table<PluginSpec>` a list of plugins |
| `branch` | `string` specific git branch |
| `tag` | `string` specific git tag |
| `type` | `string` specific plugin type, this can be git, raw or none, if it is raw, `script_type` must be set |
## Commands ## Commands

View File

@ -8,6 +8,7 @@
local M = {} local M = {}
M.bundle_dir = vim.fn.stdpath('data') .. '/bundle_dir' M.bundle_dir = vim.fn.stdpath('data') .. '/bundle_dir'
M.raw_plugin_dir = vim.fn.stdpath('data') .. '/bundle_dir/raw_plugin'
M.max_processes = 5 M.max_processes = 5
M.base_url = 'https://github.com/' M.base_url = 'https://github.com/'
M.ui = 'notify' M.ui = 'notify'

View File

@ -7,6 +7,8 @@
local M = {} local M = {}
local H = {}
local job = require('spacevim.api.job') local job = require('spacevim.api.job')
local notify = require('spacevim.api.notify') local notify = require('spacevim.api.notify')
local jobs = {} local jobs = {}
@ -34,9 +36,10 @@ local processes = 0
local installation_queue = {} local installation_queue = {}
local building_queue = {} local building_queue = {}
local updating_queue = {} local updating_queue = {}
local raw_download_queue = {}
--- @param plugSpec PluginSpec --- @param plugSpec PluginSpec
local function build(plugSpec) function H.build(plugSpec)
if processes >= config.max_processes then if processes >= config.max_processes then
table.insert(building_queue, plugSpec) table.insert(building_queue, plugSpec)
return return
@ -61,7 +64,7 @@ local function build(plugSpec)
end end
processes = processes - 1 processes = processes - 1
if #building_queue > 0 then if #building_queue > 0 then
build(table.remove(building_queue)) H.build(table.remove(building_queue))
end end
end, end,
cwd = plugSpec.path, cwd = plugSpec.path,
@ -75,7 +78,44 @@ local function build(plugSpec)
end end
--- @param plugSpec PluginSpec --- @param plugSpec PluginSpec
local function install_plugin(plugSpec) function H.download_raw(plugSpec, force)
if processes >= config.max_processes then
table.insert(raw_download_queue, plugSpec)
return
elseif vim.fn.filereadable(plugSpec.path) == 1 and not force then
on_uidate(plugSpec.name, { command = 'curl', curl_done = true })
return
end
local cmd = {'curl', '-fLo', plugSpec.path, '--create-dirs', plugSpec.url}
on_uidate(plugSpec.name, { command = 'curl'})
local jobid = job.start(cmd, {
on_exit = function(id, data, single)
if data == 0 and single == 0 then
on_uidate(plugSpec.name, { curl_done = true })
else
on_uidate(plugSpec.name, { curl_done = false })
end
processes = processes - 1
if #installation_queue > 0 then
H.install_plugin(table.remove(installation_queue, 1))
elseif #building_queue > 0 then
H.build(table.remove(building_queue, 1))
end
end,
env = {
http_proxy = config.http_proxy,
https_proxy = config.https_proxy,
},
})
processes = processes + 1
jobs['jobid_' .. jobid] = plugSpec.name
end
--- @param plugSpec PluginSpec
function H.install_plugin(plugSpec)
if processes >= config.max_processes then if processes >= config.max_processes then
table.insert(installation_queue, plugSpec) table.insert(installation_queue, plugSpec)
return return
@ -113,16 +153,16 @@ local function install_plugin(plugSpec)
if data == 0 and single == 0 then if data == 0 and single == 0 then
on_uidate(plugSpec.name, { clone_done = true, download_process = 100 }) on_uidate(plugSpec.name, { clone_done = true, download_process = 100 })
if plugSpec.build then if plugSpec.build then
build(plugSpec) H.build(plugSpec)
end end
else else
on_uidate(plugSpec.name, { clone_done = false, download_process = 0 }) on_uidate(plugSpec.name, { clone_done = false, download_process = 0 })
end end
processes = processes - 1 processes = processes - 1
if #installation_queue > 0 then if #installation_queue > 0 then
install_plugin(table.remove(installation_queue, 1)) H.install_plugin(table.remove(installation_queue, 1))
elseif #building_queue > 0 then elseif #building_queue > 0 then
build(table.remove(building_queue, 1)) H.build(table.remove(building_queue, 1))
end end
end, end,
env = { env = {
@ -135,9 +175,9 @@ local function install_plugin(plugSpec)
end end
--- @param plugSpec PluginSpec --- @param plugSpec PluginSpec
local function update_plugin(plugSpec, force) function H.update_plugin(plugSpec, force)
if processes >= config.max_processes then if processes >= config.max_processes then
table.insert(updating_queue, {plugSpec, force}) table.insert(updating_queue, { plugSpec, force })
return return
elseif vim.fn.isdirectory(plugSpec.path) ~= 1 then elseif vim.fn.isdirectory(plugSpec.path) ~= 1 then
-- if the directory does not exist, return failed -- if the directory does not exist, return failed
@ -162,16 +202,16 @@ local function update_plugin(plugSpec, force)
if data == 0 and single == 0 then if data == 0 and single == 0 then
on_uidate(plugSpec.name, { pull_done = true }) on_uidate(plugSpec.name, { pull_done = true })
if plugSpec.build then if plugSpec.build then
build(plugSpec) H.build(plugSpec)
end end
else else
on_uidate(plugSpec.name, { pull_done = false }) on_uidate(plugSpec.name, { pull_done = false })
end end
processes = processes - 1 processes = processes - 1
if #updating_queue > 0 then if #updating_queue > 0 then
update_plugin(unpack(table.remove(updating_queue, 1))) H.update_plugin(unpack(table.remove(updating_queue, 1)))
elseif #building_queue > 0 then elseif #building_queue > 0 then
build(table.remove(building_queue, 1)) H.build(table.remove(building_queue, 1))
end end
end, end,
cwd = plugSpec.path, cwd = plugSpec.path,
@ -190,13 +230,21 @@ end
M.install = function(plugSpecs) M.install = function(plugSpecs)
for _, v in ipairs(plugSpecs) do for _, v in ipairs(plugSpecs) do
install_plugin(v) if v.type == 'raw' then
H.download_raw(v)
else
H.install_plugin(v)
end
end end
end end
M.update = function(plugSpecs, force) M.update = function(plugSpecs, force)
for _, v in ipairs(plugSpecs) do for _, v in ipairs(plugSpecs) do
update_plugin(v, force) if v.type == 'raw' then
H.download_raw(v, force)
else
H.update_plugin(v, force)
end
end end
end end

View File

@ -9,6 +9,8 @@ local M = {}
local config = require('plug.config') local config = require('plug.config')
local add_raw_rtp = false
--- @class PluginSpec --- @class PluginSpec
--- @field rtp string --- @field rtp string
--- @field events table<string> --- @field events table<string>
@ -23,6 +25,8 @@ local config = require('plug.config')
--- @field is_local boolean true for local plugin --- @field is_local boolean true for local plugin
--- @field when boolean|string|function --- @field when boolean|string|function
--- @field frozen boolean --- @field frozen boolean
--- @field type string "git", "raw" or "none"
--- @field script_type string "git", "raw" or "none"
local function is_local_plugin(plugSpec) local function is_local_plugin(plugSpec)
if plugSpec.is_local or vim.fn.isdirectory(plugSpec[1]) == 1 then if plugSpec.is_local or vim.fn.isdirectory(plugSpec[1]) == 1 then
@ -31,34 +35,51 @@ local function is_local_plugin(plugSpec)
end end
end end
local function unique_name(plugSpec) local function check_name(plugSpec)
local s = vim.split(plugSpec[1], '/') if not plugSpec[1] and not plugSpec.url then
return s[#s] return false
end
local s = vim.split(plugSpec[1] or plugSpec.url, '/')
plugSpec.name = s[#s]
return true
end end
function M.parser(plugSpec) function M.parser(plugSpec)
if type(plugSpec.enabled) == "nil" then if type(plugSpec.enabled) == 'nil' then
plugSpec.enabled = true plugSpec.enabled = true
elseif type(plugSpec.enabled) == "function" then elseif type(plugSpec.enabled) == 'function' then
plugSpec.enabled = plugSpec.enabled() plugSpec.enabled = plugSpec.enabled()
elseif type(plugSpec.enabled) ~= "boolean" or plugSpec.enabled == false then elseif type(plugSpec.enabled) ~= 'boolean' or plugSpec.enabled == false then
plugSpec.enabled = false
return plugSpec
elseif not check_name(plugSpec) then
plugSpec.enabled = false plugSpec.enabled = false
return plugSpec return plugSpec
end end
plugSpec.name = unique_name(plugSpec)
if is_local_plugin(plugSpec) then if is_local_plugin(plugSpec) then
plugSpec.rtp = plugSpec[1] plugSpec.rtp = plugSpec[1]
plugSpec.path = plugSpec[1] plugSpec.path = plugSpec[1]
plugSpec.url = nil plugSpec.url = nil
elseif not plugSpec.type or plugSpec.type == 'none' then elseif plugSpec.type == 'raw' then
if not plugSpec.script_type or plugSpec.script_type == 'none' then
plugSpec.enabled = false
return plugSpec
else
plugSpec.path = config.raw_plugin_dir .. '/' .. plugSpec.script_type .. plugSpec.name
if not add_raw_rtp then
vim.opt:append(config.raw_plugin_dir)
add_raw_rtp = true
end
end
elseif not plugSpec.script_type or plugSpec.script_type == 'none' then
plugSpec.rtp = config.bundle_dir .. '/' .. plugSpec[1] plugSpec.rtp = config.bundle_dir .. '/' .. plugSpec[1]
plugSpec.path = config.bundle_dir .. '/' .. plugSpec[1] plugSpec.path = config.bundle_dir .. '/' .. plugSpec[1]
plugSpec.url = config.base_url .. '/' .. plugSpec[1] plugSpec.url = config.base_url .. '/' .. plugSpec[1]
elseif plugSpec.type == 'color' then elseif plugSpec.script_type == 'color' then
plugSpec.rtp = config.bundle_dir .. '/' .. plugSpec[1] plugSpec.rtp = config.bundle_dir .. '/' .. plugSpec[1]
plugSpec.path = config.bundle_dir .. '/' .. plugSpec[1] .. '/color' plugSpec.path = config.bundle_dir .. '/' .. plugSpec[1] .. '/color'
plugSpec.repo = config.base_url .. '/' .. plugSpec[1] plugSpec.repo = config.base_url .. '/' .. plugSpec[1]
elseif plugSpec.type == 'plugin' then elseif plugSpec.script_type == 'plugin' then
plugSpec.rtp = config.bundle_dir .. '/' .. plugSpec[1] plugSpec.rtp = config.bundle_dir .. '/' .. plugSpec[1]
plugSpec.path = config.bundle_dir .. '/' .. plugSpec[1] .. '/plugin' plugSpec.path = config.bundle_dir .. '/' .. plugSpec[1] .. '/plugin'
plugSpec.url = config.base_url .. '/' .. plugSpec[1] plugSpec.url = config.base_url .. '/' .. plugSpec[1]
@ -88,10 +109,10 @@ end
-- 'merged': 0, -- 'merged': 0,
-- 'path': 'C:/Users/wsdjeg/.SpaceVim/bundle/defx-git'} -- 'path': 'C:/Users/wsdjeg/.SpaceVim/bundle/defx-git'}
function M.load(plugSpec) function M.load(plugSpec)
if vim.fn.isdirectory(plugSpec.rtp) == 1 then if plugSpec.rtp and vim.fn.isdirectory(plugSpec.rtp) == 1 then
vim.opt.runtimepath:append(plugSpec.rtp) vim.opt.runtimepath:append(plugSpec.rtp)
if vim.fn.has('vim_starting') ~= 1 then if vim.fn.has('vim_starting') ~= 1 then
local plugin_directory_files = vim.fn.globpath(plugSpec.rtp, "plugin/*.{lua,vim}", 0, 1) local plugin_directory_files = vim.fn.globpath(plugSpec.rtp, 'plugin/*.{lua,vim}', 0, 1)
for _, f in ipairs(plugin_directory_files) do for _, f in ipairs(plugin_directory_files) do
vim.cmd.source(f) vim.cmd.source(f)
end end

View File

@ -72,6 +72,14 @@ local function build_context()
else else
table.insert(b, '- ' .. k) table.insert(b, '- ' .. k)
end end
elseif plug.command == 'curl' then
if plug.curl_done then
table.insert(b, '' .. k .. ' download')
elseif plug.curl_done == false then
table.insert(b, '× ' .. k .. ' failed to download')
else
table.insert(b, '- ' .. k .. string.format(' downloading'))
end
end end
end end

View File

@ -11,6 +11,7 @@ vim.opt.runtimepath:append('~/.SpaceVim')
require('plug').setup({ require('plug').setup({
bundle_dir = 'D:/bundle_dir', bundle_dir = 'D:/bundle_dir',
raw_plugin_dir = 'D:/bundle_dir/raw_plugin',
ui = 'default', ui = 'default',
http_proxy = 'http://127.0.0.1:7890', http_proxy = 'http://127.0.0.1:7890',
https_proxy = 'http://127.0.0.1:7890', https_proxy = 'http://127.0.0.1:7890',
@ -21,6 +22,11 @@ require('plug').add({
'wsdjeg/SourceCounter.vim', 'wsdjeg/SourceCounter.vim',
cmds = { 'SourceCounter' }, cmds = { 'SourceCounter' },
}, },
{
type = 'raw',
url = 'https://gist.githubusercontent.com/wsdjeg/4ac99019c5ca156d35704550648ba321/raw/4e8c202c74e98b5d56616c784bfbf9b873dc8868/markdown.vim',
script_type = 'after/syntax'
},
{ {
'wsdjeg/git.vim', 'wsdjeg/git.vim',
cmds = { 'Git' }, cmds = { 'Git' },