Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1602 Discover with versions #2228

Merged
merged 22 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void exec() throws IOException {
if (this.central == null) {
this.central = new Central(this.project, this.session, this.manager);
}
String before = this.tojos.status();
String before = this.scopedTojos().status();
int cycle = 0;
final Moja<?>[] mojas = {
new Moja<>(ParseMojo.class),
Expand All @@ -211,7 +211,7 @@ public void exec() throws IOException {
for (final Moja<?> moja : mojas) {
moja.copy(this).execute();
}
final String after = this.tojos.status();
final String after = this.scopedTojos().status();
++cycle;
if (Logger.isInfoEnabled(this)) {
Logger.info(
Expand Down
64 changes: 46 additions & 18 deletions eo-maven-plugin/src/main/java/org/eolang/maven/DiscoverMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.TreeSet;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -94,26 +95,33 @@ public void exec() throws IOException {
private Collection<String> discover(final Path file)
throws FileNotFoundException {
final XML xml = new XMLDocument(file);
final List<String> base = new ListOf<>(
"//o[",
"not(starts-with(@base,'.'))",
"and @base != 'Q'",
"and @base != '^'",
"and @base != '$'",
"and @base != '&'",
"and not(@ref)"
);
final List<String> regular = new ListOf<>(base);
if (this.versioned) {
regular.add("and not(@ver)");
}
regular.add("]/@base");
final Collection<String> names = new TreeSet<>(
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
new ListOf<>(
new Filtered<>(
obj -> !obj.isEmpty(),
xml.xpath(
String.join(
" ",
"//o[",
"not(starts-with(@base,'.'))",
" and @base != 'Q'",
" and @base != '^'",
" and @base != '$'",
" and @base != '&'",
" and not(@ref)",
"]/@base"
)
)
)
)
this.names(xml, regular)
);
if (this.versioned) {
final List<String> versioned = new ListOf<>(base);
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
versioned.addAll(
new ListOf<>(
"and @ver",
"]/concat(@base,'|',@ver)"
)
);
names.addAll(this.names(xml, versioned));
}
if (!xml.nodes("//o[@vararg]").isEmpty()) {
names.add("org.eolang.tuple");
}
Expand All @@ -131,4 +139,24 @@ private Collection<String> discover(final Path file)
return names;
}

/**
* Get list of object names from given XML by provided xpath.
* @param xml XML.
* @param xpath Xpath.
* @return List of object names.
* @checkstyle NonStaticMethodCheck (25 lines)
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
*/
private List<String> names(final XML xml, final List<String> xpath) {
return new ListOf<>(
new Filtered<>(
obj -> !obj.isEmpty(),
xml.xpath(
String.join(
" ",
xpath
)
)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,6 @@ public void exec() throws IOException {
.withVersion(ParseMojo.ZERO)
.withVer(ParseMojo.ZERO);
Logger.debug(this, "EO source %s registered", name);
if (this.external != null) {
this.externalTojos
.add(name)
.withSource(file.toAbsolutePath())
.withVersion(ParseMojo.ZERO);
Logger.debug(this, "EO source %s registered (external)", name);
}
}
Logger.info(
this, "Registered %d EO sources from %s to %s, included %s, excluded %s",
Expand Down
19 changes: 17 additions & 2 deletions eo-maven-plugin/src/main/java/org/eolang/maven/SafeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,20 @@ abstract class SafeMojo extends AbstractMojo {
@Parameter(property = "eo.cache")
protected Path cache = Paths.get(System.getProperty("user.home")).resolve(".eo");

/**
* Used for object versioning implementation.
* If set to TRUE, external tojos are used instead of foreign ones and all
* inherited Mojos behave a bit differently.
* @checkstyle VisibilityModifierCheck (10 lines)
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
*/
@Parameter(property = "eo.versioned", defaultValue = "false")
protected boolean versioned;

/**
* Cached tojos.
* @checkstyle VisibilityModifierCheck (5 lines)
*/
protected final ForeignTojos tojos = new ForeignTojos(
private final ForeignTojos tojos = new ForeignTojos(
() -> Catalogs.INSTANCE.make(this.foreign.toPath(), this.foreignFormat),
() -> this.scope
);
Expand Down Expand Up @@ -318,7 +327,13 @@ public final void execute() throws MojoFailureException, MojoExecutionException
* @checkstyle AnonInnerLengthCheck (100 lines)
*/
protected final ForeignTojos scopedTojos() {
return this.tojos;
final ForeignTojos tjs;
if (this.external != null && this.versioned) {
tjs = this.externalTojos;
} else {
tjs = this.tojos;
}
return tjs;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,38 @@ void discoversForDifferentScopes(@TempDir final Path tmp) throws IOException {
Matchers.is(true)
);
}

@Test
void discoversWithVersions(@TempDir final Path tmp) throws IOException {
final FakeMaven maven = new FakeMaven(tmp)
.with("versioned", true)
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
.withProgram(
"+alias org.eolang.txt.sprintf\n",
"[] > main",
" seq > @",
" QQ.io.stdout|0.29.1",
" sprintf|0.28.5",
" \"Hello world\"",
" TRUE"
)
.execute(new FakeMaven.Discover());
final String sprintf = "org.eolang.txt.sprintf|0.28.5";
final String stdout = "org.eolang.io.stdout|0.29.1";
MatcherAssert.assertThat(
String.format(
"External tojos have to contain %s object after discovering, but they don't",
sprintf
),
maven.externalTojos().contains(sprintf),
Matchers.is(true)
);
MatcherAssert.assertThat(
String.format(
"External tojos should not contain %s object after discovering, but they did",
stdout
),
maven.externalTojos().contains(stdout),
Matchers.is(false)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ void registersInExternal(@TempDir final Path temp) throws IOException {
final String source = "src/eo";
final FakeMaven maven = new FakeMaven(temp)
.with("sourcesDir", temp.resolve(source).toFile())
.with("versioned", true)
maxonfjvipon marked this conversation as resolved.
Show resolved Hide resolved
.execute(new FakeMaven.Register());
MatcherAssert.assertThat(
String.format(
Expand All @@ -115,9 +116,13 @@ void registersInExternal(@TempDir final Path temp) throws IOException {
Matchers.is(true)
);
MatcherAssert.assertThat(
"External and foreign tojos should have the same status after registering because of identical behaviour at the step but they didn't",
"External and foreign tojos should not have the same status after registering because external should replace foreign but they didn't",
maven.foreignTojos().status(),
Matchers.equalTo(maven.externalTojos().status())
Matchers.not(
Matchers.equalTo(
maven.externalTojos().status()
)
)
);
}
}
Loading