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 )
79 - return name:find( plugin_name,
82 - local extracted_dir
= vim.fs.find( contains_name,
83 - {
path = puck_root,
type =
'directory',
limit =
1, } )
84 - assert(
#extracted_dir
~= 0,
'No directory was found in tarball' )
85 - assert(
#extracted_dir
== 1,
86 - 'Too many directories were found in tarball. Expected one!' )
88 - local ok
= vim.uv.fs_rename( extracted_dir[
1], plugin_install_path )
90 - string.
format(
'Failed to move extracted plugin from %s to %s.',
91 - extracted_dir[
1], plugin_install_path ) )
92 - configure_plugin( plugin_name )
95 - vim.uv.shutdown( tarball_data_stream,
function ()
96 - vim.uv.close( curl_handle )
97 - vim.uv.close( tar_handle )
101 -local delete_directory_recursive = function ( path )
102 - local stat
= vim.uv.fs_stat( path )
103 - if stat
and stat.type
== 'directory' then 104 - local dir
= vim.uv.fs_scandir( path )
107 - local name,
type = vim.uv.fs_scandir_next( dir )
112 - local full_path
= vim.fs.joinpath( path, name )
113 - if type == 'directory' then 114 - return delete_directory_recursive( full_path )
116 - return vim.uv.fs_unlink( full_path )
120 - return vim.uv.fs_rmdir( path )
126 -local as_url_format = function ( plugin_type )
127 - assert( plugin_type
~= dev,
'local plugins need not have an url format!' )
128 - if plugin_type
== gh
then return gh_format
end 129 - if plugin_type
== cb
then return cb_format
end 130 - if plugin_type
== srht
then return srht_format
end 132 - error(
'unknown plugin type ' .. plugin_type
.. ' given.' )
135 -local install_plugin = function ( puck_root, plugin_type, plugin_coords, plugin_rev )
136 - local plugin_name
= as_name( plugin_coords )
137 - local plugin_install_path
= vim.fs.normalize( vim.fs.joinpath( puck_root, plugin_name ) )
138 - local plugin_install_path_stat
= vim.uv.fs_stat( plugin_install_path )
139 - local plugin_already_exists
= plugin_install_path_stat
and plugin_install_path_stat.type
== 'directory' 141 - local plugin_install_rev_path
= vim.fs.normalize( vim.fs.joinpath( puck_root, plugin_name
.. '.rev' ) )
144 - if (vim.uv.fs_access( plugin_install_rev_path,
'R' ))
then 145 - local plugin_install_rev_file
= io.
open( plugin_install_rev_path,
'r' )
146 - if not plugin_install_rev_file
then 147 - vim.notify(
'Unable to read rev file ' .. plugin_install_rev_path,
148 - vim.log.levels.ERROR )
150 - local current_rev
= plugin_install_rev_file:read()
151 - plugin_install_rev_file:close()
152 - if current_rev
== plugin_rev
then 153 - configure_plugin( plugin_name )
158 - local removed
= delete_directory_recursive( plugin_install_path )
159 - local url_format
= as_url_format( plugin_type )
160 - local plugin_tarball_url
= url_format:format( plugin_coords, plugin_rev )
161 - download_plugin_into( puck_root, plugin_name, plugin_tarball_url, plugin_install_path, callback )
162 - local mode
= vim.uv.fs_access( plugin_install_rev_path,
'W' )
and 'w+' or 'w' 163 - local plugin_install_rev_file
= io.
open( plugin_install_rev_path, mode )
164 - if not plugin_install_rev_file
then 165 - vim.notify(
'Unable to write rev file ' .. plugin_install_rev_path,
166 - vim.log.levels.ERROR )
168 - plugin_install_rev_file:write( plugin_rev )
169 - plugin_install_rev_file:close()
171 - vim.notify(
'Plugin ' .. plugin_coords
.. '@' .. plugin_rev
.. ' updated!', vim.log.levels.INFO )
173 - vim.notify(
'Plugin ' .. plugin_coords
.. '@' .. plugin_rev
.. ' installed!', vim.log.levels.INFO )
177 -local delete_unused_plugins = function ( puck_root )
end 179 -local register_local_plugin = function ( workspace, plugin_coords )
180 - local plugin_path
= vim.fs.normalize( vim.fs.joinpath( workspace, vim.fs.basename( plugin_coords ) ) )
181 - local plugin_path_stat
= vim.uv.fs_stat( plugin_path )
183 - if (plugin_path_stat
and plugin_path_stat.type
== 'directory')
then 184 - vim.opt.runtimepath:prepend( plugin_path )
186 - vim.notify(
'Failed to load local plugin ' .. plugin_path
.. '!', vim.log.levels.ERROR )
190 -local load_local_plugin = function ( plugin_coords )
191 - local plugin_name
= as_name( plugin_coords )
192 - local plugin_init
= {}
193 - vim.tbl_extend(
'keep', plugin_init, vim.api.nvim_get_runtime_file(
'plugin/' .. plugin_name
.. '.vim',
false ) )
194 - vim.tbl_extend(
'keep', plugin_init, vim.api.nvim_get_runtime_file(
'plugin/' .. plugin_name
.. '.lua',
false ) )
195 - for _, init
in pairs( plugin_init )
do 196 - vim.cmd.source( init )
200 -local load_registered_plugin = function ( plugin_name )
201 - vim.cmd.packadd( plugin_name )
203 - return require( plugin_name )
206 -local setup = function ( options )
207 - options
= options
or {}
209 - [
'options.workspace'] = { options.workspace,
'string', },
212 - options.plugins
= options.plugins
or {}
213 - plugins
= options.plugins
216 - vim.opt.loadplugins
= false 218 - local puck_root
= vim.fs.normalize( vim.fn.stdpath
'data' .. '/site/pack/puck/opt' )
219 - vim.fn.mkdir( puck_root,
'p' )
221 - for _, plugin
in pairs( plugins )
do 222 - local plugin_type
= plugin[
1]
223 - local plugin_coords
= plugin[
2]
224 - local plugin_rev
= plugin[
3]
225 - if plugin_type
== dev
then 226 - register_local_plugin( options.workspace, plugin_coords )
230 - install_plugin( puck_root, plugin_type, plugin_coords, plugin_rev )
234 - delete_unused_plugins( puck_root )
238 - load_local_plugin
'plugin/shada.vim' 239 - load_local_plugin
'plugin/tutor.vim' 240 - load_local_plugin
'plugin/spellfile.vim' 241 - load_local_plugin
'plugin/osc52.lua' 244 - load_local_plugin
'plugin/editorconfig.lua' 249 - load = load_registered_plugin,
252 - github = {
type = gh,
format = gh_format, },
253 - codeberg = {
type = cb,
format = cb_format, },
254 - sourcehut = {
type = srht,
format = srht_format, },
255 - development = {
type = dev,
format =
nil, },