sanovskiy

san-get (gh)

May 4th, 2020 (edited)
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.06 KB | None | 0 0
  1. args = {...}
  2. command = args[1] or "help"
  3. args[2] = args[2] or ""
  4.  
  5. if not (fs.exists("/etc")) then
  6.     fs.makeDir("/etc")
  7. end
  8. if not (fs.exists("/lib")) then
  9.     fs.makeDir("/lib")
  10. end
  11.  
  12. local InitAnnounced = false
  13.  
  14. if not (fs.exists("/lib/json")) then
  15.     if not (InitAnnounced) then
  16.         print("Initial install...")
  17.         InitAnnounced = true
  18.     end
  19.     print("  Installing JSON API")
  20.     shell.run("pastebin", "get 4nRg9CHU /lib/json")
  21. end
  22. os.loadAPI("/lib/json")
  23.  
  24. function printUsage()
  25.     print("Usage: ")
  26.     print("  san-get <selfupdate|install <progname>>")
  27.     return
  28. end
  29.  
  30. function vardump(x)
  31.     for k, v in pairs(x) do
  32.         print(k .. "  " .. v)
  33.     end
  34. end
  35.  
  36. function table.contains(table, element)
  37.     for _, value in pairs(table) do
  38.         if value == element then
  39.             return true
  40.         end
  41.     end
  42.     return false
  43. end
  44.  
  45. function table.join(table, glue)
  46.     local result
  47.     local counter = 0
  48.     for _, value in pairs(table) do
  49.         if counter == 0 then
  50.             result = value
  51.         else
  52.             result = result .. glue .. value
  53.         end
  54.     end
  55.     return result
  56. end
  57.  
  58. function download(sUrl, file)
  59.   -- Check if the URL is valid
  60.   local ok, err = http.checkURL(sUrl)
  61.   -- print("Downloading "..sUrl.." into "..file)
  62.   if not ok then
  63.       printError(err or "Invalid URL.")
  64.       return false
  65.   end
  66.  
  67.   local response = http.get(sUrl , { ["Cache-Control"] = "no-cache" } , true)
  68.   if not response then
  69.       printError("Download failed.")
  70.       return false
  71.   end
  72.  
  73.   local sResponse = response.readAll()
  74.   response.close()
  75.  
  76.   f = fs.open(file, "w")
  77.   f.write(sResponse)
  78.   f.close()
  79.   return true
  80. end
  81.  
  82. function updateRepo()
  83.     print("Updating repo info")
  84.     if fs.exists("/etc/repo.json") then
  85.         fs.delete("/etc/repo.json")
  86.     end
  87.     local randstr = tostring(math.floor(math.random() * 100000))
  88.     download("https://gitlab.com/sanovskiy/mc-lua/-/raw/master/cc/repo.json?" .. randstr,"/etc/repo.json")
  89. end
  90.  
  91. function readRepoFile()
  92.     if not (fs.exists("/etc/repo.json")) then
  93.         updateRepo()
  94.     end
  95.     return json.decodeFromFile("/etc/repo.json")
  96. end
  97.  
  98. function calcRequirements(softName, exclude)
  99.     local repo = readRepoFile()
  100.     exclude = exclude or {}
  101.     local requirements = {}
  102.     if repo[softName] ~= nil then
  103.         if repo[softName]["requires"] ~= nil then
  104.             for key, value in pairs(repo[softName]["requires"]) do
  105.                 if not (table.contains(requirements, value)) and not (table.contains(exclude, value)) then
  106.                     table.insert(requirements, value)
  107.                     table.insert(exclude, value)
  108.                     for key1, value1 in pairs(calcRequirements(value, exclude)) do
  109.                         if not (table.contains(requirements, value1)) then
  110.                             table.insert(requirements, value1)
  111.                         end
  112.                     end
  113.                 end
  114.             end
  115.         end
  116.     end
  117.     return requirements
  118. end
  119.  
  120. function dloadSoftFiles(softName)
  121.     local soft = readRepoFile()[softName]
  122.     for _, file in pairs(soft.files) do
  123.         if fs.exists(file.localname .. ".tmp") then
  124.             fs.delete(file.localname .. ".tmp")
  125.         end
  126.         -- print("DL: " .. file.url .. " -> " .. file.localname .. ".tmp")
  127.         local randstr = tostring(math.floor(math.random() * 100000))
  128.         local sUrl = file.url .. "?" .. randstr
  129.        
  130.         if not (download(sUrl, file.localname .. ".tmp")) then
  131.             error("Failed to load " .. file.url)
  132.             return
  133.         end
  134.         if fs.exists(file.localname) then
  135.             -- print("RM: " .. file.localname)
  136.             fs.delete(file.localname)
  137.         end
  138.         -- print("CP: " .. file.localname .. ".tmp -> " .. file.localname)
  139.         fs.copy(file.localname .. ".tmp", file.localname)
  140.         if fs.exists(file.localname .. ".tmp") then
  141.             fs.delete(file.localname .. ".tmp")
  142.         end
  143.     end
  144. end
  145.  
  146. function installSoftware(softName)
  147.     local repo = readRepoFile()
  148.     if repo[softName] == nil then
  149.         error(softName .. " not found in repo")
  150.         return
  151.     end
  152.     print("Installing " .. softName)
  153.     local reqs = calcRequirements(softName)
  154.     print("Requirements: " .. (table.join(reqs, ", ") or "NONE"))
  155.     for _, subSoftName in pairs(reqs) do
  156.         print("  Updating " .. subSoftName)
  157.         dloadSoftFiles(subSoftName)
  158.     end
  159.     print("  Updating " .. softName)
  160.     dloadSoftFiles(softName)
  161. end
  162.  
  163. if not (fs.exists("/etc/repo.json")) then
  164.     updateRepo()
  165. end
  166.  
  167. local command = string.lower(args[1] or "help")
  168.  
  169. local actions = {
  170.     selfupdate = function(opts)
  171.         updateRepo()
  172.         installSoftware("san-get")
  173.     end,
  174.     install = function(opts)
  175.         local softName = opts[1]
  176.         installSoftware(softName)
  177.     end,
  178.     help = function(x)
  179.         printUsage()
  180.     end
  181. }
  182.  
  183. if actions[command] ~= nil then
  184.     if args[2] ~= nil then
  185.         actions[command] {args[2]}
  186.         return
  187.     end
  188. end
  189.  
  190. printUsage()
  191. return
  192.  
Add Comment
Please, Sign In to add comment