blunty666

baseMonitor_installer

May 8th, 2016
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.50 KB | None | 0 0
  1. local updateOnly = (...) == "--update"
  2.  
  3. local noOverwrite = (...) == "true"
  4.  
  5. local rootURL = "https://raw.githubusercontent.com"
  6. local githubUsername = "blunty666"
  7.  
  8. local mainURL = table.concat({rootURL, githubUsername}, "/")
  9.  
  10. local saveDir = "baseMonitor"
  11. if fs.exists(saveDir) and not fs.isDir(saveDir) then
  12.     printError("Invalid saveDir: ", saveDir)
  13.     return
  14. end
  15.  
  16. local fileList
  17.  
  18. if not updateOnly then
  19.     fileList = {
  20.         ["baseMonitor"] = {"Tiles", "programs/baseMonitor/baseMonitor.lua"},
  21.  
  22.         ["apis/tiles"] = {"Tiles", "tiles.lua"},
  23.         ["apis/guiTiles"] = {"Tiles", "apis/guiTiles.lua"},
  24.         ["apis/advancedTiles"] = {"Tiles", "apis/advancedTiles.lua"},
  25.         ["apis/remotePeripheralClient"] = {"CC-Programs-and-APIs", "remotePeripheral/remotePeripheralClient.lua"},
  26.     }
  27. else
  28.     fileList = {}
  29. end
  30.  
  31. local function get(url)
  32.     local response = http.get(url)         
  33.     if response then
  34.         local fileData = response.readAll()
  35.         response.close()
  36.         return fileData
  37.     end
  38.     return false
  39. end
  40.  
  41. local function getDirectoryContents(author, repository, branch, directory, filesOnly)
  42.     local fType, fPath, fName = {}, {}, {}
  43.     local response = get("https://api.github.com/repos/"..author.."/"..repository.."/contents/"..directory.."?ref="..branch)
  44.     if response then
  45.         if response ~= nil then
  46.             for str in response:gmatch('"type":%s*"(%w+)",') do table.insert(fType, str) end
  47.             for str in response:gmatch('"path":%s*"([^\"]+)",') do table.insert(fPath, str) end
  48.             for str in response:gmatch('"name":%s*"([^\"]+)",') do table.insert(fName, str) end
  49.         end
  50.     else
  51.         printError("Can't fetch repository information")
  52.         return nil
  53.     end
  54.     local directoryContents = {}
  55.     for i=1, #fType do
  56.         if filesOnly ~= true or fType[i] ~= "dir" then
  57.             directoryContents[i] = {type = fType[i], path = fPath[i], name = fName[i]}
  58.         end
  59.     end
  60.     return directoryContents
  61. end
  62.  
  63. local peripheralList = getDirectoryContents(githubUsername, "Tiles", "master", "programs/baseMonitor/peripherals", true)
  64. if peripheralList then
  65.     for _, file in ipairs(peripheralList) do
  66.         local peripheralType = string.gsub(file.name, ".lua$", "")
  67.         if not updateOnly or not fs.exists(fs.combine(saveDir, "peripherals/"..peripheralType)) then
  68.             fileList["peripherals/"..peripheralType] = {"Tiles", file.path}
  69.         end
  70.     end
  71. else
  72.     printError("Could not download peripheralList")
  73. end
  74.  
  75. local sourceList = getDirectoryContents(githubUsername, "Tiles", "master", "programs/baseMonitor/sources", true)
  76. if sourceList then
  77.     for _, file in ipairs(sourceList) do
  78.         local sourceType = string.gsub(file.name, ".lua$", "")
  79.         if not updateOnly or not fs.exists(fs.combine(saveDir, "sources/"..sourceType)) then
  80.             fileList["sources/"..sourceType] = {"Tiles", file.path}
  81.         end
  82.     end
  83. else
  84.     printError("Could not download sourceList")
  85. end
  86.  
  87. local function save(fileData, path)
  88.     local handle = fs.open(path, "w")
  89.     if handle then
  90.         handle.write(fileData)
  91.         handle.close()
  92.         return true
  93.     else
  94.         return false
  95.     end
  96. end
  97.  
  98. for localPath, remotePathDetails in pairs(fileList) do
  99.     local url = table.concat({mainURL, remotePathDetails[1], "master", remotePathDetails[2]}, "/")
  100.     local path = table.concat({saveDir, localPath}, "/")
  101.     if not fs.exists(path) or not (fs.isDir(path) or noOverwrite) then
  102.         local fileData = get(url)          
  103.         if fileData then
  104.             if save(fileData, path) then
  105.                 print("Download successful: ", localPath)
  106.             else
  107.                 printError("Save failed: ", localPath)
  108.             end
  109.         else
  110.             printError("Download failed: ", localPath)
  111.         end
  112.     else
  113.         print("Skipping: ", localPath)
  114.     end
  115. end
Add Comment
Please, Sign In to add comment