Skip to content

Commit

Permalink
Merge branch 'master' into 2230
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jul 11, 2023
2 parents ed89f78 + 03620bf commit e00f461
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 33 deletions.
2 changes: 1 addition & 1 deletion eo-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ SOFTWARE.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.12.0</version>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
53 changes: 34 additions & 19 deletions eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
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.eolang.maven.rust.BuildFailureException;

/**
Expand All @@ -45,9 +49,6 @@
* Now it copies cargo project to cache directory in the end of every
* compilation. It is better to copy the project only if it was changed
* with the last compilation.
* @todo #2195:90min Make cargo compilation in parallel. Now cargo
* projects are being built consistently which is too long.
* It is much better to build them in parallel to reduce time.
*/
@Mojo(
name = "binarize",
Expand Down Expand Up @@ -77,11 +78,35 @@ public final class BinarizeMojo extends SafeMojo {
@Override
public void exec() throws IOException {
new Moja<>(BinarizeParseMojo.class).copy(this).execute();
for (final File project: targetDir.toPath().resolve("Lib").toFile().listFiles()) {
if (project.isDirectory() && project.toPath().resolve("Cargo.toml").toFile().exists()) {
this.build(project);
}
}
final int total = new SumOf(
new Threads<>(
Runtime.getRuntime().availableProcessors(),
new Mapped<>(
project -> () -> {
this.build(project);
return 1;
},
new Filtered<>(
project -> BinarizeMojo.valid(project),
targetDir.toPath().resolve("Lib").toFile().listFiles()
)
)
)
).intValue();
Logger.info(
this,
String.format("Built in total %d cargo projects", total)
);
}

/**
* 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();
}

/**
Expand All @@ -91,7 +116,7 @@ public void exec() throws IOException {
*/
private void build(final File project) throws IOException {
final File target = project.toPath().resolve("target").toFile();
final File cached = cache
final File cached = this.cache
.resolve("Lib")
.resolve(project.getName())
.resolve("target").toFile();
Expand All @@ -117,16 +142,6 @@ private void build(final File project) throws IOException {
)
) {
proc.stdout();
proc.waitFor();
} catch (final InterruptedException exception) {
Thread.currentThread().interrupt();
throw new BuildFailureException(
String.format(
"Interrupted while building %s",
project
),
exception
);
} catch (final IllegalArgumentException exc) {
throw new BuildFailureException(
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ public void exec() throws IOException {
.withVersion(ParseMojo.ZERO)
.withVer(ParseMojo.ZERO);
Logger.debug(this, "EO source %s registered", name);
if (this.external != null) {
this.externalTojos
.add(name)
.withSource(file.toAbsolutePath())
.withVersion(ParseMojo.ZERO);
Logger.debug(this, "EO source %s registered (external)", name);
}
}
Logger.info(
this, "Registered %d EO sources from %s to %s, included %s, excluded %s",
Expand Down
36 changes: 36 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/SafeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ abstract class SafeMojo extends AbstractMojo {
)
protected File foreign;

/**
* File with external "tojos".
* @checkstyle VisibilityModifierCheck (10 lines)
*/
@Parameter(
property = "eo.external",
required = true,
defaultValue = "${project.build.directory}/eo-external.csv"
)
protected File external;

/**
* Format of "foreign" file ("json" or "csv").
* @checkstyle MemberNameCheck (7 lines)
Expand Down Expand Up @@ -195,6 +206,21 @@ abstract class SafeMojo extends AbstractMojo {
() -> this.scope
);

/**
* External tojos.
* @todo #1602:30min Use external tojos to implement object versioning.
* Implementation of object versioning will bring a lot significant
* changes. That's why it's better to use independent separated tojos for
* that purpose. At the end when object versioning works - just replace
* them and remove unnecessary one.
* @checkstyle MemberNameCheck (7 lines)
* @checkstyle VisibilityModifierCheck (5 lines)
*/
protected final ForeignTojos externalTojos = new ForeignTojos(
() -> Catalogs.INSTANCE.make(this.external.toPath(), this.foreignFormat),
() -> this.scope
);

/**
* Placed tojos.
* @checkstyle MemberNameCheck (7 lines)
Expand All @@ -220,6 +246,13 @@ abstract class SafeMojo extends AbstractMojo {
@SuppressWarnings("PMD.ImmutableField")
private boolean skip;

/**
* Execute it.
* @throws MojoFailureException If fails during build
* @throws MojoExecutionException If fails during execution
* @checkstyle NoJavadocForOverriddenMethodsCheck (10 lines)
* @checkstyle CyclomaticComplexityCheck (70 lines)
*/
@Override
public final void execute() throws MojoFailureException, MojoExecutionException {
StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
Expand Down Expand Up @@ -266,6 +299,9 @@ public final void execute() throws MojoFailureException, MojoExecutionException
if (this.foreign != null) {
SafeMojo.closeTojos(this.tojos);
}
if (this.external != null) {
SafeMojo.closeTojos(this.externalTojos);
}
if (this.placed != null) {
SafeMojo.closeTojos(this.placedTojos);
}
Expand Down
80 changes: 68 additions & 12 deletions eo-maven-plugin/src/test/java/org/eolang/maven/FakeMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public final class FakeMaven {
private final Map<String, Object> params;

/**
* Attributes for eo.foreign.*.
* Attributes for eo.foreign.* and eo.external.*.
*/
private final Map<ForeignTojos.Attribute, Object> attributes;

Expand Down Expand Up @@ -156,21 +156,19 @@ public TjSmart foreign() {
* @throws java.io.IOException If some problem with filesystem have happened.
*/
public <T extends AbstractMojo> FakeMaven execute(final Class<T> mojo) throws IOException {
for (final Tojo tojo : this.foreign().select(all -> true)) {
for (final Map.Entry<ForeignTojos.Attribute, Object> entry
: this.attributes.entrySet()) {
tojo.set(entry.getKey().key(), entry.getValue());
}
}
this.fillUp(this.foreign().select(all -> true));
this.fillUp(this.external().select(all -> true));
this.params.putIfAbsent("targetDir", this.targetPath().toFile());
this.params.putIfAbsent("foreign", this.foreignPath().toFile());
this.params.putIfAbsent("external", this.externalPath().toFile());
this.params.putIfAbsent("foreignFormat", "csv");
this.params.putIfAbsent("project", new MavenProjectStub());
final Path transpiled = Paths.get("transpiled");
this.workspace.save(new TextOf(""), transpiled);
this.params.putIfAbsent("transpiled", this.workspace.absolute(transpiled).toFile());
this.params.putIfAbsent("transpiledFormat", "csv");
this.params.putIfAbsent("skipZeroVersions", true);
this.params.putIfAbsent("versioned", false);
this.params.putIfAbsent("discoverSelf", false);
this.params.putIfAbsent("ignoreVersionConflict", false);
this.params.putIfAbsent("ignoreTransitive", true);
Expand Down Expand Up @@ -239,6 +237,28 @@ ForeignTojos foreignTojos() {
);
}

/**
* External tojos for eo-external.* file.
* @return External tojos.
*/
ForeignTojos externalTojos() {
return new ForeignTojos(
() -> Catalogs.INSTANCE.make(this.externalPath()),
this::scope
);
}

/**
* Tojo for eo-external.* file.
*
* @return TjSmart of the current eo-external.file.
*/
TjSmart external() {
return new TjSmart(
Catalogs.INSTANCE.make(this.externalPath())
);
}

/**
* Sets placed tojo attribute.
*
Expand Down Expand Up @@ -293,7 +313,7 @@ FakeMaven withTojoAttribute(final ForeignTojos.Attribute attribute, final Object
}

/**
* Path to 'eo-foreign.csv' or 'eo-foreign.json' file after all changes.
* Path to eo-foreign.* file after all changes.
* @return Path to eo-foreign.* file.
*/
Path foreignPath() {
Expand Down Expand Up @@ -378,11 +398,20 @@ private FakeMaven withProgram(final String content) throws IOException {
String.format("foo/x/main%s.eo", FakeMaven.suffix(this.current.get()))
);
this.workspace.save(content, path);
final String object = String.format("foo.x.main%s", FakeMaven.suffix(this.current.get()));
final String scope = this.scope();
final String version = "0.25.0";
final Path source = this.workspace.absolute(path);
this.foreignTojos()
.add(String.format("foo.x.main%s", FakeMaven.suffix(this.current.get())))
.withScope(this.scope())
.withVersion("0.25.0")
.withSource(this.workspace.absolute(path));
.add(object)
.withScope(scope)
.withVersion(version)
.withSource(source);
this.externalTojos()
.add(object)
.withScope(scope)
.withVersion(version)
.withSource(source);
this.current.incrementAndGet();
return this;
}
Expand Down Expand Up @@ -430,6 +459,33 @@ private static Set<String> mojoFields(final Class<?> mojo, final Set<String> fie
return res;
}

/**
* Path to or eo-external.* file after all changes.
* @return Path to eo-foreign.* file.
*/
private Path externalPath() {
return this.workspace.absolute(Paths.get("eo-external.csv"));
}

/**
* Fill up given tojos by the attributes.
* @param tojos Tojos to fill up.
* @todo #1602:30min Move the method to ForeignTojos if possible.
* Let's treat ForeignTojos as an object (not as a collection of data)
* and give it a chance to fulfill itself. It knows better how to do so.
* ForeignTojo in current implementation does not have method set() so
* we either need to implement it or just stay with the method here in
* FakeMaven class.
*/
private void fillUp(final List<Tojo> tojos) {
for (final Tojo tojo : tojos) {
for (final Map.Entry<ForeignTojos.Attribute, Object> entry
: this.attributes.entrySet()) {
tojo.set(entry.getKey().key(), entry.getValue());
}
}
}

/**
* Parse full pipeline.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,33 @@ void doesNotFailWhenNoStrictNames(@TempDir final Path temp) throws IOException {
Matchers.is(true)
);
}

@Test
void registersInExternal(@TempDir final Path temp) throws IOException {
new Home(temp).save(
new ResourceOf("org/eolang/maven/file-name/abc-def.eo"),
Paths.get("src/eo/org/eolang/maven/foo.eo")
);
final String name = "org.eolang.maven.foo";
final String source = "src/eo";
final FakeMaven maven = new FakeMaven(temp)
.with("sourcesDir", temp.resolve(source).toFile())
.execute(new FakeMaven.Register());
MatcherAssert.assertThat(
String.format(
"Source object %s placed in %s should have been registered in external tojos but it didn't",
name,
source
),
maven.external()
.getById(name)
.exists("id"),
Matchers.is(true)
);
MatcherAssert.assertThat(
"External and foreign tojos should have the same status after registering because of identical behaviour at the step but they didn't",
maven.foreignTojos().status(),
Matchers.equalTo(maven.externalTojos().status())
);
}
}
6 changes: 6 additions & 0 deletions eo-parser/src/test/java/org/eolang/parser/SyntaxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand Down Expand Up @@ -247,13 +248,18 @@ void storesAsBytes(final String code) throws IOException {
void checksTypoPacks(final String yml) throws IOException, NumberFormatException {
final Yaml yaml = new Yaml();
final Map<String, Object> map = yaml.load(yml);
Assumptions.assumeTrue(map.get("skip") == null);
final ByteArrayOutputStream xmir = new ByteArrayOutputStream();
new Syntax(
"typo",
new InputOf(String.format("%s\n", map.get("eo"))),
new OutputTo(xmir)
).parse();
final XML xml = new XMLDocument(xmir.toByteArray());
MatcherAssert.assertThat(
XhtmlMatchers.xhtml(xml.toString()),
XhtmlMatchers.hasXPaths("/program/errors/error/@line")
);
MatcherAssert.assertThat(
xml.toString(),
Integer.parseInt(xml.xpath("/program/errors/error[1]/@line").get(0)),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# @todo #2230:30min The test is skipped because it doesn't work.
# The syntax below is not supported, but it passes the test and
# no typos are detected. We should fix it. Most probably EOL-s
# are not detected at the end of objects correctly.
skip: true
line: 1
eo: |
[] > a [] > b [] > c [] > d hello world
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ SOFTWARE.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.12.0</version>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>com.yegor256</groupId>
Expand Down

0 comments on commit e00f461

Please sign in to comment.