From e28c0807269d9b40ed6bf5d72c6d5a84e962e40a Mon Sep 17 00:00:00 2001 From: Elias Oelschner <62939318+levno-710@users.noreply.github.com> Date: Wed, 6 May 2026 20:51:17 +0200 Subject: [PATCH 1/2] fix package.sh script --- scripts/release/package.sh | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/scripts/release/package.sh b/scripts/release/package.sh index e03b783..0bc3d45 100755 --- a/scripts/release/package.sh +++ b/scripts/release/package.sh @@ -31,15 +31,10 @@ PKG_DIR="$DIST_DIR/prometheus-lua-$VERSION-$OS" ARCHIVE="$DIST_DIR/prometheus-lua-$VERSION-$OS.tar.gz" rm -rf "$PKG_DIR" -mkdir -p "$PKG_DIR/src" +mkdir -p "$PKG_DIR" mkdir -p "$PKG_DIR/runtime" -cp -R src/prometheus "$PKG_DIR/src/" -cp src/prometheus.lua "$PKG_DIR/src/" -cp src/logger.lua "$PKG_DIR/src/" -cp src/colors.lua "$PKG_DIR/src/" -cp src/highlightlua.lua "$PKG_DIR/src/" -cp src/presets.lua "$PKG_DIR/src/" +cp -R src "$PKG_DIR/" cp cli.lua "$PKG_DIR/" cp LICENSE README.md "$PKG_DIR/" cp prometheus-lua "$PKG_DIR/" From d36f5c04f6cbc3666fceb072c9bde0ed991cbbf3 Mon Sep 17 00:00:00 2001 From: Elias Oelschner <62939318+levno-710@users.noreply.github.com> Date: Wed, 6 May 2026 20:54:04 +0200 Subject: [PATCH 2/2] update cli error handling --- src/cli.lua | 214 ++++++++++++++++++++++++++++------------------------ 1 file changed, 115 insertions(+), 99 deletions(-) diff --git a/src/cli.lua b/src/cli.lua index 1628cdb..0e8282e 100644 --- a/src/cli.lua +++ b/src/cli.lua @@ -89,6 +89,12 @@ end ---@diagnostic disable-next-line: different-requires local Prometheus = require("prometheus") Prometheus.Logger.logLevel = Prometheus.Logger.LogLevel.Info +Prometheus.Logger.errorCallback = function(...) + local args = { ... } + local message = table.concat(args, " ") + io.stderr:write(Prometheus.colors(Prometheus.Config.NameUpper .. ": " .. message, "red") .. "\n") + os.exit(1) +end -- Check if the file exists local function file_exists(file) @@ -142,115 +148,125 @@ local function load_chunk(content, chunkName, environment) return load(content, chunkName, "t", environment) end --- CLI -local config, sourceFile, outFile, luaVersion, prettyPrint +local function run_cli() + -- CLI + local config, sourceFile, outFile, luaVersion, prettyPrint + + Prometheus.colors.enabled = true + + -- Parse Arguments + local i = 1 + while i <= #arg do + local curr = arg[i] + if curr:sub(1, 2) == "--" then + if curr == "--preset" or curr == "--p" then + if config then + Prometheus.Logger:warn("The config was set multiple times") + end + + i = i + 1 + local preset = Prometheus.Presets[arg[i]] + if not preset then + Prometheus.Logger:error(string.format('A Preset with the name "%s" was not found!', tostring(arg[i]))) + end + + config = preset + elseif curr == "--config" or curr == "--c" then + i = i + 1 + local filename = tostring(arg[i]) + if not file_exists(filename) then + Prometheus.Logger:error(string.format('The config file "%s" was not found!', filename)) + end + + local content = table.concat(lines_from(filename), "\n") + -- Load Config from File + local func, err = load_chunk(content, "@" .. filename, {}) + if not func then + Prometheus.Logger:error(string.format('Failed to parse config file "%s": %s', filename, tostring(err))) + end + config = func() + elseif curr == "--out" or curr == "--o" then + i = i + 1 + if outFile then + Prometheus.Logger:warn("The output file was specified multiple times!") + end + outFile = arg[i] + elseif curr == "--nocolors" then + Prometheus.colors.enabled = false + elseif curr == "--Lua51" then + luaVersion = "Lua51" + elseif curr == "--LuaU" then + luaVersion = "LuaU" + elseif curr == "--pretty" then + prettyPrint = true + elseif curr == "--saveerrors" then + -- Override error callback + Prometheus.Logger.errorCallback = function(...) + local args = { ... } + local message = table.concat(args, " ") + io.stderr:write(Prometheus.colors(Prometheus.Config.NameUpper .. ": " .. message, "red") .. "\n") + + local fileName = sourceFile:sub(-4) == ".lua" and sourceFile:sub(0, -5) .. ".error.txt" + or sourceFile .. ".error.txt" + local handle = io.open(fileName, "w") + handle:write(message) + handle:close() + + os.exit(1) + end + else + Prometheus.Logger:warn(string.format('The option "%s" is not valid and therefore ignored', curr)) + end + else + if sourceFile then + Prometheus.Logger:error(string.format('Unexpected argument "%s"', arg[i])) + end + sourceFile = tostring(arg[i]) + end + i = i + 1 + end -Prometheus.colors.enabled = true + if not sourceFile then + Prometheus.Logger:error("No input file was specified!") + end --- Parse Arguments -local i = 1 -while i <= #arg do - local curr = arg[i] - if curr:sub(1, 2) == "--" then - if curr == "--preset" or curr == "--p" then - if config then - Prometheus.Logger:warn("The config was set multiple times") - end + if not config then + Prometheus.Logger:warn("No config was specified, falling back to Minify preset") + config = Prometheus.Presets.Minify + end - i = i + 1 - local preset = Prometheus.Presets[arg[i]] - if not preset then - Prometheus.Logger:error(string.format('A Preset with the name "%s" was not found!', tostring(arg[i]))) - end + -- Add Option to override Lua Version + config.LuaVersion = luaVersion or config.LuaVersion + config.PrettyPrint = prettyPrint ~= nil and prettyPrint or config.PrettyPrint - config = preset - elseif curr == "--config" or curr == "--c" then - i = i + 1 - local filename = tostring(arg[i]) - if not file_exists(filename) then - Prometheus.Logger:error(string.format('The config file "%s" was not found!', filename)) - end + if not file_exists(sourceFile) then + Prometheus.Logger:error(string.format('The File "%s" was not found!', sourceFile)) + end - local content = table.concat(lines_from(filename), "\n") - -- Load Config from File - local func, err = load_chunk(content, "@" .. filename, {}) - if not func then - Prometheus.Logger:error(string.format('Failed to parse config file "%s": %s', filename, tostring(err))) - end - config = func() - elseif curr == "--out" or curr == "--o" then - i = i + 1 - if outFile then - Prometheus.Logger:warn("The output file was specified multiple times!") - end - outFile = arg[i] - elseif curr == "--nocolors" then - Prometheus.colors.enabled = false - elseif curr == "--Lua51" then - luaVersion = "Lua51" - elseif curr == "--LuaU" then - luaVersion = "LuaU" - elseif curr == "--pretty" then - prettyPrint = true - elseif curr == "--saveerrors" then - -- Override error callback - Prometheus.Logger.errorCallback = function(...) - print(Prometheus.colors(Prometheus.Config.NameUpper .. ": " .. ..., "red")) - - local args = { ... } - local message = table.concat(args, " ") - - local fileName = sourceFile:sub(-4) == ".lua" and sourceFile:sub(0, -5) .. ".error.txt" - or sourceFile .. ".error.txt" - local handle = io.open(fileName, "w") - handle:write(message) - handle:close() - - os.exit(1) - end + if not outFile then + if sourceFile:sub(-4) == ".lua" then + outFile = sourceFile:sub(0, -5) .. ".obfuscated.lua" else - Prometheus.Logger:warn(string.format('The option "%s" is not valid and therefore ignored', curr)) - end - else - if sourceFile then - Prometheus.Logger:error(string.format('Unexpected argument "%s"', arg[i])) + outFile = sourceFile .. ".obfuscated.lua" end - sourceFile = tostring(arg[i]) end - i = i + 1 -end -if not sourceFile then - Prometheus.Logger:error("No input file was specified!") -end + local source = table.concat(lines_from(sourceFile), "\n") + local pipeline = Prometheus.Pipeline:fromConfig(config) + local out = pipeline:apply(source, sourceFile) + Prometheus.Logger:info(string.format('Writing output to "%s"', outFile)) -if not config then - Prometheus.Logger:warn("No config was specified, falling back to Minify preset") - config = Prometheus.Presets.Minify + -- Write Output + local handle = io.open(outFile, "w") + handle:write(out) + handle:close() end --- Add Option to override Lua Version -config.LuaVersion = luaVersion or config.LuaVersion -config.PrettyPrint = prettyPrint ~= nil and prettyPrint or config.PrettyPrint - -if not file_exists(sourceFile) then - Prometheus.Logger:error(string.format('The File "%s" was not found!', sourceFile)) +local ok, err = xpcall(run_cli, function(e) + return tostring(e) +end) +if not ok then + local message = tostring(err):gsub("^.-:%d+:%s*", "") + io.stderr:write(Prometheus.colors(Prometheus.Config.NameUpper .. ": " .. message, "red") .. "\n") + os.exit(1) end - -if not outFile then - if sourceFile:sub(-4) == ".lua" then - outFile = sourceFile:sub(0, -5) .. ".obfuscated.lua" - else - outFile = sourceFile .. ".obfuscated.lua" - end -end - -local source = table.concat(lines_from(sourceFile), "\n") -local pipeline = Prometheus.Pipeline:fromConfig(config) -local out = pipeline:apply(source, sourceFile) -Prometheus.Logger:info(string.format('Writing output to "%s"', outFile)) - --- Write Output -local handle = io.open(outFile, "w") -handle:write(out) -handle:close()