Advertisement
promitheus_sal

pccpkg.lua

Dec 6th, 2022 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.36 KB | None | 0 0
  1. --[[
  2. (C) PROMETHEUS TECHNLOGIES 2022
  3.  
  4. pccpkg.lua v0.1
  5. Prometheus Technologies Computer Craft Package Manager
  6. ]]
  7.  
  8. local pb = require("pastebinlib")
  9.  
  10. local REPO_PB_CODE = "t1n53vfw"
  11. local PCCPKG_DIR = "/pccpkg"
  12. local REPO_FILENAME = PCCPKG_DIR.."/repo.list"
  13. local INSTALLED_LIST_FILENAME = PCCPKG_DIR.."/installed.list"
  14. if not fs.exists(PCCPKG_DIR) then
  15.     fs.makeDir(PCCPKG_DIR)
  16. end
  17. if not fs.exists(REPO_FILENAME) then
  18.     print("WARNING: LOCAL REPOSITORY INDEX MISSING!")
  19.     io.write("FETCHING FROM REMOTE...")
  20.     local f = io.open(REPO_FILENAME,"w")
  21.     f:write(pb.get(REPO_PB_CODE))
  22.     f:flush()
  23.     f:close()
  24.     print(" DONE!")
  25. end
  26. if not fs.exists(INSTALLED_LIST_FILENAME) then
  27.     io.open(INSTALLED_LIST_FILENAME,"w"):close()
  28. end
  29.  
  30. local header = [[Copyright (C) 2022 Prometheus Technologies
  31. pccpkg.lua v0.1
  32. Prometheus Technologies ComputerCraft Package Manager
  33. ]]
  34. local helptxt = [[Usage: pccpkg [command] [options]
  35.  
  36. Most commonly used commands:
  37.  
  38. update: updates local copy of repository from remote
  39. install: installs (or updates) package from remote
  40. list: list packages using regex (followed by 'any' or 'local' and then by quoted regex)]]
  41. local function printHelp()
  42.     print(header)
  43.     print(helptxt)
  44. end
  45.  
  46. if #arg==0 then
  47.     printHelp()
  48.     return
  49. end
  50. if arg[1]=="help" then
  51.     printHelp()
  52.     return
  53. end
  54.  
  55. if arg[1]=="update" then
  56.     io.write("Downloading repository data...")
  57.     local f = io.open(REPO_FILENAME,"w")
  58.     f:write(pb.get(REPO_PB_CODE))
  59.     f:flush()
  60.     f:close()
  61.     print(" Success")
  62.     print("DONE!")
  63.     return
  64. end
  65.  
  66. local fullrepo = loadfile(REPO_FILENAME)()
  67. local packages = fullrepo.packages
  68.  
  69. local function installPackage(id)
  70.     for p in io.lines(INSTALLED_LIST_FILENAME) do
  71.         if id==p then
  72.             print("Package is already installed!\nExiting...")
  73.             return
  74.         end
  75.     end
  76.     io.write(string.format("Installing package '%s'...\nFetching manifest...",id))
  77.     local pd = fs.combine(PCCPKG_DIR,id)
  78.     fs.makeDir(pd)
  79.     local mf = fs.combine(pd,"manifest")
  80.     local f = io.open(mf,"w")
  81.     f:write(pb.get(packages[id]))
  82.     f:flush()
  83.     f:close()
  84.     io.write(" Done!\nRunning installation script...")
  85.     local s,mf = pcall(loadfile(mf))
  86.     if not s then print(string.format("ERROR!\nERROR PARSING MANIFEST: %s\nABORTING...",tostring(mf))) end
  87.     if (type(mf)~="table")or (type(mf.install)~="function") then io.write(" ERROR!\nBAD MANIFEST FILE!\nABORTING!!!\n") end
  88.     local s,e = pcall(mf.install,pb,PCCPKG_DIR,pd)
  89.     if not s then print(string.format("ERROR RUNNING INSTALLATION SCRIPT: %s\nABORTING...",tostring(e))) end
  90.     print(string.format(" Done!\nPackage '%s' successfully installed!",id))
  91.     local i = io.open(INSTALLED_LIST_FILENAME,"a")
  92.     i:write("\n"..tostring(id))
  93.     i:flush()
  94.     i:close()
  95. end
  96.  
  97. local function removePackage(id)
  98.     io.write(string.format("Removing package '%s'...\nReading manifest...",id))
  99.     local pd = fs.combine(PCCPKG_DIR,id)
  100.     if not fs.exists(pd) then print(" ERROR!\nNO SUCH PACKAGE INSTALLATION FOUND!")return end
  101.     local mf = fs.combine(pd,"manifest")
  102.     if not fs.exists(mf) then print(" ERROR!\nERROR MISSING MANIFEST FILE!") end
  103.     local s,v = pcall(loadfile(mf))
  104.     if not s then io.write(" ERROR!\nERROR PARSING MANIFEST FILE:\n"..tostring(v).."\nABORTING!!!\n") end
  105.     if (type(v)~="table")or(type(v.remove)~="function") then io.write(" ERROR!\nBAD MANIFEST FILE!\nABORTING!!!\n") end
  106.     local s,e = pcall(v.remove,pb,PCCPKG_DIR,pd)
  107.     if not s then io.write(string.format("ERROR RUNNING REMOVAL SCRIPT: %s\nABORTING!!!\n",tostring(e))) end
  108.     fs.delete(pd)
  109.     io.write(string.format(" Done!\nPackage '%s' successfully removed!\n",id))
  110. end
  111.  
  112. if arg[1]=="install" then
  113.     if arg[2]==nil then print("SPECIFY PACKAGE TO INSTALL!")return -1 end
  114.     if packages[arg[2]]==nil then print("NO SUCH PACKAGE FOUND! Try `pccpkg update`...")return -1 end
  115.     installPackage(arg[2])
  116. end
  117.  
  118. if arg[1]=="remove" then
  119.     if arg[2]==nil then print("SPECIFY PACKAGE TO REMOVE!")return -1 end
  120.     removePackage(arg[2])
  121. end
  122.  
  123. if arg[1]=="list" then
  124.     arg[2]=arg[2]or"any"
  125.     arg[3] = arg[3]or".+"
  126.     if arg[2]=="any" then
  127.         print("Listing all available packages...")
  128.         for i,_ in pairs(packages) do if string.match(i,arg[3]) then print(i) end end
  129.     elseif arg[2]=="local" then
  130.         print("Listing locally installed packages...")
  131.         for v in io.lines(INSTALLED_LIST_FILENAME) do if true or string.match(v,arg[3]) then print(v) end end
  132.     end
  133.     print("Done!")
  134. end
  135.  
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement