Skip to content

Commit d5ad045

Browse files
committed
Command line: Added a way to specify an output path for each input path (--outputpaths). Added processedFileInfo.outputPath .
"init" message got another argument 'outputPaths'.
1 parent e8f84fa commit d5ad045

File tree

5 files changed

+111
-54
lines changed

5 files changed

+111
-54
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ Preprocess.cmd [options] filepath1 [filepath2 ...]
145145
#### Any System
146146
```batch
147147
lua preprocess-cl.lua [options] filepath1 [filepath2 ...]
148+
OR
149+
lua preprocess-cl.lua --outputpaths [options] inputpath1 outputpath1 [inputpath2 outputpath2 ...]
148150
```
149151

150152
If a filepath is, for example, `C:/MyApp/app.lua2p` then LuaPreprocess will write the processed file to `C:/MyApp/app.lua`.

misc/runTest.cmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ IF NOT EXIST local MD local
55

66
lua preprocess-cl.lua --debug --saveinfo=local/info.lua --data="Hello, world!" misc/test.lua2p
77
REM lua preprocess-cl.lua --debug --saveinfo=local/info.lua --data="Hello, world!" misc/test.lua2p --linenumbers
8+
9+
REM lua preprocess-cl.lua --debug --saveinfo=local/info.lua --data="Hello, world!" --outputpaths misc/test.lua2p local/test.output.lua
10+
REM lua preprocess-cl.lua --debug --saveinfo=local/info.lua --data="Hello, world!" --outputpaths misc/test.lua2p local/test.output.lua --linenumbers

misc/runTestUsingHandler.cmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ CD /D "%~dp0.."
44
IF NOT EXIST local MD local
55

66
lua preprocess-cl.lua --debug --saveinfo=local/info.lua --handler=misc/testHandler.lua misc/test.lua2p
7+
REM lua preprocess-cl.lua --debug --saveinfo=local/info.lua --handler=misc/testHandler.lua --outputpaths misc/test.lua2p local/test.output.lua

preprocess-cl.lua

Lines changed: 98 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ exec lua "$0" "$@"
1515
--==============================================================
1616
1717
Script usage:
18-
lua preprocess-cl.lua [options] [--] filepath1 [filepath2 ...]
18+
lua preprocess-cl.lua [options] [--] filepath1 [filepath2 ...]
19+
OR
20+
lua preprocess-cl.lua --outputpaths [options] [--] inputpath1 outputpath1 [inputpath2 outputpath2 ...]
1921
2022
Options:
2123
--data="Any data."
@@ -45,6 +47,10 @@ exec lua "$0" "$@"
4547
default is "lua". If any input files end in .lua then you must
4648
specify another file extension.
4749
50+
--outputpaths
51+
This flag makes every other specified path be the output path
52+
for the previous path.
53+
4854
--saveinfo=pathToSaveProcessingInfoTo
4955
Processing information includes what files had any preprocessor
5056
code in them, and things like that. The format of the file is a
@@ -71,6 +77,7 @@ exec lua "$0" "$@"
7177
Sent before any other message.
7278
Arguments:
7379
paths: Array of file paths to process. Paths can be added or removed freely.
80+
outputPaths: If the --outputpaths option is present this is an array of output paths for the respective path in inputPaths, otherwise it's nil.
7481
7582
"beforemeta"
7683
Sent before a file's metaprogram runs.
@@ -105,7 +112,7 @@ local args = arg
105112

106113
local major, minor = _VERSION:match"Lua (%d+)%.(%d+)"
107114
if not major then
108-
print("[LuaPreprocess] Warning: Could not detect Lua version.")
115+
print("[LuaPreprocess] Warning: Could not detect Lua version.") -- Note: This line does not obey the --silent option.
109116
else
110117
major = tonumber(major)
111118
minor = tonumber(minor)
@@ -122,12 +129,14 @@ local pp = dofile((args[0]:gsub("[^/\\]+$", "preprocess.lua")))
122129

123130
-- From args:
124131
local addLineNumbers = false
132+
local customData = nil
133+
local hasOutputExtension = false
134+
local hasOutputPaths = false
125135
local isDebug = false
126136
local outputExtension = "lua"
137+
local outputMeta = false
127138
local processingInfoPath = ""
128139
local silent = false
129-
local outputMeta = false
130-
local customData = nil
131140

132141
--==============================================================
133142
--= Local Functions ============================================
@@ -197,44 +206,56 @@ math.random() -- Must kickstart...
197206

198207
local processOptions = true
199208
local messageHandlerPath = ""
200-
local paths = {}
209+
local pathsIn = {}
210+
local pathsOut = {}
201211

202212
for _, arg in ipairs(args) do
203-
if processOptions and arg:find"^%-" then
204-
if arg == "--" then
205-
processOptions = false
213+
if not (processOptions and arg:find"^%-") then
214+
local paths = (hasOutputPaths and #pathsOut < #pathsIn and pathsOut or pathsIn)
215+
table.insert(paths, arg)
206216

207-
elseif arg:find"^%-%-handler=" then
208-
messageHandlerPath = arg:match"^%-%-handler=(.*)$"
217+
elseif arg == "--" then
218+
processOptions = false
209219

210-
elseif arg == "--silent" then
211-
silent = true
220+
elseif arg:find"^%-%-data=" then
221+
customData = arg:match"^%-%-data=(.*)$"
212222

213-
elseif arg == "--linenumbers" then
214-
addLineNumbers = true
223+
elseif arg == "--debug" then
224+
isDebug = true
225+
outputMeta = true
215226

216-
elseif arg == "--debug" then
217-
isDebug = true
218-
outputMeta = true
227+
elseif arg:find"^%-%-handler=" then
228+
messageHandlerPath = arg:match"^%-%-handler=(.*)$"
219229

220-
elseif arg:find"^%-%-saveinfo=" then
221-
processingInfoPath = arg:match"^%-%-saveinfo=(.*)$"
230+
elseif arg == "--linenumbers" then
231+
addLineNumbers = true
222232

223-
elseif arg:find"^%-%-outputextension=" then
224-
outputExtension = arg:match"^%-%-outputextension=(.*)$"
233+
elseif arg == "--meta" then
234+
outputMeta = true
225235

226-
elseif arg:find"^%-%-data=" then
227-
customData = arg:match"^%-%-data=(.*)$"
236+
elseif arg:find"^%-%-outputextension=" then
237+
if hasOutputPaths then
238+
errorline("Cannot specify both --outputextension and --outputpaths")
239+
end
240+
hasOutputExtension = true
241+
outputExtension = arg:match"^%-%-outputextension=(.*)$"
242+
243+
elseif arg == "--outputpaths" then
244+
if hasOutputExtension then
245+
errorline("Cannot specify both --outputpaths and --outputextension")
246+
elseif pathsIn[1] then
247+
errorline(arg.." must appear before any paths.")
248+
end
249+
hasOutputPaths = true
228250

229-
elseif arg == "--meta" then
230-
outputMeta = true
251+
elseif arg:find"^%-%-saveinfo=" then
252+
processingInfoPath = arg:match"^%-%-saveinfo=(.*)$"
231253

232-
else
233-
errorline("Unknown option '"..arg.."'.")
234-
end
254+
elseif arg == "--silent" then
255+
silent = true
235256

236257
else
237-
table.insert(paths, arg)
258+
errorline("Unknown option '"..arg.."'.")
238259
end
239260
end
240261

@@ -247,6 +268,10 @@ printfNoise(("="):rep(#header))
247268
printfNoise("%s", header)
248269
printfNoise(("="):rep(#header))
249270

271+
if hasOutputPaths and #pathsOut < #pathsIn then
272+
errorline("Missing output path for "..pathsIn[#pathsIn])
273+
end
274+
250275

251276

252277
-- Prepare metaEnvironment.
@@ -302,22 +327,38 @@ if messageHandlerPath ~= "" then
302327
end
303328
end
304329

305-
sendMessage("init", paths)
306330

307-
if not paths[1] then
331+
332+
-- Init stuff.
333+
sendMessage("init", pathsIn, (hasOutputPaths and pathsOut or nil))
334+
335+
if not hasOutputPaths then
336+
for i, pathIn in ipairs(pathsIn) do
337+
pathsOut[i] = pathIn:gsub("%.%w+$", "").."."..outputExtension
338+
end
339+
end
340+
341+
if not pathsIn[1] then
308342
errorline("No path(s) specified.")
343+
elseif #pathsIn ~= #pathsOut then
344+
errorline(F("Number of input and output paths differ. (%d in, %d out)", #pathsIn, #pathsOut))
309345
end
310346

311-
local pat = "%."..pp.escapePattern(outputExtension).."$"
312-
for _, path in ipairs(paths) do
313-
if path:find(pat) then
314-
errorline(
315-
"Invalid path '"..path.."'. (Paths must not end with ."..outputExtension
316-
.." as those will be used as output paths. You can change extension with --outputextension.)"
317-
)
318-
end
347+
local pathsSetIn = {}
348+
local pathsSetOut = {}
349+
for i = 1, #pathsIn do
350+
if pathsSetIn [pathsIn [i]] then errorline("Duplicate input path: " ..pathsIn [i]) end
351+
if pathsSetOut[pathsOut[i]] then errorline("Duplicate output path: "..pathsOut[i]) end
352+
pathsSetIn [pathsIn [i]] = true
353+
pathsSetOut[pathsOut[i]] = true
354+
if pathsSetOut[pathsIn [i]] then errorline("Path is both input and output: "..pathsIn [i]) end
355+
if pathsSetIn [pathsOut[i]] then errorline("Path is both input and output: "..pathsOut[i]) end
319356
end
320357

358+
359+
360+
-- Process files.
361+
321362
-- :SavedInfo
322363
local processingInfo = {
323364
date = os.date("%Y-%m-%d %H:%M:%S", startTime),
@@ -329,31 +370,31 @@ local lineCount = 0
329370
local lineCountCode = 0
330371
local tokenCount = 0
331372

332-
for _, path in ipairs(paths) do
373+
for i, pathIn in ipairs(pathsIn) do
333374
local startClockForPath = os.clock()
334-
printfNoise("Processing '%s'...", path)
375+
printfNoise("Processing '%s'...", pathIn)
335376

336-
local pathMeta = path:gsub("%.%w+$", "")..".meta.lua"
337-
local pathOut = path:gsub("%.%w+$", "").."."..outputExtension
377+
local pathOut = pathsOut[i]
378+
local pathMeta = pathOut:gsub("%.%w+$", "")..".meta.lua"
338379

339380
if not outputMeta then
340381
pathMeta = nil
341382
end
342383

343384
local info = pp.processFile{
344-
pathIn = path,
385+
pathIn = pathIn,
345386
pathMeta = pathMeta,
346387
pathOut = pathOut,
347388

348389
debug = isDebug,
349390
addLineNumbers = addLineNumbers,
350391

351392
onBeforeMeta = messageHandler and function()
352-
sendMessage("beforemeta", path)
393+
sendMessage("beforemeta", pathIn)
353394
end,
354395

355396
onAfterMeta = messageHandler and function(lua)
356-
local luaModified = sendMessage("aftermeta", path, lua)
397+
local luaModified = sendMessage("aftermeta", pathIn, lua)
357398

358399
if type(luaModified) == "string" then
359400
lua = luaModified
@@ -363,20 +404,20 @@ for _, path in ipairs(paths) do
363404
"%s: Message handler did not return a string for 'aftermeta'. (Got %s)",
364405
messageHandlerPath, type(luaModified)
365406
)
366-
print(F("Error @ %s", err))
367-
sendMessage("fileerror", path, err)
407+
print("Error @ "..err)
408+
sendMessage("fileerror", pathIn, err)
368409
os.exit(1)
369410
end
370411

371412
return lua
372413
end,
373414

374415
onDone = messageHandler and function(info)
375-
sendMessage("filedone", path, pathOut, info)
416+
sendMessage("filedone", pathIn, pathOut, info)
376417
end,
377418

378419
onError = function(err)
379-
sendMessage("fileerror", path, err)
420+
sendMessage("fileerror", pathIn, err)
380421
os.exit(1)
381422
end,
382423
}
@@ -393,10 +434,13 @@ for _, path in ipairs(paths) do
393434

394435
end
395436

396-
printfNoise("Processing '%s' successful! (%.3fs)", path, os.clock()-startClockForPath)
437+
printfNoise("Processing '%s' successful! (%.3fs)", pathIn, os.clock()-startClockForPath)
397438
printfNoise(("-"):rep(#header))
398439
end
399440

441+
442+
443+
-- Finalize stuff.
400444
if processingInfoPath ~= "" then
401445
printfNoise("Saving processing info to '%s'.", processingInfoPath)
402446

@@ -412,13 +456,15 @@ end
412456
printfNoise(
413457
"All done! (%.3fs, %.0f file%s, %.0f LOC, %.0f line%s, %.0f token%s, %s)",
414458
os.clock()-startClock,
415-
#paths, #paths == 1 and "" or "s",
459+
#pathsIn, #pathsIn == 1 and "" or "s",
416460
lineCountCode,
417461
lineCount, lineCount == 1 and "" or "s",
418462
tokenCount, tokenCount == 1 and "" or "s",
419463
formatBytes(byteCount)
420464
)
421465

466+
467+
422468
--[[!===========================================================
423469
424470
Copyright © 2018-2019 Marcus 'ReFreezed' Thunström

preprocess.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,11 @@ local function _processFileOrString(params, isFile)
17831783
file:close()
17841784
end
17851785

1786-
-- Test if the output is valid Lua.
1786+
-- Check if the output is valid Lua.
1787+
--
1788+
-- @Incomplete: Maybe add an option to disable this? It might be useful if
1789+
-- e.g. Lua 5.1 is used to generate Lua 5.3 code (for whatever reason).
1790+
--
17871791
local mainChunk, err = loadLuaString(lua, (isFile and params.pathMeta and "@" or "")..pathOut)
17881792
if not mainChunk then
17891793
local ln, _err = err:match'^.-:(%d+): (.*)'
@@ -1792,7 +1796,8 @@ local function _processFileOrString(params, isFile)
17921796

17931797
-- :ProcessInfo
17941798
local info = {
1795-
path = isFile and params.pathIn or "",
1799+
path = isFile and params.pathIn or "",
1800+
outputPath = isFile and params.pathOut or "",
17961801
processedByteCount = processedByteCount,
17971802
lineCount = lineCount,
17981803
linesOfCode = lineCountCode,

0 commit comments

Comments
 (0)