| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
local srcdir = "src/" |
| 21 |
local mode |
| 22 |
local default_os |
| 23 |
if arg[1] == "--unix-shellscript" then |
| 24 |
default_os, mode = "unix", "shellscript" |
| 25 |
table.remove(arg, 1) |
| 26 |
elseif arg[1] == "--windows-batchfile" then |
| 27 |
default_os, mode = "windows", "batchfile" |
| 28 |
table.remove(arg, 1) |
| 29 |
end |
| 30 |
local outfile = arg[1] |
| 31 |
local preserve_location_info = false |
| 32 |
|
| 33 |
local modules = { |
| 34 |
{ |
| 35 |
name = "texrunner.pathutil", |
| 36 |
path = "texrunner/pathutil.lua", |
| 37 |
path_unix = "texrunner/pathutil_unix.lua", |
| 38 |
path_windows = "texrunner/pathutil_windows.lua", |
| 39 |
}, |
| 40 |
{ |
| 41 |
name = "texrunner.shellutil", |
| 42 |
path = "texrunner/shellutil.lua", |
| 43 |
path_unix = "texrunner/shellutil_unix.lua", |
| 44 |
path_windows = "texrunner/shellutil_windows.lua", |
| 45 |
}, |
| 46 |
{ |
| 47 |
name = "texrunner.fsutil", |
| 48 |
path = "texrunner/fsutil.lua", |
| 49 |
}, |
| 50 |
{ |
| 51 |
name = "texrunner.luatexinit", |
| 52 |
path = "texrunner/luatexinit.lua", |
| 53 |
}, |
| 54 |
{ |
| 55 |
name = "texrunner.isatty", |
| 56 |
path = "texrunner/isatty.lua", |
| 57 |
}, |
| 58 |
{ |
| 59 |
name = "texrunner.fswatcher_windows", |
| 60 |
path = "texrunner/fswatcher_windows.lua", |
| 61 |
}, |
| 62 |
} |
| 63 |
|
| 64 |
local imported_globals = {"io", "os", "string", "table", "package", "require", "assert", "error", "ipairs", "type", "select", "arg"} |
| 65 |
|
| 66 |
|
| 67 |
local function strip_global_imports(code) |
| 68 |
local function repl(s1, s2) |
| 69 |
if s1 == s2 then |
| 70 |
for i, v in ipairs(imported_globals) do |
| 71 |
if v == s1 then |
| 72 |
return "" |
| 73 |
end |
| 74 |
end |
| 75 |
end |
| 76 |
return nil |
| 77 |
end |
| 78 |
return (code:gsub("local (%w+) = (%w+)\n", repl)) |
| 79 |
end |
| 80 |
|
| 81 |
local function strip_test_code(code) |
| 82 |
if preserve_location_info then |
| 83 |
return (code:gsub("%-%- TEST CODE\n.-%-%- END TEST CODE\n", function(s) |
| 84 |
return (s:gsub("[^\n]","")) |
| 85 |
end)) |
| 86 |
else |
| 87 |
return (code:gsub("%-%- TEST CODE\n(.-)%-%- END TEST CODE\n", "")) |
| 88 |
end |
| 89 |
end |
| 90 |
|
| 91 |
local function load_module_code(path) |
| 92 |
assert(loadfile(srcdir .. path)) |
| 93 |
return strip_test_code(assert(io.open(srcdir .. path, "r")):read("*a")) |
| 94 |
end |
| 95 |
|
| 96 |
assert(loadfile(srcdir .. "cluttex-ml.lua")) |
| 97 |
|
| 98 |
local shebang = "#!/usr/bin/env texlua\n" |
| 99 |
local main = assert(io.open(srcdir .. "cluttex-ml.lua", "r")):read("*a") |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
local lines = {} |
| 108 |
if mode == "batchfile" then |
| 109 |
lines[1] = [=[ |
| 110 |
::dummy:: --[[ |
| 111 |
@texlua "%~f0" %* |
| 112 |
@goto :eof |
| 113 |
]] |
| 114 |
]=] |
| 115 |
else |
| 116 |
if shebang then |
| 117 |
lines[1] = shebang |
| 118 |
end |
| 119 |
end |
| 120 |
|
| 121 |
if not preserve_location_info then |
| 122 |
table.insert(lines, string.format("local %s = %s\n", table.concat(imported_globals, ", "), table.concat(imported_globals, ", "))) |
| 123 |
end |
| 124 |
|
| 125 |
if default_os then |
| 126 |
table.insert(lines, string.format("os.type = os.type or %q\n", default_os)) |
| 127 |
end |
| 128 |
|
| 129 |
|
| 130 |
table.insert(lines, "if lfs and not package.loaded['lfs'] then package.loaded['lfs'] = lfs end\n") |
| 131 |
if preserve_location_info then |
| 132 |
table.insert(lines, "local loadstring = loadstring or load\n") |
| 133 |
for _,m in ipairs(modules) do |
| 134 |
if m.path_windows or m.path_unix then |
| 135 |
table.insert(lines, 'if os.type == "windows" then\n') |
| 136 |
table.insert(lines, string.format("package.preload[%q] = assert(loadstring(%q, %q))\n", m.name, load_module_code(m.path_windows or m.path), "=" .. (m.path_windows or m.path))) |
| 137 |
table.insert(lines, 'else\n') |
| 138 |
table.insert(lines, string.format("package.preload[%q] = assert(loadstring(%q, %q))\n", m.name, load_module_code(m.path_unix or m.path), "=" .. (m.path_unix or m.path))) |
| 139 |
table.insert(lines, 'end\n') |
| 140 |
else |
| 141 |
table.insert(lines, string.format("package.preload[%q] = assert(loadstring(%q, %q))\n", m.name, load_module_code(m.path), "=" .. m.path)) |
| 142 |
end |
| 143 |
end |
| 144 |
table.insert(lines, string.format("assert(loadstring(%q, %q))(...)\n", main, "=cluttex.lua")) |
| 145 |
else |
| 146 |
for _,m in ipairs(modules) do |
| 147 |
if m.path_windows or m.path_unix then |
| 148 |
table.insert(lines, 'if os.type == "windows" then\n') |
| 149 |
table.insert(lines, string.format("package.preload[%q] = function(...)\n%send\n", m.name, load_module_code(m.path_windows or m.path))) |
| 150 |
table.insert(lines, 'else\n') |
| 151 |
table.insert(lines, string.format("package.preload[%q] = function(...)\n%send\n", m.name, load_module_code(m.path_unix or m.path))) |
| 152 |
table.insert(lines, 'end\n') |
| 153 |
else |
| 154 |
table.insert(lines, string.format("package.preload[%q] = function(...)\n%send\n", m.name, load_module_code(m.path))) |
| 155 |
end |
| 156 |
end |
| 157 |
table.insert(lines, strip_global_imports(main)) |
| 158 |
end |
| 159 |
|
| 160 |
if outfile then |
| 161 |
io.output(assert(io.open(outfile, "wb"))) |
| 162 |
end |
| 163 |
io.write(table.concat(lines, "")) |