mtx-cache.lua /size: 3994 b    last modification: 2025-02-21 11:03
1if not modules then modules = { } end modules ['mtx-cache'] = {
2    version   = 1.001,
3    comment   = "companion to mtxrun.lua",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local helpinfo = [[
10<?xml version="1.0"?>
11<application>
12 <metadata>
13  <entry name="name">mtx-cache</entry>
14  <entry name="detail">ConTeXt &amp; MetaTeX Cache Management</entry>
15  <entry name="version">1.01</entry>
16 </metadata>
17 <flags>
18  <category name="basic">
19   <subcategory>
20    <flag name="make"><short>generate databases and formats</short></flag>
21    <flag name="erase"><short>completely remove cache [--bytecode]</short></flag>
22    <flag name="list"><short>show cache</short></flag>
23   </subcategory>
24   <subcategory>
25    <flag name="fonts"><short>only wipe fonts</short></flag>
26   </subcategory>
27  </category>
28 </flags>
29</application>
30]]
31
32
33local find = string.find
34local filesuffix, replacesuffix = file.suffix, file.replacesuffix
35local isfile = lfs.isfile
36local remove = os.remove
37
38local application = logs.application {
39    name     = "mtx-cache",
40    banner   = "ConTeXt & MetaTeX Cache Management 0.10",
41    helpinfo = helpinfo,
42}
43
44local report = application.report
45
46scripts       = scripts       or { }
47scripts.cache = scripts.cache or { }
48
49local bytecodes = { tmc = true, tmd = true }
50
51local function collect(path,bytecode)
52    local all = dir.glob(path .. "/**/*")
53    local ext = table.setmetatableindex("table")
54    for i=1,#all do
55        local name   = all[i]
56        local suffix = filesuffix(name)
57        if not bytecode or bytecodes[suffix] then
58            local list = ext[suffix]
59            list[#list+1] = name
60        end
61    end
62    return ext
63end
64
65local function list(banner,path,ext)
66    local total = 0
67    report("%s: %s",banner,path)
68    report()
69    for k, v in table.sortedhash(ext) do
70        total = total + #v
71        report("%-6s : %4i",k,#v)
72    end
73    report()
74    report("total  : %4i",total)
75    report()
76end
77
78local function erase(banner,path,list)
79    report("%s: %s",banner,path)
80    report()
81    for ext, list in table.sortedhash(list) do
82        local gone = 0
83        local kept = 0
84        local okay = 0
85        for i=1,#list do
86            local filename = list[i]
87         -- if find(filename,"luatex%-cache") then
88            if find(filename,LUATEXENGINE .. "%-cache") then
89                remove(filename)
90                if isfile(filename) then
91                    kept = kept + 1
92                else
93                    gone = gone + 1
94                end
95            end
96        end
97        report("%-6s : %4i gone, %4i kept",ext,gone,kept)
98    end
99    report()
100end
101
102function scripts.cache.make()
103    os.execute("mtxrun --generate")
104    os.execute("context --make")
105    os.execute("mtxrun --script font --reload")
106end
107
108function scripts.cache.erase(bytecode)
109    local writable = caches.getwritablepath()
110    local groups   = collect(writable,bytecode)
111    list("writable path",writable,groups)
112    erase("writable path",writable,groups)
113    if environment.argument("make") then
114        scripts.cache.make()
115    end
116end
117
118function scripts.cache.list()
119    local readables = caches.getreadablepaths()
120    local writable  = caches.getwritablepath()
121    local groups    = collect(writable)
122    list("writable path",writable,groups)
123    for i=1,#readables do
124        local readable = readables[i]
125        if readable ~= writable then
126            local groups = collect(readable)
127            list("readable path",readable,groups)
128        end
129    end
130end
131
132if environment.argument("erase") then
133    scripts.cache.erase(environment.argument("bytecode"))
134elseif environment.argument("list") then
135    scripts.cache.list()
136elseif environment.argument("make") then
137    scripts.cache.make()
138elseif environment.argument("exporthelp") then
139    application.export(environment.argument("exporthelp"),environment.files[1])
140else
141    application.help()
142end
143