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

Read credentials in local.properties.

This commit is contained in:
Cedric Beust 2015-12-03 01:37:48 -08:00
parent 2e1f7c4729
commit a010904b8e
9 changed files with 56 additions and 39 deletions

View file

@ -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 { fun hasAuth() : Boolean {
return (! keyUsername.isNullOrBlank()) && (! keyPassword.isNullOrBlank()) return (! username.isNullOrBlank()) && (! password.isNullOrBlank())
} }
} }
@Directive @Directive
fun repos(vararg repos : String) { fun repos(vararg repos : String) {
repos.forEach { Kobalt.addRepo(HostInfo(it)) } repos.forEach { Kobalt.addRepo(HostConfig(it)) }
} }
@Directive @Directive
fun repos(vararg repos : HostInfo) { fun authRepos(vararg repos : HostConfig) {
repos.forEach { Kobalt.addRepo(it) } repos.forEach { Kobalt.addRepo(it) }
} }
class HostConfig(var keyUsername: String? = null, var keyPassword: String? = null)
@Directive @Directive
fun authRepo(url: String, init: HostConfig.() -> Unit) : HostInfo { fun authRepo(init: HostConfig.() -> Unit) = HostConfig().apply { init() }
val r = HostConfig()
r.init()
return HostInfo(url, r.keyUsername, r.keyPassword)
}
@Directive @Directive
fun glob(g: String) : IFileSpec.Glob = IFileSpec.Glob(g) fun glob(g: String) : IFileSpec.Glob = IFileSpec.Glob(g)

View file

@ -31,7 +31,7 @@ class ResolveDependency @Inject constructor(val repoFinder: RepoFinder) {
val repoResult = repoFinder.findCorrectRepo(id) val repoResult = repoFinder.findCorrectRepo(id)
val simpleDep = SimpleDep(MavenId(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) }) AsciiArt.logBox(listOf(id, url).map { " $it" }, {s -> println(s) })
display(root.children) display(root.children)

View file

@ -1,6 +1,6 @@
package com.beust.kobalt.api package com.beust.kobalt.api
import com.beust.kobalt.HostInfo import com.beust.kobalt.HostConfig
/** /**
* Plugins that add their own repos. * 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 * 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). * build file has been parsed (then it gets called once for each project discovered).
*/ */
fun reposFor(project: Project?) : List<HostInfo> fun reposFor(project: Project?) : List<HostConfig>
} }

View file

@ -1,8 +1,8 @@
package com.beust.kobalt.api package com.beust.kobalt.api
import com.beust.kobalt.Args import com.beust.kobalt.Args
import com.beust.kobalt.HostConfig
import com.beust.kobalt.Plugins import com.beust.kobalt.Plugins
import com.beust.kobalt.HostInfo
import com.beust.kobalt.misc.MainModule import com.beust.kobalt.misc.MainModule
import com.google.inject.Guice import com.google.inject.Guice
import com.google.inject.Injector import com.google.inject.Injector
@ -24,9 +24,9 @@ public class Kobalt {
"https://jcenter.bintray.com/" "https://jcenter.bintray.com/"
) )
val repos = HashSet<HostInfo>(DEFAULT_REPOS.map { HostInfo(it) }) val repos = HashSet<HostConfig>(DEFAULT_REPOS.map { HostConfig(it) })
fun addRepo(repo: HostInfo) = repos.add( fun addRepo(repo: HostConfig) = repos.add(
if (repo.url.endsWith("/")) repo if (repo.url.endsWith("/")) repo
else repo.copy(url = (repo.url + "/"))) else repo.copy(url = (repo.url + "/")))

View file

@ -1,6 +1,6 @@
package com.beust.kobalt.maven 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.KFiles
import com.beust.kobalt.misc.log import com.beust.kobalt.misc.log
import com.beust.kobalt.misc.warn import com.beust.kobalt.misc.warn
@ -20,7 +20,7 @@ import javax.inject.Singleton
@Singleton @Singleton
class DownloadManager @Inject constructor(val factory: ArtifactFetcher.IFactory) { 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 { override fun equals(other: Any?): Boolean {
return (other as Key).hostInfo.url == hostInfo.url 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<File> = CACHE.get(Key(hostInfo, fileName, executor)) : Future<File> = CACHE.get(Key(hostInfo, fileName, executor))
} }
/** /**
* Fetches an artifact (a file in a Maven repo, .jar, -javadoc.jar, ...) to the given local file. * 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, @Assisted("fileName") val fileName: String,
val files: KFiles) : Callable<File> { val files: KFiles) : Callable<File> {
interface IFactory { 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 { override fun call() : File {

View file

@ -1,7 +1,9 @@
package com.beust.kobalt.maven 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.maven.dependency.FileDependency
import com.beust.kobalt.misc.LocalProperties
import java.io.* import java.io.*
import java.net.HttpURLConnection import java.net.HttpURLConnection
import java.net.URL import java.net.URL
@ -11,14 +13,38 @@ import java.util.*
/** /**
* Abstracts a URL so that it works transparently on either http:// or file:// * Abstracts a URL so that it works transparently on either http:// or file://
*/ */
class Kurl(val hostInfo: HostInfo) { class Kurl(val hostInfo: HostConfig) {
// constructor(url: String) : this(HostInfo(url)) 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.<host>.user=xxx and authUrl.<host>.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 val connection : URLConnection
get() { get() {
val result = URL(hostInfo.url).openConnection() val result = URL(hostInfo.url).openConnection()
if (hostInfo.hasAuth()) { if (hostInfo.hasAuth()) {
val userPass = hostInfo.keyUsername + ":" + hostInfo.keyPassword val userPass = hostInfo.username + ":" + hostInfo.password
val basicAuth = "Basic " + String(Base64.getEncoder().encode(userPass.toByteArray())) val basicAuth = "Basic " + String(Base64.getEncoder().encode(userPass.toByteArray()))
result.setRequestProperty("Authorization", basicAuth) result.setRequestProperty("Authorization", basicAuth)
} }
@ -31,9 +57,6 @@ class Kurl(val hostInfo: HostInfo) {
val exists : Boolean val exists : Boolean
get() { get() {
if (hostInfo.url.contains("localhost")) {
println("DONOTCOMMIT")
}
val url = hostInfo.url val url = hostInfo.url
val result = val result =
if (connection is HttpURLConnection) { if (connection is HttpURLConnection) {

View file

@ -1,6 +1,6 @@
package com.beust.kobalt.maven 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.api.Kobalt
import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.Strings import com.beust.kobalt.misc.Strings
@ -27,7 +27,7 @@ public class RepoFinder @Inject constructor(val executors: KobaltExecutors) {
return FOUND_REPOS.get(id) 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 = "") val hasJar: Boolean = true, val snapshotVersion: String = "")
private val FOUND_REPOS: LoadingCache<String, RepoResult> = CacheBuilder.newBuilder() private val FOUND_REPOS: LoadingCache<String, RepoResult> = CacheBuilder.newBuilder()
@ -51,14 +51,14 @@ public class RepoFinder @Inject constructor(val executors: KobaltExecutors) {
val result = cs.take().get(2000, TimeUnit.MILLISECONDS) val result = cs.take().get(2000, TimeUnit.MILLISECONDS)
log(2, "Result for repo #$i: $result") log(2, "Result for repo #$i: $result")
if (result.found) { if (result.found) {
log(2, "Located $id in ${result.repoHostInfo.url}") log(2, "Located $id in ${result.hostConfig.url}")
return result return result
} }
} catch(ex: Exception) { } catch(ex: Exception) {
warn("Error: $ex") warn("Error: $ex")
} }
} }
return RepoResult(HostInfo(""), false, id) return RepoResult(HostConfig(""), false, id)
} finally { } finally {
executor.shutdownNow() executor.shutdownNow()
} }
@ -67,7 +67,7 @@ public class RepoFinder @Inject constructor(val executors: KobaltExecutors) {
/** /**
* Execute a single HTTP request to one repo. * Execute a single HTTP request to one repo.
*/ */
inner class RepoFinderCallable(val id: String, val repo: HostInfo) : Callable<RepoResult> { inner class RepoFinderCallable(val id: String, val repo: HostConfig) : Callable<RepoResult> {
override fun call(): RepoResult { override fun call(): RepoResult {
val repoUrl = repo.url val repoUrl = repo.url
log(2, "Checking $repoUrl for $id") log(2, "Checking $repoUrl for $id")

View file

@ -1,6 +1,6 @@
package com.beust.kobalt.maven.dependency 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.KobaltException
import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.Kobalt import com.beust.kobalt.api.Kobalt
@ -37,12 +37,12 @@ public class MavenDependency @Inject constructor(mavenId: MavenId,
if (repoResult.found) { if (repoResult.found) {
jarFile = jarFile =
if (repoResult.hasJar) { if (repoResult.hasJar) {
downloadManager.download(HostInfo(url = repoResult.repoHostInfo.url + toJarFile(repoResult)), downloadManager.download(HostConfig(url = repoResult.hostConfig.url + toJarFile(repoResult)),
jar.absolutePath, executor) jar.absolutePath, executor)
} else { } else {
CompletedFuture(File("nonexistentFile")) // will be filtered out 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) pom.absolutePath, executor)
} else { } else {
throw KobaltException("Couldn't resolve ${mavenId.toId}") throw KobaltException("Couldn't resolve ${mavenId.toId}")

View file

@ -351,11 +351,11 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
} }
// IRepoContributor // IRepoContributor
override fun reposFor(project: Project?): List<HostInfo> { override fun reposFor(project: Project?): List<HostConfig> {
val home = androidHomeNoThrows(project) val home = androidHomeNoThrows(project)
return if (home != null) { return if (home != null) {
val path = Paths.get(KFiles.joinDir(home, "extras", "android", "m2repository")) val path = Paths.get(KFiles.joinDir(home, "extras", "android", "m2repository"))
listOf(HostInfo(path.toUri().toString())) listOf(HostConfig(path.toUri().toString()))
} else { } else {
emptyList() emptyList()
} }