Skip to content

Commit

Permalink
#2648: BinarizeParse is not Mojo anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
levBagryansky committed Jan 10, 2024
1 parent dd6e9cc commit 521bb64
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 62 deletions.
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -90,7 +80,7 @@ public final class BinarizeParseMojo extends SafeMojo {
* Map that matches ffi insert xpath to building of FFINode.
*/
private static final
Map<String, BiFunction<XML, BinarizeParseMojo, FFINode>> FACTORY = new MapOf<>(
Map<String, BiFunction<XML, BinarizeParse, FFINode>> FACTORY = new MapOf<>(
"/program/rusts/rust",
(node, mojo) -> new RustNode(
node,
Expand All @@ -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<Buildable> exec(final Collection<ForeignTojo> tojos) throws IOException {
final ArrayList<Buildable> 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;
}

/**
Expand All @@ -152,7 +168,7 @@ private XML addFFIs(
final String name = input.xpath("/program/@name").get(0);
final Place place = new Place(name);
final Train<Shift> trn = new SpyTrain(
BinarizeParseMojo.TRAIN,
BinarizeParse.TRAIN,
place.make(this.targetDir.toPath().resolve(BinarizeMojo.DIR), "")
);
return new Xsline(trn).pass(input);
Expand All @@ -167,7 +183,7 @@ private XML addFFIs(
private Collection<FFINode> getFFIs(final XML input) {
final Collection<FFINode> 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)))
Expand Down
Expand Up @@ -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;
Expand All @@ -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<String, Path> res = maven
.execute(new FakeMaven.BinarizeParse())
.result();
final Map<String, Path> res = execParse(
temp,
BinarizeMojoTest.SRC.resolve("simple-rust.eo")
);
final String function = String.format(
"%s0",
Names.PREFIX
Expand Down Expand Up @@ -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<String, Path> res = maven
.execute(new FakeMaven.BinarizeParse())
.result();
final Map<String, Path> res = execParse(
temp,
BinarizeMojoTest.SRC.resolve("twice-rust.eo")
);
final String one = String.format(
"target/Lib/%s0/src/foo.rs",
Names.PREFIX
Expand Down Expand Up @@ -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<String, Path> res = maven
.execute(new FakeMaven.BinarizeParse())
.result();
final Map<String, Path> 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
Expand Down Expand Up @@ -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<String, Path> 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();
}
}
Expand Up @@ -696,7 +696,7 @@ public Iterator<Class<? extends AbstractMojo>> iterator() {
OptimizeMojo.class,
ShakeMojo.class,
VerifyMojo.class,
BinarizeParseMojo.class
BinarizeMojo.class
).iterator();
}
}
Expand Down

0 comments on commit 521bb64

Please sign in to comment.