miniminater

CCVim installer

Sep 11th, 2021 (edited)
914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.98 KB | None | 0 0
  1. --[[ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  2.  
  3.                         CCVIM INSTALLER
  4.                           VERSION 0.16
  5.  
  6. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -]]
  7.  
  8. local function initialMenu()
  9.     term.clear()
  10.     term.setCursorPos(1, 1)
  11.     print("CCVIM Installer v0.16")
  12.     print() --skip a line
  13.     print("1. Install CCVIM")
  14.     print("2. Add CCVIM to universal path")
  15.     print("3. Add syntax to installation")
  16.     print("4. Update CCVIM")
  17.     print("5. Exit")
  18. end
  19.  
  20. local function find(table, query)
  21.     if table then
  22.         for i=1,#table,1 do
  23.             if table[i] == query then
  24.                 return i
  25.             end
  26.         end
  27.         return false
  28.     end
  29. end
  30.  
  31. local function toArr(filePath)
  32.     local fileHandle = fs.open(filePath, "r")
  33.     local log
  34.     if fileHandle then
  35.         log = {}
  36.         local line = fileHandle.readLine()
  37.         while line do
  38.             table.insert(log, line)
  39.             line = fileHandle.readLine()
  40.         end
  41.         fileHandle.close()
  42.         return log
  43.     else
  44.         return false
  45.     end
  46. end
  47.  
  48. local setPath = false
  49. local function addToPath()
  50.     if fs.exists("/vim/vim.lua") then
  51.         local lines = toArr("/startup")
  52.         if not find(lines, "shell.setPath(shell.path()..\":/vim/\")") then
  53.             print("This will not work if you have moved your CCVIM installation. Proceed? [y/n]")
  54.             local _, c = os.pullEvent("char")
  55.             if c == "y" or c == "Y" then
  56.                 print("Looking for existing startup file...")
  57.                 if fs.exists("startup") then
  58.                     print("Existing startup file found. Would you like to back it up (startup.bkup) before proceeding? [y/n]")
  59.                     local _,cha = os.pullEvent("char")
  60.                     if cha == "y" or cha == "Y" then
  61.                         shell.run("cp", "/startup/", "/startup.bkup/")
  62.                         if not fs.exists("startup.bkup") then
  63.                             error("Failed to create backup file.")
  64.                         end
  65.                         print("Backed up existing startup file to /startup.bkup")
  66.                     else
  67.                         print("Proceeding without backing up existing startup file")
  68.                     end
  69.                 end
  70.                 print("Adding path setup to startup file...")
  71.                 local file = fs.open("startup", "a")
  72.                 file.writeLine("shell.setPath(shell.path()..\":/vim/\")")
  73.                 file.close()
  74.                 print("Added path setup to startup file.")
  75.                 setPath = true
  76.             end
  77.         else
  78.             print("Already added to file.")
  79.         end
  80.     else
  81.         print("No vim installation found.")
  82.     end
  83.     print("Press any key to continue...")
  84.     os.pullEvent("char")
  85. end
  86.  
  87. local function download(url, file, noerr)
  88.     local content = http.get(url)
  89.     if not content then
  90.         if not noerr then
  91.             error("Failed to access resource " .. url)
  92.         else
  93.             return false
  94.         end
  95.     end
  96.     content = content.readAll()
  97.     local fi = fs.open(file, "w")
  98.     fi.write(content)
  99.     fi.close()
  100. end
  101.  
  102. local function install()
  103.     print("Downloading files from github...")
  104.     local oldx, oldy = term.getCursorPos()
  105.     local wid, hig = term.getSize()
  106.     term.setCursorPos(oldx, oldy)
  107.     for i=1,wid,1 do
  108.         term.write(" ")
  109.     end
  110.     term.setCursorPos(oldx, oldy)
  111.     term.write("Downloading vim.lua ...")
  112.     download("https://raw.githubusercontent.com/Minater247/CCVim/main/vim.lua", "/vim/vim.lua")
  113.     term.setCursorPos(oldx, oldy)
  114.     for i=1,wid,1 do
  115.         term.write(" ")
  116.     end
  117.     term.setCursorPos(oldx, oldy)
  118.     term.write("Downloading .vimrc ...")
  119.     download("https://raw.githubusercontent.com/Minater247/CCVim/main/.vimrc", "/vim/.vimrc")
  120.     term.setCursorPos(oldx, oldy)
  121.     for i=1,wid,1 do
  122.         term.write(" ")
  123.     end
  124.     term.setCursorPos(oldx, oldy)
  125.     term.write("Downloading .version ...")
  126.     download("https://raw.githubusercontent.com/Minater247/CCVim/main/.version", "/vim/.version")
  127.     download("https://raw.githubusercontent.com/Minater247/CCVim/main/lib/args.lua", "/vim/lib/args.lua")
  128.     term.setCursorPos(oldx, oldy)
  129.     for i=1,wid,1 do
  130.         term.write(" ")
  131.     end
  132.     term.setCursorPos(oldx, oldy)
  133.     term.write("Downloading args.lua ...")
  134.     download("https://raw.githubusercontent.com/Minater247/CCVim/main/lib/fil.lua", "/vim/lib/fil.lua")
  135.     term.setCursorPos(oldx, oldy)
  136.     for i=1,wid,1 do
  137.         term.write(" ")
  138.     end
  139.     term.setCursorPos(oldx, oldy)
  140.     term.write("Downloading str.lua ...")
  141.     download("https://raw.githubusercontent.com/Minater247/CCVim/main/lib/str.lua", "/vim/lib/str.lua")
  142.     term.setCursorPos(oldx, oldy)
  143.     for i=1,wid,1 do
  144.         term.write(" ")
  145.     end
  146.     term.setCursorPos(oldx, oldy)
  147.     term.write("Downloading tab.lua ...")
  148.     download("https://raw.githubusercontent.com/Minater247/CCVim/main/lib/tab.lua", "/vim/lib/tab.lua")
  149.     print("Done.")
  150.     print("Do you want to add CCVIM to your universal path?")
  151.     print("This allows you to access it from anywhere on the computer. [y/n]")
  152.     local _, c = os.pullEvent("char")
  153.     if c == "y" then
  154.         addToPath()
  155.     end
  156.     if fs.exists("/vim/vim.lua") and fs.exists("/vim/lib/args.lua") and fs.exists("/vim/lib/fil.lua") and fs.exists("/vim/lib/str.lua") and fs.exists("/vim/lib/tab.lua") then
  157.         print("Finished installing.")
  158.         print("Are you able to press tab on your device? [y/n]")
  159.         local _, eh = os.pullEvent("char")
  160.         if eh ~= "y" then
  161.             print("Adding config line to .vimrc ...")
  162.             local ff = fs.open("/vim/.vimrc", "a")
  163.             ff.writeLine("set mobile")
  164.             ff.close()
  165.             print("Configured for tabless use. Tap or click the bottom line to exit insert or append mode.")
  166.         else
  167.             print("Using default config.")
  168.         end
  169.         if setPath then
  170.             print("Please reboot your computer to complete the installation")
  171.         end
  172.         print("Press any key to continue...")
  173.         os.pullEvent("char")
  174.     else
  175.         print("Something went wrong. Do you want to delete the existing files and try again? [y/n]")
  176.         local _, eh = os.pullEvent("char")
  177.         if eh == "y" then
  178.             print("Retrying...")
  179.             install()
  180.         else
  181.             print("Vim was partially installed but failed to download some files. Please rerun the installation before usage.")
  182.             print("Press any key to contine.")
  183.             os.pullEvent("key")
  184.         end
  185.     end
  186. end
  187.  
  188. local function syntax()
  189.     print("Downloader for the official syntax files.")
  190.     print("Enter the file extension for the filetype: ")
  191.     local fts = string.lower(read())
  192.     print("Looking for extension \""..fts.."\" in repo...")
  193.     if download("https://raw.githubusercontent.com/Minater247/CCVim/main/syntax/"..fts..".lua", "/vim/syntax/"..fts..".lua", true) == false then
  194.         print("Could not find file for extension \""..fts.."\"")
  195.         print("Press any key to continue...")
  196.         os.pullEvent("key")
  197.     else
  198.         print("Downloaded syntax for \""..fts.."\"")
  199.         print("Press any key to continue...")
  200.         os.pullEvent("key")
  201.     end
  202. end
  203.  
  204. local function split(s, delimiter)
  205.     local result = {};
  206.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  207.         table.insert(result, match);
  208.     end
  209.     return result;
  210. end
  211.  
  212. local function update()
  213.     print("Fetching current version...")
  214.     local con = true
  215.     local f
  216.     local vimver
  217.     local instver
  218.     local nf
  219.     local nft
  220.     local nvimver
  221.     local ninstver
  222.     if not fs.isDir("/vim/") then
  223.         con = false
  224.     end
  225.     if fs.exists("/vim/.version") and con then
  226.         f = toArr("/vim/.version")
  227.         vimver = tonumber(f[1])
  228.         instver = tonumber(f[2])
  229.         nf = http.get("https://raw.githubusercontent.com/Minater247/CCVim/main/.version").readAll()
  230.         nft = split(nf, "\n")
  231.         nvimver = tonumber(nft[1])
  232.         ninstver = tonumber(nft[2])
  233.         if ninstver > instver then
  234.             print("This version of the installer is outdated. Please download a new version to continue.")
  235.             print("Download now? [y/n]")
  236.             local _,inp = os.pullEvent("char")
  237.             if inp == "y" then
  238.                 download("https://raw.githubusercontent.com/Minater247/CCVim/main/vim_installer.lua", "/vim/installer")
  239.                 fs.delete("/vim/installer.lua") --legacy installers
  240.                 fs.delete("/vim/vim_installer.lua")
  241.                 fs.move("/vim/installer", "/vim/vim_installer.lua")
  242.                 print("Updated installer downloaded. Local path is at /vim/vim_installer.lua")
  243.                 print("Updating local version file...")
  244.                 local filelines = toArr("/vim/.version")
  245.                 filelines[2] = ninstver
  246.                 local ff = fs.open("/vim/.version", "w")
  247.                 for i=1,#filelines,1 do
  248.                     ff.writeLine(filelines[i])
  249.                 end
  250.                 print("Updated local version.")
  251.                 if nvimver > vimver then
  252.                     print("Please restart the installer to complete the update.")
  253.                 end
  254.                 ff.close()
  255.                 print("Exiting.")
  256.                 print("Press any key to continue...")
  257.                 os.pullEvent("char")
  258.                 error() --not an actual error, displays nothing to user. Just used to quit.
  259.             end
  260.         else
  261.             print("Installer version is current.")
  262.             if nvimver > vimver then
  263.                 print("An update is available! "..vimver.." -> "..nvimver)
  264.                 print("Downloading files from github...")
  265.                 download("https://raw.githubusercontent.com/Minater247/CCVim/main/vim.lua", "/vim/vim.lua")
  266.                 download("https://raw.githubusercontent.com/Minater247/CCVim/main/lib/args.lua", "/vim/lib/args.lua")
  267.                 download("https://raw.githubusercontent.com/Minater247/CCVim/main/lib/fil.lua", "/vim/lib/fil.lua")
  268.                 download("https://raw.githubusercontent.com/Minater247/CCVim/main/lib/str.lua", "/vim/lib/str.lua")
  269.                 download("https://raw.githubusercontent.com/Minater247/CCVim/main/lib/tab.lua", "/vim/lib/tab.lua")
  270.                 print("Update complete.")
  271.                 print("Updating local version info...")
  272.                 local filelines = toArr("/vim/.version")
  273.                 filelines[1] = nvimver
  274.                 local ff = fs.open("/vim/.version", "w")
  275.                 for i=1,#filelines,1 do
  276.                     ff.writeLine(filelines[i])
  277.                 end
  278.                 print("Updated local version.")
  279.                 ff.close()
  280.                 print("Wrapping up...")
  281.                 --used to be code here
  282.                 print("Done.")
  283.                 print("Press any key to continue")
  284.                 os.pullEvent("key")
  285.             else
  286.                 print("CCVIM version is current.")
  287.                 print("Press any key to continue...")
  288.                 os.pullEvent("key")
  289.             end
  290.         end
  291.     else
  292.         print("Failed to check version.")
  293.         print("Press any key to continue.")
  294.         os.pullEvent("key")
  295.     end
  296. end
  297.  
  298. local running = true
  299. while running == true do
  300.     initialMenu()
  301.     local _, ch = os.pullEvent("char")
  302.     while tonumber(ch) == nil do
  303.         _, ch = os.pullEvent("char")
  304.     end
  305.     if ch == "1" then
  306.         term.clear()
  307.         term.setCursorPos(1, 1)
  308.         install()
  309.     elseif ch == "2" then
  310.         term.clear()
  311.         term.setCursorPos(1, 1)
  312.         addToPath()
  313.     elseif ch == "3" then
  314.         term.clear()
  315.         term.setCursorPos(1, 1)
  316.         syntax()
  317.     elseif ch == "4" then
  318.         term.clear()
  319.         term.setCursorPos(1, 1)
  320.         update()
  321.     elseif ch == "5" then
  322.         term.clear()
  323.         term.setCursorPos(1, 1)
  324.         running = false
  325.     end
  326. end
Add Comment
Please, Sign In to add comment