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

Commit

Permalink
[#21] Introduced some abstractions and did some refactoring in the at…
Browse files Browse the repository at this point in the history
…tempt to reduce code duplication
  • Loading branch information
Catalin Buleandra committed Feb 28, 2015
1 parent 4f9038d commit df7b739
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 217 deletions.
23 changes: 14 additions & 9 deletions src/main/groovy/com/ofg/uptodate/UptodatePlugin.groovy
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.ofg.uptodate
import com.ofg.uptodate.finder.Dependency
import com.ofg.uptodate.finder.JCenterNewVersionFinder
import com.ofg.uptodate.finder.MavenNewVersionFinder
import com.ofg.uptodate.finder.NewVersionFinderInAllRepositories

import com.ofg.uptodate.finder.*
import groovy.util.logging.Slf4j
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -11,6 +9,9 @@ import org.gradle.api.artifacts.Configuration

import javax.inject.Inject

import static com.ofg.uptodate.finder.NewVersionFinder.jCenterNewVersionFinder
import static com.ofg.uptodate.finder.NewVersionFinder.mavenNewVersionFinder

@Slf4j
class UptodatePlugin implements Plugin<Project> {
public static final String TASK_NAME = 'uptodate'
Expand All @@ -29,10 +30,14 @@ class UptodatePlugin implements Plugin<Project> {
@Override
void apply(Project project) {
project.extensions.create(TASK_NAME, UptodatePluginExtension)
UptodatePluginExtension uptodatePluginExtension = project.extensions.uptodate
UptodatePluginExtension uptodatePluginExtension = project.extensions.uptodate
Task createdTask = project.task(TASK_NAME) << { Task task ->
printMissingJCenterRepoIfApplicable(uptodatePluginExtension, project)
NewVersionFinderInAllRepositories newVersionFinder = new NewVersionFinderInAllRepositories(loggerProxy, [new MavenNewVersionFinder(loggerProxy, uptodatePluginExtension), new JCenterNewVersionFinder(loggerProxy, uptodatePluginExtension)])
NewVersionFinderInAllRepositories newVersionFinder = new NewVersionFinderInAllRepositories(loggerProxy,
[
mavenNewVersionFinder(loggerProxy, uptodatePluginExtension),
jCenterNewVersionFinder(loggerProxy, uptodatePluginExtension)
])
List<Dependency> dependencies = getDependencies(project)
Set<Dependency> dependenciesWithNewVersions = newVersionFinder.findNewer(dependencies)
newVersionFinder.printDependencies(dependenciesWithNewVersions)
Expand Down Expand Up @@ -68,9 +73,9 @@ class UptodatePlugin implements Plugin<Project> {
private List<Dependency> getDependencies(Set<Configuration> configurations) {
log.debug("Getting dependencies for configurations [$configurations]")
return configurations.collectNested { conf ->
conf.dependencies.findAll{ dep -> dep.name && dep.group && dep.version }.collect { dep ->
log.debug("Collecting dependency with group: [$dep.group] name: [$dep.name] and version: [$dep.version]")
new Dependency(dep.group, dep.name, dep.version)
conf.dependencies.findAll { dep -> dep.name && dep.group && dep.version }.collect { dep ->
log.debug("Collecting dependency with group: [$dep.group] name: [$dep.name] and version: [$dep.version]")
new Dependency(dep.group, dep.name, dep.version)
}
}.flatten().unique()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ofg.uptodate
import com.ofg.uptodate.finder.JCenterNewVersionFinder
import com.ofg.uptodate.finder.MavenNewVersionFinder

import com.ofg.uptodate.finder.JCenterRepositorySettingsProvider
import com.ofg.uptodate.finder.MavenRepositorySettingsProvider

class UptodatePluginExtension {

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

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

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

import groovy.transform.PackageScope

@PackageScope
class FinderConfiguration {

boolean ignore
HttpConnectionSettings httpConnectionSettings
List<String> excludedVersionPatterns
}
37 changes: 37 additions & 0 deletions src/main/groovy/com/ofg/uptodate/finder/HTTPBuilderProvider.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.ofg.uptodate.finder

import groovyx.net.http.AsyncHTTPBuilder
import groovyx.net.http.HTTPBuilder

class HTTPBuilderProvider {

private final HttpConnectionSettings httpConnectionSettings
private int poolSize

HTTPBuilderProvider(HttpConnectionSettings httpConnectionSettings) {
this.httpConnectionSettings = httpConnectionSettings
}

HTTPBuilderProvider withPoolSize(int poolSize) {
this.poolSize = poolSize
return this
}

HTTPBuilder get() {
HTTPBuilder httpBuilder = new AsyncHTTPBuilder(
uri: httpConnectionSettings.url,
timeout: httpConnectionSettings.timeout,
poolSize: poolSize
)
return configureProxySettingsIfApplicable(httpBuilder)
}

private HTTPBuilder configureProxySettingsIfApplicable(HTTPBuilder httpBuilder) {
httpConnectionSettings.proxySettings.with {
if (hostname) {
httpBuilder.setProxy(hostname, port, scheme)
}
}
return httpBuilder
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ofg.uptodate.finder

import groovy.transform.PackageScope

@PackageScope
class HttpConnectionSettings {

String url
ProxySettings proxySettings
int poolSize
int timeout
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.ofg.uptodate.finder
import com.ofg.uptodate.LoggerProxy
import groovy.transform.PackageScope
import groovy.util.logging.Slf4j
import groovy.util.slurpersupport.NodeChild
import groovyx.net.http.HTTPBuilder

import java.util.concurrent.Future

@Slf4j
@PackageScope
class JCenterLatestDependenciesProvider implements LatestDependenciesProvider {

private final LoggerProxy loggerProxy

JCenterLatestDependenciesProvider(LoggerProxy loggerProxy) {
this.loggerProxy = loggerProxy
}

@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()
Closure latestFromJCenterGetter = getLatestFromJCenterRepo.curry(httpBuilder, finderConfiguration.excludedVersionPatterns)
return dependencies.collect(latestFromJCenterGetter).collect{it.get()}.grep(getOnlyNewer).collect {it[1]}
}

public Closure<Future> getLatestFromJCenterRepo = {HTTPBuilder httpBuilder, List<String> versionToExcludePatterns, Dependency dependency ->
appendFailureHandling(httpBuilder, 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)]
} 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)) {
return new DependencyVersion(releaseVersion)
}
return xml.versioning.versions.version.findAll { NodeChild version ->
versionNotMatchesExcludes(versionToExcludePatterns, version.text())
}.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
}
}
105 changes: 0 additions & 105 deletions src/main/groovy/com/ofg/uptodate/finder/JCenterNewVersionFinder.groovy

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ofg.uptodate.finder
import com.ofg.uptodate.UptodatePluginExtension
import groovy.transform.PackageScope

@PackageScope
class JCenterRepositorySettingsProvider implements RepositorySettingsProvider {

public static final String JCENTER_REPO_URL = "http://jcenter.bintray.com/"

@Override
RepositorySettings getFrom(UptodatePluginExtension uptodatePluginExtension) {
return new RepositorySettings(
repoUrl: uptodatePluginExtension.jCenterRepo,
ignoreRepo: uptodatePluginExtension.ignoreJCenter
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ofg.uptodate.finder

interface LatestDependenciesProvider {

List<Dependency> findLatest(List<Dependency> dependencies, FinderConfiguration finderConfiguration)
}
Loading

0 comments on commit df7b739

Please sign in to comment.