Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions scripts/release/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down
214 changes: 115 additions & 99 deletions src/cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Loading