diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/Unplace.java b/eo-maven-plugin/src/main/java/org/eolang/maven/Unplace.java index 6b69f694a4c..e0a14cd5730 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/Unplace.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/Unplace.java @@ -25,6 +25,7 @@ import java.io.File; import java.nio.file.Path; +import java.util.regex.Pattern; /** * Make program name from a path. @@ -38,6 +39,11 @@ final class Unplace { */ private final Path parent; + /** + * The file extension. + */ + private final String extension; + /** * Ctor. * @param dir The name of the parent dir @@ -51,7 +57,26 @@ final class Unplace { * @param dir The name of the parent dir */ Unplace(final Path dir) { + this(dir, ".eo"); + } + + /** + * Ctor. + * @param dir The name of the parent dir + * @param extension The file extension + */ + Unplace(final File dir, final String extension) { + this(dir.toPath(), extension); + } + + /** + * Main Ctor. + * @param dir The name of the parent dir + * @param extension The file extension + */ + Unplace(final Path dir, final String extension) { this.parent = dir; + this.extension = extension; } /** @@ -62,6 +87,9 @@ final class Unplace { public String make(final Path file) { return file.toString().substring( this.parent.toString().length() + 1 - ).replaceAll(".eo$", "").replace(File.separator, "."); + ).replaceAll( + Pattern.quote(this.extension).concat("$"), + "" + ).replace(File.separator, "."); } } diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojo.java index 4c12fd5190c..99b0954771c 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojo.java @@ -117,6 +117,14 @@ public String description() { ); } + /** + * The tojo phi. + * @return The phi. + */ + public Path phi() { + return Paths.get(this.attribute(ForeignTojos.Attribute.PHI)); + } + /** * The tojo hash. * @return The hash. @@ -178,6 +186,48 @@ public boolean notParsed() { return res; } + /** + * Check if the given tojo has not been parsed to phi-expression. + * + * @return True if the tojo has not been parsed to phi-expression. + */ + public boolean notPhied() { + boolean res = true; + if (this.delegate.exists(ForeignTojos.Attribute.PHI.getKey()) + && this.delegate.exists(ForeignTojos.Attribute.XMIR.getKey())) { + final Path phi = this.phi(); + if (phi.toFile().lastModified() >= this.xmir().toFile().lastModified()) { + Logger.debug( + this, "Already executed xmir-to-phi: %s to %[file]s (it's newer than the XMIR)", + this.identifier(), phi + ); + res = false; + } + } + return res; + } + + /** + * Check if the given tojo has not been parsed to XMIR from phi-expression. + * + * @return True if the tojo has not been parsed from XMIR to phi-expression. + */ + public boolean notUnphied() { + boolean res = true; + if (this.delegate.exists(ForeignTojos.Attribute.XMIR.getKey()) + && this.delegate.exists(ForeignTojos.Attribute.PHI.getKey())) { + final Path xmir = this.xmir(); + if (xmir.toFile().lastModified() >= this.phi().toFile().lastModified()) { + Logger.debug( + this, "Already executed phi-to-xmir: %s to %[file]s (it's newer than the phi)", + this.identifier(), xmir + ); + res = false; + } + } + return res; + } + /** * Checks if tojo has hash. * @return True if has hash, false otherwise. @@ -309,6 +359,16 @@ public ForeignTojo withScope(final String scope) { return this; } + /** + * Set the phi. + * @param phi The phi. + * @return The tojo itself. + */ + public ForeignTojo withPhi(final Path phi) { + this.delegate.set(ForeignTojos.Attribute.PHI.getKey(), phi); + return this; + } + /** * Return the scope of the tojo. * @return The scope. diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojos.java b/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojos.java index a218905f0b9..bef84f838e7 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojos.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/tojos/ForeignTojos.java @@ -361,7 +361,12 @@ enum Attribute { /** * Git SHA of the object in the {@code objectionary/home}. */ - HASH("hash"); + HASH("hash"), + + /** + * Absolute path of the parsed from XMIR to phi-expression file. + */ + PHI("phi"); /** * Attribute name. diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/UnplaceTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/UnplaceTest.java index 63698b7f261..6185a0110e2 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/UnplaceTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/UnplaceTest.java @@ -26,6 +26,8 @@ import java.nio.file.Paths; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -55,4 +57,31 @@ void makesName( ); } + @ParameterizedTest + @CsvSource({ + "/tmp/foo/bar, /tmp/foo/bar/a/b/c.phi, a.b.c", + "/tmp/foo/bar, /tmp/foo/bar/a/b/.cd.ef.phi, a.b..cd.ef" + }) + void makesNameWithCustomExtension( + final String base, + final String source, + final String name + ) { + MatcherAssert.assertThat( + CatalogsTest.TO_ADD_MESSAGE, + new Unplace(Paths.get(base), ".phi").make( + Paths.get(source) + ), + Matchers.equalTo(name) + ); + } + + @Test + void interpretsExtensionAsLiteralString() { + Assertions.assertDoesNotThrow( + () -> new Unplace(Paths.get("/tmp/foo/bar"), "(") + .make(Paths.get("/tmp/foo/bar/a/b/c(")), + "Extension interpreted not as literal string" + ); + } }