Advertisement
Guest User

Untitled

a guest
Mar 24th, 2015
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.88 KB | None | 0 0
  1. local function parseFolders(pack, repo, info)
  2.  
  3.   local function getFolderTable(repo, namePath, branch)
  4.     local success, filestring = pcall(getContent,"https://api.github.com/repos/"..repo.."/contents/"..namePath.."?ref="..branch)
  5.     if not success or filestring:find("\"message\": \"Not Found\"") then
  6.       io.stderr:write("Error while trying to parse folder names in declaration of package "..pack..".\n")
  7.       io.stderr:write("Please contact the author of that package.\n")
  8.       return nil
  9.     end
  10.     return serial.unserialize(filestring:gsub("%[", "{"):gsub("%]", "}"):gsub("(\"%S-\")%s?:", "[%1] =")))
  11.   end
  12.  
  13.   local function unserializeFiles(files, repo, namePath, branch, relPath)
  14.     if not files then return nil end
  15.     local tFiles = {}
  16.     for _,v in pairs(files) do
  17.       if v["type"] == "file" then
  18.         local newPath = v["download_url"]:gsub("https?://raw.githubusercontent.com/"..repo.."(.+)$", "%1"):gsub("/?$",""):gsub("^/?","")
  19.         tFiles[newPath] = fs.concat(relPath, newPath:gsub(branch.."/(.+)$","%1"), nil)
  20.       elseif v["type"] == "dir" then
  21.         local newFiles = unserializeFiles(getFolderTable(repo, namePath.."/"..v["path"], branch), repo, branch, fs.concat(relPath, v["path"])
  22.         for p,q in pairs(newFiles) do
  23.           tFiles[p] = q
  24.         end
  25.       end
  26.     end
  27.     return tFiles
  28.   end
  29.  
  30.   local newInfo = info
  31.   for i,j in pairs(info.files) do
  32.     if string.find(j,"^:")  then
  33.       local branch =  string.gsub(i,"^(.-)/.+","%1"):gsub("/?$",""):gsub("^/?","")
  34.       local namePath = string.gsub(i,".-(/.+)$","%1"):gsub("/?$",""):gsub("^/?","")
  35.  
  36.       local files = unserializeFiles(getFolderTable(repo, namePath, branch), repo, namePath, branch, j)
  37.       if not files then return nil end
  38.       for p,q in pairs(newFiles) do
  39.         newInfo.files[p] = q
  40.       end
  41.       newInfo.files[i] = nil
  42.     end
  43.   end
  44.   return newInfo
  45. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement