Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Oct 2, 2023
2 parents 79e69a1 + 8a40156 commit 7957350
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 11 deletions.
1 change: 0 additions & 1 deletion eo-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ SOFTWARE.
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.13.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
Expand Down
61 changes: 53 additions & 8 deletions eo-maven-plugin/src/main/java/org/eolang/maven/BinarizeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -57,6 +58,11 @@
@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.
*/
Expand Down Expand Up @@ -107,6 +113,29 @@ public void exec() throws IOException {
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.
Expand All @@ -123,15 +152,10 @@ private static boolean valid(final File 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 = this.cache
.resolve("Lib")
.resolve(project.getName())
.resolve("target").toFile();
if (cached.exists()) {
Logger.info(this, "Copying %s to %s", cached, target);
FileUtils.copyDirectory(cached, target);
}
if (BinarizeMojo.sameProject(
project.toPath(),
this.cache
Expand All @@ -143,7 +167,26 @@ private void build(final File project) throws IOException {
"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();
Expand Down Expand Up @@ -194,9 +237,11 @@ private static boolean sameFile(final Path src, final Path cached) {
new TextOf(src)
).asString()
).equals(
new UncheckedText(
new TextOf(cached)
).asString()
BinarizeMojo.uncomment(
new UncheckedText(
new TextOf(cached)
).asString()
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ public void exec() throws IOException {
);
Logger.info(
this,
"Binarized %s from %s",
"Binarized %s from %s:%s",
filename,
input.xpath("/program/@name").get(0)
input.xpath("/program/@name").get(0),
node.xpath("@code_loc").get(0)
);
new Project(this.targetDir.toPath().resolve("Lib/".concat(function)))
.with(new Module(code, "src/foo"), dependencies)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.eolang.maven;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -139,4 +140,29 @@ void boostsSecondCompilation(@TempDir final Path temp) throws IOException {
Matchers.lessThan(first)
);
}

@Test
@Tag("slow")
void doesNotRecompile(@TempDir final Path temp) throws IOException {
final FakeMaven maven;
final Path cache = temp.resolve(".cache");
synchronized (BinarizeMojoTest.class) {
maven = new FakeMaven(temp)
.withProgram(BinarizeMojoTest.SRC.resolve("simple-rust.eo"))
.with("cache", cache);
}
maven.execute(new FakeMaven.Binarize());
final File executable = cache
.resolve("Lib/native0/target/debug/")
.resolve(BinarizeMojo.LIB)
.toFile();
final long first = executable.lastModified();
maven.execute(new FakeMaven.Binarize());
final long second = executable.lastModified();
MatcherAssert.assertThat(first, Matchers.not(0L));
MatcherAssert.assertThat(
second,
Matchers.equalTo(first)
);
}
}

0 comments on commit 7957350

Please sign in to comment.