From 4fa253cea26855452a3d108f2c06e9df4375db8e Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Wed, 28 Oct 2015 20:07:44 -0700 Subject: [PATCH] Kurl.IFactory. --- src/main/kotlin/com/beust/kobalt/maven/ArtifactFetcher.kt | 6 +++--- src/main/kotlin/com/beust/kobalt/maven/Kurl.kt | 6 +++++- src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt | 6 +++--- src/main/kotlin/com/beust/kobalt/misc/MainModule.kt | 6 ++---- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/maven/ArtifactFetcher.kt b/src/main/kotlin/com/beust/kobalt/maven/ArtifactFetcher.kt index 9a188ff2..30728a26 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/ArtifactFetcher.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/ArtifactFetcher.kt @@ -42,20 +42,20 @@ class DownloadManager @Inject constructor(val factory: ArtifactFetcher.IFactory) */ class ArtifactFetcher @Inject constructor(@Assisted("url") val url: String, @Assisted("fileName") val fileName: String, - val files: KFiles, val http: Http) : Callable { + val files: KFiles, val urlFactory: Kurl.IFactory) : Callable { interface IFactory { fun create(@Assisted("url") url: String, @Assisted("fileName") fileName: String) : ArtifactFetcher } override fun call() : File { - val k = Kurl(url + ".md5", http) + val k = urlFactory.create(url + ".md5") val remoteMd5 = if (k.exists) k.string.trim(' ', '\t', '\n').substring(0, 32) else null val file = File(fileName) file.parentFile.mkdirs() - Kurl(url, http).toFile(file) + urlFactory.create(url).toFile(file) log(1, " Downloaded $url") if (remoteMd5 != null && remoteMd5 != Md5.toMd5(file)) { diff --git a/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt b/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt index e0a8d6ef..48b1a0e7 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt @@ -10,11 +10,15 @@ import javax.inject.Inject /** * Abstracts a URL so that it works transparently on either http:// or file:// */ -public class Kurl @Inject constructor(@Assisted val url: String, val http: Http) { +class Kurl @Inject constructor(@Assisted val url: String) { val connection : URLConnection by lazy { URL(url).openConnection() } + interface IFactory { + fun create(url: String) : Kurl + } + val exists : Boolean get() { val result = diff --git a/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt b/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt index cf317d2a..4d9aeefe 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt @@ -21,7 +21,7 @@ import kotlin.dom.parseXml * Find the repo that contains the given dependency among a list of repos. Searches are performed in parallel and * cached so we never make a network call for the same dependency more than once. */ -public class RepoFinder @Inject constructor(val http: Http, val executors: KobaltExecutors) { +public class RepoFinder @Inject constructor(val urlFactory: Kurl.IFactory, val executors: KobaltExecutors) { public fun findCorrectRepo(id: String): RepoResult { return FOUND_REPOS.get(id) } @@ -97,12 +97,12 @@ public class RepoFinder @Inject constructor(val http: Http, val executors: Kobal val dep = SimpleDep(groupId, artifactId, packaging, version) // Try to find the jar file val urlJar = repoUrl + dep.toJarFile(dep.version) - val hasJar = Kurl(urlJar, http).exists + val hasJar = urlFactory.create(urlJar).exists val found = if (! hasJar) { // No jar, try to find the directory val url = repoUrl + File(dep.toJarFile(dep.version)).parentFile.path - http.get(url).code == 200 + urlFactory.create(url).exists } else { true } diff --git a/src/main/kotlin/com/beust/kobalt/misc/MainModule.kt b/src/main/kotlin/com/beust/kobalt/misc/MainModule.kt index 40e16bcf..bcb21dfe 100644 --- a/src/main/kotlin/com/beust/kobalt/misc/MainModule.kt +++ b/src/main/kotlin/com/beust/kobalt/misc/MainModule.kt @@ -2,10 +2,7 @@ package com.beust.kobalt.misc import com.beust.kobalt.Args import com.beust.kobalt.kotlin.BuildFileCompiler -import com.beust.kobalt.maven.ArtifactFetcher -import com.beust.kobalt.maven.LocalRepo -import com.beust.kobalt.maven.Pom -import com.beust.kobalt.maven.PomGenerator +import com.beust.kobalt.maven.* import com.beust.kobalt.plugin.publish.JCenterApi import com.google.inject.AbstractModule import com.google.inject.BindingAnnotation @@ -41,6 +38,7 @@ public open class MainModule(val args: Args) : AbstractModule() { JCenterApi.IFactory::class.java, Pom.IFactory::class.java, BuildFileCompiler.IFactory::class.java, + Kurl.IFactory::class.java, ArtifactFetcher.IFactory::class.java) .forEach { install(builder.build(it))