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

Better LocalProperties management.

This commit is contained in:
Cedric Beust 2015-11-12 03:24:44 -08:00
parent 97a5f54c9a
commit 1207f1e780
4 changed files with 66 additions and 79 deletions

View file

@ -3,8 +3,6 @@ package com.beust.kobalt.api
import com.beust.kobalt.Plugins
import com.google.inject.Injector
import java.io.InputStream
import java.nio.file.Files
import java.nio.file.Paths
import java.util.*
public class Kobalt {
@ -25,10 +23,13 @@ public class Kobalt {
private val PROPERTY_KOBALT_VERSION = "kobalt.version"
private val KOBALT_PROPERTIES = "kobalt.properties"
private val LOCAL_PROPERTIES = "local.properties"
private val properties : Properties by lazy { readProperties() }
/** kobalt.properties */
private val kobaltProperties: Properties by lazy { readProperties() }
/**
* Read the content of kobalt.properties
*/
private fun readProperties() : Properties {
val result = Properties()
@ -41,13 +42,13 @@ public class Kobalt {
}
// local.properties can be used by external users
Paths.get(LOCAL_PROPERTIES).let { path ->
if (Files.exists(path)) {
Files.newInputStream(path).use {
readProperties(result, it)
}
}
}
// Paths.get(LOCAL_PROPERTIES).let { path ->
// if (Files.exists(path)) {
// Files.newInputStream(path).use {
// readProperties(result, it)
// }
// }
// }
return result
}
@ -58,7 +59,7 @@ public class Kobalt {
properties.forEach { es -> System.setProperty(es.key.toString(), es.value.toString()) }
}
val version = properties.getProperty(PROPERTY_KOBALT_VERSION)
val version = kobaltProperties.getProperty(PROPERTY_KOBALT_VERSION)
fun findPlugin(name: String) = Plugins.findPlugin(name)
}

View file

@ -21,9 +21,6 @@ import java.io.File
import java.io.IOException
import java.io.InputStreamReader
import java.net.URL
import java.nio.file.Files
import java.nio.file.Paths
import java.util.*
import java.util.concurrent.Callable
import java.util.concurrent.Future
import javax.inject.Inject
@ -31,9 +28,12 @@ import javax.inject.Inject
/**
* Retrieve Kobalt's latest release version from github.
*/
public class GithubApi @Inject constructor(val executors: KobaltExecutors) {
public class GithubApi @Inject constructor(val executors: KobaltExecutors,
val localProperties: LocalProperties) {
companion object {
const val RELEASES_URL = "https://api.github.com/repos/cbeust/kobalt/releases"
const val PROPERTY_ACCESS_TOKEN = "github.accessToken"
const val PROPERTY_USERNAME = "github.username"
}
class RetrofitErrorResponse(val code: String?, val field: String?)
@ -48,10 +48,12 @@ public class GithubApi @Inject constructor(val executors: KobaltExecutors) {
fun uploadRelease(packageName: String, tagName: String, zipFile: File) {
log(1, "Uploading release ${zipFile.name}")
val username = localProperties.get(PROPERTY_USERNAME)
val accessToken = localProperties.get(PROPERTY_ACCESS_TOKEN)
try {
service.createRelease(Prop.username, Prop.accessToken, packageName, CreateRelease(tagName))
service.createRelease(username, accessToken, packageName, CreateRelease(tagName))
.flatMap { response ->
uploadService.uploadAsset(Prop.username, Prop.accessToken,
uploadService.uploadAsset(username, accessToken,
packageName, response.id!!, zipFile.name, TypedFile("application/zip", zipFile))
}
.toBlocking()
@ -165,37 +167,3 @@ fun Response.bodyContent() : String {
return result
// return new Gson().fromJson(data, type);
}
class Prop {
companion object {
const val ACCESS_TOKEN_PROPERTY = "github.accessToken"
const val USERNAME_PROPERTY = "github.username"
val localProperties: Properties by lazy {
val result = Properties()
val filePath = Paths.get("local.properties")
if (! Files.exists(filePath)) {
throw KobaltException("Couldn't find a local.properties file")
}
filePath.let { path ->
if (Files.exists(path)) {
Files.newInputStream(path).use {
result.load(it)
}
}
}
result
}
private fun fromProperties(name: String) : String {
val result = localProperties.get(name)
?: throw KobaltException("Couldn't find $name in local.properties")
return result as String
}
val accessToken: String get() = fromProperties(ACCESS_TOKEN_PROPERTY)
val username: String get() = fromProperties(USERNAME_PROPERTY)
}
}

View file

@ -0,0 +1,33 @@
package com.beust.kobalt.misc
import com.beust.kobalt.maven.KobaltException
import com.google.inject.Singleton
import java.nio.file.Files
import java.nio.file.Paths
import java.util.*
@Singleton
class LocalProperties {
val localProperties: Properties by lazy {
val result = Properties()
val filePath = Paths.get("local.properties")
if (! Files.exists(filePath)) {
throw KobaltException("Couldn't find a local.properties file")
}
filePath.let { path ->
if (Files.exists(path)) {
Files.newInputStream(path).use {
result.load(it)
}
}
}
result
}
fun get(name: String) : String {
val result = localProperties.get(name) ?: throw KobaltException("Couldn't find $name in local.properties")
return result as String
}
}

View file

@ -9,6 +9,7 @@ import com.beust.kobalt.internal.TaskResult
import com.beust.kobalt.maven.PomGenerator
import com.beust.kobalt.misc.GithubApi
import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.LocalProperties
import com.beust.kobalt.misc.log
import com.google.common.base.Preconditions
import java.io.File
@ -17,7 +18,7 @@ import javax.inject.Singleton
@Singleton
public class PublishPlugin @Inject constructor(val files: KFiles, val factory: PomGenerator.IFactory,
val jcenterFactory: JCenterApi.IFactory, val github: GithubApi)
val jcenterFactory: JCenterApi.IFactory, val github: GithubApi, val localProperties: LocalProperties)
: BasePlugin() {
override val name = "publish"
@ -54,38 +55,19 @@ public class PublishPlugin @Inject constructor(val files: KFiles, val factory: P
return result
}
private fun checkAuthentication(value: String, key: String) {
Preconditions.checkNotNull(value, "Couldn't find user in property $key, make sure you specified" +
"your credentials in local.properties")
}
data class UserData(val user: String, val password: String)
private fun checkCredentials(project: Project) : UserData {
val user = System.getProperty(PROPERTY_BINTRAY_USER)
val password = System.getProperty(PROPERTY_BINTRAY_PASSWORD)
checkAuthentication(user, PROPERTY_BINTRAY_USER)
checkAuthentication(password, PROPERTY_BINTRAY_PASSWORD)
validateProject(project)
return UserData(user, password)
}
@Task(name = TASK_UPLOAD_JCENTER, description = "Upload the artifacts to JCenter",
@Task(name = TASK_UPLOAD_JCENTER, description = "Upload files to JCenter",
runAfter = arrayOf(TASK_GENERATE_POM))
fun taskUploadJcenter(project: Project): TaskResult {
checkCredentials(project).let {
return uploadJcenter(project, it.user, it.password)
}
validateProject(project)
return uploadJcenter(project)
}
@Task(name = TASK_UPLOAD_GITHUB, description = "Upload the release to Github",
@Task(name = TASK_UPLOAD_GITHUB, description = "Upload files to Github",
runAfter = arrayOf(TASK_GENERATE_POM))
fun taskUploadGithub(project: Project): TaskResult {
checkCredentials(project).let {
validateProject(project)
return uploadGithub(project)
}
}
private fun uploadGithub(project: Project) : TaskResult {
val configuration = githubConfigurations.getRaw(project.name)
@ -102,7 +84,10 @@ public class PublishPlugin @Inject constructor(val files: KFiles, val factory: P
return TaskResult()
}
private fun uploadJcenter(project: Project, user: String?, password: String?) : TaskResult {
private fun uploadJcenter(project: Project) : TaskResult {
val user = localProperties.get(PROPERTY_BINTRAY_USER)
val password = localProperties.get(PROPERTY_BINTRAY_PASSWORD)
val jcenter = jcenterFactory.create(user, password)
val configuration = jcenterConfigurations.getRaw(project.name)