summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/grph-fil.lua
blob: afb303acd17b702cd2802a630e2182a50f97ec87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
if not modules then modules = { } end modules ['grph-fil'] = {
    version   = 1.001,
    comment   = "companion to grph-fig.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local type = type

local trace_run  = false  trackers.register("graphic.runfile",function(v) trace_run = v end)
local report_run = logs.reporter("graphics","run")

local isfile        = lfs.isfile
local replacesuffix = file.replacesuffix
local addsuffix     = file.addsuffix
local checksum      = file.checksum

-- Historically running files is part of graphics processing, so this is why it
-- sits here but is part of the job namespace.

local allocate = utilities.storage.allocate

local collected = allocate()
local tobesaved = allocate()

local jobfiles = {
    collected = collected,
    tobesaved = tobesaved,
    forcerun  = false, -- maybe a directive some day
}

job.files = jobfiles

local inputsuffix  = "tex"
local resultsuffix = "pdf"

local function initializer()
    tobesaved = jobfiles.tobesaved
    collected = jobfiles.collected
end

job.register('job.files.collected', tobesaved, initializer)

-- When there is a runpath specified, we're already there, so then we only need to
-- pass the orginal path. But we pass it because it will prevent prepending the
-- current direction to the given name.

local contextrunner = sandbox.registerrunner {
    name     = "hashed context run",
    program  = "context",
    template = [[%options% %?path: --path=%path% ?% %?runpath: --runpath=%runpath% ?% %filename%]],
    checkers = {
        options  = "string",
        filename = "readable",
        path     = "string",
        runpath  = "string",
    }
}

-- we can also use:
--
-- local jobvariables = job.variables
-- jobvariables.getchecksum(tag)
-- jobvariables.makechecksum(data)
-- jobvariables.setchecksum(tag,checksum)

-- The runpath features makes things more complex than needed, so we need to wrap
-- that some day in a helper. This is also very sensitive for both being set!

function jobfiles.run(action)
    local filename = action.filename
    if filename and filename ~= "" then
        local result      = action.result
        local runner      = action.runner or contextrunner
        local path        = action.path
if not isfile(filename) and path and path ~= "" then
    filename = file.join(path,filename)
end
        local oldchecksum = collected[filename]
        local newchecksum = checksum(filename)
-- print(filename,oldchecksum,newchecksum)
        local tobedone    = false
        local forcerun    = action.forcerun or jobfiles.forcerun
        if not result then
            result = replacesuffix(filename,resultsuffix)
            action.result = result
        end
        if forcerun then
            tobedone = true
            if trace_run then
                report_run("processing file, changes in %a, %s",filename,"processing forced")
            end
        end
        if not tobedone and not oldchecksum then
            tobedone = true
            if trace_run then
                report_run("processing file, changes in %a, %s",filename,"no checksum yet")
            end
        end
        if not tobedone and oldchecksum ~= newchecksum then
            tobedone = true
            if trace_run then
                report_run("processing file, changes in %a, %s",filename,"checksum mismatch")
            end
        end
        if not tobedone and not isfile(result) then
            tobedone = true
            if trace_run then
                report_run("processing file, changes in %a, %s",filename,"no result file")
            end
        end
        if tobedone then
            local kind = type(runner)
            if kind == "function" then
                if trace_run then
                    report_run("processing file, command: %s",action.name or "unknown")
                end
                -- We can have a sandbox.registerrunner here in which case we need to make
                -- sure that we don't feed a function into the checker. So one cannot use a
                -- variable named "runner" in the template but that's no big deal.
                local r = action.runner
                action.runner = nil
                runner(action)
                action.runner = r
            elseif kind == "string" then
                -- can be anything but we assume it gets checked by the sandbox
                if trace_run then
                    report_run("processing file, command: %s",runner)
                end
                os.execute(runner)
            else
                report_run("processing file, changes in %a, %s",filename,"no valid runner")
            end
        elseif trace_run then
            report_run("processing file, no changes in %a, %s",filename,"not processed")
        end
        tobesaved[filename] = newchecksum
    else
        -- silently ignore error
    end
end

--

local done = { }

local function analyzed(name,options)
    local usedname   = addsuffix(name,inputsuffix)      -- we assume tex if not set
    local resultname = replacesuffix(name,resultsuffix) -- we assume tex if not set
    local pathname   = file.pathpart(usedname)
    local path       = environment.arguments.path -- sic, no runpath
    local runpath    = environment.arguments.runpath
    local resultname = replacesuffix(name,resultsuffix) -- we assume tex if not set
    if runpath and runpath ~= "" then
        -- not really needed but probably more robust for local leftovers
        resultname = file.join(runpath,file.basename(resultname))
    end
    if path ~= "" then
        if path then
            path = file.join(path,pathname)
        else
            path = pathname
        end
        usedname = file.basename(usedname)
    end
    return {
        options  = options,
        path     = path,
        filename = usedname,
        result   = resultname,
        runpath  = runpath,
    }
end

function jobfiles.context(name,options) -- runpath ?
    if type(name) == "table" then
        local result = { }
        for i=1,#name do
            result[#result+1] = jobfiles.context(name[i],options)
        end
        return result
    elseif name ~= "" then
        local action = analyzed(name,options)
        local result = action.result
        if not done[result] then
            jobfiles.run(action)
            done[result] = true
        end
        return result
    else
        return { }
    end
end

interfaces.implement {
    name      = "runcontextjob",
    arguments = "2 strings",
    actions   = { jobfiles.context, context }
}