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
20
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,
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
46
47
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
58 }
59}
60
61
62
63
64
65
66
67
68
69
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
115
116
117 local r = action.runner
118 action.runner = nil
119 runner(action)
120 action.runner = r
121 elseif kind == "string" then
122
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
136 end
137end
138
139
140
141local done = { }
142
143local function analyzed(name,options)
144 local usedname = addsuffix(name,inputsuffix)
145 local resultname = replacesuffix(name,resultsuffix)
146 local pathname = file.pathpart(usedname)
147 local runpath = environment.arguments.path
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,
159 filename = usedname,
160 result = resultname,
161 }
162end
163
164function jobfiles.context(name,options)
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 |