eniallator

lSystem install file

Jan 7th, 2017
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.96 KB | None | 0 0
  1. local repoUrl = "https://raw.githubusercontent.com/eniallator/L-system-Computercraft/master/"
  2. local filesToInstall = {}
  3. local fileListUrl = "install/filesToInstall"
  4.  
  5. if not http then
  6.   printError("http needs to be enabled.")
  7.   return
  8. end
  9.  
  10. local fileListHandle = http.get(repoUrl .. fileListUrl)
  11. local line = fileListHandle.readLine()
  12. local existingFiles = {}
  13. local installPaths = {}
  14.  
  15. while line do
  16.   local pathString = ""
  17.   local finalPath = ""
  18.  
  19.   for word in line:gmatch("[^/]+") do
  20.     pathString = pathString == "" and word or pathString .. "/" .. word
  21.     table.insert(installPaths, {pathString, pathString == line and "file" or "folder"})
  22.  
  23.     if fs.exists(pathString) then
  24.       finalPath = pathString
  25.     end
  26.   end
  27.  
  28.   if #finalPath > 0 then
  29.     table.insert(existingFiles, finalPath)
  30.   end
  31.  
  32.   table.insert(filesToInstall, line)
  33.   line = fileListHandle.readLine()
  34. end
  35.  
  36. fileListHandle.close()
  37. print("Found install files.")
  38.  
  39. if existingFiles[1] then
  40.   print("The following paths already exist:")
  41.  
  42.   for i=1, #existingFiles do
  43.     print(existingFiles[i])
  44.   end
  45.  
  46.   print("Force install?")
  47.   local ans
  48.  
  49.   repeat
  50.     term.write("(y/n): ")
  51.     ans = read():lower()
  52.   until ans == "y" or ans == "n"
  53.  
  54.   if ans == "n" then
  55.     print("Exiting installer.")
  56.     return
  57.   end
  58. end
  59.  
  60. for i=1, #installPaths do
  61.   if not fs.exists(installPaths[i][1]) and installPaths[i][2] == "folder" then
  62.     fs.makeDir(installPaths[i][1])
  63.   end
  64. end
  65.  
  66. for i=1, #filesToInstall do
  67.   local currFile = filesToInstall[i]
  68.   local githubHandle = http.get(repoUrl .. currFile)
  69.  
  70.   if not githubHandle then
  71.     printError("Stuck at " .. currFile .. " please report to https://github.com/eniallator/L-system-Computercraft/issues")
  72.     return
  73.   end
  74.  
  75.   local currFileContents = githubHandle.readAll()
  76.   githubHandle.close()
  77.  
  78.   local locFileHandle = fs.open(currFile, "w")
  79.   locFileHandle.write(currFileContents)
  80.   locFileHandle.close()
  81.  
  82.   print("Got: " .. currFile)
  83. end
Advertisement
Add Comment
Please, Sign In to add comment