Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Commit

Permalink
Some additional changes following code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Catalin Buleandra committed Mar 2, 2015
1 parent 159bb45 commit 0b428e9
Show file tree
Hide file tree
Showing 24 changed files with 197 additions and 214 deletions.
8 changes: 4 additions & 4 deletions src/main/groovy/com/ofg/uptodate/UptodatePlugin.groovy
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.ofg.uptodate

import com.ofg.uptodate.finder.Dependency
import com.ofg.uptodate.finder.JCenterNewVersionFinderFactory
import com.ofg.uptodate.finder.MavenNewVersionFinderFactory
import com.ofg.uptodate.finder.jcenter.JCenterNewVersionFinderFactory
import com.ofg.uptodate.finder.maven.MavenNewVersionFinderFactory
import com.ofg.uptodate.finder.NewVersionFinderInAllRepositories
import groovy.util.logging.Slf4j
import org.gradle.api.Plugin
Expand Down Expand Up @@ -34,8 +34,8 @@ class UptodatePlugin implements Plugin<Project> {
Task createdTask = project.task(TASK_NAME) << { Task task ->
printMissingJCenterRepoIfApplicable(uptodatePluginExtension, project)
NewVersionFinderInAllRepositories newVersionFinder = new NewVersionFinderInAllRepositories(loggerProxy,
[new MavenNewVersionFinderFactory(loggerProxy).build(uptodatePluginExtension),
new JCenterNewVersionFinderFactory(loggerProxy).build(uptodatePluginExtension)])
[new MavenNewVersionFinderFactory(loggerProxy).create(uptodatePluginExtension),
new JCenterNewVersionFinderFactory(loggerProxy).create(uptodatePluginExtension)])
List<Dependency> dependencies = getDependencies(project)
Set<Dependency> dependenciesWithNewVersions = newVersionFinder.findNewer(dependencies)
newVersionFinder.printDependencies(dependenciesWithNewVersions)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.ofg.uptodate

import com.ofg.uptodate.finder.JCenterRepositorySettingsProvider
import com.ofg.uptodate.finder.MavenRepositorySettingsProvider
import com.ofg.uptodate.finder.jcenter.JCenterNewVersionFinderFactory
import com.ofg.uptodate.finder.maven.MavenNewVersionFinderFactory

class UptodatePluginExtension {

Expand All @@ -23,12 +22,12 @@ class UptodatePluginExtension {
/**
* Link to JCenter repo (or nay other that follows their convention)
*/
String jCenterRepo = JCenterRepositorySettingsProvider.JCENTER_REPO_URL
String jCenterRepo = JCenterNewVersionFinderFactory.JCENTER_REPO_URL

/**
* Link to Maven Central repo (or nay other that follows their convention)
*/
String mavenRepo = MavenRepositorySettingsProvider.MAVEN_CENTRAL_REPO_URL
String mavenRepo = MavenNewVersionFinderFactory.MAVEN_CENTRAL_REPO_URL

/**
* Number of maximal http connections to external repos
Expand Down
2 changes: 0 additions & 2 deletions src/main/groovy/com/ofg/uptodate/finder/Dependency.groovy
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.ofg.uptodate.finder

import groovy.transform.EqualsAndHashCode
import groovy.transform.PackageScope

@PackageScope
@EqualsAndHashCode
class Dependency {
final String group
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.ofg.uptodate.finder

import groovy.transform.Immutable
import groovy.transform.PackageScope

@PackageScope
@Immutable
class HttpConnectionSettings {

String url
ProxySettings proxySettings
int poolSize
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

30 changes: 20 additions & 10 deletions src/main/groovy/com/ofg/uptodate/finder/NewVersionFinder.groovy
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
package com.ofg.uptodate.finder

import com.ofg.uptodate.LoggerProxy
import com.ofg.uptodate.UptodatePluginExtension
import groovy.util.logging.Slf4j

@Slf4j
class NewVersionFinder {

private final LoggerProxy loggerProxy
private final LatestDependenciesProvider latestDependenciesProvider
private final FinderConfiguration finderConfiguration
private final LoggerProxy loggerProxy

NewVersionFinder(LoggerProxy loggerProxy,
RepositorySettingsProvider repositorySettingsProvider,
LatestDependenciesProvider latestDependenciesProvider,
UptodatePluginExtension uptodatePluginExtension) {

finderConfiguration = new FinderConfiguration(repositorySettingsProvider.getFrom(uptodatePluginExtension), uptodatePluginExtension)
this.latestDependenciesProvider = latestDependenciesProvider
NewVersionFinder(LoggerProxy loggerProxy, LatestDependenciesProvider latestDependenciesProvider, FinderConfiguration finderConfiguration) {
this.loggerProxy = loggerProxy
this.latestDependenciesProvider = latestDependenciesProvider
this.finderConfiguration = finderConfiguration
}

List<Dependency> findNewer(List<Dependency> dependencies) {
if (finderConfiguration.ignore || dependencies.empty) {
return []
}

List<Dependency> newerDependencies = latestDependenciesProvider.findLatest(dependencies, finderConfiguration)
List<Dependency> newerDependencies = latestDependenciesProvider.findLatest(dependencies, finderConfiguration).grep(getOnlyNewer).collect {it[1]}
loggerProxy.debug(log, "Newer dependencies found in $finderConfiguration.httpConnectionSettings.url $newerDependencies")
return newerDependencies
}

private Closure<Boolean> getOnlyNewer = { List<Dependency> dependenciesToCompare ->
loggerProxy.debug(log, "${finderConfiguration.httpConnectionSettings.url} - Dependencies to get only newer $dependenciesToCompare")
if (dependenciesToCompare.empty) {
loggerProxy.debug(log, "Dependencies to compare are empty - sth went wrong so no newer version was found")
return false
}
if (!dependenciesToCompare[1].version) {
loggerProxy.debug(log, "The retrieved dependency has null version value thus no newer version was found")
return false
}
boolean fetchedDependencyHasGreaterVersion = dependenciesToCompare[1].version > dependenciesToCompare[0].version
loggerProxy.debug(log, "Fetched dependency has greater version than the current one [$fetchedDependencyHasGreaterVersion]")
return fetchedDependencyHasGreaterVersion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import com.ofg.uptodate.UptodatePluginExtension

interface NewVersionFinderFactory {

NewVersionFinder build(UptodatePluginExtension uptodatePluginExtension)
NewVersionFinder create(UptodatePluginExtension uptodatePluginExtension)
}
4 changes: 1 addition & 3 deletions src/main/groovy/com/ofg/uptodate/finder/ProxySettings.groovy
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.ofg.uptodate.finder

import groovy.transform.Immutable
import groovy.transform.PackageScope

@PackageScope
@Immutable
class ProxySettings {

String hostname
int port
String scheme
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.ofg.uptodate.finder
package com.ofg.uptodate.finder.jcenter
import com.ofg.uptodate.LoggerProxy
import com.ofg.uptodate.finder.Dependency
import com.ofg.uptodate.finder.DependencyVersion
import com.ofg.uptodate.finder.FinderConfiguration
import com.ofg.uptodate.finder.util.HTTPBuilderProvider
import com.ofg.uptodate.finder.LatestDependenciesProvider
import com.ofg.uptodate.finder.util.StringMatcher
import groovy.transform.PackageScope
import groovy.util.logging.Slf4j
import groovy.util.slurpersupport.NodeChild
import groovyx.net.http.HTTPBuilder

import java.util.concurrent.Future

import static com.ofg.uptodate.finder.util.HTTPBuilderProvider.FailureHandlers.logOnlyFailureHandler

@Slf4j
@PackageScope
class JCenterLatestDependenciesProvider implements LatestDependenciesProvider {
Expand All @@ -19,51 +27,29 @@ class JCenterLatestDependenciesProvider implements LatestDependenciesProvider {

@Override
List<Dependency> findLatest(List<Dependency> dependencies, FinderConfiguration finderConfiguration) {
int httpPoolSize = Math.min(dependencies.size(), finderConfiguration.httpConnectionSettings.poolSize)
HTTPBuilder httpBuilder = new HTTPBuilderProvider(finderConfiguration.httpConnectionSettings).withPoolSize(httpPoolSize).get()
HTTPBuilder httpBuilder = new HTTPBuilderProvider(finderConfiguration.httpConnectionSettings).getWithPoolSizeFor(dependencies)
Closure latestFromJCenterGetter = getLatestFromJCenterRepo.curry(httpBuilder, finderConfiguration.excludedVersionPatterns)
return dependencies.collect(latestFromJCenterGetter).collect{it.get()}.grep(getOnlyNewer).collect {it[1]}
return dependencies.collect(latestFromJCenterGetter).collect{it.get()}
}

public Closure<Future> getLatestFromJCenterRepo = {HTTPBuilder httpBuilder, List<String> versionToExcludePatterns, Dependency dependency ->
appendFailureHandling(httpBuilder, dependency.name)
httpBuilder.handler.failure = logOnlyFailureHandler(loggerProxy, log, dependency.name)
httpBuilder.get(path: "/${dependency.group.split('\\.').join('/')}/${dependency.name}/maven-metadata.xml") { resp, xml ->
if(!xml) {
return []
}
def releaseVersionNode = xml.versioning.release
String releaseVersion = releaseVersionNode.text()
DependencyVersion getFirstNonExcludedVersion = getLatestDependencyVersion(releaseVersion, xml, versionToExcludePatterns)
return [dependency, new Dependency(dependency, getFirstNonExcludedVersion)]
return [dependency, new Dependency(dependency, getLatestDependencyVersion(xml.versioning.release.text(), xml, versionToExcludePatterns))]
} as Future
}

private void appendFailureHandling(HTTPBuilder httpBuilder, String dependencyName) {
httpBuilder.handler.failure = { resp ->
loggerProxy.debug(log, "Error with status [$resp.status] occurred while trying to download dependency [$dependencyName]")
return []
}
}

private DependencyVersion getLatestDependencyVersion(String releaseVersion, NodeChild xml, List<String> versionToExcludePatterns) {
if (versionNotMatchesExcludes(versionToExcludePatterns, releaseVersion)) {
if (new StringMatcher(releaseVersion).notMatchesAny(versionToExcludePatterns)) {
return new DependencyVersion(releaseVersion)
}
return xml.versioning.versions.version.findAll { NodeChild version ->
versionNotMatchesExcludes(versionToExcludePatterns, version.text())
new StringMatcher(version.text()).notMatchesAny(versionToExcludePatterns)
}.collect {
NodeChild version -> new DependencyVersion(version.text())
}.max()

}

private boolean versionNotMatchesExcludes(List<String> versionToExcludePatterns, String version) {
return versionToExcludePatterns.every {
!version.matches(it)
}
}

private final Closure<Boolean> getOnlyNewer = { List<Dependency> dependenciesToCompare ->
!dependenciesToCompare.empty && dependenciesToCompare[1].version != null && dependenciesToCompare[1].version > dependenciesToCompare[0].version
}
}

0 comments on commit 0b428e9

Please sign in to comment.