Advertisement
MrHG

fpm

Oct 26th, 2022 (edited)
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.75 KB | None | 0 0
  1. -- Frog Package Manager
  2. -- Version 1.2
  3.  
  4. local function setColor(color)
  5.     if term.isColor() then
  6.         term.setTextColor(color)
  7.     end
  8. end
  9.  
  10. local function installProgram(name, code)
  11.     if fs.exists(name) then
  12.         print("Removed old " .. name)
  13.         fs.delete(name)
  14.     end
  15.     shell.run("pastebin get " .. code .. " " .. name)
  16. end
  17.  
  18. local function errorMsg(message)
  19.     setColor(colors.red)
  20.     print(message)
  21.     setColor(colors.white)
  22. end
  23.  
  24. -- Main Program
  25. local args = { ... }
  26. local packages = {}
  27.  
  28. if #args == 0 then
  29.     errorMsg("Usage: fpm ( list | update | upgrade | install )")
  30.     return
  31. end
  32.  
  33. if args[1] == "update" then
  34.     installProgram("packages.list", "BuF1Hm9q")
  35.     print("Successfully downloaded updated Package List!")
  36.     return
  37. end
  38.  
  39. if not fs.exists("packages.list") then
  40.     errorMsg("Update your package list first! 'fpm update'")
  41.     return
  42. end
  43.  
  44. -- Loading packages into memory from file.
  45. for line in io.lines("packages.list") do
  46.     local firstCommaIndex = string.find(line, ",")
  47.     local secondCommaIndex = string.find(string.sub(line, firstCommaIndex + 1), ",") + firstCommaIndex
  48.     local packageName = string.sub(line, 1, firstCommaIndex - 1)
  49.     local packageCode = string.sub(line, firstCommaIndex + 1, secondCommaIndex - 1)
  50.     local packageDesc = string.sub(line, secondCommaIndex + 1)
  51.     local package = { packageName, packageCode, packageDesc }
  52.     table.insert(packages, package)
  53. end
  54.  
  55. if args[1] == "list" then
  56.     local lineCount = 0
  57.     for i in pairs(packages) do
  58.         setColor(colors.orange)
  59.         term.write(i .. ". ")
  60.         setColor(colors.green)
  61.         term.write(packages[i][1])
  62.         setColor(colors.white)
  63.         print(" - " .. packages[i][3])
  64.         lineCount = lineCount + 1
  65.         if lineCount == 5 then
  66.             setColor(colors.orange)
  67.             term.write("Press any key to continue")
  68.             os.pullEvent("key")
  69.             term.clearLine()
  70.             oldx, oldy = term.getCursorPos()
  71.             term.setCursorPos(1, oldy)
  72.             lineCount = 0
  73.         end
  74.     end
  75. end
  76.  
  77. if args[1] == "install" then
  78.     if args[2] == nil then
  79.         errorMsg("Please specify a package! 'fpm install [package]'")
  80.     end
  81.     local packageExists = false
  82.     for i in pairs(packages) do
  83.         if packages[i][1] == args[2] then
  84.             installProgram(packages[i][1], packages[i][2])
  85.             packageExists = true
  86.         end
  87.     end
  88.     if not packageExists then
  89.         errorMsg("Package not found!")
  90.     end
  91. end
  92.  
  93. if arg[1] == "upgrade" then
  94.     for i in pairs(packages) do
  95.         if fs.exists(packages[i][1]) then
  96.             installProgram(packages[i][1], packages[i][2])
  97.         end
  98.     end
  99.     print("\nUpgrade complete!")
  100. end
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement