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() -- after being called with dofile()
plugins = { -- the plugins you want to load plugins = { -- the plugins you want to load
{ 'https://git.squi.bid/eat-it', -- required, specifies the git repo { 'squibid/eat-it', -- required, specifies the git repo
file = 'eatit.lua', -- required, specifies the desired file -- optional, sets repo link (see advanced example for more info on how to
dir = 'scripts', -- optional, sets the dest dir -- use this)
branch = 'master', -- optional, sets the desired branch 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 dl = { -- options for dealing with the git repos
dir = '/tmp/mpv-eatit', 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'
}
} }
} }

151
eatit.lua
View File

@ -53,6 +53,35 @@ local function run(cmd)
return y return y
end 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() local function openlog()
if opts.logging.log then -- log if asked to if opts.logging.log then -- log if asked to
-- get our logfile's full path -- get our logfile's full path
@ -66,7 +95,7 @@ end
local function logwrite(string) local function logwrite(string)
if opts.logging.log then if opts.logging.log then
io.write(os.date(opts.logging.logdate) .. ' ' .. string .. '\n') io.write(os.date(opts.logging.logdate)..' '..string..'\n')
end end
end end
@ -77,100 +106,106 @@ local function closelog()
end end
-- get the requested git repos -- get the requested git repos
local function clonegit(plugdir, i) local function clonegit(plugdir, url, branch)
logwrite('downloading ' .. plugins[i][1]) logwrite('downloading '..url)
-- clone the repo -- clone the repo
-- BUG: logwriting the git command doens't actually log the output -- BUG: logwriting the git command doesn't actually log the output
run('git -C ' .. opts.dl.dir .. ' clone ' .. plugins[i][1]) run('git -C '..opts.dl.dir..' clone '..url..' '..plugdir)
run('git -C ' .. plugdir .. ' checkout -q ' .. plugins[i]['branch']) run('git -C '..plugdir..' checkout -q '..branch)
end end
-- check for updates -- check for updates
local function checkupdates(plugdir) local function checkupdates(plugdir)
local localhash = run('git -C ' .. plugdir .. ' log -1 --format=format:"%H"') local localhash = run('git -C '..plugdir..' log -1 --format=format:"%H"')
local remotehash = run('git -C ' .. plugdir .. ' rev-parse $(git -C ' .. local remotehash = run('git -C '..plugdir..' rev-parse $(git -C '..
plugdir .. ' branch -r) | tail -1') plugdir..' branch -r) | tail -1')
if localhash ~= remotehash then return true else return false end if localhash ~= remotehash then return true else return false end
end end
-- start install -- start install
local function startinstall() local function startinstall(i)
-- let the user know that we are starting install local src = getsrc(plugins[i][1])
logwrite('Starting Download...') local plugin = { -- plugin table spec
mp.osd_message('Downloading plugins!') 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 -- check if the user has defined a file for the current plugin
for i = 1, tablelength(plugins) do if not plugin['file'] then
-- check if the user has defined a file for the current plugin logwrite('WARNING! File not configured for '..plugin[1])
if not plugins[i]['file'] then goto continue
logwrite('WARNING! Git repo "' .. plugins[i][1] .. end
'" doesn\'t have a specified file. Skipping download') -- skip install of pinned files
if plugin['pin'] == true then goto continue end
-- FIXME: this results in the last plugin being downloaded & copied twice -- get the plugins tmp download dir
i = i + 1 -- if not we go to the next plugin local plugdir = opts.dl.dir..'/'..testforslash(src['repo']):gsub('.git', '')
end
-- if no destdir defined we set it to the root of the mpv config dir -- if no specified branch we use the default
if not plugins[i]['dir'] then plugin['branch'] = plugin['branch'] or testforslash(
plugins[i]['dir'] = '~~/' run('git -C '..plugdir..' symbolic-ref refs/remotes/origin/HEAD')
end )
-- get the plugins tmp download dir -- check for multiple files
local plugdir = opts.dl.dir .. '/' .. if type(plugin['file']) == 'string' then
string.match(plugins[i][1], '/([^/]+)$'):gsub('.git', '') plugin['file'] = {}
table.insert(plugin['file'], plugins[i]['file'])
-- if no specified branch we use the default end
if not plugins[i]['branch'] then
plugins[i]['branch'] = testforslash(
run('git -C ' .. plugdir .. ' symbolic-ref refs/remotes/origin/HEAD')
)
end
-- get the requested file(s)
for j = 1, #plugin['file'] or 1 do
-- get the file's dir -- get the file's dir
local pluginfile = opts.dl.dir .. '/' .. local pluginfile = plugdir..'/'..plugin['file'][j]
string.match(plugins[i][1], '/([^/]+)$'):gsub('.git', '') ..
'/' .. plugins[i]['file']
-- get the dest dir -- get the dest dir
local destfile = mp.command_native({'expand-path', '~~/'}) .. local destfile = mp.command_native({'expand-path', '~~/'})..
'/' .. plugins[i]['dir'] .. '/'..plugin['dir']..'/'..testforslash(plugin['file'][j])
'/' .. testforslash(plugins[i]['file'])
if fileexists(plugdir .. '/') then if fileexists(plugdir..'/') then
-- if we need to update, update -- if we need to update, update
if checkupdates(plugdir) then if checkupdates(plugdir) then
logwrite(plugins[i]['file'] .. ' is updating.') logwrite(plugin['file'][j]..' is updating.')
-- make sure we are on the main branch -- 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 run('git -C '..plugdir..' pull') -- get the latest commits
else else
logwrite(plugins[i]['file'] .. ' is up to date!') logwrite(plugin['file'][j]..' is up to date!')
end end
else else
clonegit(plugdir, i) clonegit(plugdir, plugin['url'], plugin['branch'])
end end
-- copy the file contents over to the desired location -- copy the file contents over to the desired location
local infile = io.open(pluginfile, 'r') cp(pluginfile, destfile)
local outfile = io.open(destfile, 'w')
outfile:write(infile:read('*a'))
outfile:close()
infile:close()
end end
::continue::
end end
local function initupdate() local function initupdate()
openlog() openlog()
logwrite('# of plugins defined in table: ' .. tablelength(plugins)) logwrite('# of plugins defined in table: '..tablelength(plugins))
os.execute('mkdir -p ' .. opts.dl.dir) -- make download dir 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 -- closing/removing everything
if opts.dl.powerwash == true then if opts.dl.powerwash == true then
logwrite('powerwashing the tmp dir "' .. opts.dl.dir .. '"') logwrite('powerwashing the tmp dir "'..opts.dl.dir..'"')
os.execute('rm -rf ' .. opts.dl.dir) os.execute('rm -rf '..opts.dl.dir)
end end
closelog() closelog()