miniminater

todo installer

Feb 27th, 2022 (edited)
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.80 KB | None | 0 0
  1. --[[ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  2.  
  3.                          TODO INSTALLER
  4.                           VERSION 0.13
  5.  
  6. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]]
  7.  
  8. local function download(url, file, noerr)
  9.     local content = http.get(url)
  10.     if not content then
  11.         if not noerr then
  12.             error("Failed to access resource " .. url)
  13.         else
  14.             return false
  15.         end
  16.     end
  17.     content = content.readAll()
  18.     local fi = fs.open(file, "w")
  19.     fi.write(content)
  20.     fi.close()
  21. end
  22.  
  23. local function toArr(filePath)
  24.     local fileHandle = fs.open(filePath, "r")
  25.     local log
  26.     if fileHandle then
  27.         log = {}
  28.         local line = fileHandle.readLine()
  29.         while line do
  30.             table.insert(log, line)
  31.             line = fileHandle.readLine()
  32.         end
  33.         fileHandle.close()
  34.         return log
  35.     else
  36.         return false
  37.     end
  38. end
  39.  
  40. local function split(s, delimiter)
  41.     local result = {};
  42.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  43.         table.insert(result, match);
  44.     end
  45.     return result;
  46. end
  47.  
  48. local function yesnochar(charone, chartwo)
  49.     local _, c = os.pullEvent("char")
  50.     --case insensitive
  51.     if c == charone or c == string.upper(charone) then
  52.         return true
  53.     elseif c == chartwo or c == string.upper(chartwo) then
  54.         return false
  55.     else
  56.         print("Please enter "..charone.." or "..chartwo)
  57.         return yesnochar()
  58.     end
  59. end
  60.  
  61. while true do
  62.     term.clear()
  63.     term.setCursorPos(1, 1)
  64.     print("TODO Installer v0.11")
  65.     print("")
  66.     print("1. Install todo")
  67.     print("2. Update todo")
  68.     print("3. Exit")
  69.  
  70.     local _, option = os.pullEvent("char")
  71.  
  72.     if option == "1" then
  73.         term.clear()
  74.         term.setCursorPos(1, 1)
  75.         local installdir = "/todo/"
  76.         print("Downloading todo to "..installdir.."...")
  77.         download("https://raw.githubusercontent.com/Minater247/todo/master/todo.lua", installdir.."todo.lua")
  78.         download("https://raw.githubusercontent.com/Minater247/todo/master/.version", installdir..".version", true)
  79.         download("https://raw.githubusercontent.com/Minater247/todo/master/todo_installer.lua", installdir.."todo_installer.lua", true)
  80.         print("Done.")
  81.         print("Add todo to universal path? (y/n)")
  82.         if yesnochar("y", "n") then
  83.             local existingstartup = toArr("/startup")
  84.             local alreadyadded = false
  85.             if existingstartup then
  86.                 for i = 1, #existingstartup do
  87.                     if string.find(existingstartup[i], "shell.setPath(shell.path()..\":/todo/\")") then
  88.                         alreadyadded = true
  89.                     end
  90.                 end
  91.             end
  92.             if not alreadyadded then
  93.                 print("Adding todo to universal path...")
  94.                 local file = fs.open("/startup", "a")
  95.                 file.writeLine("shell.setPath(shell.path()..\":/todo/\")")
  96.                 file.close()
  97.                 print("Added to path.")
  98.             else
  99.                 print("Already added to path.")
  100.             end
  101.         end
  102.         print("Do you want a custom todofile path? (y/n)")
  103.         local todofile = "/todo/todofile.todo"
  104.         local validpath = false
  105.         if yesnochar("y", "n") then
  106.             while not validpath do
  107.                 print("Please enter the path to the todofile:")
  108.                 todofile = read()
  109.                 if fs.exists(todofile) then
  110.                     print("File already exists. Overwrite? (y/n)")
  111.                     if yesnochar("y", "n") then
  112.                         validpath = true
  113.                     else
  114.                         validpath = false
  115.                     end
  116.                 else
  117.                     validpath = true
  118.                 end
  119.             end
  120.             local file = fs.open("/startup", "a")
  121.             file.writeLine("TODO_PATH = \""..todofile.."\"")
  122.             file.close()
  123.         end
  124.         print("Done.")
  125.  
  126.         print("Installation complete.")
  127.         print("Enjoy!")
  128.     elseif option == "2" then
  129.         term.clear()
  130.         term.setCursorPos(1, 1)
  131.         print("Fetching current version...")
  132.         local con = true
  133.         local f
  134.         local todover
  135.         local instver
  136.         local nf
  137.         local nft
  138.         local ntodover
  139.         local ninstver
  140.         if not fs.isDir("/todo/") then
  141.             con = false
  142.         end
  143.         if fs.exists("/todo/.version") and con then
  144.             f = toArr("/todo/.version")
  145.             todover = tonumber(f[1])
  146.             instver = tonumber(f[2])
  147.             nf = http.get("https://raw.githubusercontent.com/Minater247/todo/master/.version").readAll()
  148.             nft = split(nf, "\n")
  149.             ntodover = tonumber(nft[1])
  150.             ninstver = tonumber(nft[2])
  151.             if ninstver > instver then
  152.                 print("This version of the installer is outdated. Please download a new version to continue.")
  153.                 print("Download now? [y/n]")
  154.                 local _,inp = os.pullEvent("char")
  155.                 if inp == "y" then
  156.                     download("https://raw.githubusercontent.com/Minater247/todo/master/todo_installer.lua", "/todo/installer")
  157.                     fs.delete("/todo/todo_installer.lua")
  158.                     fs.move("/todo/installer", "/todo/todo_installer.lua")
  159.                     print("Updated installer downloaded. Local path is at /todo/todo.lua")
  160.                     print("Updating local version file...")
  161.                     local filelines = toArr("/todo/.version")
  162.                     filelines[2] = ninstver
  163.                     local ff = fs.open("/todo/.version", "w")
  164.                     for i=1,#filelines,1 do
  165.                         ff.writeLine(filelines[i])
  166.                     end
  167.                     print("Updated local version.")
  168.                     if ntodover > todover then
  169.                         print("Please restart the installer to complete the update.")
  170.                     end
  171.                     ff.close()
  172.                     print("Exiting.")
  173.                     print("Press any key to continue...")
  174.                     os.pullEvent("char")
  175.                     error() --not an actual error, displays nothing to user. Just used to quit.
  176.                 end
  177.             else
  178.                 print("Installer version is current.")
  179.                 if ntodover > todover then
  180.                     print("An update is available! "..todover.." -> "..ntodover)
  181.                     print("Downloading files from github...")
  182.                     download("https://raw.githubusercontent.com/Minater247/todo/master/todo.lua", "/todo/todo.lua")
  183.                     print("Update complete.")
  184.                     print("Updating local version info...")
  185.                     local filelines = toArr("/todo/.version")
  186.                     filelines[1] = ntodover
  187.                     local ff = fs.open("/todo/.version", "w")
  188.                     for i=1,#filelines,1 do
  189.                         ff.writeLine(filelines[i])
  190.                     end
  191.                     print("Updated local version.")
  192.                     ff.close()
  193.                     print("Wrapping up...")
  194.                     --used to be code here
  195.                     print("Done.")
  196.                     print("Press any key to continue")
  197.                     os.pullEvent("key")
  198.                 else
  199.                     print("todo version is current.")
  200.                     print("Press any key to continue...")
  201.                     os.pullEvent("key")
  202.                 end
  203.             end
  204.         else
  205.             print("Failed to check version.")
  206.             print("Press any key to continue.")
  207.             os.pullEvent("key")
  208.         end
  209.     elseif option == "3" then
  210.         return
  211.     end
  212. end
Advertisement
Add Comment
Please, Sign In to add comment