diff options
author | Squibid <me@zacharyscheiman.com> | 2023-10-17 22:03:41 -0400 |
---|---|---|
committer | Squibid <me@zacharyscheiman.com> | 2023-10-17 22:03:41 -0400 |
commit | 6f091a76482703f45f00eb3a800173ef918ddc6b (patch) | |
tree | 624293f83da695da0f70deeb88535f7607beef12 /eatit.lua | |
parent | 96c3e6f3e627e601d388f9884b81d896d82f5f1e (diff) | |
download | eat-it-6f091a76482703f45f00eb3a800173ef918ddc6b.tar.gz eat-it-6f091a76482703f45f00eb3a800173ef918ddc6b.tar.bz2 eat-it-6f091a76482703f45f00eb3a800173ef918ddc6b.zip |
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
Diffstat (limited to 'eatit.lua')
-rw-r--r-- | eatit.lua | 153 |
1 files changed, 94 insertions, 59 deletions
@@ -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 @@ -66,7 +95,7 @@ end local function logwrite(string) 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 @@ -77,100 +106,106 @@ 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 local function checkupdates(plugdir) - local localhash = run('git -C ' .. plugdir .. ' log -1 --format=format:"%H"') - local remotehash = run('git -C ' .. plugdir .. ' rev-parse $(git -C ' .. - plugdir .. ' branch -r) | tail -1') + local localhash = run('git -C '..plugdir..' log -1 --format=format:"%H"') + local remotehash = run('git -C '..plugdir..' rev-parse $(git -C '.. + plugdir..' branch -r) | tail -1') if localhash ~= remotehash then return true else return false end end -- start install -local function startinstall() - -- let the user know that we are starting install - logwrite('Starting Download...') - mp.osd_message('Downloading plugins!') - - -- 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 +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, + } + + -- check if the user has defined a file for the current plugin + 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 - -- if no destdir defined we set it to the root of the mpv config dir - if not plugins[i]['dir'] then - plugins[i]['dir'] = '~~/' - end + -- get the plugins tmp download dir + local plugdir = opts.dl.dir..'/'..testforslash(src['repo']):gsub('.git', '') - -- get the plugins tmp download dir - local plugdir = opts.dl.dir .. '/' .. - string.match(plugins[i][1], '/([^/]+)$'):gsub('.git', '') + -- if no specified branch we use the default + plugin['branch'] = plugin['branch'] or testforslash( + run('git -C '..plugdir..' symbolic-ref refs/remotes/origin/HEAD') + ) - -- if no specified branch we use the default - if not plugins[i]['branch'] then - plugins[i]['branch'] = testforslash( - run('git -C ' .. plugdir .. ' symbolic-ref refs/remotes/origin/HEAD') - ) - end + -- 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']) + local destfile = mp.command_native({'expand-path', '~~/'}).. + '/'..plugin['dir']..'/'..testforslash(plugin['file'][j]) - if fileexists(plugdir .. '/') then + 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 .. ' pull') -- get the latest commits + 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() openlog() - logwrite('# of plugins defined in table: ' .. tablelength(plugins)) - os.execute('mkdir -p ' .. opts.dl.dir) -- make download dir - startinstall() + logwrite('# of plugins defined in table: '..tablelength(plugins)) + os.execute('mkdir -p '..opts.dl.dir) -- make download dir + + -- 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 - logwrite('powerwashing the tmp dir "' .. opts.dl.dir .. '"') - os.execute('rm -rf ' .. opts.dl.dir) + logwrite('powerwashing the tmp dir "'..opts.dl.dir..'"') + os.execute('rm -rf '..opts.dl.dir) end closelog() |