View difference between Paste ID: GfwhNqpe and VTZ6CqWY
SHOW: | | - or go back to the newest paste.
1
if not term.isColor() then
2
  error("Turtle Architect is incompatible with regular "..(turtle and "turtles" or "computers").."!",0)
3
end
4
local tArgs = {...}
5
local minification = tArgs[2] ~= "-t" --trims all indentation from the files to reduce the file size by roughly 25%
6
local folder = tArgs[1] or ""
7
local tContents = {}
8
local getContents
9
local tSha = {}
10
local tIgnore = { --files to ignore
11
  ["README.md"] = true
12
}
13
14
local prevLines
15
local termX,termY = term.getSize()
16
local midY = math.floor(termY/2)
17
local print = function(text)
18
  local tLines = {}
19
  while #text > 0 do  --splits text into a table containing each line
20
    local line = text:sub(1,termX)
21
    local newLine = string.find(line.."","\n") --check for new line character
22
    if newLine then
23
      line = line:sub(1,newLine-1)
24
      text = text:sub(#line+2,#text)
25
    elseif #line == termX then
26
      local endSpace = line:find"%s$" or line:find"%s%S-$" or termX
27
      line = line:sub(1,endSpace)
28
      text = text:sub(#line+1)
29
    else
30
      text = ""
31
    end
32
    line = string.rep(" ",math.max(math.floor((termX-#line)/2),0))..line
33
    line = line..string.rep(" ",math.max(termX-#line,0))
34
    tLines[#tLines+1] = line
35
  end
36
  if prevLines and #prevLines > #tLines then
37
    for i = 1,#prevLines do
38
      if not tLines[i] then
39
        term.setCursorPos(1,midY-math.floor(#prevLines/2)+i)
40
        term.clearLine()
41
      end
42
    end
43
  end
44
  for i,line in ipairs(tLines) do
45
    term.setCursorPos(1,midY-math.floor((#tLines/2))+i)
46
    term.write(line)
47
  end
48
  prevLines = tLines
49
end
50
51
getContents = function(path)
52
  path = path or ""
53
  print("Scanning Github repo\n"..path.."\nfor files...")
54
  local web = http.get("https://api.github.com/repos/CometWolf/TurtleArchitectV2/contents"..path)
55
  assert(web,(path ~= "" and "Error: Failed to get contents of "..path) or "Error: Github download limit exceeded")
56
  local sContents = web.readAll()
57
  web.close()
58
  local _s,remainder = sContents:find'"name":"'
59
  local name = sContents:match'"name":"(.-)"'
60
  while name do
61
    if tIgnore[name] then
62
      sContents = sContents:sub(remainder)..""
63
    else
64
      sContents = sContents:sub(remainder)..""
65
      local url = sContents:match'html_url":"(.-)"'
66
      assert(url,"Error: Failed to get the url of "..path.."/"..name)
67
      url = url:gsub("https://","https://raw.")
68
      url = url:gsub("blob/","")
69
      local fileName = folder..path.."/"..name
70
      if sContents:match'"type":"(.-)"' == "file" then
71
        tContents[fileName] = url
72
        tSha[fileName] = sContents:match'"sha":"(.-)"'
73
      else
74
        if not fs.exists(fileName) then
75
          fs.makeDir(fileName)
76
        end
77
        getContents(path.."/"..name)
78
      end
79
    end
80
    _s,remainder = sContents:find'"name":"'
81
    name = sContents:match'"name":"(.-)"'
82
  end
83
end
84
local createPath
85
term.setTextColor(colors.white)
86
term.setBackgroundColor(colors.black)
87
term.clear()
88
getContents()
89
for file,url in pairs(tContents) do
90
  if not file:match"README.md" then
91
    local saveFile = fs.open(file:match"arc.Lua" and file:sub(1,#file-4) or file,"w")
92
    print("Downloading\n"..url)
93
    local webFile
94
    while not webFile do
95
      webFile = http.get(url)
96
      if not webFile then
97
        print("Download failed. Retry? (Y/N)")
98
        while true do
99
          local _e,key = os.pullEvent"key"
100
          if key == 21 then --Y
101
            print"Retrying..."
102
            break
103
          elseif key == 49 then --N
104
            print"Installation canceled"
105
            saveFile.close()
106
            return
107
          end
108
        end
109
      end
110
    end
111
    if file:match"TAFiles/Settings.Lua" then --settings file can't be minified
112
      saveFile.write(webFile.readAll())
113
    else
114
      local line = webFile.readLine()
115
      while line do
116
        saveFile.write((minification and (line:match"(%S.*)$" or "") or line).."\n")
117
        line = webFile.readLine()
118
      end
119
    end
120
    saveFile.close()
121
    webFile.close()
122
  end
123
end
124
local file = fs.open(folder.."/".."TAFiles/gitSha","w")
125
file.write(textutils.serialize(tSha))
126
file.close()
127
print("Sucessfully installed Turtle Architect V2\nType "..(folder ~= "" and folder ~= "/" and folder.."/" or "").."arc to run it.")