Major changes!

- add name expansion gh:po5/thumbfast -> https://github.com/po5/thumbfast
- allow multiple files to be downloaded to one dir
- add pin option to stop updates on specific plugins
This commit is contained in:
2023-10-17 22:03:41 -04:00
parent 96c3e6f3e6
commit 6f091a7648
2 changed files with 134 additions and 63 deletions

View File

@ -18,10 +18,36 @@ GNU General Public License for more details.
-- after being called with dofile()
plugins = { -- the plugins you want to load
{ 'https://git.squi.bid/eat-it', -- required, specifies the git repo
file = 'eatit.lua', -- required, specifies the desired file
dir = 'scripts', -- optional, sets the dest dir
branch = 'master', -- optional, sets the desired branch
{ 'squibid/eat-it', -- required, specifies the git repo
-- optional, sets repo link (see advanced example for more info on how to
-- use this)
url = 'https://git.squi.bid/eat-it',
-- required, specifies the desired file from the git repo
file = 'eatit.lua',
-- optional, sets the destination of the requested file
dir = 'scripts',
-- optional, sets the desired branch of the git repo
branch = 'master',
-- optional, stop the plugin from being updated
pin = false,
},
-- advanced example
{ 'gh:po5/thumbfast', -- expands to https://github.com/po5/thumbfast
file = { -- multiple files all going to the same place
'thumbfast.lua',
'osc.lua'
},
branch = 'ancient',
--[[
no need to specify dir as it defaults to ~~/scripts
no need to specify url as it is extrapolated from name
name expansion can be configured in the opts section
]]
},
}
@ -35,6 +61,16 @@ opts = {
},
dl = { -- options for dealing with the git repos
dir = '/tmp/mpv-eatit',
powerwash = false, -- if true the dl dir gets deleted after mpv closes
powerwash = false, -- if true the download dir gets deleted after mpv closes
},
nameexp = {
pre = 'https://',
map = {
-- shortcut = link
gl = 'gitlab.com',
cb = 'codeberg.org',
sr = 'sr.ht',
gh = 'github.com'
}
}
}

117
eatit.lua
View File

@ -53,6 +53,35 @@ local function run(cmd)
return y
end
local function cp(a, b)
local i = io.open(a, 'r')
if not i then return 1 end
local o = io.open(b, 'w')
if not o then return 2 end
o:write(i:read('*a'))
o:close()
i:close()
end
local function getsrc(id)
local src
if string.find(id, ":") then src = id:match("^[a-z]+[^:]") end
local name = id:match([[[^:]*$]])
if name then
local pre = opts.nameexp.pre or "https://"
local map = opts.nameexp.map or {
gl = 'gitlab.com',
cb = 'codeberg.org',
sr = 'sr.ht',
gh = 'github.com'
}
return {
link = (pre..map[src]) or false,
repo = name
}
end
end
local function openlog()
if opts.logging.log then -- log if asked to
-- get our logfile's full path
@ -77,13 +106,13 @@ local function closelog()
end
-- get the requested git repos
local function clonegit(plugdir, i)
logwrite('downloading ' .. plugins[i][1])
local function clonegit(plugdir, url, branch)
logwrite('downloading '..url)
-- clone the repo
-- BUG: logwriting the git command doens't actually log the output
run('git -C ' .. opts.dl.dir .. ' clone ' .. plugins[i][1])
run('git -C ' .. plugdir .. ' checkout -q ' .. plugins[i]['branch'])
-- BUG: logwriting the git command doesn't actually log the output
run('git -C '..opts.dl.dir..' clone '..url..' '..plugdir)
run('git -C '..plugdir..' checkout -q '..branch)
end
-- check for updates
@ -95,69 +124,66 @@ local function checkupdates(plugdir)
end
-- start install
local function startinstall()
-- let the user know that we are starting install
logwrite('Starting Download...')
mp.osd_message('Downloading plugins!')
local function startinstall(i)
local src = getsrc(plugins[i][1])
local plugin = { -- plugin table spec
id = plugins[i][1] or false,
url = plugins[i]['url'] or (src['link'].."/"..src['repo']),
file = plugins[i]['file'] or false,
dir = plugins[i]['dir'] or 'scripts',
pin = plugins[i]['pin'] or false,
branch = plugins[i]['branch'] or false,
}
-- start iterating through plugins
for i = 1, tablelength(plugins) do
-- check if the user has defined a file for the current plugin
if not plugins[i]['file'] then
logwrite('WARNING! Git repo "' .. plugins[i][1] ..
'" doesn\'t have a specified file. Skipping download')
-- FIXME: this results in the last plugin being downloaded & copied twice
i = i + 1 -- if not we go to the next plugin
end
-- if no destdir defined we set it to the root of the mpv config dir
if not plugins[i]['dir'] then
plugins[i]['dir'] = '~~/'
if not plugin['file'] then
logwrite('WARNING! File not configured for '..plugin[1])
goto continue
end
-- skip install of pinned files
if plugin['pin'] == true then goto continue end
-- get the plugins tmp download dir
local plugdir = opts.dl.dir .. '/' ..
string.match(plugins[i][1], '/([^/]+)$'):gsub('.git', '')
local plugdir = opts.dl.dir..'/'..testforslash(src['repo']):gsub('.git', '')
-- if no specified branch we use the default
if not plugins[i]['branch'] then
plugins[i]['branch'] = testforslash(
plugin['branch'] = plugin['branch'] or testforslash(
run('git -C '..plugdir..' symbolic-ref refs/remotes/origin/HEAD')
)
-- check for multiple files
if type(plugin['file']) == 'string' then
plugin['file'] = {}
table.insert(plugin['file'], plugins[i]['file'])
end
-- get the requested file(s)
for j = 1, #plugin['file'] or 1 do
-- get the file's dir
local pluginfile = opts.dl.dir .. '/' ..
string.match(plugins[i][1], '/([^/]+)$'):gsub('.git', '') ..
'/' .. plugins[i]['file']
local pluginfile = plugdir..'/'..plugin['file'][j]
-- get the dest dir
local destfile = mp.command_native({'expand-path', '~~/'})..
'/' .. plugins[i]['dir'] ..
'/' .. testforslash(plugins[i]['file'])
'/'..plugin['dir']..'/'..testforslash(plugin['file'][j])
if fileexists(plugdir..'/') then
-- if we need to update, update
if checkupdates(plugdir) then
logwrite(plugins[i]['file'] .. ' is updating.')
logwrite(plugin['file'][j]..' is updating.')
-- make sure we are on the main branch
run('git -C ' .. plugdir .. ' checkout -q ' .. plugins[i]['branch'])
run('git -C '..plugdir..' checkout -q '..plugin['branch'])
run('git -C '..plugdir..' pull') -- get the latest commits
else
logwrite(plugins[i]['file'] .. ' is up to date!')
logwrite(plugin['file'][j]..' is up to date!')
end
else
clonegit(plugdir, i)
clonegit(plugdir, plugin['url'], plugin['branch'])
end
-- copy the file contents over to the desired location
local infile = io.open(pluginfile, 'r')
local outfile = io.open(destfile, 'w')
outfile:write(infile:read('*a'))
outfile:close()
infile:close()
cp(pluginfile, destfile)
end
::continue::
end
local function initupdate()
@ -165,7 +191,16 @@ local function initupdate()
logwrite('# of plugins defined in table: '..tablelength(plugins))
os.execute('mkdir -p '..opts.dl.dir) -- make download dir
startinstall()
-- start the install process
logwrite('Starting Download...')
mp.osd_message('Downloading plugins!')
-- start iterating through plugins
for i = 1, tablelength(plugins) do
local f = coroutine.create(function() startinstall(i) end)
coroutine.resume(f)
end
-- closing/removing everything
if opts.dl.powerwash == true then