2019-06-28 17:50:02 +00:00
|
|
|
local BulkGet = require('opus.bulkget')
|
|
|
|
local Git = require('opus.git')
|
|
|
|
local Packages = require('opus.packages')
|
|
|
|
local Util = require('opus.util')
|
2018-11-03 22:13:41 +00:00
|
|
|
|
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
|
|
|
|
2019-11-10 04:15:07 +00:00
|
|
|
local function makeSandbox()
|
|
|
|
local sandbox = setmetatable(Util.shallowCopy(_ENV), { __index = _G })
|
|
|
|
_G.requireInjector(sandbox)
|
|
|
|
return sandbox
|
|
|
|
end
|
|
|
|
|
2018-11-03 22:13:41 +00:00
|
|
|
local function Syntax(msg)
|
2020-05-01 00:51:36 +00:00
|
|
|
print('Syntax: package list | install [name] ... | update [name] | updateall | uninstall [name]\n')
|
|
|
|
error(msg)
|
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-11-10 22:58:36 +00:00
|
|
|
local function runScript(script)
|
|
|
|
if script then
|
|
|
|
local s, m = pcall(function()
|
|
|
|
local fn, m = load(script, 'script', nil, makeSandbox())
|
|
|
|
if not fn then
|
|
|
|
error(m)
|
|
|
|
end
|
|
|
|
fn()
|
|
|
|
end)
|
|
|
|
if not s and m then
|
|
|
|
_G.printError(m)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-03 14:44:30 +00:00
|
|
|
local function install(name, isUpdate, ignoreDeps)
|
2019-03-26 04:31:25 +00:00
|
|
|
local manifest = Packages:downloadManifest(name) or error('Invalid package')
|
2019-01-08 16:38:19 +00:00
|
|
|
|
2019-07-03 14:44:30 +00:00
|
|
|
if not ignoreDeps then
|
|
|
|
if manifest.required then
|
|
|
|
for _, v in pairs(manifest.required) do
|
|
|
|
if isUpdate or not Packages:isInstalled(v) then
|
|
|
|
install(v, isUpdate)
|
|
|
|
end
|
2019-01-08 16:38:19 +00:00
|
|
|
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)
|
2020-05-01 00:51:36 +00:00
|
|
|
-- clear out contents before install/update
|
|
|
|
-- TODO: figure out whether to run
|
|
|
|
-- install/uninstall for the package
|
|
|
|
fs.delete(packageDir)
|
|
|
|
|
2019-02-07 08:10:05 +00:00
|
|
|
local showProgress = progress(Util.size(list))
|
2019-03-06 21:56:41 +00:00
|
|
|
|
|
|
|
local getList = { }
|
2019-02-07 08:10:05 +00:00
|
|
|
for path, entry in pairs(list) do
|
2019-03-06 21:56:41 +00:00
|
|
|
table.insert(getList, {
|
|
|
|
path = fs.combine(packageDir, path),
|
|
|
|
url = entry.url
|
|
|
|
})
|
2018-11-04 18:00:37 +00:00
|
|
|
end
|
2019-03-06 21:56:41 +00:00
|
|
|
|
|
|
|
BulkGet.download(getList, function(_, s, m)
|
|
|
|
if not s then
|
|
|
|
error(m)
|
|
|
|
end
|
|
|
|
showProgress()
|
|
|
|
end)
|
2019-11-10 04:15:07 +00:00
|
|
|
|
|
|
|
if not isUpdate then
|
2019-11-10 22:58:36 +00:00
|
|
|
runScript(manifest.install)
|
2019-11-10 04:15:07 +00:00
|
|
|
end
|
2018-11-04 18:00:37 +00:00
|
|
|
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
|
|
|
|
|
2019-07-03 14:44:30 +00:00
|
|
|
if action == 'refresh' then
|
|
|
|
print('Downloading...')
|
|
|
|
Packages:downloadList()
|
|
|
|
print('refresh complete')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if action == 'updateall' then
|
|
|
|
for name in pairs(Packages:installed()) do
|
|
|
|
install(name, true, true)
|
|
|
|
end
|
|
|
|
print('updateall complete')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-11-04 18:00:37 +00:00
|
|
|
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
|
2019-11-10 22:58:36 +00:00
|
|
|
|
2019-11-10 04:15:07 +00:00
|
|
|
local manifest = Packages:getManifest(name)
|
2019-11-10 22:58:36 +00:00
|
|
|
runScript(manifest.uninstall)
|
|
|
|
|
2018-11-04 18:00:37 +00:00
|
|
|
local packageDir = fs.combine('packages', name)
|
2020-05-01 00:51:36 +00:00
|
|
|
fs.delete(packageDir)
|
2018-11-04 18:00:37 +00:00
|
|
|
print('removed: ' .. packageDir)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-11-06 21:43:24 +00:00
|
|
|
Syntax('Invalid command')
|