14 -local dev
= 'plugin_under_development' 15 -local gh
= 'plugin_from_github' 16 -local srht
= 'plugin_from_srht' 17 -local cb
= 'plugin_from_codeberg' 18 -local gh_format
= 'https://github.com/%s/archive/%s.tar.gz' 19 -local cb_format
= 'https://codeberg.org/%s/archive/%s.tar.gz' 20 -local srht_format
= 'https://git.sr.ht/%s/archive/%s.tar.gz' 24 -local as_name = function ( plugin_coords )
25 - return vim.fs.basename(plugin_coords):gsub(
'(%w+)%.?%w*',
'%1' )
28 -local configure_plugin = function( plugin_name )
29 - local config
= 'config.' .. plugin_name
30 - vim.schedule(
function()
31 - local ok
= pcall(
require, config)
33 - vim.notify(
string.
format(
'unable to load configuration for %s at %s', plugin_name, config), vim.log.levels.WARN)
38 -local download_plugin_into = function ( puck_root, plugin_name, plugin_tarball_url, plugin_install_path)
40 - puck_root = { puck_root,
'string' },
41 - plugin_name = { plugin_name,
'string'},
42 - plugin_tarball_url = { plugin_tarball_url,
'string' },
43 - plugin_install_path = { plugin_install_path,
'string'},
46 - assert(vim.fn.executable
'curl',
'curl not found')
47 - assert(vim.fn.executable
'tar',
'tar not found')
52 - local tarball_data_stream
= vim.uv.new_pipe(
true)
54 - local curl_handle, curl_pid
= vim.uv.spawn(
'curl',
56 - args = {
'--silent',
'--location', plugin_tarball_url },
58 - stdio = {
nil, tarball_data_stream,
nil},
61 - function(code, signal)
63 - error(
'curl', code, signal)
66 - local tar_handle, tar_pid
= vim.uv.spawn(
'tar',
68 - args = {
'--extract',
'--gzip',
'--file',
'-' },
70 - stdio = { tarball_data_stream,
nil,
nil},
73 - function(code, signal)
75 - error(
'tar done', code, signal)
78 - local contains_name = function(name, path)
return name:find(plugin_name,
0,
true)
end 79 - local extracted_dir
= vim.fs.find( contains_name, {
path = puck_root,
type =
'directory',
limit =
1 })
80 - assert(
#extracted_dir
~= 0,
'No directory was found in tarball')
81 - assert(
#extracted_dir
== 1,
'Too many directories were found in tarball. Expected one!')
83 - local ok
= vim.uv.fs_rename(extracted_dir[
1], plugin_install_path)
84 - assert(ok,
string.
format(
'Failed to move extracted plugin from %s to %s.', extracted_dir[
1], plugin_install_path))
85 - configure_plugin(plugin_name)
88 - vim.uv.shutdown(tarball_data_stream,
function()
89 - vim.uv.close(curl_handle)
90 - vim.uv.close(tar_handle)
94 -local delete_directory_recursive = function ( path )
95 - local stat
= vim.uv.fs_stat( path )
96 - if stat
and stat.type
== 'directory' then 97 - local dir
= vim.uv.fs_scandir( path )
100 - local name,
type = vim.uv.fs_scandir_next( dir )
105 - local full_path
= vim.fs.joinpath( path, name )
106 - if type == 'directory' then 107 - return delete_directory_recursive( full_path )
109 - return vim.uv.fs_unlink( full_path )
113 - return vim.uv.fs_rmdir( path )
121 -local as_url_format = function(plugin_type)
122 - assert(plugin_type
~= dev,
'local plugins need not have an url format!')
123 - if plugin_type
== gh
then return gh_format
end 124 - if plugin_type
== cb
then return cb_format
end 125 - if plugin_type
== srht
then return srht_format
end 127 - error(
'unknown plugin type '..plugin_type
..' given.')
130 -local install_plugin = function ( puck_root, plugin_type, plugin_coords, plugin_rev)
131 - local plugin_name
= as_name( plugin_coords )
132 - local plugin_install_path
= vim.fs.normalize( vim.fs.joinpath( puck_root, plugin_name ) )
133 - local plugin_install_path_stat
= vim.uv.fs_stat( plugin_install_path )
134 - local plugin_already_exists
= plugin_install_path_stat
and plugin_install_path_stat.type
== 'directory' 136 - local plugin_install_rev_path
= vim.fs.normalize( vim.fs.joinpath(puck_root, plugin_name
.. '.rev' ))
139 - if (vim.uv.fs_access( plugin_install_rev_path,
'R' ))
then 140 - local plugin_install_rev_file
= io.
open( plugin_install_rev_path,
'r' )
141 - if not plugin_install_rev_file
then vim.notify(
'Unable to read rev file ' .. plugin_install_rev_path, vim.log.levels.ERROR)
end 142 - local current_rev
= plugin_install_rev_file:read()
143 - plugin_install_rev_file:close()
144 - if current_rev
== plugin_rev
then 145 - configure_plugin(plugin_name)
150 - local removed
= delete_directory_recursive( plugin_install_path )
151 - local url_format
= as_url_format(plugin_type)
152 - local plugin_tarball_url
= url_format:format( plugin_coords, plugin_rev )
153 - download_plugin_into(puck_root, plugin_name, plugin_tarball_url, plugin_install_path, callback )
154 - local mode
= vim.uv.fs_access(plugin_install_rev_path,
'W')
and 'w+' or 'w' 155 - local plugin_install_rev_file
= io.
open( plugin_install_rev_path, mode)
156 - if not plugin_install_rev_file
then vim.notify(
'Unable to write rev file ' .. plugin_install_rev_path, vim.log.levels.ERROR)
end 157 - plugin_install_rev_file:write( plugin_rev )
158 - plugin_install_rev_file:close()
160 - vim.notify(
'Plugin ' .. plugin_coords
.. '@' .. plugin_rev
.. ' updated!', vim.log.levels.INFO )
162 - vim.notify(
'Plugin ' .. plugin_coords
.. '@' .. plugin_rev
.. ' installed!', vim.log.levels.INFO )
166 -local delete_unused_plugins = function ( puck_root )
end 168 -local register_local_plugin = function ( workspace, plugin_coords )
169 - local plugin_path
= vim.fs.normalize( vim.fs.joinpath(workspace, vim.fs.basename(plugin_coords) ) )
170 - local plugin_path_stat
= vim.uv.fs_stat( plugin_path )
172 - if (plugin_path_stat
and plugin_path_stat.type
== 'directory')
then 173 - vim.opt.runtimepath:prepend( plugin_path )
175 - vim.notify(
'Failed to load local plugin ' .. plugin_path
.. '!', vim.log.levels.ERROR )
179 -local load_local_plugin = function ( plugin_coords )
180 - local plugin_name
= as_name( plugin_coords )
181 - local plugin_init
= {}
182 - vim.tbl_extend(
'keep', plugin_init, vim.api.nvim_get_runtime_file(
'plugin/' .. plugin_name
.. '.vim',
false ) )
183 - vim.tbl_extend(
'keep', plugin_init, vim.api.nvim_get_runtime_file(
'plugin/' .. plugin_name
.. '.lua',
false ) )
184 - for _, init
in pairs( plugin_init )
do 185 - vim.cmd.source( init )
189 -local load_registered_plugin = function ( plugin_name )
190 - vim.cmd.packadd(plugin_name)
192 - return require(plugin_name)
195 -local setup = function ( options )
196 - options
= options
or {}
198 - [
'options.workspace'] = { options.workspace,
'string'},
201 - options.plugins
= options.plugins
or {}
202 - plugins
= options.plugins
205 - vim.opt.loadplugins
= false 207 - local puck_root
= vim.fs.normalize( vim.fn.stdpath
'data' .. '/site/pack/puck/opt' )
208 - vim.fn.mkdir( puck_root,
'p' )
210 - for _, plugin
in pairs( plugins )
do 211 - local plugin_type
= plugin[
1]
212 - local plugin_coords
= plugin[
2]
213 - local plugin_rev
= plugin[
3]
214 - if plugin_type
== dev
then 215 - register_local_plugin(options.workspace, plugin_coords )
219 - install_plugin( puck_root, plugin_type, plugin_coords, plugin_rev)
223 - delete_unused_plugins( puck_root )
227 - load_local_plugin
'plugin/shada.vim' 228 - load_local_plugin
'plugin/tutor.vim' 229 - load_local_plugin
'plugin/spellfile.vim' 230 - load_local_plugin
'plugin/osc52.lua' 233 - load_local_plugin
'plugin/editorconfig.lua' 238 - load = load_registered_plugin,
241 - github = {
type = gh,
format = gh_format },
242 - codeberg = {
type = cb,
format = cb_format },
243 - sourcehut = {
type = srht,
format = srht_format },
244 - development = {
type = dev,
format =
nil },