packages1/pkgm/lib/pkgm.lua

125 lines
2.3 KiB
Lua

local mfs=dofile "/lib/mfs"
local local_pkg_files=false
local pkg_url="https://git.osmarks.tk/heavpoot/packages1/raw/branch/master/"
local res={}
local cache={}
function res.set_pkg_url(x)
pkg_url=x
end
function res.get_pkg_url()
return pkg_url
end
function res.set_local(x)
local_pkg_files=x
end
function res.set_install_dir(x)
return mfs.setcd(x)
end
function res.get_install_dir()
return mfs.getcd()
end
function hget(x)
local f=http.get(x)
if not f then return nil end
local i=f.readAll()
f.close()
return i
end
function res.pkgfile(p,x)
if local_pkg_files then
local cd=mfs.getcd()
mfs.setcd("")
local i=mfs.read(pkg_url.."/"..p.."/"..x)
mfs.setcd(cd)
return i
else
return hget(pkg_url..p.."/"..x)
end
end
function res.pkgmeta(p)
local mt=res.pkgfile(p,"pkgmeta.ltn")
if not mt then error("Package "..p.." does not have a pkgmeta.ltn!") end
return textutils.unserialize(mt)
end
function res.pkginst(p)
mt=res.pkgmeta(p)
local packinfo=mfs.load(".packinfo")
if not packinfo[p] then packinfo[p]={} end
packinfo[p].version=mt.version or "0.1.0"
mfs.save(".packinfo",packinfo)
if mt.files then
for i,thing in pairs(mt.files) do
mfs.mmkdir("/"..i)
for _,v in pairs(thing) do
mfs.write(i.."/"..v,res.pkgfile(p,i.."/"..v))
end
end
end
end
function res.pkguinst(p)
mt=res.pkgmeta(p)
local packinfo=mfs.load(".packinfo")
if not packinfo[p] then return end
packinfo[p]=nil
mfs.save(".packinfo",packinfo)
if mt.files then
for i,thing in pairs(mt.files) do
for _,v in pairs(thing) do
mfs.rm(i.."/"..v)
end
end
end
end
function res.pkginstmulti(pkgs)
local tmp={}
for i=1,#pkgs do
tmp[i]=function() res.pkginst(pkgs[i]) end
end
parallel.waitForAll(unpack(tmp))
end
function res.dependencies(p,toplvl,x)
toplvl=toplvl or true
if toplvl then
cache.vi=mfs.load(".packinfo")
end
x=x or {}
if x[p]~=nil then return end
local mt=res.pkgmeta(p)
if cache.vi[p] then
if mt.version==cache.vi[p].version then
x[p]=0
return
end
end
x[p]=1
for _,v in pairs(mt.dependencies)do
if type(v)=="string" then
res.dependencies(v,false,x)
else
res.dependencies(v.name,false,x)
end
end
if toplvl then
local res={}
for i,v in pairs(x) do
if v==1 then
res[#res+1]=i
end
end
return res
end
end
return res