data-aux.lua /size: 2610 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['data-aux'] = {
2    version   = 1.001,
3    comment   = "companion to luat-lib.mkiv",
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 find = string.find
10local type, next = type, next
11local addsuffix, removesuffix = file.addsuffix, file.removesuffix
12local loaddata, savedata = io.loaddata, io.savedata
13
14local trace_locating = false  trackers.register("resolvers.locating", function(v) trace_locating = v end)
15
16local resolvers = resolvers
17local cleanpath = resolvers.cleanpath
18local findfiles = resolvers.findfiles
19
20local report_scripts = logs.reporter("resolvers","scripts")
21
22function resolvers.updatescript(oldname,newname) -- oldname -> own.name, not per se a suffix
23 -- local scriptpath = "scripts/context/lua"
24    local scriptpath = "context/lua"
25    local oldscript  = cleanpath(oldname)
26    local newname    = addsuffix(newname,"lua")
27    local newscripts = findfiles(newname) or { }
28    if trace_locating then
29        report_scripts("to be replaced old script %a", oldscript)
30    end
31    if #newscripts == 0 then
32        if trace_locating then
33            report_scripts("unable to locate new script")
34        end
35    else
36        for i=1,#newscripts do
37            local newscript = cleanpath(newscripts[i])
38            if trace_locating then
39                report_scripts("checking new script %a", newscript)
40            end
41            if oldscript == newscript then
42                if trace_locating then
43                    report_scripts("old and new script are the same")
44                end
45            elseif not find(newscript,scriptpath,1,true) then
46                if trace_locating then
47                    report_scripts("new script should come from %a",scriptpath)
48                end
49            elseif not (find(oldscript,removesuffix(newname).."$") or find(oldscript,newname.."$")) then
50                if trace_locating then
51                    report_scripts("invalid new script name")
52                end
53            else
54                local newdata = loaddata(newscript)
55                if newdata then
56                    if trace_locating then
57                        report_scripts("old script content replaced by new content: %s",oldscript)
58                    end
59                    savedata(oldscript,newdata)
60                    break
61                elseif trace_locating then
62                    report_scripts("unable to load new script")
63                end
64            end
65        end
66    end
67end
68