grph-fil.lua /size: 6148 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['grph-fil'] = {
2    version   = 1.001,
3    comment   = "companion to grph-fig.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 type = type
10
11local trace_run  = false  trackers.register("graphic.runfile",function(v) trace_run = v end)
12local report_run = logs.reporter("graphics","run")
13
14local isfile        = lfs.isfile
15local replacesuffix = file.replacesuffix
16local addsuffix     = file.addsuffix
17local checksum      = file.checksum
18
19-- Historically running files is part of graphics processing, so this is why it
20-- sits here but is part of the job namespace.
21
22local allocate = utilities.storage.allocate
23
24local collected = allocate()
25local tobesaved = allocate()
26
27local jobfiles = {
28    collected = collected,
29    tobesaved = tobesaved,
30    forcerun  = false, -- maybe a directive some day
31}
32
33job.files = jobfiles
34
35local inputsuffix  = "tex"
36local resultsuffix = "pdf"
37
38local function initializer()
39    tobesaved = jobfiles.tobesaved
40    collected = jobfiles.collected
41end
42
43job.register('job.files.collected', tobesaved, initializer)
44
45-- When there is a runpath specified, we're already there, so then we only need to
46-- pass the orginal path. But we pass it because it will prevent prepending the
47-- current direction to the given name.
48
49local contextrunner = sandbox.registerrunner {
50    name     = "hashed context run",
51    program  = "context",
52    template = [[%options% %?path: --runpath=%path% ?% %filename%]],
53    checkers = {
54        options  = "string",
55        filename = "readable",
56        path     = "string",
57     -- runpath  = "string",
58    }
59}
60
61-- we can also use:
62--
63-- local jobvariables = job.variables
64-- jobvariables.getchecksum(tag)
65-- jobvariables.makechecksum(data)
66-- jobvariables.setchecksum(tag,checksum)
67
68-- The runpath features makes things more complex than needed, so we need to wrap
69-- that some day in a helper. This is also very sensitive for both being set!
70
71function jobfiles.run(action)
72    local filename = action.filename
73    if filename and filename ~= "" then
74        local result      = action.result
75        local runner      = action.runner or contextrunner
76        local oldchecksum = collected[filename]
77        local newchecksum = checksum(filename)
78        local tobedone    = false
79        local forcerun    = action.forcerun or jobfiles.forcerun
80        if not result then
81            result = replacesuffix(usedname,resultsuffix)
82            action.result = result
83        end
84        if forcerun then
85            tobedone = true
86            if trace_run then
87                report_run("processing file, changes in %a, %s",filename,"processing forced")
88            end
89        end
90        if not tobedone and not oldchecksum then
91            tobedone = true
92            if trace_run then
93                report_run("processing file, changes in %a, %s",filename,"no checksum yet")
94            end
95        end
96        if not tobedone and oldchecksum ~= newchecksum then
97            tobedone = true
98            if trace_run then
99                report_run("processing file, changes in %a, %s",filename,"checksum mismatch")
100            end
101        end
102        if not tobedone and not isfile(result) then
103            tobedone = true
104            if trace_run then
105                report_run("processing file, changes in %a, %s",filename,"no result file")
106            end
107        end
108        if tobedone then
109            local kind = type(runner)
110            if kind == "function" then
111                if trace_run then
112                    report_run("processing file, command: %s",action.name or "unknown")
113                end
114                -- We can have a sandbox.registerrunner here in which case we need to make
115                -- sure that we don't feed a function into the checker. So one cannot use a
116                -- variable named "runner" in the template but that's no big deal.
117                local r = action.runner
118                action.runner = nil
119                runner(action)
120                action.runner = r
121            elseif kind == "string" then
122                -- can be anything but we assume it gets checked by the sandbox
123                if trace_run then
124                    report_run("processing file, command: %s",runner)
125                end
126                os.execute(runner)
127            else
128                report_run("processing file, changes in %a, %s",filename,"no valid runner")
129            end
130        elseif trace_run then
131            report_run("processing file, no changes in %a, %s",filename,"not processed")
132        end
133        tobesaved[filename] = newchecksum
134    else
135        -- silently ignore error
136    end
137end
138
139--
140
141local done = { }
142
143local function analyzed(name,options)
144    local usedname   = addsuffix(name,inputsuffix)      -- we assume tex if not set
145    local resultname = replacesuffix(name,resultsuffix) -- we assume tex if not set
146    local pathname   = file.pathpart(usedname)
147    local runpath    = environment.arguments.path -- sic, no runpath
148    if pathname ~= "" then
149        if runpath then
150            runpath = file.join(runpath,pathname)
151        else
152            runpath = pathname
153        end
154        usedname = file.basename(usedname)
155    end
156    return {
157        options  = options,
158        path     = runpath, -- or nil
159        filename = usedname,
160        result   = resultname,
161    }
162end
163
164function jobfiles.context(name,options) -- runpath ?
165    if type(name) == "table" then
166        local result = { }
167        for i=1,#name do
168            result[#result+1] = jobfiles.context(name[i],options)
169        end
170        return result
171    elseif name ~= "" then
172        local action = analyzed(name,options)
173        local result = action.result
174        if not done[result] then
175            jobfiles.run(action)
176            done[result] = true
177        end
178        return result
179    else
180        return { }
181    end
182end
183
184interfaces.implement {
185    name      = "runcontextjob",
186    arguments = "2 strings",
187    actions   = { jobfiles.context, context }
188}
189