-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosLib.lua
405 lines (271 loc) · 7.03 KB
/
osLib.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
--require 'waterlua'
local t = {}
local log
local logDetailedPath
local function get()
local t = package.cpath:split(';')
local extension
local i = 1
while ((extension == nil) and (i <= #t)) do
extension = io.getFileExtension(t[i])
i = i + 1
end
local t = {
dll = 'win',
so = 'linux',
dylib = 'mac'
}
end
t.get = get
local function pause()
io.flush(io.stdout)
io.flush(io.stderr)
os.execute('pause')
end
t.pause = pause
local function createTimer()
local this = {}
function this:start()
this.startPoint = os.clock()
end
function this:getElapsed()
return (os.clock() - this.startPoint)
end
this:start()
return this
end
t.createTimer = createTimer
local function clearScreen()
io.flush(io.stdout)
io.flush(io.stderr)
os.execute('cls')
print('clearing screen...')
io.flush(io.stdout)
io.flush(io.stderr)
end
t.clearScreen = clearScreen
local function setLogPaths(path, detailedPath)
assert(path, 'no path')
assert(detailedPath, 'no detailedPath')
path = io.toAbsPath(path)
detailedPath = io.toAbsPath(detailedPath)
log = io.open(path, 'w+')
logDetailedPath = detailedPath
end
t.setLogPaths = setLogPaths
local tempCallsDir = io.toAbsPath([[tempCalls\]], io.local_dir())
io.createDir(tempCallsDir)
--os.execute(string.format('rd 2>NUL %s /s /q', tempCallsDir:quote()))
--os.execute(string.format('mkdir 2>NUL %s', tempCallsDir:quote()))
local function toDOS(val)
assert(val, 'no value')
if (val == nil) then
return [[""]]
end
if (type(val) ~= 'string') then
return val
end
if (val:sub(val:len(), val:len()) == [[\]]) then
val = val..[[\]]
end
val = val:gsub([["]], [[\"]])
return [["]]..val..[["]]
end
local function ack()
io.setGlobal('success', true)
end
t.ack = ack
local function run(cmd, args, options, fromFolder, doNotWait, name)
cmd = cmd:gsub('/', '\\')
local folder = io.getFolder(cmd)
local fileName = io.getFileName(cmd)
--if doNotWait then
-- cmd = fileName
--else
if (folder and (folder ~= '')) then
cmd = cmd:quote()
end
--end
if options then
for k, v in pairs(options) do
v = tostring(v)
options[k] = '/'..v
end
options = table.concat(options, ' ')
cmd = cmd..' '..options
end
if args then
for k, v in pairs(args) do
v = tostring(v)
--args[k] = v:quote()
args[k] = toDOS(v)
end
args = table.concat(args, ' ')
cmd = cmd..' '..args
end
local tempCPath = 'tempC'
local f = io.local_open(tempCPath, 'r')
local tempC
if f then
tempC = tonumber(f:read())
f:close()
else
tempC = 0
end
tempC = tempC + 1
local f = io.local_open(tempCPath, 'w+')
f:write(tempC)
f:close()
local tempFilePath = io.toAbsPath(tempCallsDir..[[temp]]..tempC..[[.bat]])
local file = io.open(tempFilePath, 'w+')
assert(file, 'cannot open '..tempFilePath)
local lines = {}
if (fromFolder and (fromFolder ~= '')) then
lines[#lines + 1] = string.format('cd /d %s', fromFolder:quote())
end
local successFilePath = tempCallsDir..[[success]]
local returnOutputFilePath = tempCallsDir..[[outMsg.txt]]
local returnErrorMsgFilePath = tempCallsDir..[[errorMsg.txt]]
io.removeFile(returnOutputFilePath)
io.removeFile(returnErrorMsgFilePath)
cmd = cmd..' 1>'..returnOutputFilePath:quote()..' 2>'..returnErrorMsgFilePath:quote()
if doNotWait then
--lines[#lines + 1] = cmd
lines[#lines + 1] = 'echo success > '..successFilePath:quote()
lines[#lines + 1] = 'set ERRORLEVEL='
if name then
lines[#lines + 1] = '@echo | call '..cmd
else
lines[#lines + 1] = cmd
end
lines[#lines + 1] = 'if %ERRORLEVEL% NEQ 0 del '..successFilePath:quote()
lines[#lines + 1] = 'exit'
else
lines[#lines + 1] = 'echo success > '..successFilePath:quote()
lines[#lines + 1] = 'set ERRORLEVEL='
if name then
lines[#lines + 1] = '@echo | call '..cmd
else
lines[#lines + 1] = cmd
end
lines[#lines + 1] = 'if %ERRORLEVEL% NEQ 0 del '..successFilePath:quote()
lines[#lines + 1] = 'exit'
end
file:write(table.concat(lines, '\n'))
file:close()
local result
os.execute('@echo OFF')
local function exec(cmd)
io.setGlobal('success', false)
os.execute(cmd)
--[[local f = io.popen(cmd, 'r')
local output = f:read('*a')
print(output)
f:close()
local f2 = io.open(io.toAbsPath('hello.txt', io.local_dir()), 'w+')
f2:write(cmd, '\nOUTPUT:', output, '\n')
f2:close()]]
--result = io.getGlobal('success')
result = io.pathExists(successFilePath)
io.setGlobal('success', nil)
end
if doNotWait then
--exec(string.format('start /wait /min %s 2>>NUL', tempFilePath:quote()))
exec(string.format('start /min %s %s >> NUL'..' 2>>NUL', name:quote(), tempFilePath:quote()))
else
if (name == nil) then
name = ''
end
if logDetailedPath then
exec(string.format('start /wait /min %s %s >> %s 2>>NUL', name:quote(), tempFilePath:quote(), logDetailedPath:quote()))
else
exec(string.format('start /wait /min %s %s >> NUL'..' 2>>NUL', name:quote(), tempFilePath:quote()))
end
end
os.execute('@echo ON')
local errorMsg = nil
local outMsg = nil
local f = io.open(returnOutputFilePath, 'r')
if (f ~= nil) then
outMsg = f:read('*a')
f:close()
end
local f = io.open(returnErrorMsgFilePath, 'r')
if (f ~= nil) then
errorMsg = f:read('*a')
f:close()
end
return result, errorMsg, outMsg
end
t.run = run
local function waitForKeystroke()
local folder = io.local_dir()
local outPath = folder..'waitForKeystrokeOut.txt'
io.removeFile(outPath)
local cmd = 'call '..(folder..'waitForKeystroke.exe'):quote()..' '..outPath:quote()..' >NUL'
os.execute(cmd)
local f = io.open(outPath, 'r')
assert(f, 'cannot open '..outPath)
local result = f:read('*a')
f:close()
return result
end
t.waitForKeystroke = waitForKeystroke
local function runProg(interpreter, path, args, options, doNotWait, fromFolder)
path = io.toAbsPath(path, io.local_dir(1))
local folder = io.getFolder(path)
local fileName = io.getFileName(path)
if interpreter then
if args then
local tmp = {}
for k, v in pairs(args) do
tmp[k] = v
end
for k, v in pairs(tmp) do
args[k + 1] = v
end
else
args = {}
end
args[1] = fileName
end
if (fromFolder == nil) then
fromFolder = folder
end
local t = createTimer()
print('run '..path)
local result = nil
local errorMsg = nil
local outMsg = nil
if interpreter then
result, errorMsg, outMsg = run(interpreter, args, options, fromFolder, doNotWait, nil)
else
result, errorMsg, outMsg = run(path, args, options, fromFolder, doNotWait, path)
end
if log then
print('log ', log)
log:write('finished '..path..' after '..math.cutFloat(t:getElapsed())..' seconds'..'\n')
end
local resultMsg
if result then
resultMsg = 'success'
else
resultMsg = 'failure'
end
io.write(' - '..math.cutFloat(t:getElapsed())..'seconds, '..resultMsg..'\n')
return result, errorMsg, outMsg
end
t.runProg = runProg
local function exit(val)
if (val == 0) then
io.setGlobal('success', true)
end
os.exit(val)
end
t.exit = exit
local function clearTempCalls()
io.removeDir(tempCallsDir)
io.setGlobal('tempC')
end
t.clearTempCalls = clearTempCalls
expose('osLib', t)