mtx-cache.lua /size: 3752 b    last modification: 2023-12-21 09:43
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</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 function collect(path)
50    local all = dir.glob(path .. "/**/*")
51    local ext = table.setmetatableindex("table")
52    for i=1,#all do
53        local name = all[i]
54        local list = ext[filesuffix(name)]
55        list[#list+1] = name
56    end
57    return ext
58end
59
60local function list(banner,path,ext)
61    local total = 0
62    report("%s: %s",banner,path)
63    report()
64    for k, v in table.sortedhash(ext) do
65        total = total + #v
66        report("%-6s : %4i",k,#v)
67    end
68    report()
69    report("total  : %4i",total)
70    report()
71end
72
73local function erase(banner,path,list)
74    report("%s: %s",banner,path)
75    report()
76    for ext, list in table.sortedhash(list) do
77        local gone = 0
78        local kept = 0
79        for i=1,#list do
80            local filename = list[i]
81         -- if find(filename,"luatex%-cache") then
82            if find(filename,LUATEXENGINE .. "%-cache") then
83                remove(filename)
84                if isfile(filename) then
85                    kept = kept + 1
86                else
87                    gone = gone + 1
88                end
89            end
90        end
91        report("%-6s : %4i gone, %4i kept",ext,gone,kept)
92    end
93    report()
94end
95
96function scripts.cache.make()
97    os.execute("mtxrun --generate")
98    os.execute("context --make")
99    os.execute("mtxrun --script font --reload")
100end
101
102function scripts.cache.erase()
103    local writable = caches.getwritablepath()
104    local groups   = collect(writable)
105    list("writable path",writable,groups)
106    erase("writable path",writable,groups)
107    if environment.argument("make") then
108        scripts.cache.make()
109    end
110end
111
112function scripts.cache.list()
113    local readables = caches.getreadablepaths()
114    local writable  = caches.getwritablepath()
115    local groups    = collect(writable)
116    list("writable path",writable,groups)
117    for i=1,#readables do
118        local readable = readables[i]
119        if readable ~= writable then
120            local groups = collect(readable)
121            list("readable path",readable,groups)
122        end
123    end
124end
125
126if environment.argument("erase") then
127    scripts.cache.erase()
128elseif environment.argument("list") then
129    scripts.cache.list()
130elseif environment.argument("make") then
131    scripts.cache.make()
132elseif environment.argument("exporthelp") then
133    application.export(environment.argument("exporthelp"),environment.files[1])
134else
135    application.help()
136end
137