Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed Sep 20, 2023
1 parent d28982f commit e10ac76
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,8 @@ class StandardLibraryUpdateResolver implements LibraryUpdateResolver {
StandardLibraryUpdateResolver(VersionResolver versionResolver,
List<BiPredicate<Library, DependencyVersion>> predicates) {
this.versionResolver = versionResolver;
BiPredicate<Library, DependencyVersion> predicate = null;
for (BiPredicate<Library, DependencyVersion> p : predicates) {
if (predicate == null) {
predicate = p;
}
else {
predicate = predicate.and(p);
}
}
this.predicate = predicate;
this.predicate = (library, dependencyVersion) -> predicates.stream()
.allMatch((predicate) -> predicate.test(library, dependencyVersion));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import javax.inject.Inject;

import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.VersionRange;
import org.gradle.api.DefaultTask;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.internal.tasks.userinput.UserInputHandler;
Expand Down Expand Up @@ -228,42 +229,37 @@ private List<Upgrade> resolveUpgrades(Milestone milestone) {
}

protected List<BiPredicate<Library, DependencyVersion>> determineUpdatePredicates(Milestone milestone) {
BiPredicate<Library, DependencyVersion> compilesWithUpgradePolicy = (library,
candidate) -> this.bom.getUpgrade().getPolicy().test(candidate, library.getVersion().getVersion());
BiPredicate<Library, DependencyVersion> isAnUpgrade = (library,
candidate) -> library.getVersion().getVersion().isUpgrade(candidate, this.movingToSnapshots);
BiPredicate<Library, DependencyVersion> isPermitted = (library, candidate) -> {
for (ProhibitedVersion prohibitedVersion : library.getProhibitedVersions()) {
String candidateString = candidate.toString();
if (prohibitedVersion.getRange() != null
&& prohibitedVersion.getRange().containsVersion(new DefaultArtifactVersion(candidateString))) {
return false;
}
for (String startsWith : prohibitedVersion.getStartsWith()) {
if (candidateString.startsWith(startsWith)) {
return false;
}
}
for (String endsWith : prohibitedVersion.getEndsWith()) {
if (candidateString.endsWith(endsWith)) {
return false;
}
}
for (String contains : prohibitedVersion.getContains()) {
if (candidateString.contains(contains)) {
return false;
}
}
}
return true;
};
List<BiPredicate<Library, DependencyVersion>> updatePredicates = new ArrayList<>();
updatePredicates.add(compilesWithUpgradePolicy);
updatePredicates.add(isAnUpgrade);
updatePredicates.add(isPermitted);
updatePredicates.add(this::compilesWithUpgradePolicy);
updatePredicates.add(this::isAnUpgrade);
updatePredicates.add(this::isNotProhibited);
return updatePredicates;
}

private boolean compilesWithUpgradePolicy(Library library, DependencyVersion candidate) {
return this.bom.getUpgrade().getPolicy().test(candidate, library.getVersion().getVersion());
}

private boolean isAnUpgrade(Library library, DependencyVersion candidate) {
return library.getVersion().getVersion().isUpgrade(candidate, this.movingToSnapshots);
}

private boolean isNotProhibited(Library library, DependencyVersion candidate) {
return !library.getProhibitedVersions()
.stream()
.anyMatch((prohibited) -> isProhibited(prohibited, candidate.toString()));
}

private boolean isProhibited(ProhibitedVersion prohibited, String candidate) {
boolean result = false;
VersionRange range = prohibited.getRange();
result = result || (range != null && range.containsVersion(new DefaultArtifactVersion(candidate)));
result = result || prohibited.getStartsWith().stream().anyMatch(candidate::startsWith);
result = result || prohibited.getStartsWith().stream().anyMatch(candidate::endsWith);
result = result || prohibited.getStartsWith().stream().anyMatch(candidate::contains);
return result;
}

private List<Library> matchingLibraries() {
List<Library> matchingLibraries = this.bom.getLibraries()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private FileDescriptor open(String path) {
return new FileDescriptor(handle, this::close);
}
catch (RuntimeException ex) {
this.close(handle);
close(handle);
throw ex;
}
}
Expand Down

0 comments on commit e10ac76

Please sign in to comment.