Forte40

Act Turtle Scripting

Aug 9th, 2013
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. local branch = "master"
  2.  
  3. local files = {
  4.   {
  5.     name = "act-get",
  6.     url = "https://raw.github.com/Forte40/act/"..branch.."/act-get.lua"
  7.   },
  8.   {
  9.     name = "act",
  10.     folder = "apis",
  11.     url = "https://raw.github.com/Forte40/act/"..branch.."/act.lua"
  12.   },
  13.   {
  14.     name = "do",
  15.     url = "https://raw.github.com/Forte40/act/"..branch.."/do.lua"
  16.   },
  17.   {
  18.     name = "forman",
  19.     url = "https://raw.github.com/Forte40/act/"..branch.."/forman.lua"
  20.   },
  21.   {
  22.     name = "worker",
  23.     url = "https://raw.github.com/Forte40/act/"..branch.."/worker.lua"
  24.   }
  25. }
  26.  
  27. local scripts = {"ethos_rail"}
  28.  
  29. local cmd = ...
  30. if cmd == "list" then
  31.   textutils.pagedPrint(table.concat(scripts, "\n"), "...")
  32. elseif cmd then
  33.   local found = false
  34.   for _, name in ipairs(scripts) do
  35.     if cmd == name then
  36.       found = true
  37.       break
  38.     end
  39.   end
  40.   if found then
  41.     files = {
  42.       name = cmd..".act",
  43.       url = "https://raw.github.com/Forte40/act/"..branch.."/"..cmd..".act"
  44.     }
  45.   else
  46.     print("script '"..cmd.."' does not exists")
  47.   end
  48. end
  49.  
  50. if not http then
  51.   print("No access to web")
  52.   return
  53. end
  54.  
  55. for _, file in ipairs(files) do
  56.   local path
  57.   if file.folder then
  58.     if not fs.exists(file.folder) then
  59.       fs.makeDir(file.folder)
  60.     end
  61.     path = fs.combine(file.folder, file.name)
  62.   else
  63.     path = file.name
  64.   end
  65.   local currText = ""
  66.   if fs.exists(path) then
  67.     local f = fs.open(path, "r")
  68.     currText = f.readAll()
  69.     f.close()
  70.     io.write("update  ")
  71.   else
  72.     io.write("install ")
  73.   end
  74.   io.write("'"..file.name.."'"..string.rep(" ", math.max(0, 8 - #file.name)))
  75.   if file.folder then
  76.     io.write(" in '"..file.folder.."'"..string.rep(".", math.max(0, 8 - #file.folder)).."...")
  77.   else
  78.     io.write("    .............")
  79.   end
  80.   local request = http.get(file.url)
  81.   if request then
  82.     local response = request.getResponseCode()
  83.     if response == 200 then
  84.       local newText = request.readAll()
  85.       if newText == currText then
  86.         print("skip")
  87.       else
  88.         local f = fs.open(path, "w")
  89.         f.write(newText)
  90.         f.close()
  91.         print("done")
  92.       end
  93.     else
  94.       print(" bad HTTP response code " .. response)
  95.     end
  96.   else
  97.     print(" no request handle")
  98.   end
  99.   os.sleep(0.1)
  100. end
Advertisement
Add Comment
Please, Sign In to add comment