Files
Halyde/halyde/apps/argentum.lua
T
2026-04-30 15:09:56 +00:00

1 line
14 KiB
Lua

local a={...}local b=a[1]table.remove(a,1)local c=import("filesystem")local d=import("component")local e=import("/argentum/registry.cfg")if not b then shell.run("help argentum")return end;if not d.list("internet")()then print("\27[91mThis program requires an internet card to run.")return end;local f=d.internet;local g;if table.find(a,"-s")then g=table.remove(a,table.find(a,"-s")+1)table.remove(a,table.find(a,"-s"))print("Using "..g.." as package source")elseif table.find(a,"--source")then g=table.remove(a,table.find(a,"--source")+1)table.remove(a,table.find(a,"--source"))print("Using "..g.." as package source")else print("Using main registry as package source")end;if g and g:sub(1,1)=="/"and g:sub(-1,-1)~="/"then g=g.."/"end;local h=table.copy(a)local function i(a)if a:sub(1,1)=="/"then if not c.exists(a)then return false,"file does not exist"end;local a,b,c=c.open(a,"r"),"",nil;repeat c=a:read(math.huge)b=b..(c or"")until not c;a:close()return b else local b,c,d=nil,"",nil;local a,e=pcall(function()b=f.request(a)b:finishConnect()end)if not a then return false,e end;local a=b:response()if a and a~=200 then return false,a end;repeat d=b.read(math.huge)c=c..(d or"")until not d;return c end end;local f=1;local function j(a,b)b=b or e[a]local b,c=i(b.."argentum.cfg")if not b or b==""then print("\27[91mCould not fetch Ag config: "..(c or"returned nil data"))return false end;local b,c=load(b,"=argentum.cfg","bt",{})if not b then print("\27[91mCould not fetch Ag config: "..c.."\nPlease contact the package owner.")return false end;local c;local b,d=pcall(function()c=b()end)if not b then print("\27[91mCould not fetch Ag config: "..d.."\nPlease contact the package owner.")return false end;if not c[a]or not c[a].maindir or not c[a].directories or not c[a].files or not c[a].version then local a=("\27[91mAg config of "..a.." is improperly configured.\nPlease contact the package owner.")end;return c end;local function k(b)if not e[b]and not g then print("\27[91mPackage "..b.." does not exist.")return false end;if c.exists("/argentum/store/"..b)then print("\27[91mPackage "..b.." is already installed.")return false end;agcfg=j(b,g)if not agcfg then return false end;if agcfg[b].dependencies then for a,a in ipairs(agcfg[b].dependencies)do if not e[a]and not agcfg[a]then local a=read(nil,"\27[91mPackage "..b.." requires dependency "..a.." that does not exist.\n[A - Abort/s - Skip]")if a:lower()~="s"then c.remove("/argentum/store/"..b)return false end end end;for c,c in pairs(agcfg[b].dependencies)do print(b.." depends on "..c)if not table.find(a,c)and k(c)then table.insert(a,table.find(a,b),c)table.insert(h,c)f=f+1 end end end;return true end;local function l(a,b,c)a=tostring(a)if c==nil then c=' 'end;return string.rep(c,b-#a)..a end;local d=d.gpu;local m,n=d.getResolution()local function o(a,b)local a=string.format("%s %s%%",a,l(math.floor(b*100),2))a=a..string.rep(" ",m-#a)local b=math.floor(b*m)d.setBackground(65280)d.setForeground(0)d.set(1,n,a:sub(1,b))d.setBackground(0)d.setForeground(16777215)d.set(b+1,n,a:sub(b+1))end;local function l()d.setBackground(0)d.fill(1,n,m,1," ")end;local function d(a,b)if not b then b=false end;if not b then print("Installing "..a.."...")end;local d=j(a,g)if not d then return false end;local f=g or e[a]local g="V"..d[a].version;if d[a].dependencies then for b,b in ipairs(d[a].dependencies)do if not e[b]and not d[b]then local b=read(nil,"\27[91mPackage "..a.." requires dependency "..b.." that does not exist.\n[A - Abort/s - Skip]")if b:lower()~="s"then c.remove("/argentum/store/"..a)return false end end end;for a,a in pairs(d[a].dependencies)do if e[a]or d[a]then g=g.."\nD"..a end end end;if d[a].directories then for a,a in pairs(d[a].directories)do if a:sub(-1,-1)~="/"then a=a.."/"end;g="A"..a.."\n"..g;if not c.exists(a)then c.makeDirectory(a)end end end;for e,h in ipairs(d[a].files)do l()::retry::print(" Downloading "..h.."...")o(a,e/#d[a].files)local d,e=i(f..d[a].maindir..h)if not d then l()local b=read(nil,"\27[91mCould not fetch "..h..": "..e.."\n\27[0m[a - Abort/R - Retry/s - Skip]")if b:lower()=="a"then c.remove("/argentum/store/"..a)return false elseif b:lower()=="s"then goto skip else goto retry end end;if c.exists(h)and not b then if not c.exists("/argentum/store/"..a.."/files/"..h:match("(.*/)"))then c.makeDirectory("/argentum/store/"..a.."/files/"..h:match("(.*/)"))end;c.copy(h,"/argentum/store/"..a.."/files/"..h)g=g.."\nM"..h else g=g.."\nA"..h end;local a=c.open(h,"w")a:write(d)a:close()::skip::end;l()c.makeDirectory("/argentum/store/"..a)local a=c.open("/argentum/store/"..a.."/package.cfg","w")a:write(g)a:close()return true end;local function m(a)print("Removing "..a.."...")if not c.exists("/argentum/store/"..a.."/package.cfg")then print("\27[91mLocal Ag config of "..a.." does not exist.")return false end;local b,d,e=c.open("/argentum/store/"..a.."/package.cfg","r"),"",nil;repeat e=b:read(math.huge)d=d..(e or"")until not e;b:close()d=d.."\n"local b=0;for e in d:gmatch("(.-)\n")do b=b+1;if e:sub(1,1)=="A"then l()::retry::print(" Removing "..e:sub(2).."...")if e:sub(-1,-1)=="/"and c.list(e:sub(2))[1]then print(" There are still files in "..e:sub(2)..". Skipping.")else o(a,b/select(2,d:gsub("\n","\n")))local a,b=c.remove(e:sub(2))if not a then l()local a=read(nil,"\27[91mFailed to remove "..e:sub(2)..": "..b.."\n\27[0m[a - Abort/r - Retry/S - Skip]")if a:lower()=="a"then return false elseif a:lower()=="r"then goto retry end end end elseif e:sub(1,1)=="M"then l()::retry::print(" Reverting "..e:sub(2).."...")o(a,b/select(2,d:gsub("\n","\n")))local a,b,d=c.open("/argentum/store/"..a.."/files/"..e:sub(2),"r"),"",nil;if not a then l()local a=read(nil,"\27[91mFailed to revert "..e:sub(2)..": "..b.."\n\27[0m[a - Abort/R - Retry/s - Skip]")if a:lower()=="a"then return false elseif a:lower()=="s"then goto skip else goto retry end end;repeat d=a:read(math.huge)b=b..(d or"")until not d;a:close()local a=c.open(e:sub(2),"w")a:write(b)a:close()::skip::end end;l()c.remove("/argentum/store/"..a.."/")return true end;local function l(a)print("Updating "..a.."...")local b=j(a,g)if not b then return false end;local c,e,f=c.open("/argentum/store/"..a.."/package.cfg","r"),"",nil;repeat f=c:read(math.huge)e=e..(f or"")until not f;c:close()local c={}for d in(e.."\n"):gmatch("(.-)\n")do if d:sub(1,1)=="A"or d:sub(1,1)=="M"then if b[a].directories then if not table.find(b[a].files,d:sub(2))and not table.find(b[a].directories,d:sub(2,-2))then table.insert(c,d:sub(2))end else if not table.find(b[a].files,d:sub(2))then table.insert(c,d:sub(2))end end end end;for a,a in pairs(c)do print(" Removing "..a.."...")end;return d(a,true)end;local n={}if b=="install"then if not a or not a[1]then print("Please specify packages to install.")return end;print("Fetching Ag registry...")local b,g=i("https://raw.githubusercontent.com/Team-Cerulean-Blue/Halyde/refs/heads/main/argentum/registry.cfg")if b then local a=c.open("/argentum/registry.cfg","w")a:write(b)a:close()else print("\27[91mFailed to fetch Ag registry: "..(g or"returned nil"))end;e=import("/argentum/registry.cfg")while true do if not k(a[f])then table.insert(n,a[f])table.remove(h,table.find(h,a[f]))end;f=f+1;if f>#a then break end end;local b;if#n==0 then print("Packages that will be installed: "..table.concat(h,", "))if read(nil,"Would you like to proceed? [Y/n] "):lower()=="n"then return end elseif#h==0 then print("None of the packages can be installed.")return else print("Some packages cannot be installed.")print("Packages that will be installed: "..table.concat(h,", "))print("Packages that cannot be installed: "..table.concat(n,", "))if read(nil,"Would you like to proceed? [y/N] "):lower()~="y"then return end end;for b,b in pairs(n)do table.remove(a,table.find(a,b))end;n={}for a,a in ipairs(a)do if not d(a)then table.insert(n,a)table.remove(h,table.find(h,a))end end;if#n==0 then print("Installation completed successfully.")print("Packages installed: "..table.concat(h,", "))elseif#h==0 then print("All packages failed to install.")print("Packages that could not be installed: "..table.concat(n,", "))else print("Some packages failed to install.")print("Packages installed: "..table.concat(h,", "))print("Packages that could not be installed: "..table.concat(n,", "))end elseif b=="remove"then if not a or not a[1]then print("Please specify packages to remove.")return end;while true do if not c.exists("/argentum/store/"..a[f])then print("\27[91mPackage "..a[f].." is not installed.")table.insert(n,a[f])table.remove(h,table.find(h,a[f]))table.remove(a,table.find(a,a[f]))f=f-1 elseif a[f]=="halyde"then print("\27[91mFor obvious reasons, you can't uninstall Halyde.")table.insert(n,a[f])table.remove(h,table.find(h,a[f]))table.remove(a,table.find(a,a[f]))f=f-1 elseif a[f]=="argentum"then print("\27[91mFor obvious reasons, you can't uninstall Argentum.")table.insert(n,a[f])table.remove(h,table.find(h,a[f]))table.remove(a,table.find(a,a[f]))f=f-1 end;f=f+1;if f>#a then break end end;local b=c.list("/argentum/store")for b,b in pairs(b)do if b:sub(-1,-1)=="/"and c.exists("/argentum/store/"..b.."package.cfg")then local c,d,e=c.open("/argentum/store/"..b.."package.cfg","r"),"",nil;repeat e=c:read(math.huge)d=d..(e or"")until not e;c:close()for c in(d.."\n"):gmatch("(.-)\n")do for d=1,#a do if c=="D"..a[d]then print(a[d].." depends on "..b:sub(1,-2))if not table.find(a,b:sub(1,-2))then table.insert(a,table.find(a,a[d]),b:sub(1,-2))table.insert(h,b:sub(1,-2))d=d+1 end end end end end end;local b;if#n==0 then print("Packages that will be removed: "..table.concat(h,", "))if read(nil,"Would you like to proceed? [Y/n] "):lower()=="n"then return end elseif#h==0 then print("None of the packages can be removed.")return else print("Some packages cannot be removed.")print("Packages that will be removed: "..table.concat(h,", "))print("Packages that cannot be removed: "..table.concat(n,", "))if read(nil,"Would you like to proceed? [y/N] "):lower()~="y"then return end end;for b,b in pairs(n)do table.remove(a,table.find(a,b))end;n={}for a,a in ipairs(a)do if not m(a)then table.insert(n,a)table.remove(h,table.find(h,a))end end;if#n==0 then print("Removal completed successfully.")print("Packages removed: "..table.concat(h,", "))elseif#h==0 then print("All packages failed to be removed.")print("Packages that could not be removed: "..table.concat(n,", "))else print("Some packages failed to be removed.")print("Packages removed: "..table.concat(h,", "))print("Packages that could not be removed: "..table.concat(n,", "))end elseif b=="update"then print("Fetching Ag registry...")local b,d=i("https://raw.githubusercontent.com/Team-Cerulean-Blue/Halyde/refs/heads/main/argentum/registry.cfg")if b then local a=c.open("/argentum/registry.cfg","w")a:write(b)a:close()else print("\27[91mFailed to fetch Ag registry: "..(d or"returned nil"))end;e=import("/argentum/registry.cfg")if not a[1]then local b=c.list("/argentum/store/")for b,b in pairs(b)do if b:sub(-1,-1)=="/"and c.exists("/argentum/store/"..b.."package.cfg")then table.insert(a,b:sub(1,-2))table.insert(h,b:sub(1,-2))end end end;while true do local b=false;if not c.exists("/argentum/store/"..a[f])then print("\27[91mPackage "..a[f].." is not installed.")table.insert(n,a[f])table.remove(h,table.find(h,a[f]))table.remove(a,table.find(a,a[f]))f=f-1;b=true end;if not b then local b=j(a[f],g)if not b then return false end;local c,d,e=c.open("/argentum/store/"..a[f].."/package.cfg","r"),"",nil;repeat e=c:read(math.huge)d=d..(e or"")until not e;c:close()local c="0.0.0"for a in(d.."\n"):gmatch("(.-)\n")do if a:sub(1,1)=="V"then c=a:sub(2)break end end;if b[a[f]].version==c then table.remove(h,table.find(h,a[f]))table.remove(a,table.find(a,a[f]))f=f-1 else print(a[f].." is out of date [\x1b[93m"..c.."\x1b[39m < \x1b[92m"..b[a[f]].version.."\x1b[39m]")end end;f=f+1;if f>#a then break end end;local b;if#h==0 then if#n==0 then print("All packages are up to date.")return else print("None of the packages can be updated.")return end elseif#n==0 then print("Packages that will be updated: "..table.concat(h,", "))if read(nil,"Would you like to proceed? [Y/n] "):lower()=="n"then return end else print("Some packages cannot be updated.")print("Packages that will be updated: "..table.concat(h,", "))print("Packages that cannot be updated: "..table.concat(n,", "))if read(nil,"Would you like to proceed? [y/N] "):lower()~="y"then return end end;for b,b in pairs(n)do table.remove(a,table.find(a,b))end;n={}for b,b in pairs(a)do if not l(b)then table.insert(n,a[f])table.remove(h,table.find(h,a[f]))table.remove(a,table.find(a,a[f]))goto skip end::skip::end;if#n==0 then print("Update completed successfully.")print("Packages updated: "..table.concat(h,", "))elseif#h==0 then print("All packages failed to update.")print("Packages that could not update: "..table.concat(n,", "))else print("Some packages failed to update.")print("Packages updated: "..table.concat(h,", "))print("Packages that could not update: "..table.concat(n,", "))end elseif b=="info"then if not a[1]then print("Please specify a package to show information about.")return end;print("Fetching Ag registry...")local b,d=i("https://raw.githubusercontent.com/Team-Cerulean-Blue/Halyde/refs/heads/main/argentum/registry.cfg")if b then local a=c.open("/argentum/registry.cfg","w")a:write(b)a:close()else print("\27[91mFailed to fetch Ag registry: "..(d or"returned nil"))end;e=import("/argentum/registry.cfg")if not e[a[1]]and not g then print("\27[91mPackage "..a[1].." does not exist.")return end;local b=j(a[1],g)if not b then return false end;print("\27[93m"..a[1].."\27[0m v"..b[a[1]].version.."\n "..(b[a[1]].description or"No description."):gsub("\n"," \n"))elseif b=="search"then if not a[1]then print("Please specify a search term.")return end;print("Fetching Ag registry...")local b,d=i("https://raw.githubusercontent.com/Team-Cerulean-Blue/Halyde/refs/heads/main/argentum/registry.cfg")if b then local a=c.open("/argentum/registry.cfg","w")a:write(b)a:close()else print("\27[91mFailed to fetch Ag registry: "..(d or"returned nil"))end;e=import("/argentum/registry.cfg")local b={}for c,d in pairs(e)do if c:find(a[1],1,true)then table.insert(b,c)end end;if not b[1]then print("No search results found for "..a[1]..".")return end;table.sort(b)print("Search results: \n "..table.concat(b,"\n "))elseif b=="list"then print("Fetching Ag registry...")local a,b=i("https://raw.githubusercontent.com/Team-Cerulean-Blue/Halyde/refs/heads/main/argentum/registry.cfg")if a then local b=c.open("/argentum/registry.cfg","w")b:write(a)b:close()else print("\27[91mFailed to fetch Ag registry: "..(b or"returned nil"))end;e=import("/argentum/registry.cfg")local a={}for b,c in pairs(e)do table.insert(a,b)end;table.sort(a)print("List of available Ag packages: \n "..table.concat(a,"\n "))else shell.run("help ag")end