opus/sys/apps/package.lua

102 lines
2.4 KiB
Lua
Raw Normal View History

2018-11-04 18:00:37 +00:00
local Git = require('git')
2018-11-03 22:13:41 +00:00
local Packages = require('packages')
local Util = require('util')
2018-11-06 21:43:24 +00:00
local fs = _G.fs
local term = _G.term
2018-11-03 22:13:41 +00:00
2018-11-06 21:43:24 +00:00
local args = { ... }
local action = table.remove(args, 1)
2018-11-03 22:13:41 +00:00
local function Syntax(msg)
2018-11-06 21:43:24 +00:00
_G.printError(msg)
print('\nSyntax: Package list | install [name] ... | update [name] | uninstall [name]')
error(0)
2018-11-03 22:13:41 +00:00
end
2018-11-04 18:00:37 +00:00
local function progress(max)
-- modified from: https://pastebin.com/W5ZkVYSi (apemanzilla)
local _, y = term.getCursorPos()
local wide, _ = term.getSize()
term.setCursorPos(1, y)
term.write("[")
term.setCursorPos(wide - 6, y)
term.write("]")
local done = 0
return function()
done = done + 1
local value = done / max
term.setCursorPos(2,y)
term.write(("="):rep(math.floor(value * (wide - 8))))
local percent = math.floor(value * 100) .. "%"
term.setCursorPos(wide - percent:len(),y)
term.write(percent)
2018-11-03 22:13:41 +00:00
end
2018-11-04 18:00:37 +00:00
end
2019-01-08 16:38:19 +00:00
local function install(name, isUpdate)
2018-11-03 22:13:41 +00:00
local manifest = Packages:getManifest(name) or error('Invalid package')
2019-01-08 16:38:19 +00:00
if manifest.required then
for _, v in pairs(manifest.required) do
if isUpdate or not Packages:isInstalled(v) then
install(v, isUpdate)
end
end
end
print(string.format('%s: %s',
isUpdate and 'Updating' or 'Installing',
name))
2018-11-04 18:00:37 +00:00
local packageDir = fs.combine('packages', name)
2019-02-07 08:10:05 +00:00
local list = Git.list(manifest.repository)
local showProgress = progress(Util.size(list))
for path, entry in pairs(list) do
Util.download(entry.url, fs.combine(packageDir, path))
showProgress()
2018-11-04 18:00:37 +00:00
end
end
2018-11-06 21:43:24 +00:00
if action == 'list' then
for k in pairs(Packages:list()) do
Util.print('[%s] %s', Packages:isInstalled(k) and 'x' or ' ', k)
end
return
end
2018-11-04 18:00:37 +00:00
if action == 'install' then
local name = args[1] or Syntax('Invalid package')
if Packages:isInstalled(name) then
error('Package is already installed')
end
install(name)
2019-01-08 16:38:19 +00:00
print('installation complete\n')
_G.printError('Reboot is required')
2018-11-04 18:00:37 +00:00
return
end
if action == 'update' then
local name = args[1] or Syntax('Invalid package')
if not Packages:isInstalled(name) then
error('Package is not installed')
2018-11-03 22:13:41 +00:00
end
2019-01-08 16:38:19 +00:00
install(name, true)
2018-11-04 18:00:37 +00:00
print('update complete')
return
2018-11-03 22:13:41 +00:00
end
2018-11-04 18:00:37 +00:00
if action == 'uninstall' then
local name = args[1] or Syntax('Invalid package')
if not Packages:isInstalled(name) then
error('Package is not installed')
end
local packageDir = fs.combine('packages', name)
fs.delete(packageDir)
print('removed: ' .. packageDir)
return
end
2018-11-06 21:43:24 +00:00
Syntax('Invalid command')