Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jul 10, 2023
2 parents dd820b4 + a437b77 commit e9ef31a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 43 deletions.
5 changes: 5 additions & 0 deletions eo-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ SOFTWARE.
<version>3.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
Expand Down
113 changes: 70 additions & 43 deletions eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,26 @@

import com.jcabi.log.Logger;
import com.jcabi.log.VerboseProcess;
import java.io.ByteArrayOutputStream;
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.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.io.InputOf;
import org.cactoos.io.OutputTo;
import org.cactoos.io.TeeInput;
import org.cactoos.scalar.LengthOf;
import org.cactoos.scalar.Unchecked;
import org.eolang.maven.rust.BuildFailureException;

/**
* Compile binaries.
*
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
* @since 0.1
* @todo #2197:45min Update cached rust insert if it was changed.
* 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.
Expand Down Expand Up @@ -78,45 +77,73 @@ public final class BinarizeMojo extends SafeMojo {
@Override
public void exec() throws IOException {
new Moja<>(BinarizeParseMojo.class).copy(this).execute();
for (final File file: targetDir.toPath().resolve("Lib").toFile().listFiles()) {
if (file.isDirectory() && file.toPath().resolve("Cargo.toml").toFile().exists()) {
Logger.info(this, String.format("Building rust project.."));
final Process building = new ProcessBuilder("cargo", "build")
.directory(file)
.start();
try {
building.waitFor();
} catch (final InterruptedException exception) {
Thread.currentThread().interrupt();
throw new BuildFailureException(
String.format(
"Interrupted while building %s",
file
),
exception
);
}
if (building.exitValue() != 0) {
Logger.error(this, "There was an error in compilation");
try (VerboseProcess process = new VerboseProcess(building)) {
new Unchecked<>(
new LengthOf(
new TeeInput(
new InputOf(process.stdoutQuietly()),
new OutputTo(new ByteArrayOutputStream())
)
)
).value();
}
throw new BuildFailureException(
String.format(
"Failed to build cargo project with dest = %s",
file
)
);
}
for (final File project: targetDir.toPath().resolve("Lib").toFile().listFiles()) {
if (project.isDirectory() && project.toPath().resolve("Cargo.toml").toFile().exists()) {
this.build(project);
}
}
}

/**
* 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 target = project.toPath().resolve("target").toFile();
final File cached = cache
.resolve("Lib")
.resolve(project.getName())
.resolve("target").toFile();
if (cached.exists()) {
Logger.info(
this,
String.format(
"Copying %s to %s",
cached,
target
)
);
FileUtils.copyDirectory(
cached,
target
);
}
Logger.info(this, "Building rust project..");
try (
VerboseProcess proc = new VerboseProcess(
new ProcessBuilder("cargo", "build")
.directory(project)
)
) {
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(
"Failed to build cargo project with dest = %s",
project
),
exc
);
}
Logger.info(
this,
String.format(
"Cargo building succeeded, update cached %s with %s",
cached,
target
)
);
FileUtils.copyDirectory(project, cached);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -72,4 +75,25 @@ void failsWithIncorrectInsert(@TempDir final Path temp) throws IOException {
() -> maven.execute(new FakeMaven.Binarize())
);
}

@Test
@Tag("slow")
void savesToCache(@TempDir final Path temp) throws IOException {
final FakeMaven maven;
synchronized (BinarizeMojoTest.class) {
maven = new FakeMaven(temp)
.with("cache", temp.resolve(".cache"))
.withProgram(Paths.get("src/test/resources/org/eolang/maven/simple-rust.eo"));
}
final Map<String, Path> res = maven
.execute(new FakeMaven.Binarize())
.result();
MatcherAssert.assertThat(
res,
Matchers.hasValue(temp.resolve(".cache").resolve("Lib"))
);
Assertions.assertDoesNotThrow(
() -> maven.execute(new FakeMaven.Binarize())
);
}
}

1 comment on commit e9ef31a

@0pdd
Copy link

@0pdd 0pdd commented on e9ef31a Jul 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 2197-52b4993f discovered in eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java) and submitted as #2223. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.