| 1 |
#!/usr/bin/env texlua |
| 2 |
|
| 3 |
-- Description: Install TeX packages and their dependencies |
| 4 |
-- Copyright: 2023-2024 (c) Jianrui Lyu <[email protected]> |
| 5 |
-- Repository: https://github.com/lvjr/texfindpkg |
| 6 |
-- License: GNU General Public License v3.0 |
| 7 |
|
| 8 |
tfp = { building = true } |
| 9 |
|
| 10 |
kpse.set_program_name("kpsewhich") |
| 11 |
local tfp = require(kpse.lookup("texfindpkg.lua")) |
| 12 |
|
| 13 |
------------------------------------------------------------ |
| 14 |
--> \section{Some variables and functions} |
| 15 |
------------------------------------------------------------ |
| 16 |
|
| 17 |
local tfpMain = tfp.tfpMain |
| 18 |
local showdbg = tfp.showdbg |
| 19 |
local dbgPrint = tfp.dbgPrint |
| 20 |
local fileRead = tfp.fileRead |
| 21 |
local fileWrite = tfp.fileWrite |
| 22 |
local getFiles = tfp.getFiles |
| 23 |
local valueExists = tfp.valueExists |
| 24 |
local json = tfp.json |
| 25 |
local gzip = tfp.gzip |
| 26 |
local tlParsePackageDB = tfp.tlParsePackageDB |
| 27 |
local mtParsePackageDB = tfp.mtParsePackageDB |
| 28 |
|
| 29 |
local insert = table.insert |
| 30 |
local remove = table.remove |
| 31 |
local concat = table.concat |
| 32 |
local gmatch = string.gmatch |
| 33 |
local match = string.match |
| 34 |
|
| 35 |
------------------------------------------------------------ |
| 36 |
--> \section{Compare TeX Live and MiKTeX packages} |
| 37 |
------------------------------------------------------------ |
| 38 |
|
| 39 |
local tlpkgname = "download/texlive.tlpdb" |
| 40 |
local mtpkgname = "download/package-manifests.ini" |
| 41 |
|
| 42 |
local function compareDistributions() |
| 43 |
tlpkgtext = fileRead(tlpkgname) |
| 44 |
if tlpkgtext then |
| 45 |
tlpkgdata = tlParsePackageDB(tlpkgtext) |
| 46 |
else |
| 47 |
print("error in reading texlive package database!") |
| 48 |
end |
| 49 |
mtpkgtext = fileRead(mtpkgname) |
| 50 |
if mtpkgtext then |
| 51 |
mtpkgdata = mtParsePackageDB(mtpkgtext) |
| 52 |
else |
| 53 |
print("error in reading miktex package database!") |
| 54 |
end |
| 55 |
local tlmissing, mkmissing = {}, {} |
| 56 |
for k, vt in pairs(tlpkgdata) do |
| 57 |
local vm = mtpkgdata[k] -- or "[none]" |
| 58 |
if vm then |
| 59 |
mtpkgdata[k] = nil -- remove it |
| 60 |
if vm ~= vt then |
| 61 |
print("texlive->" .. vt, "miktex->" .. vm, k) |
| 62 |
end |
| 63 |
else |
| 64 |
insert(mkmissing, {vt, k}) |
| 65 |
end |
| 66 |
end |
| 67 |
for k, v in pairs(mtpkgdata) do |
| 68 |
print("texlive doesn't include " .. v .. " -> " .. k) |
| 69 |
end |
| 70 |
for _, v in ipairs(mkmissing) do |
| 71 |
print("miktex doesn't include " .. v[1] .. " -> " .. v[2]) |
| 72 |
end |
| 73 |
end |
| 74 |
|
| 75 |
------------------------------------------------------------ |
| 76 |
--> \section{Generate json file from cwl files} |
| 77 |
------------------------------------------------------------ |
| 78 |
|
| 79 |
local function insertNewValue(tbl, val) |
| 80 |
if not valueExists(tbl, val) then |
| 81 |
insert(tbl, val) |
| 82 |
end |
| 83 |
end |
| 84 |
|
| 85 |
local latexcwls = {"tex", "xetex", "luatex", "plaintex", "latex-document", "latex-dev"} |
| 86 |
local islatexcwl = false |
| 87 |
|
| 88 |
local function cwlToRealName(base) |
| 89 |
if valueExists(latexcwls, base) then return end |
| 90 |
local n = match(base, "^tikzlibrary(.+)$") |
| 91 |
if n then return base .. ".code.tex" end |
| 92 |
local n = match(base, "^tcolorboxlibrary(.+)$") |
| 93 |
if n then |
| 94 |
if n == "many" or n == "most" or n == "all" then |
| 95 |
return nil |
| 96 |
else |
| 97 |
return "tcb" .. n .. ".code.tex" |
| 98 |
end |
| 99 |
end |
| 100 |
n = match(base, "^class%-(.+)$") |
| 101 |
if n then return n .. ".cls" end |
| 102 |
return base .. ".sty" |
| 103 |
end |
| 104 |
|
| 105 |
local cwldata = {} |
| 106 |
|
| 107 |
-- Ignore lines after "# from xxx option of somepkg", see #7 |
| 108 |
local function ignoreCmdsFromOptions(cwl) |
| 109 |
local result = "" |
| 110 |
local isignored = false |
| 111 |
for line in gmatch(cwl .. "\n\n", "([^\r\n]-)\r?\n") do |
| 112 |
if not isignored then |
| 113 |
if match(line, "# from [^ ]+ option of [^ ]+") then |
| 114 |
isignored = true |
| 115 |
--print("start ignoring: " .. line) |
| 116 |
else |
| 117 |
result = result .. line .. "\n" |
| 118 |
end |
| 119 |
else |
| 120 |
if match(line, "^ -$") or match(line, "^#endif$") then |
| 121 |
isignored = false |
| 122 |
--print("stop ignoring: " .. line) |
| 123 |
end |
| 124 |
end |
| 125 |
end |
| 126 |
if isignored then |
| 127 |
dbgPrint("missing stop point!") |
| 128 |
end |
| 129 |
--print(result) |
| 130 |
return result |
| 131 |
end |
| 132 |
|
| 133 |
local function extractFileData(cwl) |
| 134 |
cwl = ignoreCmdsFromOptions(cwl) |
| 135 |
-- the cwl files have different eol characters |
| 136 |
local deps = {} |
| 137 |
for base in gmatch(cwl, "\n#include:(.-)[\r\n]") do |
| 138 |
--dbgPrint(base) |
| 139 |
local n = cwlToRealName(base) |
| 140 |
if n then insertNewValue(deps, n) end |
| 141 |
end |
| 142 |
local envs = {} |
| 143 |
if islatexcwl and cwldata["latex.ltx"] then |
| 144 |
envs = cwldata["latex.ltx"].envs or {} |
| 145 |
end |
| 146 |
for e in gmatch(cwl, "\n\\begin{(.-)}") do |
| 147 |
--dbgPrint("{" .. e .. "}") |
| 148 |
insertNewValue(envs, e) |
| 149 |
end |
| 150 |
local cmds = {} |
| 151 |
if islatexcwl and cwldata["latex.ltx"] then |
| 152 |
cmds = cwldata["latex.ltx"].cmds or {} |
| 153 |
end |
| 154 |
for c in gmatch(cwl, "\n\\(%a+)") do |
| 155 |
if c ~= "begin" and c ~= "end" then |
| 156 |
--dbgPrint("\\" .. c) |
| 157 |
if not valueExists(cmds, c) then |
| 158 |
insert(cmds, c) |
| 159 |
end |
| 160 |
end |
| 161 |
end |
| 162 |
--return {deps, envs, cmds} |
| 163 |
if #deps == 0 then deps = nil end |
| 164 |
if #cmds == 0 then cmds = nil end |
| 165 |
if #envs == 0 then envs = nil end |
| 166 |
return {deps = deps, envs = envs, cmds = cmds} |
| 167 |
end |
| 168 |
|
| 169 |
local function writeJson() |
| 170 |
print("writing json database to file...") |
| 171 |
local tbl1 = {} |
| 172 |
for k, v in pairs(cwldata) do |
| 173 |
insert(tbl1, {k, v}) |
| 174 |
end |
| 175 |
table.sort(tbl1, function(a, b) |
| 176 |
-- make latex.ltx the first item in the json file |
| 177 |
local aa, bb |
| 178 |
if a[1] == "latex.ltx" then aa = "001" else aa = a[1] end |
| 179 |
if b[1] == "latex.ltx" then bb = "001" else bb = b[1] end |
| 180 |
return aa < bb |
| 181 |
end) |
| 182 |
local tbl2 = {} |
| 183 |
for _, v in ipairs(tbl1) do |
| 184 |
local item = '"' .. v[1] .. '":' .. json.tostring(v[2]) |
| 185 |
insert(tbl2, item) |
| 186 |
end |
| 187 |
local text = "{\n" .. concat(tbl2, "\n,\n") .. "\n}" |
| 188 |
fileWrite(text, "texfindpkg.json") |
| 189 |
fileWrite(gzip.compress(text), "texfindpkg.json.gz") |
| 190 |
end |
| 191 |
|
| 192 |
local cwlpath = "completion" |
| 193 |
|
| 194 |
local ignoredcwls = {"optex"} |
| 195 |
|
| 196 |
local function generateJsonData() |
| 197 |
local list = getFiles(cwlpath, "%.cwl$") |
| 198 |
local fname |
| 199 |
for _, v in ipairs(list) do |
| 200 |
local base = match(v, "^(.+)%.cwl") |
| 201 |
if not valueExists(ignoredcwls, base) then |
| 202 |
if valueExists(latexcwls, base) then |
| 203 |
islatexcwl = true |
| 204 |
fname = "latex.ltx" |
| 205 |
else |
| 206 |
islatexcwl = false |
| 207 |
fname = cwlToRealName(base) |
| 208 |
end |
| 209 |
if fname then |
| 210 |
--print(fname) |
| 211 |
local cwl = fileRead(cwlpath .. "/" .. v) |
| 212 |
--print(cwl) |
| 213 |
if cwl then |
| 214 |
local item = extractFileData(cwl) |
| 215 |
dbgPrint(item) |
| 216 |
cwldata[fname] = item |
| 217 |
else |
| 218 |
print("error in reading " .. v) |
| 219 |
end |
| 220 |
end |
| 221 |
end |
| 222 |
end |
| 223 |
writeJson() |
| 224 |
end |
| 225 |
|
| 226 |
------------------------------------------------------------ |
| 227 |
--> \section{Print help or version text} |
| 228 |
------------------------------------------------------------ |
| 229 |
|
| 230 |
local helptext = [[ |
| 231 |
usage: texfindpkg <action> [<options>] [<name>] |
| 232 |
|
| 233 |
valid actions are: |
| 234 |
generate Generate texfindpkg.json and texfindpkg.json.gz |
| 235 |
compare Compare packages in TeXLive and MiKTeX |
| 236 |
check Check test files and report the results |
| 237 |
save Check test files and save the results |
| 238 |
help Print this message and exit |
| 239 |
version Print version information and exit |
| 240 |
|
| 241 |
please report bug at https://github.com/lvjr/texfindpkg |
| 242 |
]] |
| 243 |
|
| 244 |
local function help() |
| 245 |
print(helptext) |
| 246 |
end |
| 247 |
|
| 248 |
local function version() |
| 249 |
print("TeXFindPkg Version " .. tfp.version .. " (" .. tfp.date .. ")\n") |
| 250 |
end |
| 251 |
|
| 252 |
------------------------------------------------------------ |
| 253 |
--> \section{Check or save test files} |
| 254 |
------------------------------------------------------------ |
| 255 |
|
| 256 |
local function cmdLineToArgList(cmdline) |
| 257 |
local tfparg = {} |
| 258 |
for v in gmatch(cmdline, "([^ ]+)") do |
| 259 |
insert(tfparg, v) |
| 260 |
end |
| 261 |
remove(tfparg, 1) |
| 262 |
return tfparg |
| 263 |
end |
| 264 |
|
| 265 |
local testpath = "test" |
| 266 |
|
| 267 |
local function readTestFiles(v) |
| 268 |
local tests = {} |
| 269 |
local text = fileRead(testpath .. "/" .. v) |
| 270 |
for i, o in gmatch(text, "<<<<+\n(.-)\n%-%-%-%-+\n(.-)\n>>>>+") do |
| 271 |
--print(i, o) |
| 272 |
insert(tests, {i, o}) |
| 273 |
end |
| 274 |
return tests |
| 275 |
end |
| 276 |
|
| 277 |
local function checkTestFiles() |
| 278 |
local files = getFiles(testpath, "%.tfp$") |
| 279 |
for _, v in ipairs(files) do |
| 280 |
print("checking test file " .. v) |
| 281 |
local tests = readTestFiles(v) |
| 282 |
for _, v in ipairs(tests) do |
| 283 |
local i, o = v[1], v[2] .. "\n" |
| 284 |
local result = tfpMain(cmdLineToArgList(i)) |
| 285 |
if o == result then |
| 286 |
dbgPrint("test passed for '" .. i .. "'") |
| 287 |
else |
| 288 |
print("test failed for '" .. i .. "'") |
| 289 |
end |
| 290 |
end |
| 291 |
end |
| 292 |
end |
| 293 |
|
| 294 |
local function saveTestFiles() |
| 295 |
local files = getFiles(testpath, "%.tfp$") |
| 296 |
for _, v in ipairs(files) do |
| 297 |
print("reading test file " .. v) |
| 298 |
local tests = readTestFiles(v) |
| 299 |
local text = "" |
| 300 |
for _, v in ipairs(tests) do |
| 301 |
local i = v[1] |
| 302 |
dbgPrint("running test '" .. i .. "'") |
| 303 |
local o = tfpMain(cmdLineToArgList(i)) |
| 304 |
local sect = "<<<<<<<<\n" .. i .. "\n--------\n" .. o .. ">>>>>>>>\n" |
| 305 |
text = text .. sect |
| 306 |
end |
| 307 |
print("saving test file " .. v) |
| 308 |
fileWrite(text, testpath .. "/" .. v) |
| 309 |
end |
| 310 |
end |
| 311 |
|
| 312 |
------------------------------------------------------------ |
| 313 |
--> \section{Respond to user input} |
| 314 |
------------------------------------------------------------ |
| 315 |
|
| 316 |
local function main() |
| 317 |
if arg[1] == nil then return help() end |
| 318 |
local action = remove(arg, 1) |
| 319 |
action = match(action, "^%-*(.*)$") -- remove leading dashes |
| 320 |
--print(action) |
| 321 |
if action == "help" then |
| 322 |
help() |
| 323 |
elseif action == "version" then |
| 324 |
version() |
| 325 |
elseif action == "generate" then |
| 326 |
generateJsonData() |
| 327 |
elseif action == "compare" then |
| 328 |
compareDistributions() |
| 329 |
elseif action == "check" then |
| 330 |
checkTestFiles() |
| 331 |
elseif action == "save" then |
| 332 |
saveTestFiles() |
| 333 |
else |
| 334 |
print("unknown action '" .. action .. "'") |
| 335 |
help() |
| 336 |
end |
| 337 |
end |
| 338 |
|
| 339 |
main() |