1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-27 00:38:11 -07:00

Convert to Kurl.

This commit is contained in:
Cedric Beust 2015-10-28 05:59:21 -07:00
parent 4ca1af597e
commit a3d4558dd7
6 changed files with 99 additions and 48 deletions

View file

@ -53,23 +53,14 @@ class ArtifactFetcher @Inject constructor(@Assisted("url") val url: String,
private fun getBytes(url: String) : ByteArray { private fun getBytes(url: String) : ByteArray {
log(2, "$url: downloading to $fileName") log(2, "$url: downloading to $fileName")
val body = http.get(url) return Kurl(url, http).bytes
if (body.code == 200) {
val buffer = ByteArrayOutputStream(estimatedSize)
body.getAsStream().copyTo(buffer, estimatedSize)
return buffer.toByteArray()
} else {
throw KobaltException("$url: failed to download, code: ${body.code}")
}
} }
override fun call() : File { override fun call() : File {
val md5Body = http.get(url + ".md5") val k = Kurl(url + ".md5", http)
val remoteMd5 = if (md5Body.code == 200) { val remoteMd5 =
md5Body.getAsString().trim(' ', '\t', '\n').substring(0, 32) if (k.exists) k.string.trim(' ', '\t', '\n').substring(0, 32)
} else { else null
null
}
val file = File(fileName) val file = File(fileName)
file.parentFile.mkdirs() file.parentFile.mkdirs()

View file

@ -8,7 +8,7 @@ import java.util.concurrent.Future
interface IClasspathDependency { interface IClasspathDependency {
companion object { companion object {
val PREFIX_FILE: String = "file:/" val PREFIX_FILE: String = "file://"
} }
/** Identifier for this dependency */ /** Identifier for this dependency */

View file

@ -0,0 +1,65 @@
package com.beust.kobalt.maven
import com.google.common.io.ByteStreams
import com.google.inject.assistedinject.Assisted
import java.io.*
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLConnection
import javax.inject.Inject
import kotlin.properties.Delegates
/**
* 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) {
val connection : URLConnection by lazy {
URL(url).openConnection()
}
val exists : Boolean
get() {
if (url.contains("android")) {
println("DONOTCOMMIT")
}
val result =
if (connection is HttpURLConnection) {
(connection as HttpURLConnection).responseCode == 200
} else if (url.startsWith(IClasspathDependency.PREFIX_FILE)) {
val fileName = url.substring(IClasspathDependency.PREFIX_FILE.length)
File(fileName).exists()
} else {
false
}
return result
}
val estimatedSize = 18000000
val bytes : ByteArray
get() {
val buffer = ByteArrayOutputStream(estimatedSize)
ByteStreams.copy(connection.inputStream, buffer)
return buffer.toByteArray()
}
val string: String
get() {
val sb = StringBuilder()
connection.inputStream.let { inputStream ->
val reader = BufferedReader(InputStreamReader(inputStream))
var line: String? = reader.readLine()
try {
while (line != null) {
sb.append(line).append('\n')
line = reader.readLine()
}
} finally {
inputStream.close()
}
}
return sb.toString()
}
}

View file

@ -31,7 +31,7 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v
jarFile = CompletedFuture(jar) jarFile = CompletedFuture(jar)
pomFile = CompletedFuture(pom) pomFile = CompletedFuture(pom)
} else { } else {
val repoResult = repoFinder.findCorrectRepo(toId(groupId, artifactId, packaging, version)) val repoResult = repoFinder.findCorrectRepo(MavenId.toId(groupId, artifactId, packaging, version))
if (repoResult.found) { if (repoResult.found) {
jarFile = jarFile =
if (repoResult.hasJar) { if (repoResult.hasJar) {
@ -42,7 +42,8 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v
pomFile = downloadManager.download(repoResult.repoUrl + toPomFile(repoResult), pom.absolutePath, pomFile = downloadManager.download(repoResult.repoUrl + toPomFile(repoResult), pom.absolutePath,
executor) executor)
} else { } else {
throw KobaltException("Couldn't resolve ${toId(groupId, artifactId, packaging, version)}") throw KobaltException("Couldn't resolve " +
"${MavenId.toId(groupId, artifactId, packaging, version)}")
} }
} }
} }
@ -62,15 +63,15 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v
return depFactory.create(id, ex) return depFactory.create(id, ex)
} }
fun toId(g: String, a: String, packaging: String?, v: String) = fun _toId(g: String, a: String, packaging: String?, v: String) =
if (packaging.isNullOrBlank()) "$g:$a:$v" if (packaging.isNullOrBlank()) "$g:$a:$v"
else "$g:$a:$packaging:$v" else "$g:$a:$packaging:$v"
} }
public override fun toString() = toId(groupId, artifactId, packaging, version) public override fun toString() = MavenId.toId(groupId, artifactId, packaging, version)
override val id = toId(groupId, artifactId, packaging, version) override val id = MavenId.toId(groupId, artifactId, packaging, version)
override fun toMavenDependencies(): org.apache.maven.model.Dependency { override fun toMavenDependencies(): org.apache.maven.model.Dependency {
with(org.apache.maven.model.Dependency()) { with(org.apache.maven.model.Dependency()) {
@ -89,10 +90,14 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v
override fun directDependencies() : List<IClasspathDependency> { override fun directDependencies() : List<IClasspathDependency> {
val result = arrayListOf<IClasspathDependency>() val result = arrayListOf<IClasspathDependency>()
pomFactory.create(id, pomFile.get()).dependencies.filter { try {
it.mustDownload && it.isValid pomFactory.create(id, pomFile.get()).dependencies.filter {
}.forEach { it.mustDownload && it.isValid
result.add(create(toId(it.groupId, it.artifactId, it.packaging, it.version))) }.forEach {
result.add(create(MavenId.toId(it.groupId, it.artifactId, it.packaging, it.version)))
}
} catch(ex: Exception) {
warn("Exception when trying to resolve dependencies for $id: " + ex.message)
} }
return result return result
} }

View file

@ -6,6 +6,16 @@ public class MavenId(val id: String) {
var packaging: String? = null var packaging: String? = null
var version: String? = null var version: String? = null
companion object {
fun create(groupId: String, artifactId: String, packaging: String?, version: String?) =
MavenId(toId(groupId, artifactId, packaging, version))
fun toId(groupId: String, artifactId: String, packaging: String? = null, version: String?) =
"$groupId:$artifactId" +
(if (packaging != null) ":$packaging" else "") +
":$version"
}
init { init {
val c = id.split(":") val c = id.split(":")
if (c.size != 3 && c.size != 4) { if (c.size != 3 && c.size != 4) {
@ -18,4 +28,7 @@ public class MavenId(val id: String) {
} }
val hasVersion = version != null val hasVersion = version != null
val toId = MavenId.toId(groupId, artifactId, packaging, version)
} }

View file

@ -9,9 +9,6 @@ import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader import com.google.common.cache.CacheLoader
import com.google.common.cache.LoadingCache import com.google.common.cache.LoadingCache
import java.io.File import java.io.File
import java.net.HttpURLConnection
import java.net.URI
import java.net.URL
import java.util.concurrent.Callable import java.util.concurrent.Callable
import java.util.concurrent.ExecutorCompletionService import java.util.concurrent.ExecutorCompletionService
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -66,20 +63,6 @@ public class RepoFinder @Inject constructor(val http: Http, val executors: Kobal
} }
} }
private fun urlExists(url: String) : Boolean {
val connection = URL(url).openConnection()
val result =
if (connection is HttpURLConnection) {
connection.responseCode == 200
} else if (url.startsWith(IClasspathDependency.PREFIX_FILE)) {
val fileName = url.substring(IClasspathDependency.PREFIX_FILE.length)
File(fileName).exists()
} else {
false
}
return result
}
/** /**
* Execute a single HTTP request to one repo. * Execute a single HTTP request to one repo.
*/ */
@ -114,13 +97,7 @@ public class RepoFinder @Inject constructor(val http: Http, val executors: Kobal
val dep = SimpleDep(groupId, artifactId, packaging, version) val dep = SimpleDep(groupId, artifactId, packaging, version)
// Try to find the jar file // Try to find the jar file
val urlJar = repoUrl + dep.toJarFile(dep.version) val urlJar = repoUrl + dep.toJarFile(dep.version)
val hasJar = Kurl(urlJar, http).exists
if (repoUrl.contains("beust")) {
println("DONOTCOMMIT")
}
val hasJar = urlExists(urlJar)
val found = val found =
if (! hasJar) { if (! hasJar) {
// No jar, try to find the directory // No jar, try to find the directory