This repository has been archived by the owner on Sep 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
package
166 lines (139 loc) · 4.37 KB
/
package
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
--[[
Silica Packager. (c) 2015 Oliver 'oeed' Cooper
Creates a package of Silica files.
Usage: package /path/to/source path/to/package
Once it's packaged simply do dofile( "path/to/package" ) in your startup file AFTER the Silica dofile.
]]
local args = { ... }
if #args ~= 2 then
print( "Usage: package /path/to/source path/to/package" )
return
end
local sourcePath, destinationPath = args[1], args[2]
if not fs.exists( sourcePath ) then
print( "Source path does not exist: " .. sourcePath )
return
elseif not fs.isDir( sourcePath ) then
print( "Source path is not a directory: " .. sourcePath )
return
end
local fileTree = {}
local char = string.char
local start = os.clock()
local function addFolder( path, tree, level )
if not fs.isDir( path ) then
error( "Path is not a directory: " .. path, 0 )
end
for i, fileName in ipairs( fs.list( path ) ) do
if fileName:sub( 1, 1 ) ~= "." then
local filePath = path .. "/" .. fileName
if fs.isDir( filePath ) then
local subTree = level < 1 and {} or tree
addFolder( filePath, subTree, level + 1 )
if level < 1 then tree[fileName] = subTree end
else
local name, ex = fileName:match( "^(.+)%.(%w-)$" )
local h = fs.open( filePath, "r" )
if not h then
error( "Failed to read file: " .. filePath, 0 )
end
tree[name] = { ["text/lua"] = h.readAll() }
h.close()
end
end
end
end
addFolder( sourcePath, fileTree, 0 )
local output = ""
local g_tLuaKeywords = {
[ "and" ] = true,
[ "break" ] = true,
[ "do" ] = true,
[ "else" ] = true,
[ "elseif" ] = true,
[ "end" ] = true,
[ "false" ] = true,
[ "for" ] = true,
[ "function" ] = true,
[ "if" ] = true,
[ "in" ] = true,
[ "local" ] = true,
[ "nil" ] = true,
[ "not" ] = true,
[ "or" ] = true,
[ "repeat" ] = true,
[ "return" ] = true,
[ "then" ] = true,
[ "true" ] = true,
[ "until" ] = true,
[ "while" ] = true,
}
-- A modified textutils.serialise
local function serialise( t, tTracking )
local sType = type(t)
if sType == "table" then
if tTracking[t] ~= nil then
error( "Cannot serialize table with recursive entries", 0 )
end
tTracking[t] = true
if next(t) == nil then
return "{}"
else
local sResult = "{"
local tSeen = {}
for k,v in ipairs(t) do
tSeen[k] = true
sResult = sResult .. serialise( v, tTracking ) .. ","
end
for k,v in pairs(t) do
if not tSeen[k] then
local sEntry
if type(k) == "string" and not g_tLuaKeywords[k] and string.match( k, "^[%a_][%a%d_]*$" ) then
sEntry = k .. " = " .. serialise( v, tTracking ) .. ","
else
sEntry = "[" .. serialise( k, tTracking ) .. "] = " .. serialise( v, tTracking ) .. ","
end
sResult = sResult .. sEntry
end
end
sResult = sResult .. "}"
return sResult
end
elseif sType == "string" then
return string.format( "%q", t )
else
error( "Cannot serialize type "..sType, 0 )
end
end
local fileString = serialise( fileTree, {} )
local f = fs.open( destinationPath, "w" )
if not f then
error( "Failed to write to destination file: " .. destinationPath, 0 )
end
local template =
"local files = " .. fileString .. [[
_G.__resourceTables = _G.__resourceTables or {}
_G.__resourceTables[#_G.__resourceTables + 1] = files
local loaded = {}
local classes = files["classes"]
local loadClass
_G.shell = shell
local f, err = loadstring( classes["class"]["text/lua"], "class.lua" )
if err then error( err, 0 ) end
local ok, err = pcall( f )
if err then error( err, 0 ) end
table.insert( class.tables, classes )
-- we need to load any Exception subclasses first
for name, contents in pairs( classes ) do
if name:sub( -9 ) == "Exception" then
class.get( name )
end
end
for name, contents in pairs( classes ) do
if name ~= "class" then
class.get( name )
end
end
]]
f.write( template )
f.close()