From e87438910fa379b49d33e73ee770f9cc9ad3084a Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Tue, 9 Feb 2016 03:54:30 +0400 Subject: [PATCH] Improve aar support. --- .../com/beust/kobalt/ResolveDependency.kt | 6 +- .../com/beust/kobalt/maven/RepoFinder.kt | 66 ++++++++++++------- .../com/beust/kobalt/maven/SimpleDep.kt | 1 + .../maven/dependency/MavenDependency.kt | 11 ++-- .../com/beust/kobalt/maven/DownloadTest.kt | 5 +- .../com/beust/kobalt/maven/RemoteRepoTest.kt | 23 +++++-- 6 files changed, 72 insertions(+), 40 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt index e169e42a..87ee7c29 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt @@ -4,7 +4,6 @@ import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.maven.LocalRepo import com.beust.kobalt.maven.MavenId import com.beust.kobalt.maven.RepoFinder -import com.beust.kobalt.maven.SimpleDep import com.beust.kobalt.maven.dependency.MavenDependency import com.beust.kobalt.misc.Node import com.beust.kobalt.misc.log @@ -36,9 +35,8 @@ class ResolveDependency @Inject constructor(val repoFinder: RepoFinder, val loca val seen = hashSetOf(id) root.addChildren(findChildren(root, seen)) - val simpleDep = SimpleDep(mavenId) - val url = repoResult.hostConfig.url + simpleDep.toJarFile(repoResult) - val localFile = localRepo.toFullPath(simpleDep.toJarFile(repoResult)) + val url = repoResult.hostConfig.url + repoResult.path + val localFile = localRepo.toFullPath(repoResult.path!!) AsciiArt.logBox(listOf(id, url, localFile).map { " $it" }, {s -> println(s) }) display(root.children) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt index 4acdb496..ebbf0dd5 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt @@ -28,8 +28,12 @@ import javax.xml.xpath.XPathFactory class RepoFinder @Inject constructor(val executors: KobaltExecutors) { fun findCorrectRepo(id: String) = FOUND_REPOS.get(id) - data class RepoResult(val hostConfig: HostConfig, val found: Boolean, val version: Version? = null, - val hasJar: Boolean = true, val snapshotVersion: Version? = null) + data class RepoResult(val hostConfig: HostConfig, val version: Version? = null, + val archiveUrl: String? = null, val snapshotVersion: Version? = null) { + val found = archiveUrl != null + + val path = archiveUrl?.substring(hostConfig.url.length) + } private val FOUND_REPOS: LoadingCache = CacheBuilder.newBuilder() .build(object : CacheLoader() { @@ -69,7 +73,7 @@ class RepoFinder @Inject constructor(val executors: KobaltExecutors) { results.sort({ left, right -> right.version!!.compareTo(left.version!!) }) return results[0] } else { - return RepoResult(HostConfig(""), false, Version.of(id)) + return RepoResult(HostConfig(""), Version.of(id)) } } @@ -88,11 +92,12 @@ class RepoFinder @Inject constructor(val executors: KobaltExecutors) { if (mavenId.version == null) { val ud = UnversionedDep(groupId, artifactId) val isLocal = repoUrl.startsWith(FileDependency.PREFIX_FILE) - val foundVersion = findCorrectVersionRelease(ud.toMetadataXmlPath(false, isLocal), repoUrl) + val path = ud.toMetadataXmlPath(false, isLocal) + val foundVersion = findCorrectVersionRelease(path, repoUrl) if (foundVersion != null) { - return RepoResult(repo, true, Version.of(foundVersion)) + return RepoResult(repo, Version.of(foundVersion), path) } else { - return RepoResult(repo, false) + return RepoResult(repo) } } else { val version = Version.of(mavenId.version) @@ -107,35 +112,48 @@ class RepoFinder @Inject constructor(val executors: KobaltExecutors) { val url = repoUrl + metadataXmlPath val kurl = Kurl(HostConfig(url)) val found = kurl.exists - return RepoResult(repo, found, version, true /* hasJar, potential bug here */, - snapshotVersion) + return RepoResult(repo, version, url, snapshotVersion) } else { - return RepoResult(repo, false) + return RepoResult(repo) } } else if (version.isRangedVersion() ) { val foundVersion = findRangedVersion(SimpleDep(mavenId), repoUrl) if (foundVersion != null) { - return RepoResult(repo, true, foundVersion) + return RepoResult(repo, foundVersion) } else { - return RepoResult(repo, false) + return RepoResult(repo) } } else { val dep = SimpleDep(mavenId) // Try to find the jar file - val urlJar = repo.copy(url = repo.url + dep.toJarFile(dep.version)) - val hasJar = Kurl(urlJar).exists - val found = - if (! hasJar) { - // No jar, try to find the directory - val url = repo.copy(url = repoUrl - + File(dep.toJarFile(dep.version)).parentFile.path.replace("\\", "/")) - Kurl(url).exists - } else { - true - } - log(2, "Result for $repoUrl for $id: $found") - return RepoResult(repo, found, Version.of(dep.version), hasJar) + val attemptPaths = listOf(dep.toJarFile(dep.version), dep.toAarFile(dep.version)) + val attemptUrls = attemptPaths.map { repo.copy(url = repo.url + it )} + + attemptPaths.map { repo.copy(url = repo.url + File(it).parentFile.path.replace("\\", "/")) } + + val firstFound = attemptUrls.map { Kurl(it)}.firstOrNull { it.exists } +// val urlJar = repo.copy(url = repo.url + dep.toJarFile(dep.version)) +// var foundPath = "" +// if (Kurl(urlJar).exists) { +// foundPath = dep.toJarFile(dep.version) +// } else { +// val urlAar = repo.copy(url = repo.url + dep.toAarFile(dep.version)) +// if (Kurl(urlAar).exists) { +// foundPath = dep.toAarFile(dep.version) +// } +// } +// val hasJar = true +// val found = +// if (! hasJar) { +// // No jar, try to find the directory +// val url = repo.copy(url = repoUrl +// + File(dep.toJarFile(dep.version)).parentFile.path.replace("\\", "/")) +// Kurl(url).exists +// } else { +// true +// } + log(2, "Result for $repoUrl for $id: $firstFound") + return RepoResult(repo, Version.of(dep.version), firstFound?.hostInfo?.url) } } } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/SimpleDep.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/SimpleDep.kt index 5f544674..6f0e25ae 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/SimpleDep.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/SimpleDep.kt @@ -27,6 +27,7 @@ open class SimpleDep(open val mavenId: MavenId) : UnversionedDep(mavenId.groupId fun toPomFile(r: RepoFinder.RepoResult) = toFile(r.snapshotVersion ?: r.version!!, ".pom") fun toJarFile(v: String = version) = toFile(Version.of(v), suffix) + fun toAarFile(v: String = version) = toFile(Version.of(v), ".aar") fun toPomFileName() = "$artifactId-$version.pom" diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt index f5c83cd1..66a93f19 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt @@ -28,17 +28,18 @@ public class MavenDependency @Inject constructor(mavenId: MavenId, init { val jar = File(localRepo.toFullPath(toJarFile(version))) + val aar = File(localRepo.toFullPath(toAarFile(version))) val pom = File(localRepo.toFullPath(toPomFile(version))) - if (pom.exists()) { - jarFile = CompletedFuture(jar) + if (pom.exists() && (jar.exists() || aar.exists())) { + jarFile = CompletedFuture(if (jar.exists()) jar else aar) pomFile = CompletedFuture(pom) } else { val repoResult = repoFinder.findCorrectRepo(mavenId.toId) if (repoResult.found) { + val path = if (jar.exists()) jar.absolutePath else aar.absolutePath jarFile = - if (repoResult.hasJar) { - downloadManager.download(HostConfig(url = repoResult.hostConfig.url + toJarFile(repoResult)), - jar.absolutePath, executor) + if (repoResult.archiveUrl != null) { + downloadManager.download(HostConfig(url = repoResult.archiveUrl), path, executor) } else { CompletedFuture(File("nonexistentFile")) // will be filtered out } diff --git a/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt b/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt index 902f7312..3922c0d4 100644 --- a/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt +++ b/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt @@ -120,8 +120,9 @@ public class DownloadTest @Inject constructor( // For now, just hardcoding the result we should have received // val repoResult = repoFinder.findCorrectRepo(id) - val repoResult = RepoFinder.RepoResult(HostConfig("http://repository.jetbrains.com/all/"), - true, Version.of("0.1-SNAPSHOT"), true, Version("0.1-SNAPSHOT", "20151011.112011-29")) + val hc = HostConfig("http://repository.jetbrains.com/all/") + val repoResult = RepoFinder.RepoResult(hc, + Version.of("0.1-SNAPSHOT"), hc.url, Version("0.1-SNAPSHOT", "20151011.112011-29")) val jarFile = dep.toJarFile(repoResult) val url = repoResult.hostConfig.url + jarFile diff --git a/src/test/kotlin/com/beust/kobalt/maven/RemoteRepoTest.kt b/src/test/kotlin/com/beust/kobalt/maven/RemoteRepoTest.kt index 723b7a65..69966069 100644 --- a/src/test/kotlin/com/beust/kobalt/maven/RemoteRepoTest.kt +++ b/src/test/kotlin/com/beust/kobalt/maven/RemoteRepoTest.kt @@ -4,19 +4,18 @@ import com.beust.kobalt.Args import com.beust.kobalt.TestModule import com.beust.kobalt.maven.dependency.MavenDependency import com.beust.kobalt.misc.DependencyExecutor -import com.beust.kobalt.app.MainModule -import com.google.inject.Guice import org.testng.Assert import org.testng.annotations.Test import java.util.concurrent.ExecutorService import javax.inject.Inject +@Test @org.testng.annotations.Guice(modules = arrayOf(TestModule::class)) -public class RemoteRepoTest @Inject constructor(val repoFinder: RepoFinder, +class RemoteRepoTest @Inject constructor(val repoFinder: RepoFinder, @DependencyExecutor val executor: ExecutorService, val args: Args){ @Test - public fun mavenMetadata() { + fun mavenMetadata() { val dep = MavenDependency.create("org.codehaus.groovy:groovy-all:") // Note: this test might fail if a new version of Groovy gets uploaded, need // to find a stable (i.e. abandoned) package @@ -24,9 +23,23 @@ public class RemoteRepoTest @Inject constructor(val repoFinder: RepoFinder, } @Test(enabled = false) - public fun metadataForSnapshots() { + fun metadataForSnapshots() { val jar = MavenDependency.create("org.apache.maven.wagon:wagon-provider-test:2.10-SNAPSHOT", executor) .jarFile Assert.assertTrue(jar.get().exists()) } + + fun resolveAarWithVersion() { + val repoResult = repoFinder.findCorrectRepo("com.jakewharton.timber:timber:4.1.0") + with(repoResult) { + Assert.assertEquals(path, "com/jakewharton/timber/timber/4.1.0/timber-4.1.0.aar") + } + } + +// fun resolveAarWithoutVersion() { +// val repoResult = repoFinder.findCorrectRepo("com.jakewharton.timber:timber:") +// with(repoResult) { +// Assert.assertEquals(path, "com/jakewharton/timber/timber/4.1.0/timber-4.1.0.aar") +// } +// } }