From 4566cc8e943ce607ce8d54d7392aaf464f7e95aa Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Wed, 10 Jan 2024 19:15:55 +0300 Subject: [PATCH 1/6] #2648: Added Buildable java --- .../java/org/eolang/maven/rust/Buildable.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 eo-maven-plugin/src/main/java/org/eolang/maven/rust/Buildable.java diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/rust/Buildable.java b/eo-maven-plugin/src/main/java/org/eolang/maven/rust/Buildable.java new file mode 100644 index 0000000000..319cb13581 --- /dev/null +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/rust/Buildable.java @@ -0,0 +1,39 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2023 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.rust; + +import java.nio.file.Path; + +/** + * Such {@link FFINode} that should build (compiled). + * @since 0.35 + */ +public interface Buildable extends FFINode { + + /** + * Build it. + * @param cache Where to save for caching. + */ + void build(Path cache); +} From dd6e9ccf7747b1f2488237b797ae586fd2079d23 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Wed, 10 Jan 2024 19:17:19 +0300 Subject: [PATCH 2/6] #2648: Transferred functionality from here from BinarizeMojo.java --- .../java/org/eolang/maven/BinarizeMojo.java | 191 +---------------- .../java/org/eolang/maven/rust/RustNode.java | 195 +++++++++++++++++- .../org/eolang/maven/BinarizeMojoTest.java | 3 +- 3 files changed, 197 insertions(+), 192 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java index 474b1be90c..438f437632 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java @@ -24,24 +24,16 @@ package org.eolang.maven; import com.jcabi.log.Logger; -import com.yegor256.Jaxec; import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang3.SystemUtils; +import java.util.Collection; 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.Filtered; -import org.cactoos.iterable.Mapped; -import org.cactoos.number.SumOf; -import org.cactoos.text.TextOf; -import org.cactoos.text.UncheckedText; -import org.eolang.maven.rust.BuildFailureException; +import org.eolang.maven.rust.Buildable; import org.eolang.maven.rust.Names; /** @@ -49,9 +41,6 @@ * * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) * @since 0.1 - * @todo #2118:90min Make it more generic. Perhaps by compilation of - * RustNode instead of going and building throw directories. Maybe it - * is better to get from BinarizeParseMojo. */ @Mojo( name = "binarize", @@ -62,11 +51,6 @@ @SuppressWarnings("PMD.LongVariable") public final class BinarizeMojo extends SafeMojo { - /** - * Name of executable file which is result of cargo building. - */ - public static final String LIB = BinarizeMojo.common(); - /** * The directory where to binarize to. */ @@ -109,169 +93,14 @@ public final class BinarizeMojo extends SafeMojo { @Override public void exec() throws IOException { - new Moja<>(BinarizeParseMojo.class).with( - "names", new Names(this.namesDir.toPath()) - ).copy(this).execute(); - final int total = new SumOf( - new Threads<>( - Runtime.getRuntime().availableProcessors(), - new Mapped<>( - project -> () -> { - this.build(project); - return 1; - }, - new Filtered<>( - BinarizeMojo::valid, - targetDir.toPath().resolve("Lib").toFile().listFiles() - ) - ) - ) - ).intValue(); - Logger.info(this, "Built in total %d cargo projects", total); - } - - /** - * Calculates name for Rust shared library depending on OS. - * @return Name. - */ - private static String common() { - final String result; - if (SystemUtils.IS_OS_WINDOWS) { - result = "common.dll"; - } else if (SystemUtils.IS_OS_LINUX) { - result = "libcommon.so"; - } else if (SystemUtils.IS_OS_MAC) { - result = "libcommon.dylib"; - } else { - throw new IllegalArgumentException( - String.format( - "Rust inserts are not supported in %s os. Only windows, linux and macos are allowed.", - System.getProperty("os.name") - ) - ); - } - return result; - } - - /** - * Is the project valid? - * @param project File to check. - * @return True if valid. Otherwise, false. - */ - private static boolean valid(final File project) { - return project.isDirectory() - && project.toPath().resolve("Cargo.toml").toFile().exists(); - } - - /** - * Builds cargo project. - * @param project Path to the project. - * @throws IOException If any issues with IO. - */ - private void build(final File project) throws IOException { - final File cached = this.cache - .resolve("Lib") - .resolve(project.getName()) - .resolve("target").toFile(); - if (BinarizeMojo.sameProject( - project.toPath(), - this.cache - .resolve("Lib") - .resolve(project.getName()) - )) { - Logger.info( - this, - "content of %s was not changed since the last launch", - project.getName() - ); - final File executable = cached.toPath() - .resolve("debug") - .resolve(BinarizeMojo.LIB) - .toFile(); - if (executable.exists()) { - FileUtils.copyFile( - executable, - project.toPath() - .resolve("target") - .resolve("debug") - .resolve(BinarizeMojo.LIB) - .toFile() - ); - } - } else { - final File target = project.toPath().resolve("target").toFile(); - if (cached.exists()) { - Logger.info(this, "Copying %s to %s", cached, target); - FileUtils.copyDirectory(cached, target); - } - Logger.info(this, "Building %s rust project..", project.getName()); - try { - new Jaxec("cargo", "build").withHome(project).execUnsafe(); - } catch (final IOException | IllegalArgumentException ex) { - throw new BuildFailureException( - String.format( - "Failed to build cargo project with dest = %s", - project - ), - ex - ); - } - Logger.info( - this, - "Cargo building succeeded, update cached %s with %s", - cached, - target - ); - FileUtils.copyDirectory(target.getParentFile(), cached.getParentFile()); - } + final Collection ffis = new BinarizeParse( + this.generatedDir, + this.eoPortalDir, + new Names(this.namesDir.toPath()), + this.targetDir + ).exec(this.scopedTojos().withOptimized()); + ffis.stream().parallel().forEach(ffi -> ffi.build(this.cache)); + Logger.info(this, "Built in total %d cargo projects", ffis.size()); } - /** - * Check if the project was not changed. - * @param src Directory in current target. - * @param cached Directory in cache. - * @return True if the project is the same. - */ - private static boolean sameProject(final Path src, final Path cached) { - return BinarizeMojo.sameFile( - src.resolve("src/foo.rs"), cached.resolve("src/foo.rs") - ) && BinarizeMojo.sameFile( - src.resolve("src/lib.rs"), cached.resolve("src/lib.rs") - ) && BinarizeMojo.sameFile( - src.resolve("Cargo.toml"), cached.resolve("Cargo.toml") - ); - } - - /** - * Check if the source file is the same as in cache. - * @param src Source file. - * @param cached Cache file. - * @return True if the same. - */ - private static boolean sameFile(final Path src, final Path cached) { - return cached.toFile().exists() && BinarizeMojo.uncomment( - new UncheckedText( - new TextOf(src) - ).asString() - ).equals( - BinarizeMojo.uncomment( - new UncheckedText( - new TextOf(cached) - ).asString() - ) - ); - } - - /** - * Removed the first line from the string. - * We need it because generated files are disclaimed. - * @param content Content. - * @return String without the first line. - * @checkstyle StringLiteralsConcatenationCheck (8 lines) - */ - private static String uncomment(final String content) { - return content.substring( - 1 + content.indexOf(System.getProperty("line.separator")) - ); - } } diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/rust/RustNode.java b/eo-maven-plugin/src/main/java/org/eolang/maven/rust/RustNode.java index 0353b68276..711e41892d 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/rust/RustNode.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/rust/RustNode.java @@ -25,7 +25,10 @@ import com.jcabi.log.Logger; import com.jcabi.xml.XML; +import com.yegor256.Jaxec; +import java.io.File; import java.io.IOException; +import java.io.UncheckedIOException; import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.util.ArrayList; @@ -33,14 +36,23 @@ import java.util.stream.Collectors; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.binary.Hex; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.SystemUtils; import org.cactoos.map.MapOf; +import org.cactoos.text.TextOf; +import org.cactoos.text.UncheckedText; import org.eolang.maven.footprint.FtDefault; /** * {@link FFINode} for Rust inserts. * @since 0.34 */ -public final class RustNode implements FFINode { +public final class RustNode implements Buildable { + + /** + * Name of executable file which is result of cargo building. + */ + public static final String LIB = RustNode.common(); /** * Corresponding node from xmir. @@ -50,7 +62,7 @@ public final class RustNode implements FFINode { /** * To name the insert according to its location. */ - private final Names names; + private final String name; /** * Where is Lib directory- the directory with cargo projects. @@ -79,9 +91,30 @@ public final class RustNode implements FFINode { */ public RustNode( final XML node, final Names names, final Path lib, final Path portal, final Path generated + ) { + this( + node, + names.name(node.xpath("@code_loc").get(0)), + lib, + portal, + generated + ); + } + + /** + * Main constructor. + * @param node Node. + * @param name Name. + * @param lib Lib directory. + * @param portal Portal directory. + * @param generated Generated directory. + * @checkstyle ParameterNumberCheck (10 lines) + */ + public RustNode( + final XML node, final String name, final Path lib, final Path portal, final Path generated ) { this.node = node; - this.names = names; + this.name = name; this.lib = lib; this.portal = portal; this.generated = generated; @@ -95,17 +128,14 @@ public void generate() throws IOException { .stream() .map(RustNode::unhex) .collect(Collectors.toList()); - final String function = this.names.name( - this.node.xpath("@code_loc").get(0) - ); final String filename = String.format( "%s%s", - function, + this.name, ".rs" ); - new Project(this.lib.resolve(function)) + new Project(this.lib.resolve(this.name)) .with(new Module(code, "src/foo"), dependencies) - .with(new PrimeModule(function, "src/lib"), new ArrayList<>(1)) + .with(new PrimeModule(this.name, "src/lib"), new ArrayList<>(1)) .dependency( "eo", new MapOf<>("path", this.portal.toAbsolutePath().toString()) @@ -118,7 +148,7 @@ public void generate() throws IOException { this.node.xpath("@code_loc").get(0) ); new Commented( - new Native(function, "EOrust.natives"), + new Native(this.name, "EOrust.natives"), "//" ).save(new FtDefault(this.generated)); Logger.info( @@ -129,6 +159,79 @@ public void generate() throws IOException { ); } + @Override + public void build(final Path cache) { + try { + this.buildChecked(cache); + } catch (final IOException exception) { + throw new UncheckedIOException(exception); + } + } + + /** + * Build the project. + * @param cache Cache directory. + * @throws IOException If any issues with IO. + */ + private void buildChecked(final Path cache) throws IOException { + final File project = this.lib.resolve(this.name).toFile(); + final File cached = cache + .resolve("Lib") + .resolve(this.name) + .resolve("target").toFile(); + if (RustNode.sameProject( + project.toPath(), + cache + .resolve("Lib") + .resolve(this.name) + )) { + Logger.info( + this, + "content of %s was not changed since the last launch", + this.name + ); + final File executable = cached.toPath() + .resolve("debug") + .resolve(RustNode.LIB) + .toFile(); + if (executable.exists()) { + FileUtils.copyFile( + executable, + project.toPath() + .resolve("target") + .resolve("debug") + .resolve(RustNode.LIB) + .toFile() + ); + } + } else { + final File target = project.toPath().resolve("target").toFile(); + if (cached.exists()) { + Logger.info(this, "Copying %s to %s", cached, target); + FileUtils.copyDirectory(cached, target); + } + Logger.info(this, "Building %s rust project..", project.getName()); + try { + new Jaxec("cargo", "build").withHome(project).execUnsafe(); + } catch (final IOException | IllegalArgumentException ex) { + throw new BuildFailureException( + String.format( + "Failed to build cargo project with dest = %s", + project + ), + ex + ); + } + Logger.info( + this, + "Cargo building succeeded, update cached %s with %s", + cached, + target + ); + FileUtils.copyDirectory(target.getParentFile(), cached.getParentFile()); + } + } + /** * Makes a text from Hexed text. * @param txt Hexed chars separated by backspace. @@ -154,4 +257,76 @@ private static String unhex(final String txt) { } return result; } + + /** + * Check if the project was not changed. + * @param src Directory in current target. + * @param cached Directory in cache. + * @return True if the project is the same. + */ + private static boolean sameProject(final Path src, final Path cached) { + return RustNode.sameFile( + src.resolve("src/foo.rs"), cached.resolve("src/foo.rs") + ) && RustNode.sameFile( + src.resolve("src/lib.rs"), cached.resolve("src/lib.rs") + ) && RustNode.sameFile( + src.resolve("Cargo.toml"), cached.resolve("Cargo.toml") + ); + } + + /** + * Check if the source file is the same as in cache. + * @param src Source file. + * @param cached Cache file. + * @return True if the same. + */ + private static boolean sameFile(final Path src, final Path cached) { + return cached.toFile().exists() && RustNode.uncomment( + new UncheckedText( + new TextOf(src) + ).asString() + ).equals( + RustNode.uncomment( + new UncheckedText( + new TextOf(cached) + ).asString() + ) + ); + } + + /** + * Removed the first line from the string. + * We need it because generated files are disclaimed. + * @param content Content. + * @return String without the first line. + * @checkstyle StringLiteralsConcatenationCheck (8 lines) + */ + private static String uncomment(final String content) { + return content.substring( + 1 + content.indexOf(System.getProperty("line.separator")) + ); + } + + /** + * Calculates name for Rust shared library depending on OS. + * @return Name. + */ + private static String common() { + final String result; + if (SystemUtils.IS_OS_WINDOWS) { + result = "common.dll"; + } else if (SystemUtils.IS_OS_LINUX) { + result = "libcommon.so"; + } else if (SystemUtils.IS_OS_MAC) { + result = "libcommon.dylib"; + } else { + throw new IllegalArgumentException( + String.format( + "Rust inserts are not supported in %s os. Only windows, linux and macos are allowed.", + System.getProperty("os.name") + ) + ); + } + return result; + } } diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/BinarizeMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/BinarizeMojoTest.java index 1846bdf1de..f7647cf8ed 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/BinarizeMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/BinarizeMojoTest.java @@ -29,6 +29,7 @@ import java.nio.file.Paths; import java.util.Map; import org.eolang.maven.rust.Names; +import org.eolang.maven.rust.RustNode; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Assertions; @@ -154,7 +155,7 @@ void doesNotRecompile(@TempDir final Path temp) throws IOException { maven.execute(new FakeMaven.Binarize()); final File executable = cache .resolve("Lib/native0/target/debug/") - .resolve(BinarizeMojo.LIB) + .resolve(RustNode.LIB) .toFile(); final long first = executable.lastModified(); maven.execute(new FakeMaven.Binarize()); From 521bb64c85d3a109eb529ce9340ec69ce060b4cb Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Wed, 10 Jan 2024 19:18:05 +0300 Subject: [PATCH 3/6] #2648: BinarizeParse is not Mojo anymore --- ...arizeParseMojo.java => BinarizeParse.java} | 84 +++++++++++-------- ...seMojoTest.java => BinarizeParseTest.java} | 75 +++++++++++------ .../test/java/org/eolang/maven/FakeMaven.java | 2 +- 3 files changed, 99 insertions(+), 62 deletions(-) rename eo-maven-plugin/src/main/java/org/eolang/maven/{BinarizeParseMojo.java => BinarizeParse.java} (73%) rename eo-maven-plugin/src/test/java/org/eolang/maven/{BinarizeParseMojoTest.java => BinarizeParseTest.java} (76%) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeParseMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeParse.java similarity index 73% rename from eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeParseMojo.java rename to eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeParse.java index bc05402caa..54daf174b5 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeParseMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeParse.java @@ -39,11 +39,8 @@ import java.util.Collection; import java.util.Map; import java.util.function.BiFunction; -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.map.MapOf; +import org.eolang.maven.rust.Buildable; import org.eolang.maven.rust.FFINode; import org.eolang.maven.rust.Names; import org.eolang.maven.rust.RustNode; @@ -56,15 +53,8 @@ * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) * @since 0.1 */ -@Mojo( - name = "binarize_parse", - defaultPhase = LifecyclePhase.PROCESS_SOURCES, - threadSafe = true, - requiresDependencyResolution = ResolutionScope.COMPILE -) - @SuppressWarnings("PMD.LongVariable") -public final class BinarizeParseMojo extends SafeMojo { +public final class BinarizeParse { /** * The directory where to binarize to. @@ -90,7 +80,7 @@ public final class BinarizeParseMojo extends SafeMojo { * Map that matches ffi insert xpath to building of FFINode. */ private static final - Map> FACTORY = new MapOf<>( + Map> FACTORY = new MapOf<>( "/program/rusts/rust", (node, mojo) -> new RustNode( node, @@ -105,39 +95,65 @@ public final class BinarizeParseMojo extends SafeMojo { * Target directory. * @checkstyle MemberNameCheck (7 lines) */ - @Parameter( - required = true, - defaultValue = "${project.build.directory}/eo-binaries" - ) - @SuppressWarnings("PMD.UnusedPrivateField") - private File generatedDir; + private final File generatedDir; /** * The directory with portal project. * @checkstyle MemberNameCheck (8 lines) */ - @Parameter( - property = "eo.portal", - required = true, - defaultValue = "${project.basedir}/src/main/rust/eo" - ) - @SuppressWarnings("PMD.UnusedPrivateField") - private File eoPortalDir; + private final File eoPortalDir; /** * To uniquely name different ffi inerts. */ - private Names names; + private final Names names; - @Override - public void exec() throws IOException { + /** + * To uniquely name different ffi inerts. + * @checkstyle MemberNameCheck (7 lines) + */ + private final File targetDir; + + /** + * Ctor. + * @param generated Generated directory. + * @param portal Directory to portal dependency. + * @param names Names. + * @param target Target directory. + * @checkstyle ParameterNumberCheck (10 lines) + */ + public BinarizeParse( + final File generated, + final File portal, + final Names names, + final File target + ) { + this.generatedDir = generated; + this.eoPortalDir = portal; + this.names = names; + this.targetDir = target; + } + + /** + * Parse ffi nodes in tojos. + * @param tojos Tojos where to parse ffi node, + * @return Collection of {@link FFINode}s that will be then also compiled. + * @throws IOException if any issue with IO. + */ + public Collection exec(final Collection tojos) throws IOException { + final ArrayList res = new ArrayList<>(0); new File(this.targetDir.toPath().resolve("Lib/").toString()).mkdirs(); - for (final ForeignTojo tojo : this.scopedTojos().withOptimized()) { + for (final ForeignTojo tojo : tojos) { final Path file = tojo.verified(); - this.getFFIs(new XMLDocument(file)) - .forEach(FFINode::generateUnchecked); + for (final FFINode ffi: this.getFFIs(new XMLDocument(file))) { + ffi.generateUnchecked(); + if (ffi instanceof Buildable) { + res.add((Buildable) ffi); + } + } } this.names.save(); + return res; } /** @@ -152,7 +168,7 @@ private XML addFFIs( final String name = input.xpath("/program/@name").get(0); final Place place = new Place(name); final Train trn = new SpyTrain( - BinarizeParseMojo.TRAIN, + BinarizeParse.TRAIN, place.make(this.targetDir.toPath().resolve(BinarizeMojo.DIR), "") ); return new Xsline(trn).pass(input); @@ -167,7 +183,7 @@ private XML addFFIs( private Collection getFFIs(final XML input) { final Collection ret = new ArrayList<>(0); final XML injected = this.addFFIs(input); - BinarizeParseMojo.FACTORY.forEach( + BinarizeParse.FACTORY.forEach( (xpath, ctor) -> injected .nodes(xpath) .forEach(node -> ret.add(ctor.apply(node, this))) diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/BinarizeParseMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/BinarizeParseTest.java similarity index 76% rename from eo-maven-plugin/src/test/java/org/eolang/maven/BinarizeParseMojoTest.java rename to eo-maven-plugin/src/test/java/org/eolang/maven/BinarizeParseTest.java index 14e5f89519..bbeecb11b7 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/BinarizeParseMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/BinarizeParseTest.java @@ -23,6 +23,8 @@ */ package org.eolang.maven; +import java.io.File; +import java.io.IOException; import java.nio.file.Path; import java.util.Map; import org.cactoos.text.TextOf; @@ -38,23 +40,19 @@ import org.junit.jupiter.params.ParameterizedTest; /** - * Test case for {@link BinarizeParseMojo}. + * Test case for {@link BinarizeParse}. * * @since 0.1 */ @Execution(ExecutionMode.CONCURRENT) -final class BinarizeParseMojoTest { +final class BinarizeParseTest { @Test void parsesSimpleEoProgram(@TempDir final Path temp) throws Exception { - final Path src = BinarizeMojoTest.SRC.resolve("simple-rust.eo"); - final FakeMaven maven; - synchronized (BinarizeParseMojoTest.class) { - maven = new FakeMaven(temp).withProgram(src); - } - final Map res = maven - .execute(new FakeMaven.BinarizeParse()) - .result(); + final Map res = execParse( + temp, + BinarizeMojoTest.SRC.resolve("simple-rust.eo") + ); final String function = String.format( "%s0", Names.PREFIX @@ -93,14 +91,10 @@ void parsesSimpleEoProgram(@TempDir final Path temp) throws Exception { @Test void binarizesTwiceRustProgram(@TempDir final Path temp) throws Exception { - final Path src = BinarizeMojoTest.SRC.resolve("twice-rust.eo"); - final FakeMaven maven; - synchronized (BinarizeParseMojoTest.class) { - maven = new FakeMaven(temp).withProgram(src); - } - final Map res = maven - .execute(new FakeMaven.BinarizeParse()) - .result(); + final Map res = execParse( + temp, + BinarizeMojoTest.SRC.resolve("twice-rust.eo") + ); final String one = String.format( "target/Lib/%s0/src/foo.rs", Names.PREFIX @@ -146,15 +140,11 @@ void createsDependenciesSection(final String yaml) { @Test void createsCorrectRustProject(@TempDir final Path temp) throws Exception { - final FakeMaven maven; - synchronized (BinarizeParseMojoTest.class) { - maven = new FakeMaven(temp) - .withProgram(BinarizeMojoTest.SRC.resolve("simple-rust.eo")) - .withProgram(BinarizeMojoTest.SRC.resolve("twice-rust.eo")); - } - final Map res = maven - .execute(new FakeMaven.BinarizeParse()) - .result(); + final Map res = execParse( + temp, + BinarizeMojoTest.SRC.resolve("simple-rust.eo"), + BinarizeMojoTest.SRC.resolve("twice-rust.eo") + ); final String dir = String.format( "target/Lib/%s1/", Names.PREFIX @@ -192,4 +182,35 @@ void createsCorrectRustProject(@TempDir final Path temp) throws Exception { ); } + /** + * Executes all mojos before {@link BinarizeMojo} and {@link BinarizeParse}. + * @param temp Temp dir. + * @param programs Paths of eo sources. + * @return The resulting map with relatives paths as keys and absolute paths as values. + * @throws IOException + */ + private static + Map execParse(final Path temp, final Path... programs) throws IOException { + final FakeMaven maven; + final File generated = temp.resolve("target/generated").toFile(); + final File portal = new File("../eo-runtime/src/main/rust/eo"); + final File target = temp.resolve("target").toFile(); + synchronized (BinarizeParseTest.class) { + maven = new FakeMaven(temp) + .with("generatedDir", generated) + .with("eoPortalDir", portal) + .with("targetDir", target); + for (final Path program : programs) { + maven.withProgram(program); + } + maven.execute(new FakeMaven.Verify()); + } + new BinarizeParse( + generated, + portal, + new Names(generated.toPath().resolve("names")), + target + ).exec(maven.foreignTojos().withOptimized()); + return maven.result(); + } } diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java b/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java index 37e117fd93..40db871b43 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java @@ -696,7 +696,7 @@ public Iterator> iterator() { OptimizeMojo.class, ShakeMojo.class, VerifyMojo.class, - BinarizeParseMojo.class + BinarizeMojo.class ).iterator(); } } From 3b25da5310a69227a1d0e1a6fc7db31dd4c02275 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Wed, 10 Jan 2024 19:33:47 +0300 Subject: [PATCH 4/6] #2648: nnot IOException --- .../src/main/java/org/eolang/maven/BinarizeMojo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java index 438f437632..333bf49c68 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java @@ -92,7 +92,7 @@ public final class BinarizeMojo extends SafeMojo { private File eoPortalDir; @Override - public void exec() throws IOException { + public void exec() { final Collection ffis = new BinarizeParse( this.generatedDir, this.eoPortalDir, From 2009ce9e82d2a6e20264580e32bd93d5427fe100 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Wed, 10 Jan 2024 19:38:54 +0300 Subject: [PATCH 5/6] #2648: Collection instead of ArrayList --- .../src/main/java/org/eolang/maven/BinarizeParse.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeParse.java b/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeParse.java index 54daf174b5..5a8a624e48 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeParse.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeParse.java @@ -141,7 +141,7 @@ public BinarizeParse( * @throws IOException if any issue with IO. */ public Collection exec(final Collection tojos) throws IOException { - final ArrayList res = new ArrayList<>(0); + final Collection res = new ArrayList<>(0); new File(this.targetDir.toPath().resolve("Lib/").toString()).mkdirs(); for (final ForeignTojo tojo : tojos) { final Path file = tojo.verified(); From 26c02e137947b81698f07737ac015351f46519e6 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Wed, 10 Jan 2024 20:22:23 +0300 Subject: [PATCH 6/6] #2648: Removed BinarizeParse --- .../test/java/org/eolang/maven/FakeMaven.java | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java b/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java index 8eda256b86..43d1107fd5 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java @@ -698,25 +698,6 @@ public Iterator> iterator() { } } - /** - * Parse rust insert pipeline. - * - * @since 0.29.0 - */ - static final class BinarizeParse implements Iterable> { - - @Override - public Iterator> iterator() { - return Arrays.>asList( - ParseMojo.class, - OptimizeMojo.class, - ShakeMojo.class, - VerifyMojo.class, - BinarizeMojo.class - ).iterator(); - } - } - /** * Resolve all eo dependencies. *