-
Notifications
You must be signed in to change notification settings - Fork 21
/
directories.lua
316 lines (274 loc) · 10.1 KB
/
directories.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
-- The location of the ZeroBraneStudio executable `zbstudio`
local metaDirectories =
{
iBase = 0, -- The ID of the current IDE installation
tBase = {}, -- Contains the ZBS install directories on different machines
tPath = {}, -- Contains different sub-paths included relative to the install path
bSupr = true, -- Global output supression for OS commants for terminal
sInam = "[/\\:*?<>|]+", -- Illegal characters for a file name
sNmOS = tostring(jit.os):lower(), -- Operating system we are running on
sArch = tostring(jit.arch):lower(), -- CPU architecture we are running on
tSupr = {name = "SUPOUT", ["windows"] = " >nul 2>nul" , ["linux"] = " &> /dev/null"},
tCdir = {name = "CHNDIR", ["windows"] = "cd " , ["linux"] = "cd " },
tMdir = {name = "NEWDIR", ["windows"] = "mkdir " , ["linux"] = "mkdir " },
tEdir = {name = "ERSDIR", ["windows"] = "rmdir /S /Q " , ["linux"] = "rm -rf " },
tNdir = {name = "RENDIR", ["windows"] = "ren " , ["linux"] = "mv " },
tDcpy = {name = "CPYDIR", ["windows"] = "xcopy /q /s /e /y ", ["linux"] = "cp -r " },
tErec = {name = "ERSREC", ["windows"] = "del -f " , ["linux"] = "rm -f " },
tRrec = {name = "RENREC", ["windows"] = "ren " , ["linux"] = "mv " },
tRcpy = {name = "CPYREC", ["windows"] = "xcopy /q /y " , ["linux"] = "cp " }
}
local directories = {}
--------------- HELPER FUNCTIONS ---------------
function directories.getNorm(sD)
local sS = tostring(sD or ""):gsub("\\","/"):gsub("/+","/")
return ((sS:sub(-1,-1) == "/") and sS:sub(1,-2) or sS)
end
local function errorOptions(tT, iT, sS, nT)
local iT = tostring(iT or "N/A")
local sS, nT = tostring(sS or "N/A"), (nT or #tT)
io.write(("Available %s is:\n"):format(sS))
for iK = 1, nT do local vK = tostring(tT[iK])
io.write(" ["..iK.."]["..vK.."]\n")
end; error(("Invalid %s ID ["..iT.."]"):format(sS))
end
local function tableClear(tT)
if(not tT) then error("Table missing") end
local sT = type(tT); if(sT ~= "table") then
error("Type mismatch ["..sT.."]") end
if(not next(tT)) then return tT end
for k, v in pairs(tT) do tT[k] = nil end
return directories
end
local function tableRemove(tT, iD, sS)
local sS = tostring(sS or ""); sS = ((sS == "") and "N/A" or sS)
local iV, nT = math.floor(tonumber(iD) or 0), #tT
if((iV <= 0) or (iV > nT)) then errorOptions(tT, iV, sS, nT) end
table.remove(tT, iV); return directories
end
local function tableInsert(tT, iT, ...)
local tA, iK = {...}, 1
local iT = (tonumber(iT) or 0)
while(tA[iK]) do
local sV = tostring(tA[iK] or "")
if(iT > 0) then
table.insert(tT, iT, sV)
elseif(iT < 0) then
table.insert(tT, #tT - iT + 1, sV)
else
table.insert(tT, sV)
end
iK = iK + 1
end; return directories
end
--------------- COMMAND LINE ---------------
local function getPrepareOS(tTY, sBS, sNS, sBD, sND, bPM)
local sPF = metaDirectories.sInam -- File name check
local bPM = metaDirectories.bSupr -- Supress messages
local sNS = tostring(sNS or ""); if(sNS:find(sPF)) then
error("Invalid source ["..sNS.."]: "..tTY.name) end
local sND = tostring(sND or ""); if(sND:find(sPF)) then
error("Invalid destin ["..sND.."]: "..tTY.name) end
local sOS = metaDirectories.sNmOS
local sMD = tTY[sOS]; if(not sMD) then
error("Invalid request ["..sOS.."]: "..tTY.name) end
local sSP = metaDirectories.tSupr[sOS]
local sCD = metaDirectories.tCdir[sOS]
local sBS = directories.getNorm(sBS)
local sBD = tostring(sBD or ""); if(sBD ~= "") then
sBD = directories.getNorm(sBD).."/" end; sND = sBD..sND
local sSP = ((sSP and (bP or bP == nil)) and sSP or "")
if(sBS:find("%s+")) then sBS = "\""..sBS.."\"" end
if(sNS:find("%s+")) then sNS = "\""..sNS.."\"" end
if(sND:find("%s+")) then sND = "\""..sND.."\"" end
if(sOS == "windows") then
if(sND ~= "") then sND = " "..sND
if (tTY.name == "CPYDIR") then sND = (sND.."/")
elseif(tTY.name == "CPYREC") then sND = (sND.."*") end
end
sBS = sBS:gsub("/","\\")
sNS = sNS:gsub("/","\\")
sND = sND:gsub("/","\\")
if(sBS ~= "") then
local bD = sBS:find(":", 1, true)
if(sNS ~= "") then -- Return the terminal command
return sCD..(bD and "/d " or "")..sBS..sSP.." && "..sMD..sNS..sND..sSP
else -- File name is not provided. Change directory
return sCD..(bD and "/d " or "")..sBS..sSP
end -- Otherwise execute in current folder
else return sMD..sNS..sND..sSP end
elseif(sOS == "linux") then
if(sBS ~= "") then
if(sNS ~= "") then -- Return the terminal command
return sCD..sBS..sSP.." && "..sMD..sNS..sND..sSP
else -- File name is not provided. Change directory
return sCD..sBS..sSP
end -- Otherwise execute in current folder
else return sMD..sNS..sND..sSP end
else error("Unsupported OS: "..sOS) end
end
local function getExecuteOS(sC)
local bS, sE, nE = os.execute(sC)
return bS, sE, nE, sC
end
function directories.supCMD(bP) -- Supress CMD messages globally
metaDirectories.bSupr = ((bP or bP == nil) and true or false)
end
function directories.swcDir(sB) -- Use the current directory
return getExecuteOS(getPrepareOS(metaDirectories.tCdir, sB, "", "", ""))
end
function directories.newDir(sN, sB)
return getExecuteOS(getPrepareOS(metaDirectories.tMdir, sB, sN, "", ""))
end
function directories.ersDir(sN, sB)
return getExecuteOS(getPrepareOS(metaDirectories.tEdir, sB, sN, "", ""))
end
function directories.renDir(sO, sN, sB) -- Name will always contain space
return getExecuteOS(getPrepareOS(metaDirectories.tNdir, sB, sO, "", sN))
end
function directories.cpyDir(sO, sN, sB, sD) -- Name will always contain space
return getExecuteOS(getPrepareOS(metaDirectories.tDcpy, sB, sO, sD, sN))
end
function directories.ersRec(sN, sB)
return getExecuteOS(getPrepareOS(metaDirectories.tErec, sB, sN, "", ""))
end
function directories.renRec(sO, sN, sB) -- Name will always contain space
return getExecuteOS(getPrepareOS(metaDirectories.tRrec, sB, sO.."\" \""..sN, "", ""))
end
function directories.cpyRec(sO, sN, sB, sD) -- Name will always contain space
return getExecuteOS(getPrepareOS(metaDirectories.tRcpy, sB, sO, sD, sN))
end
--------------- LIBRARY METHODS ---------------
function directories.getCount()
return metaDirectories.iCount
end
--------------- PATH ---------------
function directories.retPath()
return metaDirectories.tPath
end
function directories.remPathID(vP)
return tableRemove(directories.retPath(), vP, "path")
end
function directories.addPath(...)
return tableInsert(directories.retPath(), 0, ...)
end
function directories.addPathID(iD, ...)
return tableInsert(directories.retPath(), iD, ...)
end
function directories.setPath(...)
tableClear(directories.retPath())
return directories.addPath(...)
end
--------------- BASE ---------------
local function setBaseID(iBase)
local tBase = directories.retBase()
if(not (tBase and next(tBase))) then
error("Base table missing") end
local sBase = tBase[iBase]
if(not (type(sBase) == "string" and sBase:len() > 0)) then
error("Base path missing ["..tostring(sBase).."]") end
local bS, sE, nE = directories.swcDir(sBase, true)
if(not (bS and bS ~= nil and nE == 0)) then
error("Base path invalid ["..sBase.."]") end
local iCount = 0 -- Stores the number of paths processed
local tPath = metaDirectories.tPath
metaDirectories.iCount = iCount
metaDirectories.iBase = iBase
metaDirectories.sBase = sBase
local iS = (iBase or 1)
local iE = (iBase or #tBase)
for iK = iS, iE do
local sBase = tostring(tBase[iK] or "")
if(sBase:len() <= 0 and iK > 0) then errorOptions(tBase, iBase, "install") end
for iD = 1, #tPath do
local sP = tostring(tPath[iD] or "")
if(sP:len() > 0) then
local sD = (sBase.."/"..sP)
local bS, sE, nE = directories.swcDir(sD, true)
if(bS and bS ~= nil and nE == 0) then
iCount = iCount + 1
metaDirectories[iCount] = sD
package.path = package.path..";"..sD.."/?.lua"
package.cpath = package.cpath..";"..sD.."/?.dll"
print("[V]["..iD.."]["..sD.."]")
else
print("[X]["..iD.."]["..sD.."]")
end
end
end
end
metaDirectories.iCount = iCount
return directories
end
function directories.getBase(vB)
if(vB) then
local iB = tonumber(vB)
if(iB) then
local iB = math.floor(iB)
local tB = directories.retBase()
local sB = tB[iB]
if(sB) then return sB, iB end
else
local iB = tostring(vB or "")
local tB = directories.retBase()
for iK = 1, #tB do local sB = tB[iK]
if(tostring(sB):find(iB)) then
return sB, iK
end
end
end
else
return metaDirectories.sBase,
metaDirectories.iBase
end
end
function directories.retBase()
return metaDirectories.tBase
end
function directories.remBaseID(vB)
return tableRemove(directories.retBase(), vB, "base")
end
function directories.addBase(...)
return tableInsert(directories.retBase(), 0, ...)
end
function directories.addBaseID(iD, ...)
return tableInsert(directories.retBase(), iD, ...)
end
function directories.setBase(vD)
if(vD) then
local iD = tonumber(vD)
if(iD) then
if(iD > 0) then setBaseID(iD) else
errorOptions(directories.retBase(), iD, "base")
end
else
tableClear(directories.retBase()); directories.addBase(vD)
setBaseID(1)
end
else
local iN = #directories.retBase()
setBaseID(iN)
end
end
--------------- INITIALIATION ---------------
function directories.getLast()
return metaDirectories[metaDirectories.Count]
end
function directories.getFirst()
return metaDirectories[1]
end
function directories.getList()
local tO, iO = {}, 1
while(metaDirectories[iO]) do
tO[iO] = metaDirectories[iO]
iO = iO + 1
end; return tO
end
function directories.getByID(vD)
local iD = (tonumber(vD) or 0); if(iD < 1) then
error("Identifier mismatch ["..iD.."]") end
local sP = metaDirectories[iD]; if(not sP) then
error("Missing path under ID ["..iD.."]") end
return sP
end
return directories