From a010904b8e5c251497724f3c242925cb68acfb37 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Thu, 3 Dec 2015 01:37:48 -0800 Subject: [PATCH] Read credentials in local.properties. --- .../kotlin/com/beust/kobalt/BuildScript.kt | 16 +++----- .../com/beust/kobalt/ResolveDependency.kt | 2 +- .../com/beust/kobalt/api/IRepoContributor.kt | 4 +- .../kotlin/com/beust/kobalt/api/Kobalt.kt | 6 +-- .../com/beust/kobalt/maven/ArtifactFetcher.kt | 10 ++--- .../kotlin/com/beust/kobalt/maven/Kurl.kt | 37 +++++++++++++++---- .../com/beust/kobalt/maven/RepoFinder.kt | 10 ++--- .../maven/dependency/MavenDependency.kt | 6 +-- .../kobalt/plugin/android/AndroidPlugin.kt | 4 +- 9 files changed, 56 insertions(+), 39 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/BuildScript.kt b/src/main/kotlin/com/beust/kobalt/BuildScript.kt index 585dc995..726f3b94 100644 --- a/src/main/kotlin/com/beust/kobalt/BuildScript.kt +++ b/src/main/kotlin/com/beust/kobalt/BuildScript.kt @@ -29,30 +29,24 @@ fun plugins(vararg dependencies : String) { } } -data class HostInfo(val url: String, var keyUsername: String? = null, var keyPassword: String? = null) { +data class HostConfig(var url: String = "", var username: String? = null, var password: String? = null) { fun hasAuth() : Boolean { - return (! keyUsername.isNullOrBlank()) && (! keyPassword.isNullOrBlank()) + return (! username.isNullOrBlank()) && (! password.isNullOrBlank()) } } @Directive fun repos(vararg repos : String) { - repos.forEach { Kobalt.addRepo(HostInfo(it)) } + repos.forEach { Kobalt.addRepo(HostConfig(it)) } } @Directive -fun repos(vararg repos : HostInfo) { +fun authRepos(vararg repos : HostConfig) { repos.forEach { Kobalt.addRepo(it) } } -class HostConfig(var keyUsername: String? = null, var keyPassword: String? = null) - @Directive -fun authRepo(url: String, init: HostConfig.() -> Unit) : HostInfo { - val r = HostConfig() - r.init() - return HostInfo(url, r.keyUsername, r.keyPassword) -} +fun authRepo(init: HostConfig.() -> Unit) = HostConfig().apply { init() } @Directive fun glob(g: String) : IFileSpec.Glob = IFileSpec.Glob(g) diff --git a/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt b/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt index 2b40cf75..876902c7 100644 --- a/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt +++ b/src/main/kotlin/com/beust/kobalt/ResolveDependency.kt @@ -31,7 +31,7 @@ class ResolveDependency @Inject constructor(val repoFinder: RepoFinder) { val repoResult = repoFinder.findCorrectRepo(id) val simpleDep = SimpleDep(MavenId(id)) - val url = repoResult.repoHostInfo.url + simpleDep.toJarFile(repoResult) + val url = repoResult.hostConfig.url + simpleDep.toJarFile(repoResult) AsciiArt.logBox(listOf(id, url).map { " $it" }, {s -> println(s) }) display(root.children) diff --git a/src/main/kotlin/com/beust/kobalt/api/IRepoContributor.kt b/src/main/kotlin/com/beust/kobalt/api/IRepoContributor.kt index 6057e0ed..0df2baee 100644 --- a/src/main/kotlin/com/beust/kobalt/api/IRepoContributor.kt +++ b/src/main/kotlin/com/beust/kobalt/api/IRepoContributor.kt @@ -1,6 +1,6 @@ package com.beust.kobalt.api -import com.beust.kobalt.HostInfo +import com.beust.kobalt.HostConfig /** * Plugins that add their own repos. @@ -11,6 +11,6 @@ interface IRepoContributor : IContributor { * before the build file gets parsed (so we don't have any projects yet) and after the * build file has been parsed (then it gets called once for each project discovered). */ - fun reposFor(project: Project?) : List + fun reposFor(project: Project?) : List } diff --git a/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt b/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt index 14089070..853621d8 100644 --- a/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt +++ b/src/main/kotlin/com/beust/kobalt/api/Kobalt.kt @@ -1,8 +1,8 @@ package com.beust.kobalt.api import com.beust.kobalt.Args +import com.beust.kobalt.HostConfig import com.beust.kobalt.Plugins -import com.beust.kobalt.HostInfo import com.beust.kobalt.misc.MainModule import com.google.inject.Guice import com.google.inject.Injector @@ -24,9 +24,9 @@ public class Kobalt { "https://jcenter.bintray.com/" ) - val repos = HashSet(DEFAULT_REPOS.map { HostInfo(it) }) + val repos = HashSet(DEFAULT_REPOS.map { HostConfig(it) }) - fun addRepo(repo: HostInfo) = repos.add( + fun addRepo(repo: HostConfig) = repos.add( if (repo.url.endsWith("/")) repo else repo.copy(url = (repo.url + "/"))) diff --git a/src/main/kotlin/com/beust/kobalt/maven/ArtifactFetcher.kt b/src/main/kotlin/com/beust/kobalt/maven/ArtifactFetcher.kt index 3f0585a6..e067b67d 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/ArtifactFetcher.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/ArtifactFetcher.kt @@ -1,6 +1,6 @@ package com.beust.kobalt.maven -import com.beust.kobalt.HostInfo +import com.beust.kobalt.HostConfig import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.log import com.beust.kobalt.misc.warn @@ -20,7 +20,7 @@ import javax.inject.Singleton @Singleton class DownloadManager @Inject constructor(val factory: ArtifactFetcher.IFactory) { - class Key(val hostInfo: HostInfo, val fileName: String, val executor: ExecutorService) { + class Key(val hostInfo: HostConfig, val fileName: String, val executor: ExecutorService) { override fun equals(other: Any?): Boolean { return (other as Key).hostInfo.url == hostInfo.url } @@ -37,18 +37,18 @@ class DownloadManager @Inject constructor(val factory: ArtifactFetcher.IFactory) } }) - fun download(hostInfo: HostInfo, fileName: String, executor: ExecutorService) + fun download(hostInfo: HostConfig, fileName: String, executor: ExecutorService) : Future = CACHE.get(Key(hostInfo, fileName, executor)) } /** * Fetches an artifact (a file in a Maven repo, .jar, -javadoc.jar, ...) to the given local file. */ -class ArtifactFetcher @Inject constructor(@Assisted("hostInfo") val hostInfo: HostInfo, +class ArtifactFetcher @Inject constructor(@Assisted("hostInfo") val hostInfo: HostConfig, @Assisted("fileName") val fileName: String, val files: KFiles) : Callable { interface IFactory { - fun create(@Assisted("hostInfo") hostInfo: HostInfo, @Assisted("fileName") fileName: String) : ArtifactFetcher + fun create(@Assisted("hostInfo") hostInfo: HostConfig, @Assisted("fileName") fileName: String) : ArtifactFetcher } override fun call() : File { diff --git a/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt b/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt index c91061a9..b7922bac 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt @@ -1,7 +1,9 @@ package com.beust.kobalt.maven -import com.beust.kobalt.HostInfo +import com.beust.kobalt.HostConfig +import com.beust.kobalt.KobaltException import com.beust.kobalt.maven.dependency.FileDependency +import com.beust.kobalt.misc.LocalProperties import java.io.* import java.net.HttpURLConnection import java.net.URL @@ -11,14 +13,38 @@ import java.util.* /** * Abstracts a URL so that it works transparently on either http:// or file:// */ -class Kurl(val hostInfo: HostInfo) { -// constructor(url: String) : this(HostInfo(url)) +class Kurl(val hostInfo: HostConfig) { + companion object { + const val KEY = "authUrl" + const val VALUE_USER = "username" + const val VALUE_PASSWORD = "password" + } + + init { + // See if the URL needs to be authenticated. Look in local.properties for keys + // of the format authUrl..user=xxx and authUrl..password=xxx + val properties = LocalProperties().localProperties + val host = java.net.URL(hostInfo.url).host + properties.entries.forEach { + val key = it.key.toString() + if (key == "$KEY.$host.$VALUE_USER") { + hostInfo.username = properties.getProperty(key) + } else if (key == "$KEY.$host.$VALUE_PASSWORD") { + hostInfo.password = properties.getProperty(key) + } + } + if (! hostInfo.username.isNullOrBlank() && hostInfo.password.isNullOrBlank()) { + throw KobaltException("Found \"username\" but not \"password\" in local.properties for $KEY.$host") + } else if(hostInfo.username.isNullOrBlank() && ! hostInfo.password.isNullOrBlank()) { + throw KobaltException("Found \"password\" but not \"username\" in local.properties for $KEY.$host") + } + } val connection : URLConnection get() { val result = URL(hostInfo.url).openConnection() if (hostInfo.hasAuth()) { - val userPass = hostInfo.keyUsername + ":" + hostInfo.keyPassword + val userPass = hostInfo.username + ":" + hostInfo.password val basicAuth = "Basic " + String(Base64.getEncoder().encode(userPass.toByteArray())) result.setRequestProperty("Authorization", basicAuth) } @@ -31,9 +57,6 @@ class Kurl(val hostInfo: HostInfo) { val exists : Boolean get() { - if (hostInfo.url.contains("localhost")) { - println("DONOTCOMMIT") - } val url = hostInfo.url val result = if (connection is HttpURLConnection) { diff --git a/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt b/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt index 82b506ed..666c2f9e 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt @@ -1,6 +1,6 @@ package com.beust.kobalt.maven -import com.beust.kobalt.HostInfo +import com.beust.kobalt.HostConfig import com.beust.kobalt.api.Kobalt import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.Strings @@ -27,7 +27,7 @@ public class RepoFinder @Inject constructor(val executors: KobaltExecutors) { return FOUND_REPOS.get(id) } - data class RepoResult(val repoHostInfo: HostInfo, val found: Boolean, val version: String, + data class RepoResult(val hostConfig: HostConfig, val found: Boolean, val version: String, val hasJar: Boolean = true, val snapshotVersion: String = "") private val FOUND_REPOS: LoadingCache = CacheBuilder.newBuilder() @@ -51,14 +51,14 @@ public class RepoFinder @Inject constructor(val executors: KobaltExecutors) { val result = cs.take().get(2000, TimeUnit.MILLISECONDS) log(2, "Result for repo #$i: $result") if (result.found) { - log(2, "Located $id in ${result.repoHostInfo.url}") + log(2, "Located $id in ${result.hostConfig.url}") return result } } catch(ex: Exception) { warn("Error: $ex") } } - return RepoResult(HostInfo(""), false, id) + return RepoResult(HostConfig(""), false, id) } finally { executor.shutdownNow() } @@ -67,7 +67,7 @@ public class RepoFinder @Inject constructor(val executors: KobaltExecutors) { /** * Execute a single HTTP request to one repo. */ - inner class RepoFinderCallable(val id: String, val repo: HostInfo) : Callable { + inner class RepoFinderCallable(val id: String, val repo: HostConfig) : Callable { override fun call(): RepoResult { val repoUrl = repo.url log(2, "Checking $repoUrl for $id") diff --git a/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt b/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt index 5d932162..5b150366 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/dependency/MavenDependency.kt @@ -1,6 +1,6 @@ package com.beust.kobalt.maven.dependency -import com.beust.kobalt.HostInfo +import com.beust.kobalt.HostConfig import com.beust.kobalt.KobaltException import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.api.Kobalt @@ -37,12 +37,12 @@ public class MavenDependency @Inject constructor(mavenId: MavenId, if (repoResult.found) { jarFile = if (repoResult.hasJar) { - downloadManager.download(HostInfo(url = repoResult.repoHostInfo.url + toJarFile(repoResult)), + downloadManager.download(HostConfig(url = repoResult.hostConfig.url + toJarFile(repoResult)), jar.absolutePath, executor) } else { CompletedFuture(File("nonexistentFile")) // will be filtered out } - pomFile = downloadManager.download(HostInfo(url = repoResult.repoHostInfo.url + toPomFile(repoResult)), + pomFile = downloadManager.download(HostConfig(url = repoResult.hostConfig.url + toPomFile(repoResult)), pom.absolutePath, executor) } else { throw KobaltException("Couldn't resolve ${mavenId.toId}") diff --git a/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt index 6e8ed8b2..bbe6d737 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/android/AndroidPlugin.kt @@ -351,11 +351,11 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v } // IRepoContributor - override fun reposFor(project: Project?): List { + override fun reposFor(project: Project?): List { val home = androidHomeNoThrows(project) return if (home != null) { val path = Paths.get(KFiles.joinDir(home, "extras", "android", "m2repository")) - listOf(HostInfo(path.toUri().toString())) + listOf(HostConfig(path.toUri().toString())) } else { emptyList() }