-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalize_requires.lua
executable file
·71 lines (55 loc) · 1.26 KB
/
normalize_requires.lua
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
#!/usr/bin/lua
local function usage(fh)
fh:write( ("%s: <prefix> <file>\n"):format(arg[0]) )
fh:write( "\n" )
fh:write( "Normalizes the intra-mod require calls to '.' as a separator.\n" )
fh:write( "the 'prefix' parameter is the name of the mod's private scripts/ subdirectory.\n" )
end
local prefix, file = ...
if not (prefix and file) then
usage(io.stderr)
os.exit(1)
end
assert( prefix )
assert( file )
local function slurpFile()
local lines = {}
local fh = assert( io.open(file, "r") )
for l in fh:lines() do
table.insert(lines, l)
end
fh:close()
return lines
end
local function writeFile(lines)
local fh = assert( io.open(file, "wb") )
for _, l in ipairs(lines) do
fh:write(l, "\n")
end
fh:close()
end
local SEP = "./\\"
local function processString(str)
local pieces = {}
for m in str:gmatch("[^"..SEP.."]+") do
table.insert(pieces, m)
end
assert(#pieces > 0)
local MYSEP = "/"
if pieces[1] == prefix then
MYSEP = "."
end
return table.concat(pieces, MYSEP)
end
local function processLine(l)
return l:gsub([=[require ["'](.-)["']]=], function(str)
local ret = 'require "'..processString(str)..'"'
print(ret)
return ret
end)
end
local lines = slurpFile()
for i, l in ipairs(lines) do
lines[i] = processLine(l)
end
writeFile(lines)