-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathcli.lua
More file actions
272 lines (238 loc) · 7.51 KB
/
cli.lua
File metadata and controls
272 lines (238 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
-- This Script is Part of the Prometheus Obfuscator by levno-710
--
-- cli.lua
--
-- This Script contains the Code for the Prometheus CLI.
-- Configure package.path for requiring Prometheus.
local function script_path()
local str = debug.getinfo(2, "S").source:sub(2)
return str:match("(.*[/%\\])")
end
package.path = script_path() .. "?.lua;" .. package.path
local INSTALL_SCRIPT_URL = "https://raw.githubusercontent.com/prometheus-lua/Prometheus/master/install.sh"
local function run_shell(command)
local ok = os.execute(command)
if type(ok) == "number" then
return ok == 0
end
if type(ok) == "boolean" then
return ok
end
return false
end
local function get_version()
local version = os.getenv("PROMETHEUS_LUA_VERSION")
if version and version ~= "" then
return version
end
return "dev"
end
local function print_help()
print("Prometheus Lua CLI")
print("Usage: prometheus-lua [command] [options] <input.lua>")
print("")
print("Commands:")
print(" update Install latest release via installer script")
print(" --version, -v Print CLI version")
print("")
print("Obfuscation options:")
print(" --preset, --p <name> Use preset (e.g. Minify, Medium, High)")
print(" --config, --c <file> Use custom config Lua file")
print(" --out, --o <file> Set output path")
print(" --Lua51 Force Lua 5.1 target")
print(" --LuaU Force LuaU target")
print(" --pretty Pretty print output")
print(" --nocolors Disable colored logs")
print(" --saveerrors Persist parser errors to file")
end
local function run_update()
local command
if run_shell("command -v curl >/dev/null 2>&1") then
command = string.format("curl -fsSL '%s' | sh", INSTALL_SCRIPT_URL)
elseif run_shell("command -v wget >/dev/null 2>&1") then
command = string.format("wget -qO- '%s' | sh", INSTALL_SCRIPT_URL)
else
io.stderr:write("Neither curl nor wget was found. Please install one of them and retry.\n")
os.exit(1)
end
print("Updating prometheus-lua using official installer")
if not run_shell(command) then
io.stderr:write("Update failed\n")
os.exit(1)
end
print("Update completed")
end
if arg[1] == "update" then
run_update()
os.exit(0)
end
if arg[1] == "--version" or arg[1] == "-v" then
print(get_version())
os.exit(0)
end
if arg[1] == "--help" or arg[1] == "-h" or arg[1] == "help" then
print_help()
os.exit(0)
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)
local f = io.open(file, "rb")
if f then
f:close()
end
return f ~= nil
end
string.split = function(str, sep)
local fields = {}
local pattern = string.format("([^%s]+)", sep)
str:gsub(pattern, function(c)
fields[#fields + 1] = c
end)
return fields
end
-- get all lines from a file, returns an empty
-- list/table if the file does not exist
local function lines_from(file)
if not file_exists(file) then
return {}
end
local lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end
return lines
end
local function load_chunk(content, chunkName, environment)
if type(loadstring) == "function" then
local func, err = loadstring(content, chunkName)
if not func then
return nil, err
end
if environment and type(setfenv) == "function" then
setfenv(func, environment)
elseif environment and type(load) == "function" then
return load(content, chunkName, "t", environment)
end
return func
end
if type(load) ~= "function" then
return nil, "No load function available"
end
return load(content, chunkName, "t", environment)
end
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
if not sourceFile then
Prometheus.Logger:error("No input file was specified!")
end
if not config then
Prometheus.Logger:warn("No config was specified, falling back to Minify preset")
config = Prometheus.Presets.Minify
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))
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()
end
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