forked from bndtools/bnd
/
ProjectGenerate.java
397 lines (325 loc) · 10.8 KB
/
ProjectGenerate.java
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package aQute.bnd.build;
import static aQute.bnd.result.Result.err;
import static aQute.bnd.result.Result.ok;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import org.osgi.framework.VersionRange;
import aQute.bnd.exceptions.Exceptions;
import aQute.bnd.help.instructions.ProjectInstructions.GeneratorSpec;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Processor;
import aQute.bnd.result.Result;
import aQute.bnd.service.generate.BuildContext;
import aQute.bnd.service.generate.Generator;
import aQute.lib.fileset.FileSet;
import aQute.lib.io.IO;
import aQute.lib.redirect.Redirect;
import aQute.lib.specinterface.SpecInterface;
import aQute.lib.strings.Strings;
import aQute.service.reporter.Reporter.SetLocation;
public class ProjectGenerate implements AutoCloseable {
final Project project;
ProjectGenerate(Project project) {
this.project = project;
}
public Result<Set<File>> generate(boolean force) {
project.clear();
if (force)
clean();
Set<File> files = new TreeSet<>();
// First prepare, otherwise we might delete overlapping directories
for (Entry<String, GeneratorSpec> e : project.instructions.generate()
.entrySet()) {
Result<Void> result = prepare(e.getKey(), e.getValue());
if (!result.isOk())
return result.asError();
}
for (Entry<String, GeneratorSpec> e : project.instructions.generate()
.entrySet()) {
Result<Set<File>> step = step(e.getKey(), e.getValue());
if (step.isErr()) {
String qsrc = e.getKey();
SetLocation error = project.error("%s : %s;%s", step.error()
.get(), e.getKey(),
e.getValue()
._attrs());
error.context(qsrc);
error.header(Constants.GENERATE);
return step.asError();
}
files.addAll(step.unwrap());
}
return Result.ok(files);
}
private Result<Void> prepare(String sourceWithDuplicate, GeneratorSpec st) {
String source = Strings.trim(Processor.removeDuplicateMarker(sourceWithDuplicate));
if (source.isEmpty() || source.equals(Constants.EMPTY_HEADER))
return Result.ok(null);
String output = st.output();
if (output == null)
return err("No mandatory 'output' files/directories specified");
if (!output.endsWith("/"))
output += "/";
Set<File> sourceFiles = new FileSet(project.getBase(), source).getFiles();
if (sourceFiles.isEmpty())
return err("No source files/directories specified");
File out = project.getFile(output);
if (out.isDirectory()) {
if (st.clear()
.orElseGet(() -> Boolean.TRUE)) {
for (File f : out.listFiles()) {
IO.delete(f);
}
}
} else {
out.mkdirs();
}
long latestModifiedSource = sourceFiles.stream()
.mapToLong(File::lastModified)
.max()
.getAsLong();
// the out folder serves as our trigger if a build is needed. Thus we
// use to output directory timestamp as marker we can compare against
// later. If clear is false a generator might decided to do nothing and
// could cause a build loop.
out.setLastModified(latestModifiedSource);
return Result.ok(null);
}
private Result<Set<File>> step(String sourceWithDuplicate, GeneratorSpec st) {
try {
String source = Strings.trim(Processor.removeDuplicateMarker(sourceWithDuplicate));
if (source.isEmpty() || source.equals(Constants.EMPTY_HEADER))
return Result.ok(Collections.emptySet());
if (st.system()
.isPresent())
project.system(st.system()
.get(), null);
if (st.generate()
.isPresent()) {
boolean ignoreErrors = false;
String plugin = st.generate()
.get();
if (plugin.startsWith("-")) {
ignoreErrors = true;
plugin = plugin.substring(1);
}
String result = doGenerate(plugin, st);
if (result != null)
return err(ignoreErrors ? "-" + result : result);
}
Set<File> affected = new FileSet(project.getBase(), st.output()).getFiles();
if (affected.isEmpty()) {
return err(
"ran command or generate but no files were changed; Is `output` correct wrt to the command?");
}
return ok(affected);
} catch (Exception e) {
return err(Exceptions.causes(e));
}
}
private String doGenerate(String commandline, GeneratorSpec st) {
try {
String result = null;
List<String> blocks = Strings.splitQuoted(commandline, ";");
for (String block : blocks) {
if (block.isEmpty())
continue;
List<String> arguments = Strings.splitQuoted(block, " \t");
if (arguments.isEmpty()) {
return block + " : no command name";
}
File fstdin = null;
File fstdout = null;
File fstderr = null;
String pluginName = arguments.remove(0);
for (Iterator<String> it = arguments.iterator(); it.hasNext();) {
String arg = it.next();
if (arg.startsWith("<")) {
if (fstdin != null)
return "only 1 redirection of stdin supported";
fstdin = getFile(arg.substring(1), false);
if (fstdin == null || !fstdin.isFile())
return "cannot find redirected input file " + fstdin;
it.remove();
} else if (arg.startsWith(">")) {
if (fstdout != null)
return "only 1 redirection of stdout supported";
fstdout = getFile(arg.substring(1), true);
it.remove();
} else if (arg.startsWith("1>")) {
if (fstdout != null)
return "only 1 redirection of stdout supported";
fstdout = getFile(arg.substring(2), true);
it.remove();
} else if (arg.startsWith("2>")) {
if (fstderr != null)
return "only 1 redirection of stderr supported";
fstderr = getFile(arg.substring(2), true);
it.remove();
}
}
InputStream stdin = fstdin == null ? null : IO.stream(fstdin);
OutputStream stdout = fstdout == null ? null : IO.outputStream(fstdout);
OutputStream stderr = fstderr == null ? null : IO.outputStream(fstderr);
try {
if (pluginName.indexOf('.') >= 0) {
if (pluginName.startsWith("."))
pluginName = pluginName.substring(1);
VersionRange range = st.version()
.map(VersionRange::valueOf)
.orElse(null);
result = doGenerateMain(pluginName, range, st._attrs(), arguments, stdin, stdout, stderr);
} else {
result = doGeneratePlugin(pluginName, st._attrs(), arguments, stdin, stdout, stderr);
}
if (result != null)
return block + " : " + result;
} finally {
IO.closeAll(stdout, stderr, stdin);
}
}
return result;
} catch (Exception e) {
return Exceptions.causes(e);
}
}
private File getFile(String path, boolean mkdirs) {
File f = project.getFile(path);
if (mkdirs)
f.getParentFile()
.mkdirs();
return f;
}
private String doGeneratePlugin(String pluginName, Map<String, String> attrs, List<String> arguments,
InputStream stdin, OutputStream stdout, OutputStream stderr) {
BuildContext bc = new BuildContext(project, attrs, arguments, stdin, stdout, stderr);
Result<Boolean> call = project.getWorkspace()
.getExternalPlugins()
.call(pluginName, Generator.class, p -> {
Class<?> type = SpecInterface.getParameterizedInterfaceType(p.getClass(), Generator.class);
if (type == null) {
return err("Cannot find the options type in %s", p.getClass());
}
SpecInterface<?> spec = SpecInterface.getOptions(type, arguments, bc.getBase());
if (spec.isFailure()) {
return err(spec.failure());
}
Redirect redirect = new Redirect(stdin, stdout, stderr).captureStdout()
.captureStderr();
@SuppressWarnings("unchecked")
Optional<String> error = redirect.apply(() -> p.generate(bc, spec.instance()));
project.getInfo(bc, pluginName);
if (error.isPresent())
return err(error.get() + " : " + redirect.getContent());
if (!bc.isOk()) {
return err("errors");
}
return ok(true);
});
return call.error()
.orElse(null);
}
private String doGenerateMain(String mainClass, VersionRange range, Map<String, String> attrs,
List<String> arguments, InputStream stdin, OutputStream stdout, OutputStream stderr) {
Result<Integer> call = project.getWorkspace()
.getExternalPlugins()
.call(mainClass, range, project, attrs, arguments, stdin, stdout, stderr);
if (call.isErr())
return call.error()
.get();
if (call.unwrap() != 0) {
return "process returned with non-zero exit code: " + call.unwrap();
}
return null;
}
public Result<Set<File>> getInputs() {
Set<String> inputs = project.instructions.generate()
.keySet();
if (inputs.isEmpty())
return Result.ok(Collections.emptySet());
Set<File> files = new HashSet<>();
StringBuilder errors = new StringBuilder();
for (String input : inputs) {
Set<File> inputFiles = new FileSet(project.getBase(), input).getFiles();
if (inputFiles.isEmpty()) {
project.error("-generate: no content for %s", input);
errors.append(input)
.append(" has no matching files\n");
} else {
files.addAll(inputFiles);
}
}
if (errors.length() == 0)
return Result.ok(files);
else
return Result.err(errors);
}
public Set<File> getOutputDirs() {
return getOutputDirs(false);
}
private Set<File> getOutputDirs(boolean toClear) {
return project.instructions.generate()
.values()
.stream()
.filter(spec -> !toClear || spec.clear()
.orElse(Boolean.TRUE))
.map(GeneratorSpec::output)
.filter(Objects::nonNull)
.map(project::getFile)
.collect(Collectors.toSet());
}
public boolean needsBuild() {
for (Entry<String, GeneratorSpec> e : project.instructions.generate()
.entrySet()) {
GeneratorSpec spec = e.getValue();
String source = Strings.trim(Processor.removeDuplicateMarker(e.getKey()));
if (source.isEmpty() || source.equals(Constants.EMPTY_HEADER))
continue;
String output = spec.output();
if (output == null)
return true; // error handling
if (!output.endsWith("/"))
output += "/";
Set<File> sourceFiles = new FileSet(project.getBase(), source).getFiles();
if (sourceFiles.isEmpty())
return true; // error handling
sourceFiles.addAll(project.getSelfAndAncestors());
long latestModifiedSource = sourceFiles.stream()
.mapToLong(File::lastModified)
.max()
.getAsLong();
Set<File> outputFiles = new FileSet(project.getBase(), output).getFiles();
if (outputFiles.isEmpty())
return true;
File out = project.getFile(output);
long latestModifiedTarget = out.lastModified();
boolean staleFiles = latestModifiedSource > latestModifiedTarget;
if (staleFiles)
return true;
}
return false;
}
public void clean() {
for (File output : getOutputDirs(true))
try {
project.clean(output, "generate output " + output, false);
} catch (IOException e) {
Exceptions.duck(e);
}
}
@Override
public void close() {}
}