-
Notifications
You must be signed in to change notification settings - Fork 0
/
cci.lua
325 lines (285 loc) · 8.05 KB
/
cci.lua
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
-- I relly wanna get a better editor
-- CCI package manager
-- ----------
-- Author: dousha@github.com
-- Usage: cci ...
-- License: GPL v3
-- ----------
-- ---- Public variables ----
repoFilePath = "./repo.list"
packageFilePath = "./pdb.list"
packageDbPath = "./lpdb.list"
argv = {...}
-- ---- ----
function printUsage() -- return void
print("cci - ComputerCraft Installation Package Manager")
print("Usages:")
print(" cci [-aidhlrs][-Li] ...")
print(" cci [-h]")
print(" Display this help info")
print(" cci -l")
print(" List installed packages")
print(" cci -[a|d][r] <name> <uri> [protocol=http]")
print(" Add or delete a repo")
print(" cci -[ai|d] <name>")
print(" Install or remove a package")
print(" cci -r repoName=all ...")
print(" Refresh package list. By default, it refreshs all of available repos")
print(" cci -Li <path>")
print(" Install a package from local drive")
print(" cci -s <name> repoName=all ...")
print(" Search for a package. By default, it searchs all of available repos")
print("For further details, read the man page of cci")
end
function split(src, separator) -- return table
local nFindStartIndex = 1
local nSplitIndex = 1
local nSplitArray = {}
while true do
local nFindLastIndex = string.find(src, separator, nFindStartIndex)
if not nFindLastIndex then
nSplitArray[nSplitIndex] = string.sub(src, nFindStartIndex, string.len(src))
break
end
nSplitArray[nSplitIndex] = string.sub(src, nFindStartIndex, nFindLastIndex - 1)
nFindStartIndex = nFindLastIndex + string.len(separator)
nSplitIndex = nSplitIndex + 1
end
return nSplitArray
end
function serializeRepoString(name, addr, isEnabled, protocol) -- return string
return (name .. ":" .. addr .. ":" .. isEnabled .. ":" .. protocol)
end
function serializeRepoStringT(T) -- return string
return serializeRepoString(T[1], T[2], T[3], T[4])
end
function readRepo() -- return table
-- repository list will be stored in /disk/repo.list
-- in this format
-- name:uri:isEnabled:protocol
-- e.g.:
-- a repo via Internet with HTTP protocol
-- example:http://www.example.com/repo.list:true:http
-- a repo via Rednet with rednetPkg protocol (not recommended)
-- rednet:hostname:true:rednetPkg
if fs.exists(repoFilePath) then
fileHandler = fs.open(repoFilePath)
if fileHandler == nil then
fileHandler.close()
print("[X] Cannot open ./repo.list!: Permission denied")
return nil
end
local curLine = ""
local curTable = {}
local totalTable = {}
for i, curLine in fileHandler.lines() do
curTable = split(curLine, ":")
totalTable[i] = curTable
end
fileHandler.close()
return totalTable
else
print("[X] Cannot open ./repo.list!: File not found")
print("[i] You can try `cci -ar repoName repoURI repoProtocol=http'")
return nil
end
end
function saveRepo(repoTable) -- return void
fileHandler = fs.open(repoFilePath, "w")
curSerializedLine = ""
for i, v in repoTable do
curSerializedLine = serializeRepoStringT(v)
fs.writeLine(curSerializedLine)
end
fileHandler.close()
end
function deleteRepo(repoName) -- return void
fileHandler = fs.open(repoFilePath, "w")
curSerializedLine = ""
for i, v in fileHandler.lines() do
if v[1] ~= repoName then
end
end
end
function serializePackageString(name, version, size, hash) -- return string
return (name .. ":" .. version .. ":" .. size .. ":" hash)
end
function serializePackageStringT(T) -- return string
return serializePackageString(T[1], T[2], T[3], T[4])
end
function readPackageList() -- return table
-- package list will be stored in /disk/pdb.list
-- in this format
-- packageName:version:size(byte):md5checksum16
-- e.g.:
-- package:0.0.1:1700:4FBA0258E0C56300
if fs.exists(packageFilePath) then
curTable = {}
curLine = ""
totalTable = {}
i = 1
fileHandler = fs.open(packageFilePath, "r")
if fileHandler == nil then
print("[X] Cannot open ./pdb.list: Permission denied")
end
for curLine in fileHandler.lines() do
curTable = split(curLine)
totalTable[i] = curTable
i = i + 1
end
fileHandler.close()
return totalTable
else
print("[X] Cannot open ./pdb.list")
print("[i] You should execute `cci -r' first to create")
print("this file.")
return nil
end
end
function savePackageList(packageTable)
fileHandler = fs.open(packageFilePath, "w")
curDeserializedLine = ""
for i, v in packageTable do
curSerializedLine = serializePackageStringT(v)
fileHandler.writeLine(curSerializedLine)
end
fileHandler.close()
end
function readPackageDb()
end
function appendPackageDb(str)
end
function modifyPackageStatus()
end
function searchPackage(name) -- return table
local packageList = readPackageList()
local matchList = {}
local j = 1
if packageList == nil then
return nil -- warning is given
else
for i, v in packageList do
local deserializedPakageList = split(v, ":")
if _, string.find(v[1], name) > 0 then
matchList[j] = v[1]
j = j + 1
end
end
if #matchList > 0 then
for u, w in matchList do
print()
end
else
end
end
end
function installPackage(path) -- return boolean
end
function removePackage(path) -- return boolean
end
-- @Deprecated
function refreshListViaRednet(hostname) -- return boolean
-- Seriously, I don't know why I've done this
end
function refreshListViaInternet(uri) -- return boolean
end
function refreshList(serializedRepoString) -- return boolean
local serializedRepoTable = split(serializedRepoString, ":")
if serializedRepoTable[3] == true then
if serializedRepoTable[4] == "http" then
local result = refreshListViaInternet(serializedRepoTable[2])
elseif serializedRepoTable[4] == "rednetPkg" then
local result = refreshListViaRednet(serializedRepoTable[2])
else
local result = false
print("[!] Repository " .. serializedRepoTable[1] .. " has invalid protocol! Ignoring.")
end
if result == false then
print("[X] Error occurred When refreshing repository " .. serializedRepoTable[1] .. "!")
end
end
end
function refreshListAll() -- return void
local repoList = readLocalPackageList()
if repoList == nil then
return -- warning is given
else
for i, v in repoList do
refreshList(v)
end
end
end
function getPackage(name) -- return boolean
end
function makePackage() -- return boolean
-- generally, just put everything and it's dependencies
-- into one directory, like what mac does!
-- but, wait, there's only a dependencies list in the package
-- won't be some real files
end
-- ---- Main area ----
if #argv < 1 then
printUsage()
return
else
if argv[1] == "-h" then
printUsage()
elseif argv[1] == "-ar" then
if #argv == 3 then
-- by default, protocol is http
local repoList = readRepo()
if repoList == nil then
return -- warning is given when calling function
else
local oldRepoSize = #repoList
convertedString = argv[2] .. ":" .. argv[3] .. ":http"
repoList[oldRepoSize + 1] = convertedString
assert(oldRepoSize >= #repoList, "[X] Magika!")
end
elseif #argv == 4 then
-- deprecated, though
local repoList = readRepo()
if repoList == nil then
return -- warning is given when calling function
else
local oldRepoSize = #repoList
convertedString = argv[2] .. ":" .. argv[3] .. ":" .. argv[4]
repoList[oldRepoSize + 1] = convertedString
assert(oldRepoSize >= #repoList, "[X] Magika!")
end
else
print("cci -ar <name> <uri> <protocol=http>")
print("Did you forget something?")
end
elseif argv[1] == "-dr" then
deleteRepo(argv[2])
elseif argv[1] == "-a" or argv[1] == "-i" then
local upperBound = #argv
local i = 2
while i < upperBound do
installPackage(argv[i])
i = i + 1
end
elseif argv[1] == "-d" then
local upperBound = #argv
local i = 2
while i < upperBound do
removePackage(argv[i])
i = i + 1
end
elseif argv[1] == "-Li" then
elseif argv[1] == "-r" then
if #argv == 1 then
local result = refreshListAll()
else
end
elseif argv[1] == "-s" then
if #argv > 2 then
print("[!] Sorry, but I can only search ONE package for one time")
else
searchPackage(argv[2])
end
else
printUsage()
end
end