Skip to content
30 changes: 29 additions & 1 deletion eo-maven-plugin/src/main/java/org/eolang/maven/Unplace.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.File;
import java.nio.file.Path;
import java.util.regex.Pattern;

/**
* Make program name from a path.
Expand All @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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, ".");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/UnplaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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"
);
}
}
Loading