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

#1361 Add eo-runtime dependency even if foreign dependencies are absent #1357

Merged
merged 19 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
19 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
@@ -0,0 +1,102 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven;

import org.apache.maven.model.Dependency;

/**
* RuntimeDependency is a class for keeping and getting the last eo-runtime dependency.
*
* @since 0.28.11
*/
final class EoRuntimeDependency {
/**
* EO runtime dependency group.
*/
private final String group;

/**
* EO runtime dependency artifact name.
*/
private final String artifact;

/**
* EO runtime dependency version.
*/
private final String version;

/**
* Default constructor.
*/
EoRuntimeDependency() {
this("0.28.10");
}

/**
* Constructor with version.
*
* @param version Version of the eo-runtime library
*/
EoRuntimeDependency(final String version) {
this("org.eolang", "eo-runtime", version);
}

/**
* The main constructor.
*
* @param group Dependency group
* @param artifact Dependency artifact name
* @param version Dependency version
*/
EoRuntimeDependency(final String group, final String artifact, final String version) {
this.group = group;
this.artifact = artifact;
this.version = version;
}

/**
* Compares current dependency with other dependency.
*
* @param other Other dependency
* @return True if other dependency is the eo-runtime dependency
*/
boolean same(final Dependency other) {
return this.group.equals(other.getGroupId())
&& this.artifact.equals(other.getArtifactId());
}

/**
* Converts EoRuntimeDependency to Maven Dependency.
*
* @return Maven Dependency
*/
Dependency dependency() {
final Dependency dependency = new Dependency();
dependency.setGroupId(this.group);
dependency.setArtifactId(this.artifact);
dependency.setVersion(this.version);
dependency.setClassifier("");
return dependency;
}
}
25 changes: 25 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/ResolveMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ private Collection<Dependency> deps() throws IOException {
tojo.set(AssembleMojo.ATTR_JAR, coords);
}
this.checkConflicts(deps);
ResolveMojo.addRuntimeDependency(deps);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to obrain the issue I had to add a few private static classes. Please, take a look at AllDependencies and UniqueDependencies classes.

return deps.stream()
.map(ResolveMojo.Wrap::new)
.sorted()
Expand All @@ -202,6 +203,30 @@ private Collection<Dependency> deps() throws IOException {
.collect(Collectors.toList());
}

/**
* Check if runtime dependency is absent.
* @param deps Dependencies
* @todo #1361:90min Hardcoded version of EoRuntimeDependency.
* See the EoRuntimeDependency constructor for more info.
* It's much better to determine the version of the runtime library
* dynamically. For example, we can fetch the latest version by http
* or from config files.
*/
private static void addRuntimeDependency(final Collection<Dependency> deps) {
if (deps.stream().noneMatch(ResolveMojo::isRuntimeDependency)) {
deps.add(new EoRuntimeDependency().dependency());
}
}

/**
* Check if dependency is runtime dependency.
* @param dependency Dependency
* @return True if dependency is runtime dependency
*/
private static boolean isRuntimeDependency(final Dependency dependency) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volodya-lombrozo This can be inlined. I believe no need in separate static method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return new EoRuntimeDependency().same(dependency);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volodya-lombrozo May we create only one instance of EoRuntimeDependency and reuse it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

/**
* Check dependencies for conflicts.
* @param deps Dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void assemblesTogether(@TempDir final Path temp) throws Exception {
.with("placed", temp.resolve("list").toFile())
.with("cache", temp.resolve("cache/parsed"))
.with("skipZeroVersions", true)
.with("central", Central.EMPTY)
.with(
"objectionary",
(Objectionary) input -> new InputOf(
Expand Down Expand Up @@ -124,6 +125,7 @@ void assemblesNotFailWithFailOnErrorFlag(@TempDir final Path temp) throws Except
.with("cache", temp.resolve("cache/parsed"))
.with("skipZeroVersions", true)
.with("failOnError", false)
.with("central", Central.EMPTY)
.with(
"objectionary",
(Objectionary) input -> new InputOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void fullCompilingLifecycleSuccessfully(@TempDir final Path temp) throws IOExcep
.with("placed", temp.resolve("list").toFile())
.with("cache", temp.resolve("cache/parsed"))
.with("skipZeroVersions", true)
.with("central", Central.EMPTY)
.with(
"objectionary",
(Objectionary) input -> new InputOf(
Expand Down
147 changes: 147 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/MockMavenCentral.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2022 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven;

import java.nio.file.Path;
import java.util.Deque;
import java.util.LinkedList;
import java.util.function.BiConsumer;
import org.apache.maven.model.Dependency;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;

/**
* The class for emulating of Maven Central repository.
*
* @since 0.28.11
*/
final class MockMavenCentral implements BiConsumer<Dependency, Path> {

/**
* All saved dependencies.
*/
private final Deque<Dependency> dependencies;

/**
* All paths where dependencies were saved.
*/
private final Deque<Path> paths;

/**
* Default constructor with predefined containers.
*/
MockMavenCentral() {
this(new LinkedList<>(), new LinkedList<>());
}

/**
* The main constructor.
*
* @param dependencies Dependencies container
* @param paths Paths container
*/
private MockMavenCentral(
final Deque<Dependency> dependencies,
final Deque<Path> paths
) {
this.dependencies = dependencies;
this.paths = paths;
}

@Override
public void accept(final Dependency dependency, final Path path) {
this.dependencies.addLast(dependency);
this.paths.addLast(path);
}

/**
* The method check the last saved dependency.
*
* @param dependency Expected dependency that was downloaded the last
* @param path Expected path where we saved the last dependency
*/
void assertLastDownloadedDependencyAndPathEquals(final Dependency dependency, final Path path) {
MatcherAssert.assertThat(this.dependencies, Matchers.not(Matchers.empty()));
MatcherAssert.assertThat(this.paths, Matchers.not(Matchers.empty()));
final Dependency dep = this.dependencies.getLast();
final Path actual = this.paths.getLast();
MatcherAssert.assertThat(true, Matchers.is(dependenciesEquals(dep, dependency)));
MatcherAssert.assertThat(actual, Matchers.equalTo(path));
}

/**
* The method checks if dependencies container is empty.
*
* @return True if at least one dependency was accepted
*/
boolean isNotEmpty() {
return !this.dependencies.isEmpty();
}

/**
* Compare dependency fields.
*
* @param expected Expected dependency
* @param actual Actual dependency
* @return True if the main properties are equal
*/
private static boolean dependenciesEquals(
final Dependency expected,
final Dependency actual
) {
return theSameMainInformation(expected, actual)
&& theSameTypeAndClassifier(expected, actual);
}

/**
* Compare dependency fields.
*
* @param expected Expected dependency
* @param actual Actual dependency
* @return True if group, artifact and version are the same
*/
private static boolean theSameMainInformation(
final Dependency expected,
final Dependency actual
) {
return actual.getGroupId().equals(expected.getGroupId())
&& actual.getArtifactId().equals(expected.getArtifactId())
&& actual.getVersion().equals(expected.getVersion());
}

/**
* Compare dependency fields.
*
* @param expected Expected dependency
* @param actual Actual dependency
* @return True of type and classifier the same
*/
private static boolean theSameTypeAndClassifier(
final Dependency expected,
final Dependency actual
) {
return actual.getType().equals(expected.getType())
&& actual.getClassifier().equals(expected.getClassifier());
}
}
Loading