mtx-cache.lua /size: 3691 b    last modification: 2020-07-01 14:35
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                remove(filename)
83                if isfile(filename) then
84                    kept = kept + 1
85                else
86                    gone = gone + 1
87                end
88            end
89        end
90        report("%-6s : %4i gone, %4i kept",ext,gone,kept)
91    end
92    report()
93end
94
95function scripts.cache.make()
96    os.execute("mtxrun --generate")
97    os.execute("context --make")
98    os.execute("mtxrun --script font --reload")
99end
100
101function scripts.cache.erase()
102    local writable = caches.getwritablepath()
103    local groups   = collect(writable)
104    list("writable path",writable,groups)
105    erase("writable path",writable,groups)
106    if environment.argument("make") then
107        scripts.cache.make()
108    end
109end
110
111function scripts.cache.list()
112    local readables = caches.getreadablepaths()
113    local writable  = caches.getwritablepath()
114    local groups    = collect(writable)
115    list("writable path",writable,groups)
116    for i=1,#readables do
117        local readable = readables[i]
118        if readable ~= writable then
119            local groups = collect(readable)
120            list("readable path",readable,groups)
121        end
122    end
123end
124
125if environment.argument("erase") then
126    scripts.cache.erase()
127elseif environment.argument("list") then
128    scripts.cache.list()
129elseif environment.argument("make") then
130    scripts.cache.make()
131elseif environment.argument("exporthelp") then
132    application.export(environment.argument("exporthelp"),environment.files[1])
133else
134    application.help()
135end
136