summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/grph-fil.lua
blob: 1044d29d99c226330c40e3accb897b01c9e27843 (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
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 runner = sandbox.registerrunner {
    name     = "hashed context run",
    program  = "context",
    template = [[%options% %?path: --runpath=%path% ?% %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!

local function analyzed(name)
    local actiontype = type(action)
    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 runpath    = environment.arguments.path -- sic, no runpath
    if pathname ~= "" then
        if runpath then
            runpath = file.join(action.path,pathname)
        else
            runpath = pathname
        end
        usedname = file.basename(usedname)
    end
    return {
        options  = options,
        path     = runpath,
        filename = usedname,
        result   = resultname,
    }
end

function jobfiles.run(action)
    local filename    = action.filename
    local result      = action.result
    local oldchecksum = collected[filename]
    local newchecksum = checksum(filename)
    local tobedone    = false
    if jobfiles.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
        runner(action)
    elseif trace_run then
        report_run("processing file, no changes in %a, not processed",name)
    end
    tobesaved[filename] = newchecksum
end

--

local done = { }

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 }
}