modify helper functions...

1. modified fileexists now works on both files and directories
2. added test for slash to return the last part of a string before /
3. added run function to make running commands easier
4. modified clonegit to stop using all the crappy shell script stuff
  and make it use new helper functions
5. modified check for updates to stop using the crappy shell stuff and
  actually work
This commit is contained in:
2023-07-16 20:31:26 -04:00
parent 2162b9cd2e
commit a19f79b53a

View File

@ -27,16 +27,31 @@ local function tablelength(T)
end
local function fileexists(name)
local f = io.open(name, 'r')
if f ~= nil then io.close(f) return true else return false end
local ok, err, code = os.rename(name, name)
if not ok then
if code == 13 then
-- Permission denied, but it exists
return true
end
end
return ok, err
end
local function logwrite(string)
if opts.logging.log then
io.write(os.date(opts.logging.logdate) .. ' ' .. string .. '\n')
local function testforslash(str)
if string.match(str, '/') then
return string.match(str, '/([^/]+)$')
else
return str
end
end
local function run(cmd)
local x = io.popen(cmd)
local y = x:read("*a")
x:close()
return y
end
local function openlog()
if opts.logging.log then -- log if asked to
-- get our logfile's full path
@ -47,6 +62,12 @@ local function openlog()
end
end
local function logwrite(string)
if opts.logging.log then
io.write(os.date(opts.logging.logdate) .. ' ' .. string .. '\n')
end
end
local function closelog()
if opts.logging.log then
io.close(f)
@ -54,45 +75,21 @@ local function closelog()
end
-- get the requested git repos
local function clonegit(i)
logwrite('downloading ' .. plugins[i][1])
local function clonegit(plugdir, i)
logwrite('downloading ' .. plugins[i][1])
-- construct the git cmd
local cmd = 'git -C ' .. opts.dl.dir .. ' clone ' .. plugins[i][1]
-- run the command and get the result for logging
local run = io.popen(cmd)
-- BUG: logwriting the git command doens't actually log the output
-- (commented out for now)
-- logwrite(run:read('*a'))
run:close()
-- 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'])
end
-- check for updates
local function checkupdates(i)
local plugdir = opts.dl.dir .. '/' ..
string.match(plugins[i][1], '/([^/]+)$'):gsub('.git', '')
local localhashcmd = 'git -C ' .. plugdir .. ' log -1 --format=format:"%H"'
local remotehashcmd = 'git -C ' .. plugdir ..
' log -n 1 $(git -C ' .. plugdir ..
' branch -r | head -1 | cut -d " " -f 3)' ..
' | head -1 | cut -d " " -f 2'
local a = io.popen(localhashcmd)
local localhash = string.gsub(a:read("*a"), '[\n\r]', '')
a:close()
a = io.popen(remotehashcmd)
local remotehash = string.gsub(a:read("*a"), '[\n\r]', '')
a:close()
if localhash ~= remotehash then
return true
else
return false
end
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')
if localhash ~= remotehash then return true else return false end
end
-- start install