-
Notifications
You must be signed in to change notification settings - Fork 125
/
TranspileMojo.java
312 lines (296 loc) · 11.4 KB
/
TranspileMojo.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
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven;
import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.StClasspath;
import com.yegor256.xsline.TrClasspath;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.TrJoined;
import com.yegor256.xsline.Train;
import com.yegor256.xsline.Xsline;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.cactoos.experimental.Threads;
import org.cactoos.iterable.Mapped;
import org.cactoos.number.SumOf;
import org.eolang.maven.tojos.AttributeNotFoundException;
import org.eolang.maven.tojos.ForeignTojo;
import org.eolang.maven.util.HmBase;
import org.eolang.maven.util.Rel;
/**
* Compile.
*
* @since 0.1
*/
@Mojo(
name = "transpile",
defaultPhase = LifecyclePhase.PROCESS_SOURCES,
threadSafe = true,
requiresDependencyResolution = ResolutionScope.COMPILE
)
@SuppressWarnings("PMD.LongVariable")
public final class TranspileMojo extends SafeMojo {
/**
* The directory where to put pre-transpile files.
*/
public static final String PRE = "7-pre";
/**
* The directory where to transpile to.
*/
public static final String DIR = "8-transpile";
/**
* Extension for compiled sources in XMIR format (XML).
*/
static final String EXT = "xmir";
/**
* Parsing train with XSLs.
*/
static final Train<Shift> TRAIN = new TrJoined<>(
new TrClasspath<>(
"/org/eolang/maven/pre/classes.xsl",
"/org/eolang/maven/pre/package.xsl",
"/org/eolang/maven/pre/tests.xsl",
"/org/eolang/maven/pre/rename-tests-inners.xsl",
"/org/eolang/maven/pre/align-test-classes.xsl",
"/org/eolang/maven/pre/remove-high-level-inner-classes.xsl",
"/org/eolang/maven/pre/attrs.xsl",
"/org/eolang/maven/pre/data.xsl"
).back(),
new TrDefault<>(
new StClasspath(
"/org/eolang/maven/pre/to-java.xsl",
String.format("disclaimer %s", new Disclaimer())
)
)
);
/**
* Java extension.
*/
private static final Pattern JAVA_EXT = Pattern.compile(".java", Pattern.LITERAL);
/**
* Target directory.
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(
property = "eo.generatedDir",
required = true,
defaultValue = "${project.build.directory}/generated-sources"
)
private File generatedDir;
/**
* Output.
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(
property = "eo.outputDir",
required = true,
defaultValue = "${project.build.outputDirectory}"
)
private File outputDir;
/**
* Add to source root.
*
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(property = "eo.addSourcesRoot")
@SuppressWarnings("PMD.ImmutableField")
private boolean addSourcesRoot = true;
/**
* Add to test source root.
*
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(property = "eo.addTestSourcesRoot")
private boolean addTestSourcesRoot;
@Override
public void exec() {
final Collection<ForeignTojo> sources = this.scopedTojos().withOptimized();
final long saved = new SumOf(
new Threads<>(
Runtime.getRuntime().availableProcessors(),
new Mapped<>(tojo -> () -> this.transpile(tojo), sources)
)
).longValue();
Logger.info(
this, "Transpiled %d XMIRs, created %d Java files in %s",
sources.size(), saved, new Rel(this.generatedDir)
);
if (this.addSourcesRoot) {
this.project.addCompileSourceRoot(this.generatedDir.getAbsolutePath());
Logger.info(
this, "The directory added to Maven 'compile-source-root': %s",
new Rel(this.generatedDir)
);
}
if (this.addTestSourcesRoot) {
this.project.addTestCompileSourceRoot(this.generatedDir.getAbsolutePath());
Logger.info(
this, "The directory added to Maven 'test-compile-source-root': %s",
new Rel(this.generatedDir)
);
}
}
/**
* Transpile.
* @param tojo Tojo that should be transpiled.
* @return Number of transpiled files.
* @throws java.io.IOException If any issues with I/O
*/
private int transpile(final ForeignTojo tojo) throws IOException {
final Path file;
try {
file = tojo.verified();
} catch (final AttributeNotFoundException exception) {
throw new IllegalStateException(
"You should check that 'Verify' goal of the plugin was run first",
exception
);
}
final XML input = new XMLDocument(file);
final String name = input.xpath("/program/@name").get(0);
final Place place = new Place(name);
final Path target = place.make(
this.targetDir.toPath().resolve(TranspileMojo.DIR),
TranspileMojo.EXT
);
final int saved;
if (
target.toFile().exists()
&& target.toFile().lastModified() >= file.toFile().lastModified()
&& target.toFile().lastModified() >= tojo.source().toFile().lastModified()
) {
Logger.info(
this, "XMIR %s (%s) were already transpiled to %s",
new Rel(file), name, new Rel(target)
);
saved = 0;
} else {
final List<Path> paths = this.transpile(input, target);
paths.forEach(p -> this.transpiledTojos.add(p, file));
saved = paths.size();
}
return saved;
}
/**
* Transpile.
* @param input The .xmir file
* @param target The path to transpiled .xmir file
* @return List of Paths to generated java file
* @throws java.io.IOException If any issues with I/O
*/
private List<Path> transpile(
final XML input,
final Path target
) throws IOException {
final String name = input.xpath("/program/@name").get(0);
final Place place = new Place(name);
final Train<Shift> trn = new SpyTrain(
TranspileMojo.TRAIN,
place.make(this.targetDir.toPath().resolve(TranspileMojo.PRE), "")
);
final Path dir = this.targetDir.toPath().resolve(TranspileMojo.DIR);
new HmBase(dir).save(new Xsline(trn).pass(input).toString(), dir.relativize(target));
final List<Path> javas = new JavaFiles(target, this.generatedDir.toPath()).save();
this.cleanUpClasses(javas);
return javas;
}
/**
* Clean up dirty classes.
* The method is trying to fix problem produced by dirty libraries:
* <a href="https://github.com/objectionary/eo-strings/issues/147"> eo-strings example </a>
* Some libraries by mistake can put ALL their compiled classes right into the final library
* jar, instead of only adding atoms. This can cause different runtime errors since the
* classpath will contain two classes with the same name:
* - The first class file will be added from dirty library
* - The second class with the same name will be compiled from the transpiled java file
* In order to prevent this, we remove all classes that have the java analog in the
* generated sources. In other words, if generated-sources (or generated-test-sources) folder
* has java classes, we expect that they will be only compiled from that folder.
* _____
* Synchronization in this method is necessary to prevent
* {@link java.nio.file.AccessDeniedException} on the Windows OS.
* You can read more about the original problem in the following issue:
* - <a href="https://github.com/objectionary/eo/issues/2370">issue link</a>
* In other words, concurrent file deletions on the Windows OS can lead to an
* {@link java.nio.file.AccessDeniedException}, which could crash the build.
* _____
* @param java The list of java files.
* @todo #2375:90min. Add concurrency tests for the TranspileMojo.cleanUpClasses method.
* We should be sure that the method works correctly in a concurrent environment.
* In order to do so we should add a test that will run the cleanUpClasses method in
* multiple threads and check that the method works correctly without exceptions.
* We can apply the same approach as mentioned in that post:
* <a href="https://www.yegor256.com/2018/03/27/how-to-test-thread-safety.html">Post</a>
*/
private void cleanUpClasses(final Collection<? extends Path> java) {
final Set<Path> unexpected = java.stream()
.map(path -> this.generatedDir.toPath().relativize(path))
.map(TranspileMojo::classExtension)
.map(binary -> this.outputDir.toPath().resolve(binary))
.collect(Collectors.toSet());
for (final Path binary : unexpected) {
try {
synchronized (TranspileMojo.class) {
Files.deleteIfExists(binary);
}
} catch (final IOException cause) {
throw new IllegalStateException(
String.format("Can't delete file %s", binary),
cause
);
}
}
}
/**
* Rename java to class.
* @param java The java file
* @return The class file with the same (java) content.
*/
private static Path classExtension(final Path java) {
final Path result;
final String filename = TranspileMojo.JAVA_EXT.matcher(java.getFileName().toString())
.replaceAll(".class");
if (java.getParent() == null) {
result = Paths.get(filename);
} else {
result = java.getParent().resolve(filename);
}
return result;
}
}