gridcaster

githubinstall

Jun 5th, 2021 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. -- Easy installer. Bootstrapped by http://pastebin.com/p8PJVxC4
  2.  
  3. local repo, tree = select(1,...)
  4. if not tree then
  5. -- assume tree as the preferred argument.
  6. tree = repo or 'master'
  7. end
  8. if not repo then
  9. repo = 'eric-wieser/computercraft-github'
  10. end
  11.  
  12. local REPO_BASE = ('https://raw.githubusercontent.com/%s/%s/'):format(repo, tree)
  13.  
  14. local FILES = {
  15. 'apis/dkjson',
  16. 'apis/github',
  17. 'programs/github',
  18. 'github'
  19. }
  20.  
  21. local function request(url_path)
  22. local request = http.get(REPO_BASE..url_path)
  23. local status = request.getResponseCode()
  24. local response = request.readAll()
  25. request.close()
  26. return status, response
  27. end
  28.  
  29. local function makeFile(file_path, data)
  30. local path = 'github.rom/'..file_path
  31. local dir = path:match('(.*/)')
  32. fs.makeDir(dir)
  33. local file = fs.open(path,'w')
  34. file.write(data)
  35. file.close()
  36. end
  37.  
  38. local function rewriteDofiles()
  39. for _, file in pairs(FILES) do
  40. local filename = ('github.rom/%s'):format(file)
  41. local r = fs.open(filename, 'r')
  42. local data = r.readAll()
  43. r.close()
  44. local w = fs.open(filename, 'w')
  45. data = data:gsub('dofile%("', 'dofile("github.rom/')
  46. w.write(data)
  47. w.close()
  48. end
  49. end
  50.  
  51. -- install github
  52. for key, path in pairs(FILES) do
  53. local try = 0
  54. local status, response = request(path)
  55. while status ~= 200 and try <= 3 do
  56. status, response = request(path)
  57. try = try + 1
  58. end
  59. if status then
  60. makeFile(path, response)
  61. else
  62. printError(('Unable to download %s'):format(path))
  63. fs.delete('github.rom')
  64. fs.delete('github')
  65. break
  66. end
  67. end
  68.  
  69. rewriteDofiles()
  70. fs.move('github.rom/github', 'github')
  71. print("github by Eric Wieser installed!")
  72. dofile('github')
Add Comment
Please, Sign In to add comment