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: --path=%path% ?% %?runpath: --runpath=%runpath% ?% %filename%]],
53 checkers = {
54 options = "string",
55 filename = "readable",
56 path = "string",
57 runpath = "string",
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 path = action.path
77if not isfile(filename) and path and path ~= "" then
78 filename = file.join(path,filename)
79end
80 local oldchecksum = collected[filename]
81 local newchecksum = checksum(filename)
82
83 local tobedone = false
84 local forcerun = action.forcerun or jobfiles.forcerun
85 if not result then
86 result = replacesuffix(filename,resultsuffix)
87 action.result = result
88 end
89 if forcerun then
90 tobedone = true
91 if trace_run then
92 report_run("processing file, changes in %a, %s",filename,"processing forced")
93 end
94 end
95 if not tobedone and not oldchecksum then
96 tobedone = true
97 if trace_run then
98 report_run("processing file, changes in %a, %s",filename,"no checksum yet")
99 end
100 end
101 if not tobedone and oldchecksum ~= newchecksum then
102 tobedone = true
103 if trace_run then
104 report_run("processing file, changes in %a, %s",filename,"checksum mismatch")
105 end
106 end
107 if not tobedone and not isfile(result) then
108 tobedone = true
109 if trace_run then
110 report_run("processing file, changes in %a, %s",filename,"no result file")
111 end
112 end
113 if tobedone then
114 local kind = type(runner)
115 if kind == "function" then
116 if trace_run then
117 report_run("processing file, command: %s",action.name or "unknown")
118 end
119
120
121
122 local r = action.runner
123 action.runner = nil
124 runner(action)
125 action.runner = r
126 elseif kind == "string" then
127
128 if trace_run then
129 report_run("processing file, command: %s",runner)
130 end
131 os.execute(runner)
132 else
133 report_run("processing file, changes in %a, %s",filename,"no valid runner")
134 end
135 elseif trace_run then
136 report_run("processing file, no changes in %a, %s",filename,"not processed")
137 end
138 tobesaved[filename] = newchecksum
139 else
140
141 end
142end
143
144
145
146local done = { }
147
148local function analyzed(name,options)
149 local usedname = addsuffix(name,inputsuffix)
150 local resultname = replacesuffix(name,resultsuffix)
151 local pathname = file.pathpart(usedname)
152 local path = environment.arguments.path
153 local runpath = environment.arguments.runpath
154 local resultname = replacesuffix(name,resultsuffix)
155 if runpath and runpath ~= "" then
156
157 resultname = file.join(runpath,file.basename(resultname))
158 end
159 if path ~= "" then
160 if path then
161 path = file.join(path,pathname)
162 else
163 path = pathname
164 end
165 usedname = file.basename(usedname)
166 end
167 return {
168 options = options,
169 path = path,
170 filename = usedname,
171 result = resultname,
172 runpath = runpath,
173 }
174end
175
176function jobfiles.context(name,options)
177 if type(name) == "table" then
178 local result = { }
179 for i=1,#name do
180 result[#result+1] = jobfiles.context(name[i],options)
181 end
182 return result
183 elseif name ~= "" then
184 local action = analyzed(name,options)
185 local result = action.result
186 if not done[result] then
187 jobfiles.run(action)
188 done[result] = true
189 end
190 return result
191 else
192 return { }
193 end
194end
195
196interfaces.implement {
197 name = "runcontextjob",
198 arguments = "2 strings",
199 actions = { jobfiles.context, context }
200}
201 |