-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartup.lua
More file actions
111 lines (87 loc) · 2.43 KB
/
startup.lua
File metadata and controls
111 lines (87 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
-- this startup should download fresh startup.second.lua and call it
--exports:
--require
--require_try
-- Loads a table from file
function loadTable()
sPath="gitget.cfg"
if not fs.exists(sPath) then
--error("loadTable() file not found: " .. sPath)
saveString(textutils.serialize({user=githubUser or "keneo",project="swarm",branch="master",filename=""}),sPath)
end
if fs.isDir(sPath) then
error("loadTable() cannot open a directory")
end
local file = fs.open(sPath, "r")
local sTable = file.readAll()
file.close()
return textutils.unserialize(sTable)
end
-- Saves a string to file
function saveString(sData, sPath)
if fs.isDir(sPath) then
error("saveString() cannot save over a directory")
end
local file = fs.open(sPath, "w")
file.write(sData)
file.close()
return true
end
function createAddress(c)
local sUrl = "https://raw.github.com"
c["filename"] = string.gsub(c["filename"], "%s", "%%20")
return (sUrl .. "/" .. c["user"] .. "/" .. c["project"] .. "/" .. c["branch"] .. "/" .. c["filename"])
end
-- Downloads a file from url provided
function download(sUrl, sPath)
print("Downloading: " .. sUrl)
if http then
local sData = http.get(sUrl)
if sData then
print("Fetched file")
return saveString(sData.readAll(), sPath)
else
print("Failed to fetch file: " .. sUrl)
return false
end
else
error("download() needs http api enabled to fetch files")
end
end
function ensureNewest(localFileName)
if (fs.exists("/.git")) then
-- we're running in the working copy - probably we're the source of everything. if not - do: git pull
else
local tc = loadTable()
local githubFileName = localFileName
tc["filename"]=githubFileName
download(createAddress(tc),localFileName)
end
end
local alreadyRequired = {}
function require(filename)
if (alreadyRequired[filename]) then
return alreadyRequired[filename]
else
write(filename .. "...")
alreadyRequired[filename]=true
ensureNewest(filename)
if (fs.exists(filename)) then
write("go\n")
export = true
shell.run(filename)
alreadyRequired[filename]=export
return export
else
write("miss\n")
error("require('".. filename .."'): failed. file not found")
end
end
end
function require_try(filename)
return pcall(function() require(filename) end)
end
function main()
require("startup.second.lua")
end
main()