Skip to content

Commit 5fc87d5

Browse files
committed
Added --fullpaths option.
1 parent 21ff41b commit 5fc87d5

File tree

5 files changed

+184
-152
lines changed

5 files changed

+184
-152
lines changed

build/exe.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
*
33
* Lua launcher
44
*
5+
* @Cleanup: Decrease the amount of mallocs!
6+
*
57
*---------------------------------------------------------------
68
*
79
* LuaWebGen
@@ -169,7 +171,7 @@ char *getErrorText(u32 errCode) {
169171

170172

171173

172-
char *getPathToExecutable() {
174+
char *getNormalizedPathToExecutable() {
173175
static const size_t INITIAL_BUFFER_SIZE = MAX_PATH; // Note: MAX_PATH includes null, people say.
174176
static const size_t MAX_ITERATIONS = 7;
175177

@@ -188,8 +190,14 @@ char *getPathToExecutable() {
188190
if (!charCount) return nullptr;
189191

190192
if (charCount < bufferSizeInChars) {
191-
auto path = utf16To8(pathWide); // May fail.
193+
auto path = utf16To8(pathWide);
192194
free(pathWide);
195+
if (!path) return nullptr;
196+
197+
for (int i = 0, len = strlen(path); i < len; ++i) {
198+
if (path[i] == '\\') path[i] = '/';
199+
}
200+
193201
return path;
194202
}
195203

@@ -206,23 +214,23 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char *pCmdLin
206214
// Construct command
207215
//
208216
#ifdef DEV
209-
static const char *LUA_EXE_PATH = "..\\bin\\lua.exe";
210-
static const char *LUA_SCRIPT_PATH = "..\\webgen.lua";
217+
static const char *LUA_EXE_WIN_PATH = "..\\bin\\lua.exe";
218+
static const char *LUA_SCRIPT_PATH = "../webgen.lua";
211219
#else
212-
static const char *LUA_EXE_PATH = "bin\\lua.exe";
213-
static const char *LUA_SCRIPT_PATH = "webgen.lua";
220+
static const char *LUA_EXE_WIN_PATH = "bin\\lua.exe";
221+
static const char *LUA_SCRIPT_PATH = "webgen.lua";
214222
#endif
215223

216224
wchar_t *cmdWide = nullptr;
217225

218226
{
219-
auto exeDir = getPathToExecutable();
227+
auto exeDir = getNormalizedPathToExecutable();
220228
if (!exeDir) errorf("Error before starting Lua: Could not get path to executable.");
221229
defer { free(exeDir); };
222230

223231
auto exeDirLen = strlen(exeDir);
224232

225-
while (exeDirLen && exeDir[exeDirLen] != '\\') exeDirLen--;
233+
while (exeDirLen && exeDir[exeDirLen] != '/') exeDirLen--;
226234
exeDir[exeDirLen+1] = 0;
227235
// Note: exeDir includes the trailing slash.
228236

@@ -240,7 +248,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char *pCmdLin
240248
argsStr++;
241249
}
242250

243-
auto cmd = CONCAT_STRINGS("\"", LUA_EXE_PATH, "\" \"", exeDir, LUA_SCRIPT_PATH, "\"", argsStr);
251+
auto cmd = CONCAT_STRINGS("\"", LUA_EXE_WIN_PATH, "\" \"", exeDir, LUA_SCRIPT_PATH, "\"", argsStr);
244252
if (!cmd) errorf("Error before starting Lua: Command construction error.");
245253
defer { free(cmd); };
246254

@@ -289,7 +297,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char *pCmdLin
289297
/*lpStartupInfo */ &startupInfo,
290298
/*lpProcessInformation*/ &processInfo
291299
)) {
292-
errorf("Could not start Lua (%s): %s", LUA_EXE_PATH, getErrorText(GetLastError()));
300+
errorf("Could not start Lua (%s): %s", LUA_EXE_WIN_PATH, getErrorText(GetLastError()));
293301
}
294302
defer {
295303
CloseHandle(processInfo.hThread);

examples/testsite/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ end
3636
-- Before regular generation.
3737
local dogPageTemplate = [[
3838
{{
39-
page.date = "2012-01-14"
4039
page.title = "A Dog of Mine: "..P.dog.name
40+
page.date = "2012-01-14"
4141
}}
4242
4343
The dog named {{P.dog.name}} is {{P.dog.age}} years old.

src/app.lua2p

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ printf!("==== LuaWebGen v"..getReleaseVersion().." ====")
1414

1515

1616

17-
-- done = setup( )
17+
-- readyToExit = setup( )
1818
local function setup()
1919

2020
-- Parse arguments.
@@ -317,13 +317,16 @@ local function setup()
317317
elseif arg == "--verbose" then
318318
_G.verbosePrint = true
319319

320-
elseif arg == "--meta" then
320+
elseif arg == "--meta" then -- @Doc?
321321
_G.outputMetaprograms = true
322322

323-
elseif arg == "--nogc" then
323+
elseif arg == "--nogc" then -- @Doc?
324324
_G.enableGc = false
325325
collectgarbage("stop")
326326

327+
elseif arg == "--fullpaths" then -- @Doc?
328+
_G.useFullPaths = true
329+
327330
elseif arg:find"^%-" then
328331
errorNoPos("[Arguments] Unknown option '%s'.", argRaw)
329332
else
@@ -372,7 +375,7 @@ local function setup()
372375
_G.logPath = basePath .. "_" .. i .. ".log"
373376
end
374377

375-
_G.logFile = io.open(logPath, "w")
378+
_G.logFile = io.open(logPath, "w") -- We don't sweat it if this fails.
376379
end
377380

378381

@@ -1073,12 +1076,12 @@ local function setup()
10731076
local path = F("%s/%s.html", DIR_LAYOUTS, htmlFileBasename)
10741077
local template, err = getFileContentsText(path)
10751078
if not template then
1076-
errorf(2, "Could not read file '%s': %s", path, err)
1079+
errorf(2, "Could not read file '%s': %s", maybeFullPath(path), err)
10771080
end
10781081

1079-
-- timestampPrintVerbose("--> Processing(start): %s", path)
1082+
-- timestampPrintVerbose("--> Processing(start): %s", maybeFullPath(path))
10801083
local html = parseAndRunTemplate(getContext().page, path, template, "html", true, nil)
1081-
-- timestampPrintVerbose("--> Processing(finish): %s", path)
1084+
-- timestampPrintVerbose("--> Processing(finish): %s", maybeFullPath(path))
10821085
return html
10831086
end,
10841087

@@ -1119,7 +1122,7 @@ local function setup()
11191122
local path = DIR_OUTPUT.."/"..pathOutputRel
11201123

11211124
if not isFile(path) then
1122-
errorf(2, "File does not exist. (%s)", path)
1125+
errorf(2, "File does not exist. (%s)", maybeFullPath(path))
11231126
end
11241127

11251128
preserveExistingOutputFile("raw", pathRel, sitePathRel, "")
@@ -1348,16 +1351,14 @@ local function buildWebsite()
13481351
end
13491352

13501353
if not isFile"config.lua" then
1351-
errorNoPos("Missing config.lua in site folder.")
1354+
errorNoPos("Missing %s in site folder.", maybeFullPath"config.lua")
13521355
end
13531356

1354-
local chunk, err = loadfile"config.lua"
1355-
if not chunk then errorNoPos(err) end
1356-
1357-
setfenv(chunk, scriptEnvironment)
1357+
local main_chunk, err = loadLuaFile("config.lua", scriptEnvironment)
1358+
if not main_chunk then errorNoPos(err) end
13581359

1359-
local config = chunk()
1360-
lineAssertTable(config, nil, nil, "config.lua: The file must return a table.")
1360+
local config = main_chunk()
1361+
lineAssertTable(config, nil, nil, "%s: The file must return a table.", maybeFullPath"config.lua")
13611362

13621363
local function get(kPath, default, assertFunc, ...)
13631364
local v = config
@@ -1383,22 +1384,22 @@ local function buildWebsite()
13831384
local function getV(kPath, default, vType) -- Value.
13841385
return get(
13851386
kPath, default, lineAssertType, vType,
1386-
"config.lua: config.%s must be a %s.",
1387-
kPath, vType
1387+
"%s: config.%s must be a %s.",
1388+
maybeFullPath"config.lua", kPath, vType
13881389
)
13891390
end
13901391
local function getT(kPath, default, kType, vType) -- Table
13911392
return get(
13921393
kPath, default, lineAssertTable, kType, vType,
1393-
"config.lua: config.%s must be a table of [%s]=%s.",
1394-
kPath, (kType or "value"), (vType or "value")
1394+
"%s: config.%s must be a table of [%s]=%s.",
1395+
maybeFullPath"config.lua", kPath, (kType or "value"), (vType or "value")
13951396
)
13961397
end
13971398
local function getA(kPath, default, vType) -- Array.
13981399
return get(
13991400
kPath, default, lineAssertTable, "number", vType,
1400-
"config.lua: config.%s must be an array of %s.",
1401-
kPath, vType
1401+
"%s: config.%s must be an array of %s.",
1402+
maybeFullPath"config.lua", kPath, vType
14021403
)
14031404
end
14041405

@@ -1448,46 +1449,46 @@ local function buildWebsite()
14481449
end
14491450

14501451
if not isAny(type(site._outputPathFormat), "string","function") then
1451-
errorNoPos("config.lua: config.rewriteOutputPath must be a string or a function.")
1452+
errorNoPos("%s: config.rewriteOutputPath must be a string or a function.", maybeFullPath"config.lua")
14521453
end
14531454

14541455
-- Validate config.types
14551456
for ext, fileType in pairs(site._fileTypes) do
14561457
if ext ~= ext:lower() then
1457-
errorNoPos("config.lua: File extensions must be lower case: config.types[\"%s\"]", ext)
1458+
errorNoPos("%s: File extensions must be lower case: config.types[\"%s\"]", maybeFullPath"config.lua", ext)
14581459
elseif not FILE_TYPE_SET[fileType] then
1459-
errorNoPos("config.lua: Invalid generator file type '%s'.", fileType)
1460+
errorNoPos("%s: Invalid generator file type '%s'.", maybeFullPath"config.lua", fileType)
14601461
end
14611462
end
14621463

14631464
-- Validate config.processors
14641465
for ext in pairs(site._fileProcessors) do
14651466
if ext ~= ext:lower() then
1466-
errorNoPos("config.lua: File extensions must be lower case: config.processors[\"%s\"]", ext)
1467+
errorNoPos("%s: File extensions must be lower case: config.processors[\"%s\"]", maybeFullPath"config.lua", ext)
14671468
end
14681469
end
14691470

14701471
-- Validate config.baseUrl
14711472
local parsedUrl = require"url".parse(site.baseUrl.v)
14721473

14731474
if site.baseUrl.v == "" then
1474-
errorNoPos("config.lua: config.baseUrl is missing or empty.", site.baseUrl.v)
1475+
errorNoPos("%s: config.baseUrl is missing or empty.", maybeFullPath"config.lua", site.baseUrl.v)
14751476

14761477
elseif not parsedUrl.host then
1477-
errorNoPos("config.lua: Missing host in config.baseUrl (%s)", site.baseUrl.v)
1478+
errorNoPos("%s: Missing host in config.baseUrl (%s)", maybeFullPath"config.lua", site.baseUrl.v)
14781479

14791480
elseif not parsedUrl.scheme then
1480-
errorNoPos("config.lua: Missing scheme in config.baseUrl (%s)", site.baseUrl.v)
1481+
errorNoPos("%s: Missing scheme in config.baseUrl (%s)", maybeFullPath"config.lua", site.baseUrl.v)
14811482
elseif not isAny(parsedUrl.scheme, "http","https") then
1482-
errorNoPos("config.lua: Invalid scheme in config.baseUrl (Expected http or https, got '%s')", parsedUrl.scheme)
1483+
errorNoPos("%s: Invalid scheme in config.baseUrl (Expected http or https, got '%s')", maybeFullPath"config.lua", parsedUrl.scheme)
14831484

14841485
elseif not (parsedUrl.path or ""):find"/$" then
1485-
errorNoPos("config.lua: config.baseUrl must end with '/'. (%s)", site.baseUrl.v)
1486+
errorNoPos("%s: config.baseUrl must end with '/'. (%s)", maybeFullPath"config.lua", site.baseUrl.v)
14861487

14871488
elseif parsedUrl.query then
1488-
errorNoPos("config.lua: config.baseUrl Cannot contain a query. (%s)", site.baseUrl.v)
1489+
errorNoPos("%s: config.baseUrl Cannot contain a query. (%s)", maybeFullPath"config.lua", site.baseUrl.v)
14891490
elseif parsedUrl.fragment then
1490-
errorNoPos("config.lua: config.baseUrl Cannot contain a fragment. (%s)", site.baseUrl.v)
1491+
errorNoPos("%s: config.baseUrl Cannot contain a fragment. (%s)", maybeFullPath"config.lua", site.baseUrl.v)
14911492
end
14921493

14931494
-- Final config handling.
@@ -1498,7 +1499,7 @@ local function buildWebsite()
14981499
dataParserIsBinary[ext] = false
14991500

15001501
if customDataBinaryParsers[ext] then
1501-
errorNoPos("config.lua: Both config.dataTextParsers.%s and config.dataBinaryParsers.%s are defined.", ext, ext)
1502+
errorNoPos("%s: Both config.dataTextParsers.%s and config.dataBinaryParsers.%s are defined.", maybeFullPath"config.lua", ext, ext)
15021503
end
15031504
end
15041505

@@ -1509,7 +1510,7 @@ local function buildWebsite()
15091510
dataParserIsBinary[ext] = true
15101511

15111512
if customDataTextParsers[ext] then
1512-
errorNoPos("config.lua: Both config.dataTextParsers.%s and config.dataBinaryParsers.%s are defined.", ext, ext)
1513+
errorNoPos("%s: Both config.dataTextParsers.%s and config.dataBinaryParsers.%s are defined.", maybeFullPath"config.lua", ext, ext)
15131514
end
15141515
end
15151516

@@ -1863,7 +1864,7 @@ local function buildWebsite()
18631864
--
18641865
traverseFiles(DIR_OUTPUT, nil, function(path, pathOutputRel, filename, extLower)
18651866
if not site._writtenOutputFiles[pathOutputRel] then
1866-
timestampPrintVerbose("Removing: %s", path)
1867+
timestampPrintVerbose("Removing: %s", maybeFullPath(path))
18671868
assert(os.remove(path))
18681869
end
18691870
end)
@@ -1915,7 +1916,7 @@ if autobuild then
19151916

19161917
if lastDirTree and lastDirTree[path] and dirTree[path] > lastDirTree[path] then
19171918
if not silent then
1918-
printf("Detected file change: %s", path)
1919+
printf("Detected file change: %s", maybeFullPath(path))
19191920
end
19201921
somethingChanged = true
19211922
end
@@ -1935,7 +1936,7 @@ if autobuild then
19351936
if lastDirTree and not lastDirTree[path] then
19361937
somethingChanged = true
19371938
if not silent then
1938-
printf("Detected addition: %s", path)
1939+
printf("Detected addition: %s", maybeFullPath(path))
19391940
end
19401941
end
19411942
end)
@@ -1954,7 +1955,7 @@ if autobuild then
19541955
for path in pairs(lastDirTree) do
19551956
if not dirTree[path] then
19561957
somethingChanged = true
1957-
printf("Detected removal: %s", path)
1958+
printf("Detected removal: %s", maybeFullPath(path))
19581959
break
19591960
end
19601961
end
@@ -1993,8 +1994,10 @@ if not enableGc then
19931994
printf("Memory usage: %s", formatBytes(collectgarbage"count"*1024))
19941995
end
19951996

1996-
printfNoLog("Check log for details: %s", logPath)
1997-
logFile:close()
1998-
_G.logFile = nil
1997+
printfNoLog("Check log for details: %s", maybeFullPath(logPath))
1998+
if logFile then
1999+
logFile:close()
2000+
_G.logFile = nil
2001+
end
19992002

20002003
return appOk

0 commit comments

Comments
 (0)