From 984c6e84bab2db94113557527d630625c94a1ebd Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Tue, 31 Dec 2024 01:42:49 +0300 Subject: [PATCH] bug(#3481): thread --- .../main/java/org/eolang/maven/SafeMojo.java | 10 ++++ .../main/java/org/eolang/maven/ShakeMojo.java | 48 +++++++-------- .../java/org/eolang/maven/TranspileMojo.java | 60 +++++++++++-------- .../test/resources/org/eolang/maven/main.xml | 8 +-- 4 files changed, 71 insertions(+), 55 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/SafeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/SafeMojo.java index 97bdca9f5b..b368a93ace 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/SafeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/SafeMojo.java @@ -196,6 +196,16 @@ abstract class SafeMojo extends AbstractMojo { @Parameter(property = "eo.transpiledFormat", required = true, defaultValue = "csv") protected String transpiledFormat = "csv"; + /** + * Track optimization steps into intermediate XMIR files? + * + * @since 0.24.0 + * @checkstyle MemberNameCheck (7 lines) + */ + @SuppressWarnings("PMD.LongVariable") + @Parameter(property = "eo.trackTransformationSteps", required = true, defaultValue = "false") + protected boolean trackTransformationSteps; + /** * If set to TRUE, the exception on exit will be printed in details * to the log. diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java index 8a67639e80..37d87538ae 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java @@ -31,9 +31,9 @@ import com.yegor256.xsline.Xsline; import java.nio.file.Path; import java.util.Collection; +import java.util.function.Function; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; import org.cactoos.func.StickyFunc; import org.cactoos.iterable.Filtered; import org.eolang.maven.footprint.FpDefault; @@ -56,7 +56,7 @@ public final class ShakeMojo extends SafeMojo { /** * The directory where to shake to. */ - public static final String DIR = "2-shake"; + static final String DIR = "2-shake"; /** * Subdirectory for shaken cache. @@ -66,29 +66,19 @@ public final class ShakeMojo extends SafeMojo { /** * The directory where to place intermediary files. */ - static final String STEPS = "2-shake-steps"; - - /** - * Track optimization steps into intermediate XMIR files? - * - * @since 0.24.0 - * @checkstyle MemberNameCheck (7 lines) - */ - @SuppressWarnings("PMD.LongVariable") - @Parameter(property = "eo.trackTransformationSteps", required = true, defaultValue = "false") - private boolean trackTransformationSteps; + private static final String STEPS = "2-shake-steps"; @Override public void exec() { final long start = System.currentTimeMillis(); final Collection tojos = this.scopedTojos().withXmir(); - final Xsline xsline = this.transformations(); + final Function transform = this.transformations(); final int total = new Threaded<>( new Filtered<>( ForeignTojo::notShaken, tojos ), - tojo -> this.shaken(tojo, xsline) + tojo -> this.shaken(tojo, transform) ).total(); if (total > 0) { Logger.info( @@ -105,11 +95,11 @@ public void exec() { /** * XMIR shaken to another XMIR. * @param tojo Foreign tojo - * @param xsline Transformations to apply to XMIR + * @param transform Transformations to apply to XMIR * @return Amount of optimized XMIR files * @throws Exception If fails */ - private int shaken(final ForeignTojo tojo, final Xsline xsline) + private int shaken(final ForeignTojo tojo, final Function transform) throws Exception { final Path source = tojo.xmir(); final XML xmir = new XMLDocument(source); @@ -118,7 +108,7 @@ private int shaken(final ForeignTojo tojo, final Xsline xsline) final Path target = new Place(name).make(base, AssembleMojo.XMIR); tojo.withShaken( new FpDefault( - src -> xsline.pass(xmir).toString(), + src -> transform.apply(xmir).toString(), this.cache.toPath().resolve(ShakeMojo.CACHE), this.plugin.getVersion(), new TojoHash(tojo), @@ -130,21 +120,25 @@ private int shaken(final ForeignTojo tojo, final Xsline xsline) /** * Shake XSL transformations. + * If {@link SafeMojo#trackTransformationSteps} is {@code true} - we create new {@link Xsline} + * for every XMIR in purpose of thread safety. * @return Shake XSL transformations for all tojos. */ - private Xsline transformations() { + private Function transformations() { final Train measured = this.measured(new TrShaking()); - final Train train; + final Function func; if (this.trackTransformationSteps) { - train = new TrSpy( - measured, - new StickyFunc<>( - new ProgramPlace(this.targetDir.toPath().resolve(ShakeMojo.STEPS)) + func = xml -> new Xsline( + new TrSpy( + measured, + new StickyFunc<>( + new ProgramPlace(this.targetDir.toPath().resolve(ShakeMojo.STEPS)) + ) ) - ); + ).pass(xml); } else { - train = measured; + func = new Xsline(measured)::pass; } - return new Xsline(train); + return func; } } diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java index aeb38059f1..b7615299b2 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java @@ -42,6 +42,7 @@ import java.util.Collection; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; import java.util.function.Supplier; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; @@ -78,26 +79,25 @@ ) @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"; + static final String DIR = "8-transpile"; /** * Cache directory for transpiled sources. */ - public static final String CACHE = "transpiled"; + private static final String CACHE = "transpiled"; /** * Java extension. */ - public static final String JAVA = "java"; + private static final String JAVA = "java"; + + /** + * The directory where to put pre-transpile files. + */ + private static final String PRE = "7-pre"; /** * Parsing train with XSLs. @@ -165,10 +165,10 @@ public final class TranspileMojo extends SafeMojo { @Override public void exec() { final Collection sources = this.scopedTojos().withShaken(); - final Xsline xsline = this.xsline(); + final Function transform = this.transpilation(); final int saved = new Threaded<>( sources, - tojo -> this.transpiled(tojo, xsline) + tojo -> this.transpiled(tojo, transform) ).total(); Logger.info( this, "Transpiled %d XMIRs, created %d Java files in %[file]s", @@ -193,11 +193,14 @@ public void exec() { /** * Transpile. * @param tojo Tojo that should be transpiled. - * @param xsline Optimization that transpiles + * @param transform Optimization that transpiles * @return Number of transpiled files. * @throws java.io.IOException If any issues with I/O */ - private int transpiled(final ForeignTojo tojo, final Xsline xsline) throws IOException { + private int transpiled( + final ForeignTojo tojo, + final Function transform + ) throws IOException { final Path source = tojo.shaken(); final XML xmir = new XMLDocument(source); final Path base = this.targetDir.toPath().resolve(TranspileMojo.DIR); @@ -209,7 +212,7 @@ private int transpiled(final ForeignTojo tojo, final Xsline xsline) throws IOExc new FpDefault( src -> { rewrite.set(true); - return xsline.pass(xmir).toString(); + return transform.apply(xmir).toString(); }, this.cache.toPath().resolve(TranspileMojo.CACHE), this.plugin.getVersion(), @@ -220,18 +223,27 @@ private int transpiled(final ForeignTojo tojo, final Xsline xsline) throws IOExc } /** - * Transpile optimization. - * @return Optimization that transpiles + * Transpile XSL transformations. + * If {@link SafeMojo#trackTransformationSteps} is {@code true} - we create new {@link Xsline} + * for every XMIR in purpose of thread safety. + * @return XSL transformations that transpiles XMIR to Java. */ - private Xsline xsline() { - return new Xsline( - new TrSpy( - this.measured(TranspileMojo.TRAIN), - new StickyFunc<>( - new ProgramPlace(this.targetDir.toPath().resolve(TranspileMojo.PRE)) + private Function transpilation() { + final Train measured = this.measured(TranspileMojo.TRAIN); + final Function func; + if (this.trackTransformationSteps) { + func = xml -> new Xsline( + new TrSpy( + measured, + new StickyFunc<>( + new ProgramPlace(this.targetDir.toPath().resolve(TranspileMojo.PRE)) + ) ) - ) - ); + ).pass(xml); + } else { + func = new Xsline(measured)::pass; + } + return func; } /** diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/main.xml b/eo-maven-plugin/src/test/resources/org/eolang/maven/main.xml index b69f6b3b8d..63fbdbfa08 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/main.xml +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/main.xml @@ -24,13 +24,13 @@ SOFTWARE. --> - - + + - Dummy - + Dummy +