Skip to content

Commit

Permalink
objectionary#2032: Separate project for every rust insert
Browse files Browse the repository at this point in the history
  • Loading branch information
levBagryansky committed Jul 3, 2023
1 parent 7d25041 commit 27ea2fe
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 45 deletions.
73 changes: 39 additions & 34 deletions eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand Down Expand Up @@ -75,42 +76,46 @@ public final class BinarizeMojo extends SafeMojo {
@Override
public void exec() throws IOException {
new Moja<>(BinarizeParseMojo.class).copy(this).execute();
final Path dest = targetDir.toPath().resolve("Lib");
final ProcessBuilder builder = new ProcessBuilder("cargo", "build")
.directory(dest.toFile());
Logger.info(this, "Building rust project..");
final Process building = builder.start();
try {
building.waitFor();
} catch (final InterruptedException exception) {
Thread.currentThread().interrupt();
throw new BuildFailureException(
String.format(
"Interrupted while building %s",
dest.toAbsolutePath()
),
exception
);
}
if (building.exitValue() != 0) {
Logger.error(this, "There was an error in compilation");
final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
try (VerboseProcess process = new VerboseProcess(building)) {
new Unchecked<>(
new LengthOf(
new TeeInput(
new InputOf(process.stdoutQuietly()),
new OutputTo(stdout)
final File dest = targetDir.toPath().resolve("Lib").toFile();
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 ProcessBuilder builder = new ProcessBuilder("cargo", "build")
.directory(file);
final Process building = builder.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");
final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
try (VerboseProcess process = new VerboseProcess(building)) {
new Unchecked<>(
new LengthOf(
new TeeInput(
new InputOf(process.stdoutQuietly()),
new OutputTo(stdout)
)
)
).value();
}
throw new BuildFailureException(
String.format(
"Failed to build cargo project with dest = %s",
file
)
)
).value();
);
}
}
throw new BuildFailureException(
String.format(
"Failed to build cargo project with dest = %s",
dest.toAbsolutePath()
)
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public final class BinarizeParseMojo extends SafeMojo {

@Override
public void exec() throws IOException {
final Project project = new Project(this.targetDir.toPath().resolve("Lib"));
final Names names = new Names(targetDir.toPath());
for (final ForeignTojo tojo : this.scopedTojos().withOptimized()) {
final Path file = tojo.optimized();
Expand Down Expand Up @@ -136,14 +135,11 @@ public void exec() throws IOException {
filename,
input.xpath("/program/@name").get(0)
);
project.add(
function,
code,
dependencies
);
new Project(this.targetDir.toPath().resolve("Lib/".concat(function)))
.with(function, code, dependencies)
.save();
}
}
project.save();
names.save();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ public Project(final Path target) {
* @param name Name of function in project.
* @param raw Content of rust insert.
* @param crates Dependencies of the module.
* @return this.
* @throws IOException If any issues with I/O
*/
public void add(final String name, final String raw, final List<String> crates)
public Project with(final String name, final String raw, final List<String> crates)
throws IOException {
this.modules.add(
String.format("pub mod %s;", name)
Expand All @@ -86,6 +87,7 @@ public void add(final String name, final String raw, final List<String> crates)
final String[] split = crate.split("[=:]");
this.cargo.add(split[0], split[1].replace("\"", "").trim());
}
return this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,15 @@ void createsCorrectRustProject(@TempDir final Path temp) throws Exception {
final String prefix = temp.resolve("target").toString()
.toLowerCase(Locale.ENGLISH)
.replaceAll("[^a-z0-9]", "x");
final String cargo = "target/Lib/Cargo.toml";
final String lib = "target/Lib/src/lib.rs";
final String dir = String.format(
"target/Lib/%s1/",
prefix
);
final String cargo = dir.concat("Cargo.toml");
final String lib = dir.concat("src/lib.rs");;
final String module = String.format(
"target/Lib/src/%s1.rs",
"%ssrc/%s1.rs",
dir,
prefix
);
MatcherAssert.assertThat(
Expand Down

0 comments on commit 27ea2fe

Please sign in to comment.