|
89 | 89 | ---@diagnostic disable-next-line: different-requires |
90 | 90 | local Prometheus = require("prometheus") |
91 | 91 | Prometheus.Logger.logLevel = Prometheus.Logger.LogLevel.Info |
| 92 | +Prometheus.Logger.errorCallback = function(...) |
| 93 | + local args = { ... } |
| 94 | + local message = table.concat(args, " ") |
| 95 | + io.stderr:write(Prometheus.colors(Prometheus.Config.NameUpper .. ": " .. message, "red") .. "\n") |
| 96 | + os.exit(1) |
| 97 | +end |
92 | 98 |
|
93 | 99 | -- Check if the file exists |
94 | 100 | local function file_exists(file) |
@@ -142,115 +148,125 @@ local function load_chunk(content, chunkName, environment) |
142 | 148 | return load(content, chunkName, "t", environment) |
143 | 149 | end |
144 | 150 |
|
145 | | --- CLI |
146 | | -local config, sourceFile, outFile, luaVersion, prettyPrint |
| 151 | +local function run_cli() |
| 152 | + -- CLI |
| 153 | + local config, sourceFile, outFile, luaVersion, prettyPrint |
| 154 | + |
| 155 | + Prometheus.colors.enabled = true |
| 156 | + |
| 157 | + -- Parse Arguments |
| 158 | + local i = 1 |
| 159 | + while i <= #arg do |
| 160 | + local curr = arg[i] |
| 161 | + if curr:sub(1, 2) == "--" then |
| 162 | + if curr == "--preset" or curr == "--p" then |
| 163 | + if config then |
| 164 | + Prometheus.Logger:warn("The config was set multiple times") |
| 165 | + end |
| 166 | + |
| 167 | + i = i + 1 |
| 168 | + local preset = Prometheus.Presets[arg[i]] |
| 169 | + if not preset then |
| 170 | + Prometheus.Logger:error(string.format('A Preset with the name "%s" was not found!', tostring(arg[i]))) |
| 171 | + end |
| 172 | + |
| 173 | + config = preset |
| 174 | + elseif curr == "--config" or curr == "--c" then |
| 175 | + i = i + 1 |
| 176 | + local filename = tostring(arg[i]) |
| 177 | + if not file_exists(filename) then |
| 178 | + Prometheus.Logger:error(string.format('The config file "%s" was not found!', filename)) |
| 179 | + end |
| 180 | + |
| 181 | + local content = table.concat(lines_from(filename), "\n") |
| 182 | + -- Load Config from File |
| 183 | + local func, err = load_chunk(content, "@" .. filename, {}) |
| 184 | + if not func then |
| 185 | + Prometheus.Logger:error(string.format('Failed to parse config file "%s": %s', filename, tostring(err))) |
| 186 | + end |
| 187 | + config = func() |
| 188 | + elseif curr == "--out" or curr == "--o" then |
| 189 | + i = i + 1 |
| 190 | + if outFile then |
| 191 | + Prometheus.Logger:warn("The output file was specified multiple times!") |
| 192 | + end |
| 193 | + outFile = arg[i] |
| 194 | + elseif curr == "--nocolors" then |
| 195 | + Prometheus.colors.enabled = false |
| 196 | + elseif curr == "--Lua51" then |
| 197 | + luaVersion = "Lua51" |
| 198 | + elseif curr == "--LuaU" then |
| 199 | + luaVersion = "LuaU" |
| 200 | + elseif curr == "--pretty" then |
| 201 | + prettyPrint = true |
| 202 | + elseif curr == "--saveerrors" then |
| 203 | + -- Override error callback |
| 204 | + Prometheus.Logger.errorCallback = function(...) |
| 205 | + local args = { ... } |
| 206 | + local message = table.concat(args, " ") |
| 207 | + io.stderr:write(Prometheus.colors(Prometheus.Config.NameUpper .. ": " .. message, "red") .. "\n") |
| 208 | + |
| 209 | + local fileName = sourceFile:sub(-4) == ".lua" and sourceFile:sub(0, -5) .. ".error.txt" |
| 210 | + or sourceFile .. ".error.txt" |
| 211 | + local handle = io.open(fileName, "w") |
| 212 | + handle:write(message) |
| 213 | + handle:close() |
| 214 | + |
| 215 | + os.exit(1) |
| 216 | + end |
| 217 | + else |
| 218 | + Prometheus.Logger:warn(string.format('The option "%s" is not valid and therefore ignored', curr)) |
| 219 | + end |
| 220 | + else |
| 221 | + if sourceFile then |
| 222 | + Prometheus.Logger:error(string.format('Unexpected argument "%s"', arg[i])) |
| 223 | + end |
| 224 | + sourceFile = tostring(arg[i]) |
| 225 | + end |
| 226 | + i = i + 1 |
| 227 | + end |
147 | 228 |
|
148 | | -Prometheus.colors.enabled = true |
| 229 | + if not sourceFile then |
| 230 | + Prometheus.Logger:error("No input file was specified!") |
| 231 | + end |
149 | 232 |
|
150 | | --- Parse Arguments |
151 | | -local i = 1 |
152 | | -while i <= #arg do |
153 | | - local curr = arg[i] |
154 | | - if curr:sub(1, 2) == "--" then |
155 | | - if curr == "--preset" or curr == "--p" then |
156 | | - if config then |
157 | | - Prometheus.Logger:warn("The config was set multiple times") |
158 | | - end |
| 233 | + if not config then |
| 234 | + Prometheus.Logger:warn("No config was specified, falling back to Minify preset") |
| 235 | + config = Prometheus.Presets.Minify |
| 236 | + end |
159 | 237 |
|
160 | | - i = i + 1 |
161 | | - local preset = Prometheus.Presets[arg[i]] |
162 | | - if not preset then |
163 | | - Prometheus.Logger:error(string.format('A Preset with the name "%s" was not found!', tostring(arg[i]))) |
164 | | - end |
| 238 | + -- Add Option to override Lua Version |
| 239 | + config.LuaVersion = luaVersion or config.LuaVersion |
| 240 | + config.PrettyPrint = prettyPrint ~= nil and prettyPrint or config.PrettyPrint |
165 | 241 |
|
166 | | - config = preset |
167 | | - elseif curr == "--config" or curr == "--c" then |
168 | | - i = i + 1 |
169 | | - local filename = tostring(arg[i]) |
170 | | - if not file_exists(filename) then |
171 | | - Prometheus.Logger:error(string.format('The config file "%s" was not found!', filename)) |
172 | | - end |
| 242 | + if not file_exists(sourceFile) then |
| 243 | + Prometheus.Logger:error(string.format('The File "%s" was not found!', sourceFile)) |
| 244 | + end |
173 | 245 |
|
174 | | - local content = table.concat(lines_from(filename), "\n") |
175 | | - -- Load Config from File |
176 | | - local func, err = load_chunk(content, "@" .. filename, {}) |
177 | | - if not func then |
178 | | - Prometheus.Logger:error(string.format('Failed to parse config file "%s": %s', filename, tostring(err))) |
179 | | - end |
180 | | - config = func() |
181 | | - elseif curr == "--out" or curr == "--o" then |
182 | | - i = i + 1 |
183 | | - if outFile then |
184 | | - Prometheus.Logger:warn("The output file was specified multiple times!") |
185 | | - end |
186 | | - outFile = arg[i] |
187 | | - elseif curr == "--nocolors" then |
188 | | - Prometheus.colors.enabled = false |
189 | | - elseif curr == "--Lua51" then |
190 | | - luaVersion = "Lua51" |
191 | | - elseif curr == "--LuaU" then |
192 | | - luaVersion = "LuaU" |
193 | | - elseif curr == "--pretty" then |
194 | | - prettyPrint = true |
195 | | - elseif curr == "--saveerrors" then |
196 | | - -- Override error callback |
197 | | - Prometheus.Logger.errorCallback = function(...) |
198 | | - print(Prometheus.colors(Prometheus.Config.NameUpper .. ": " .. ..., "red")) |
199 | | - |
200 | | - local args = { ... } |
201 | | - local message = table.concat(args, " ") |
202 | | - |
203 | | - local fileName = sourceFile:sub(-4) == ".lua" and sourceFile:sub(0, -5) .. ".error.txt" |
204 | | - or sourceFile .. ".error.txt" |
205 | | - local handle = io.open(fileName, "w") |
206 | | - handle:write(message) |
207 | | - handle:close() |
208 | | - |
209 | | - os.exit(1) |
210 | | - end |
| 246 | + if not outFile then |
| 247 | + if sourceFile:sub(-4) == ".lua" then |
| 248 | + outFile = sourceFile:sub(0, -5) .. ".obfuscated.lua" |
211 | 249 | else |
212 | | - Prometheus.Logger:warn(string.format('The option "%s" is not valid and therefore ignored', curr)) |
213 | | - end |
214 | | - else |
215 | | - if sourceFile then |
216 | | - Prometheus.Logger:error(string.format('Unexpected argument "%s"', arg[i])) |
| 250 | + outFile = sourceFile .. ".obfuscated.lua" |
217 | 251 | end |
218 | | - sourceFile = tostring(arg[i]) |
219 | 252 | end |
220 | | - i = i + 1 |
221 | | -end |
222 | 253 |
|
223 | | -if not sourceFile then |
224 | | - Prometheus.Logger:error("No input file was specified!") |
225 | | -end |
| 254 | + local source = table.concat(lines_from(sourceFile), "\n") |
| 255 | + local pipeline = Prometheus.Pipeline:fromConfig(config) |
| 256 | + local out = pipeline:apply(source, sourceFile) |
| 257 | + Prometheus.Logger:info(string.format('Writing output to "%s"', outFile)) |
226 | 258 |
|
227 | | -if not config then |
228 | | - Prometheus.Logger:warn("No config was specified, falling back to Minify preset") |
229 | | - config = Prometheus.Presets.Minify |
| 259 | + -- Write Output |
| 260 | + local handle = io.open(outFile, "w") |
| 261 | + handle:write(out) |
| 262 | + handle:close() |
230 | 263 | end |
231 | 264 |
|
232 | | --- Add Option to override Lua Version |
233 | | -config.LuaVersion = luaVersion or config.LuaVersion |
234 | | -config.PrettyPrint = prettyPrint ~= nil and prettyPrint or config.PrettyPrint |
235 | | - |
236 | | -if not file_exists(sourceFile) then |
237 | | - Prometheus.Logger:error(string.format('The File "%s" was not found!', sourceFile)) |
| 265 | +local ok, err = xpcall(run_cli, function(e) |
| 266 | + return tostring(e) |
| 267 | +end) |
| 268 | +if not ok then |
| 269 | + local message = tostring(err):gsub("^.-:%d+:%s*", "") |
| 270 | + io.stderr:write(Prometheus.colors(Prometheus.Config.NameUpper .. ": " .. message, "red") .. "\n") |
| 271 | + os.exit(1) |
238 | 272 | end |
239 | | - |
240 | | -if not outFile then |
241 | | - if sourceFile:sub(-4) == ".lua" then |
242 | | - outFile = sourceFile:sub(0, -5) .. ".obfuscated.lua" |
243 | | - else |
244 | | - outFile = sourceFile .. ".obfuscated.lua" |
245 | | - end |
246 | | -end |
247 | | - |
248 | | -local source = table.concat(lines_from(sourceFile), "\n") |
249 | | -local pipeline = Prometheus.Pipeline:fromConfig(config) |
250 | | -local out = pipeline:apply(source, sourceFile) |
251 | | -Prometheus.Logger:info(string.format('Writing output to "%s"', outFile)) |
252 | | - |
253 | | --- Write Output |
254 | | -local handle = io.open(outFile, "w") |
255 | | -handle:write(out) |
256 | | -handle:close() |
|
0 commit comments