-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAbstractRascalMojo.java
More file actions
493 lines (402 loc) · 16.1 KB
/
AbstractRascalMojo.java
File metadata and controls
493 lines (402 loc) · 16.1 KB
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
* Copyright (c) 2009-2025, NWO-I Centrum Wiskunde & Informatica (CWI)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.rascalmpl.maven;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.compiler.util.scan.InclusionScanException;
import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner;
import org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping;
import java.lang.Process;
import static org.twdata.maven.mojoexecutor.MojoExecutor.executeMojo;
import static org.twdata.maven.mojoexecutor.MojoExecutor.plugin;
import static org.twdata.maven.mojoexecutor.MojoExecutor.goal;
import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration;
import static org.twdata.maven.mojoexecutor.MojoExecutor.executionEnvironment;
import static org.twdata.maven.mojoexecutor.MojoExecutor.element;
/**
* Abstract Maven Goal for Rascal tools. All tools (checker, tutor, compiler)
* are configured via PathConfig and their main function.
*
* All tools run in the JVM with the rascal dependency loaded.
*/
public abstract class AbstractRascalMojo extends AbstractMojo
{
@Parameter(defaultValue="${project}", readonly=true, required=true)
protected MavenProject project;
@Parameter(property="memory", defaultValue="2G", readonly=false, required=false)
protected String memory;
@Parameter(defaultValue = "${project.build.outputDirectory}", property = "bin", required = true )
protected File bin;
@Parameter(property = "srcs", required = true )
protected List<File> srcs;
@Parameter(property = "ignores", required = false )
protected List<File> ignores;
@Parameter(property = "libs", required = false )
protected List<File> libs;
@Parameter(property = "resources", required = false)
protected List<File> resources;
@Parameter(defaultValue="false", property= "verbose", required=true)
protected boolean verbose;
@Parameter(defaultValue = "${session}", required = true, readonly = true)
protected MavenSession session;
@Parameter(defaultValue = "0.42.3-SNAPSHOT", required = false, readonly = false)
protected String bootstrapRascalVersion;
@SuppressWarnings("deprecation") // Can't get @Parameter to work for the pluginManager.
@Component
protected BuildPluginManager pluginManager;
@Parameter(defaultValue="")
protected String mainModule;
/**
* Which `-Drascal.skipTag.skip` to use
*/
protected final String skipTag;
/**
* Class with `static int main(String[] arg)` method to call
*/
protected final String mainClass;
/**
* If there are more than the normal PathConfig parameters
* they can be added here.
*/
protected Map<String, String> extraParameters = new HashMap<>();
/**
* The Rascal runtime jar that will be used to load classes (including the main class) from.
* This is where bootstrap issues are resolved.
*/
protected Path cachedRascalRuntime = null;
public AbstractRascalMojo(String mainClass, String skipTag) {
this.mainClass = mainClass;
this.skipTag = skipTag;
}
protected Path getRascalRuntime() {
if (cachedRascalRuntime == null) {
cachedRascalRuntime = detectedDependentRascalArtifact(getLog(), project, session);
getLog().info("The Rascal runtime was resolved at " + cachedRascalRuntime);
}
return cachedRascalRuntime;
}
protected boolean isRascalProject() {
return project.getGroupId().equals("org.rascalmpl") && project.getArtifactId().equals("rascal");
}
/**
* Converts a list of files to a string;Of;Paths using the OS's native path separator
*/
protected String files(List<File> list) {
return list.stream()
.map(Object::toString)
.collect(Collectors.joining(File.pathSeparator));
}
protected void setExtraParameters() {
// do nothing yet
}
protected boolean isSkipped() {
return System.getProperty("rascal." + skipTag + ".skip") != null;
}
@Override
public void execute() throws MojoExecutionException {
try {
if (isSkipped()) {
getLog().info("Skipping " + getClass().getName() + " completely");
return;
}
getLog().debug("configuring paths");
for (File src : srcs) {
getLog().debug("\tregistered source location: " + src);
}
for (File ignore : ignores) {
getLog().debug("\tignoring sources in: " + ignore);
}
for (File resource : resources) {
getLog().debug("\tregistered resource: " + resource);
}
libs.addAll(collectDependentArtifactLibraries(project));
for (File lib : libs) {
getLog().debug("\tregistered library location: " + lib);
}
getLog().debug("Paths have been configured.");
setExtraParameters();
int exitVal = runMain(
verbose,
"",
srcs,
ignores,
libs,
resources,
bin,
extraParameters,
true)
.waitFor();
if (exitVal != 0) {
throw new MojoExecutionException(mainClass + " exited with error code " + exitVal);
}
return;
}
catch (InterruptedException e) {
throw new MojoExecutionException(mainClass + " process was killed.", e);
}
catch (MojoExecutionException e) {
throw e;
}
catch (Throwable e) {
throw new MojoExecutionException(mainClass + " process execution threw an unexpected exception.", e);
}
}
/**
* Finds the rascal.jar file that the pom.xml depends on.
* When the current project is rascal itself we resolve to a declared bootstrap
* dependency.
*/
protected Path detectedDependentRascalArtifact(Log log, MavenProject project, MavenSession session) {
try {
if (project.getGroupId().equals("org.rascalmpl") && project.getArtifactId().equals("rascal")) {
// we are in bootstrap mode and must find a previously released
// version to kick-off from
log.info("Maven Rascal Mojo detected rascal project self-application. Downloading the configured bootstrap rascal-" + bootstrapRascalVersion + ".jar");
log.info("Find <rascalBootstrapVersion>" + bootstrapRascalVersion + "</rascalBootstrapVersion> in rascal/pom.xml");
if (bootstrapRascalVersion.endsWith("SNAPSHOT")) {
throw new IllegalArgumentException("The rascalBootstrapVersion configuration parameter must not be a SNAPSHOT release, because of the required reproducibility of any Rascal release. The Maven build will bail out now.");
}
return installBootstrapRascalVersion(project, session);
}
for (Object o : project.getArtifacts()) {
Artifact a = (Artifact) o;
if (a.getGroupId().equals("org.rascalmpl") && a.getArtifactId().equals("rascal")) {
File file = a.getFile().getAbsoluteFile();
if (a.getSelectedVersion().compareTo(getReferenceRascalVersion()) >= 0) {
return file.toPath();
}
else {
log.warn("Rascal version in pom.xml dependency is too old for this Rascal maven plugin. " + getReferenceRascalVersion() + " or later expected.");
log.warn("Downloading and using a newer version org.rascalmpl:rascal:" + bootstrapRascalVersion + "; please add it to the pom.xml");
return installBootstrapRascalVersion(project, session);
}
}
}
throw new MojoExecutionException("Pom.xml is missing a dependency on org.rascalmpl:rascal:" + getReferenceRascalVersion() + " (or later).");
}
catch (OverConstrainedVersionException e) {
log.error("Rascal version is over-constrained (impossible to resolve). Expected " + getReferenceRascalVersion() + " or later. Have to abort.");
throw new RuntimeException(e);
}
catch (MojoExecutionException e) {
log.error("Unable to just-in-time-install the required (bootstrap) version of Rascal. Have to abort.");
throw new RuntimeException(e);
}
}
protected List<File> collectDependentArtifactLibraries(MavenProject project) throws URISyntaxException, IOException {
List<File> libs = new LinkedList<>();
for (Object o : project.getArtifacts()) {
Artifact a = (Artifact) o;
File file = a.getFile().getAbsoluteFile();
libs.add(file);
}
return libs;
}
protected final ArtifactVersion getReferenceRascalVersion() {
return new DefaultArtifactVersion("0.42.0");
}
protected Path installBootstrapRascalVersion(MavenProject project, MavenSession session) throws MojoExecutionException {
// download the boostrap version from our maven site
executeMojo(
plugin(
"org.apache.maven.plugins",
"maven-dependency-plugin",
"3.9.0"
),
goal("get"),
configuration(
element("artifact", "org.rascalmpl:rascal:" + bootstrapRascalVersion)
),
executionEnvironment(
project,
session,
pluginManager
)
);
// resolve the jar file in the .m2 repository
return Path.of(session.getSettings().getLocalRepository(),
"org", "rascalmpl", "rascal", bootstrapRascalVersion, "rascal-" + bootstrapRascalVersion + ".jar");
}
protected Process runMain(boolean verbose, String moreClasspath, List<File> srcs, List<File> ignores, List<File> libs, List<File> resources, File bin, Map<String, String> extraParameters, boolean inheritIO) throws IOException {
String javaHome = System.getProperty("java.home");
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
List<String> command = new LinkedList<>();
command.add(javaBin);
System.getProperties().forEach((key, value) -> {
// Do not propagate `user.dir`, since that breaks multi-module maven projects
if (!key.equals("user.dir")) {
command.add("-D" + key + "=" + value);
}
});
command.add("-Xmx" + memory);
// we put the entire pathConfig on the commandline, and finally the todoList for compilation.
command.add("-cp");
command.add(getRascalRuntime().toString() + (moreClasspath.isEmpty() ? "" : File.pathSeparator + moreClasspath));
assert mainClass != null : "mainClass is null";
command.add(mainClass);
if (mainClass.endsWith("RascalShell")) {
if (mainModule != null && !mainModule.isEmpty()) {
// the main module parameter is specific to the RascalShell command
// here for backward compatibility reasons for older client-owned shell scripts
command.add(mainModule);
}
}
if (!mainClass.endsWith("RascalShell")) {
if (!srcs.isEmpty()) {
command.add("-srcs");
command.add(files(srcs));
}
if (!resources.isEmpty()) {
command.add("-resources");
command.add(files(resources));
}
if (!ignores.isEmpty()) {
command.add("-ignores");
command.add(files(ignores));
}
if (!libs.isEmpty()) {
command.add("-libs");
command.add(files(libs));
}
command.add("-bin");
assert bin != null : "bin is null";
command.add(bin.toString());
for (Entry<String, String> e : extraParameters.entrySet()) {
command.add("-" + e.getKey());
assert e.getValue() != null : "value with " + e.getKey() + " is null in extraParameters";
command.add(e.getValue());
}
if (verbose) {
command.add("-verbose");
}
}
assert command.stream().map(Objects::nonNull).allMatch(b -> b) : "command had a null parameter";
getLog().debug("Java exec: " + command.get(0));
getLog().debug("Starting process:\n\t java " +
command.stream()
.skip(1)
.map(s -> s.replace("\n", "\\\\n"))
.map(s -> "'" + s + "'")
.collect(Collectors.joining(" ")));
ProcessBuilder p = new ProcessBuilder(command);
p.directory(project.getBasedir());
if (inheritIO) {
// everything merges with the current process' streams
p.inheritIO();
}
else {
// stderr goes to stdout as well
p.redirectErrorStream(true);
}
Process runningProcess = p.start();
// try to clean up the forked process as nicely as possible if we get killed prematurely ourselves
if (runningProcess != null) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (runningProcess.isAlive()) {
runningProcess.destroy();
}
}));
}
return runningProcess;
}
protected List<File> allRascalSourceFiles(List<File> sourceLocs, List<File> ignoredLocs) {
var result = new LinkedList<File>();
allRascalSourceFiles(sourceLocs.stream().toArray(File[]::new), ignoredLocs, result);
return result;
}
private void allRascalSourceFiles(File[] sourceLocs, List<File> ignoredLocs, List<File> result) {
for (File f : sourceLocs) {
if (!ignoredLocs.contains(f)) {
if (f.getName().endsWith(".rsc")) {
result.add(f);
}
else if (f.isDirectory()) {
allRascalSourceFiles(f.listFiles(), ignoredLocs, result);
}
}
}
}
protected List<File> getTodoList(File binLoc, List<File> srcLocs, List<File> ignoredLocs, String dirtyExtension, String binaryExtension, String binaryPrefix) throws InclusionScanException, URISyntaxException {
StaleSourceScanner scanner = new StaleSourceScanner(100);
scanner.addSourceMapping(new SourceMapping() {
@Override
public Set<File> getTargetFiles(File targetDir, String source) throws InclusionScanException {
File file = new File(source);
String name = file.getName();
if (name.endsWith("." + dirtyExtension)) {
return Set.of(
new File(targetDir, new File(file.getParentFile(), "$" + name.substring(0, name.length() - ("." + dirtyExtension).length()) + "." + binaryExtension).getPath())
);
}
else {
return Set.of();
}
}
});
binLoc = new File(binLoc, binaryPrefix);
Set<File> staleSources = new HashSet<>();
for (File src : srcs) {
staleSources.addAll(scanner.getIncludedSources(src, binLoc));
}
List<File> filteredStaleSources = new LinkedList<>();
for (File file : staleSources) {
if (ignoredLocs.stream().noneMatch(l -> isIgnoredBy(l, file))) {
filteredStaleSources.add(file);
}
}
return filteredStaleSources;
}
protected boolean isIgnoredBy(File prefix, File loc) {
String prefixPath = prefix.toString();
String locPath = loc.toString();
return locPath.startsWith(prefixPath);
}
@FunctionalInterface
protected interface FunctionWithException<T, R, E extends Exception> {
R apply(T t) throws E;
}
protected <T, R, E extends Exception> Function<T, R> handleExceptions(FunctionWithException<T, R, E> fe) {
return arg -> {
try {
return fe.apply(arg);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}
}