Skip to content

Commit

Permalink
Add Spotless (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton authored Nov 13, 2023
1 parent 6390d8c commit 05037d8
Show file tree
Hide file tree
Showing 17 changed files with 369 additions and 185 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ insert_final_newline = true

[*.yaml]
indent_style = space

[*.{kt,kts}]
ij_kotlin_imports_layout=*
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM azul/zulu-openjdk:21-latest AS build
ENV GRADLE_OPTS="-Dorg.gradle.daemon=false -Dkotlin.incremental=false"
WORKDIR /app

COPY gradlew settings.gradle ./
COPY gradlew settings.gradle .editorconfig ./
COPY gradle ./gradle
RUN ./gradlew --version

Expand Down
11 changes: 11 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ buildscript {
dependencies {
classpath libs.kotlin.gradlePlugin
classpath libs.kotlin.serialization
classpath libs.spotless.gradlePlugin
}
}

Expand Down Expand Up @@ -70,3 +71,13 @@ test {
exceptionFormat = 'full'
}
}

apply plugin: 'com.diffplug.spotless'
spotless {
kotlin {
target("src/**/*.kt")
ktlint(libs.ktlint.get().version).editorConfigOverride([
'ktlint_standard_filename': 'disabled',
])
}
}
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", ve
kotlin-serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test" }

spotless-gradlePlugin = "com.diffplug.spotless:spotless-plugin-gradle:6.22.0"
ktlint = "com.pinterest.ktlint:ktlint-cli:1.0.1"

kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }

Expand Down
58 changes: 29 additions & 29 deletions src/main/kotlin/watch/dependency/config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import org.tomlj.Toml
import org.tomlj.TomlTable
import watch.dependency.RepositoryConfig.Companion.GoogleMavenHost
import watch.dependency.RepositoryConfig.Companion.GoogleMavenId
import watch.dependency.RepositoryConfig.Companion.GoogleMavenName
import watch.dependency.RepositoryConfig.Companion.MavenCentralHost
import watch.dependency.RepositoryConfig.Companion.MavenCentralId
import watch.dependency.RepositoryConfig.Companion.MavenCentralName
import watch.dependency.RepositoryConfig.Companion.GOOGLE_MAVEN_HOST
import watch.dependency.RepositoryConfig.Companion.GOOGLE_MAVEN_ID
import watch.dependency.RepositoryConfig.Companion.GOOGLE_MAVEN_NAME
import watch.dependency.RepositoryConfig.Companion.MAVEN_CENTRAL_HOST
import watch.dependency.RepositoryConfig.Companion.MAVEN_CENTRAL_ID
import watch.dependency.RepositoryConfig.Companion.MAVEN_CENTRAL_NAME
import watch.dependency.RepositoryType.Maven2

fun MavenRepository.Factory.parseWellKnownIdOrUrl(value: String): MavenRepository {
return when (value) {
MavenCentralId -> maven2(MavenCentralName, MavenCentralHost)
GoogleMavenId -> maven2(GoogleMavenName, GoogleMavenHost)
MAVEN_CENTRAL_ID -> maven2(MAVEN_CENTRAL_NAME, MAVEN_CENTRAL_HOST)
GOOGLE_MAVEN_ID -> maven2(GOOGLE_MAVEN_NAME, GOOGLE_MAVEN_HOST)
else -> maven2("Maven Repository", value.toHttpUrl())
}
}
Expand All @@ -27,16 +27,16 @@ data class RepositoryConfig(
val coordinates: List<MavenCoordinate>,
) {
companion object {
const val MavenCentralId = "MavenCentral"
const val MavenCentralName = "Maven Central"
val MavenCentralHost = "https://repo1.maven.org/maven2/".toHttpUrl()
const val GoogleMavenId = "GoogleMaven"
const val GoogleMavenName = "Google Maven"
val GoogleMavenHost = "https://maven.google.com/".toHttpUrl()
private const val TomlKeyName = "name"
private const val TomlKeyHost = "host"
private const val TomlKeyType = "type"
private const val TomlKeyCoordinates = "coordinates"
const val MAVEN_CENTRAL_ID = "MavenCentral"
const val MAVEN_CENTRAL_NAME = "Maven Central"
val MAVEN_CENTRAL_HOST = "https://repo1.maven.org/maven2/".toHttpUrl()
const val GOOGLE_MAVEN_ID = "GoogleMaven"
const val GOOGLE_MAVEN_NAME = "Google Maven"
val GOOGLE_MAVEN_HOST = "https://maven.google.com/".toHttpUrl()
private const val TOML_KEY_NAME = "name"
private const val TOML_KEY_HOST = "host"
private const val TOML_KEY_TYPE = "type"
private const val TOML_KEY_COORDINATES = "coordinates"

private fun TomlTable.getCoordinates(key: String): List<MavenCoordinate> {
val coordinateArray = getArray(key)!!
Expand All @@ -49,14 +49,14 @@ data class RepositoryConfig(
var coordinates: List<MavenCoordinate>? = null
for (key in keySet()) {
when (key) {
TomlKeyCoordinates -> coordinates = getCoordinates(key)
TomlKeyName, TomlKeyHost, TomlKeyType -> {
TOML_KEY_COORDINATES -> coordinates = getCoordinates(key)
TOML_KEY_NAME, TOML_KEY_HOST, TOML_KEY_TYPE -> {
throw IllegalArgumentException("'$self' table must not define a '$key' key")
}
else -> throw IllegalArgumentException("'$self' table contains unknown '$key' key")
}
}
requireNotNull(coordinates) { "'$self' table missing required '$TomlKeyCoordinates' key" }
requireNotNull(coordinates) { "'$self' table missing required '$TOML_KEY_COORDINATES' key" }
return RepositoryConfig(name, host, Maven2, coordinates)
}

Expand All @@ -67,15 +67,15 @@ data class RepositoryConfig(
var coordinates: List<MavenCoordinate>? = null
for (key in keySet()) {
when (key) {
TomlKeyName -> name = getString(key)!!
TomlKeyHost -> host = getString(key)!!.toHttpUrl()
TomlKeyType -> type = RepositoryType.valueOf(getString(key)!!)
TomlKeyCoordinates -> coordinates = getCoordinates(key)
TOML_KEY_NAME -> name = getString(key)!!
TOML_KEY_HOST -> host = getString(key)!!.toHttpUrl()
TOML_KEY_TYPE -> type = RepositoryType.valueOf(getString(key)!!)
TOML_KEY_COORDINATES -> coordinates = getCoordinates(key)
else -> throw IllegalArgumentException("'$self' table contains unknown key '$key'")
}
}
requireNotNull(host) { "'$self' table missing required '$TomlKeyHost' key" }
requireNotNull(coordinates) { "'$self' table missing required '$TomlKeyCoordinates' key" }
requireNotNull(host) { "'$self' table missing required '$TOML_KEY_HOST' key" }
requireNotNull(coordinates) { "'$self' table missing required '$TOML_KEY_COORDINATES' key" }
return RepositoryConfig(name, host, type, coordinates)
}

Expand All @@ -87,8 +87,8 @@ data class RepositoryConfig(
for (key in parseResult.keySet()) {
val table = parseResult.getTable(key)!!
this += when (key) {
MavenCentralId -> table.tryParseWellKnown(MavenCentralId, MavenCentralName, MavenCentralHost)
GoogleMavenId -> table.tryParseWellKnown(GoogleMavenId, GoogleMavenName, GoogleMavenHost)
MAVEN_CENTRAL_ID -> table.tryParseWellKnown(MAVEN_CENTRAL_ID, MAVEN_CENTRAL_NAME, MAVEN_CENTRAL_HOST)
GOOGLE_MAVEN_ID -> table.tryParseWellKnown(GOOGLE_MAVEN_ID, GOOGLE_MAVEN_NAME, GOOGLE_MAVEN_HOST)
else -> table.tryParseCustom(key)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/watch/dependency/database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ class FileSystemDatabase(private val root: Path) : Database {

override fun coordinateVersionSeen(
coordinate: MavenCoordinate,
version: String
version: String,
): Boolean {
return path(coordinate, version).exists()
}

override fun markCoordinateVersionSeen(
coordinate: MavenCoordinate,
version: String
version: String,
) {
val path = path(coordinate, version)
if (path.notExists()) {
Expand Down
30 changes: 17 additions & 13 deletions src/main/kotlin/watch/dependency/dependencyAwait.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.io.PrintStream
import kotlin.time.Duration
import kotlinx.coroutines.delay

internal const val cursorUpAndClearLine = "\u001B[F\u001B[K"
internal const val CURSOR_UP_AND_CLEAR_LINE = "\u001B[F\u001B[K"

class DependencyAwait(
private val mavenRepository: MavenRepository,
Expand All @@ -16,7 +16,7 @@ class DependencyAwait(
) {
suspend fun await(
coordinate: MavenCoordinate,
version: String
version: String,
) {
var needsClear = false
while (true) {
Expand All @@ -29,14 +29,16 @@ class DependencyAwait(
}

if (progress != null) {
progress.print(buildString {
if (needsClear) {
append(cursorUpAndClearLine)
}
append("Last checked: ")
append(timestampSource.now())
append('\n')
})
progress.print(
buildString {
if (needsClear) {
append(CURSOR_UP_AND_CLEAR_LINE)
}
append("Last checked: ")
append(timestampSource.now())
append('\n')
},
)
progress.flush()
}
needsClear = true
Expand All @@ -46,9 +48,11 @@ class DependencyAwait(
}

if (progress != null && needsClear) {
progress.print(buildString {
append(cursorUpAndClearLine)
})
progress.print(
buildString {
append(CURSOR_UP_AND_CLEAR_LINE)
},
)
progress.flush()
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/watch/dependency/dependencyNotifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private class DependencyChecker(
supervisorScope {
for (coordinates in coordinates) {
launch(start = UNDISPATCHED) {
debug.log { "Fetching metadata for $coordinates..." }
debug.log { "Fetching metadata for $coordinates..." }
val versions = mavenRepository.versions(coordinates)
debug.log { "$coordinates $versions" }

Expand Down
21 changes: 13 additions & 8 deletions src/main/kotlin/watch/dependency/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import okhttp3.logging.HttpLoggingInterceptor.Level.BASIC
import watch.dependency.RepositoryConfig.Companion.MavenCentralId
import watch.dependency.RepositoryConfig.Companion.MAVEN_CENTRAL_ID

fun main(vararg args: String) {
NoOpCliktCommand(name = "dependency-watch")
Expand All @@ -38,7 +38,7 @@ fun main(vararg args: String) {

private abstract class DependencyWatchCommand(
name: String,
help: String = ""
help: String = "",
) : CliktCommand(name = name, help = help) {
protected val debug by option(hidden = true)
.switch<Debug>(mapOf("--debug" to Debug.Console))
Expand Down Expand Up @@ -91,12 +91,14 @@ private class AwaitCommand : DependencyWatchCommand(
help = "Wait for an artifact to appear in a Maven repository then exit",
) {
private val repo by option("--repo", metavar = "URL")
.help("""
.help(
"""
|URL or well-known ID of maven repository to check (default is "MavenCentral").
|Available well-known IDs: "MavenCentral", "GoogleMaven".
|""".trimMargin()
|
""".trimMargin(),
)
.default(MavenCentralId)
.default(MAVEN_CENTRAL_ID)

private val quiet by option("--quiet", "-q")
.flag()
Expand Down Expand Up @@ -129,13 +131,14 @@ private class AwaitCommand : DependencyWatchCommand(
}

private class NotifyCommand(
fs: FileSystem
fs: FileSystem,
) : DependencyWatchCommand(
name = "notify",
help = "Monitor Maven coordinates in a Maven repository for new versions",
) {
private val configPath by argument("CONFIG")
.help("""
.help(
"""
|TOML file containing repositories and coordinates to watch
|
|Format:
Expand Down Expand Up @@ -163,7 +166,9 @@ private class NotifyCommand(
|"MavenCentral" and "GoogleMaven" are two optional well-known repositories
|which only require a list of coordinates. Other repositories also require
|a host and can specify an optional name.
|""".trimMargin())
|
""".trimMargin(),
)
.path(fileSystem = fs)

@Suppress("USELESS_CAST") // Needed to keep the type abstract.
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/watch/dependency/maven.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private class HttpMaven2Repository(
val metadata = xmlFormat.decodeFromString(ArtifactMetadata.serializer(), body)
return Versions(
latest = metadata.versioning.release,
all = metadata.versioning.versions.toSet()
all = metadata.versioning.versions.toSet(),
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/watch/dependency/notify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class IftttVersionNotifier(
private data class PostBody(
val value1: String? = null,
val value2: String? = null,
val value3: String? = null
val value3: String? = null,
) {
fun toJson(): RequestBody {
val json = format.encodeToString(serializer(), this)
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/watch/dependency/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

package watch.dependency

import java.io.IOException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

class HttpException(val code: Int, message: String) : RuntimeException("$code $message")

Expand All @@ -22,7 +22,7 @@ suspend fun Call.await(): String {
continuation.resume(body)
} else {
continuation.resumeWithException(
HttpException(response.code, response.message)
HttpException(response.code, response.message),
)
}
}
Expand Down
Loading

0 comments on commit 05037d8

Please sign in to comment.