tidy up UpdatePackageJson task, tidy up some Gradle config
This commit is contained in:
parent
e9f96fb0fc
commit
9f48270a0a
4 changed files with 58 additions and 119 deletions
|
|
@ -82,11 +82,11 @@ tasks.withType<KotlinCompile>().configureEach {
|
|||
|
||||
kotlinOptions.freeCompilerArgs += listOf(
|
||||
// "-Xcontext-receivers",
|
||||
"-Xopt-in=kotlin.RequiresOptIn",
|
||||
"-Xopt-in=kotlin.ExperimentalStdlibApi",
|
||||
"-Xopt-in=kotlin.time.ExperimentalTime",
|
||||
"-opt-in=kotlin.RequiresOptIn",
|
||||
"-opt-in=kotlin.ExperimentalStdlibApi",
|
||||
"-opt-in=kotlin.time.ExperimentalTime",
|
||||
// "-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
|
||||
"-Xopt-in=kotlinx.serialization.ExperimentalSerializationApi",
|
||||
"-opt-in=kotlinx.serialization.ExperimentalSerializationApi",
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,5 @@
|
|||
package dev.adamko.kafkatorio.task
|
||||
|
||||
import dev.adamko.kafkatorio.jsonMapper
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
import org.gradle.api.Task
|
||||
import org.gradle.api.internal.specs.ExplainingSpec
|
||||
|
||||
|
|
@ -13,34 +10,29 @@ object JsonPropertiesUpToDateSpec : ExplainingSpec<Task> {
|
|||
|
||||
// Returns a description explaining why the task is outdated,
|
||||
// or null if the task is up to date
|
||||
override fun whyUnsatisfied(element: Task?): String? = with(element) {
|
||||
when (this) {
|
||||
null -> "task is null"
|
||||
override fun whyUnsatisfied(task: Task?): String? {
|
||||
require(task is UpdatePackageJson) { "$task is not a UpdatePackageJson task" }
|
||||
|
||||
!is UpdatePackageJson ->
|
||||
"task ${this::class.simpleName} is not instance of ${UpdatePackageJson::class.simpleName}"
|
||||
val current = task.currentPackageJson()
|
||||
val updated = task.updatedPackageJson(current)
|
||||
|
||||
else -> {
|
||||
return if (current != updated) {
|
||||
(current.keys + updated.keys)
|
||||
.asSequence()
|
||||
.distinct()
|
||||
.sorted()
|
||||
.mapNotNull {
|
||||
val currentVal = current[it]
|
||||
val updatedVal = updated[it]
|
||||
|
||||
val packageJsonContent = packageJsonFile2.get().asFile.readText()
|
||||
val packageJson = jsonMapper.parseToJsonElement(packageJsonContent).jsonObject
|
||||
|
||||
val outdatedProperties = propertiesToCheck.get().mapNotNull { (key, expectedVal) ->
|
||||
val actualVal = packageJson[key]?.jsonPrimitive?.content
|
||||
when {
|
||||
expectedVal == actualVal -> null
|
||||
else -> "'$key' (expected: $expectedVal, actual: $actualVal)"
|
||||
if (currentVal == updatedVal) {
|
||||
null
|
||||
} else {
|
||||
"'$it' - expected:$updatedVal, actual:$currentVal"
|
||||
}
|
||||
}
|
||||
|
||||
when {
|
||||
outdatedProperties.isEmpty() -> null
|
||||
outdatedProperties.size == 1 ->
|
||||
"package.json has outdated property: ${outdatedProperties.single()}"
|
||||
else ->
|
||||
"package.json has outdated properties: ${outdatedProperties.joinToString(", ")}"
|
||||
}
|
||||
}
|
||||
}.joinToString(prefix = "package.json is not up to date. ")
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ import com.github.gradle.node.NodePlugin
|
|||
import dev.adamko.kafkatorio.jsonMapper
|
||||
import javax.inject.Inject
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.file.RegularFileProperty
|
||||
|
|
@ -13,7 +13,6 @@ import org.gradle.api.provider.MapProperty
|
|||
import org.gradle.api.provider.ProviderFactory
|
||||
import org.gradle.api.tasks.CacheableTask
|
||||
import org.gradle.api.tasks.Input
|
||||
import org.gradle.api.tasks.Internal
|
||||
import org.gradle.api.tasks.OutputFile
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
import org.gradle.kotlin.dsl.support.useToRun
|
||||
|
|
@ -27,9 +26,9 @@ abstract class UpdatePackageJson @Inject constructor(
|
|||
@get:Input
|
||||
abstract val propertiesToCheck: MapProperty<String, String>
|
||||
|
||||
// @get:OutputFile
|
||||
@get:Internal
|
||||
abstract val packageJsonFile2: RegularFileProperty
|
||||
@get:OutputFile
|
||||
abstract val packageJsonFile: RegularFileProperty
|
||||
|
||||
|
||||
init {
|
||||
group = NodePlugin.NODE_GROUP
|
||||
|
|
@ -37,34 +36,16 @@ abstract class UpdatePackageJson @Inject constructor(
|
|||
"Read the package.json file and update the version and name, based on the project."
|
||||
|
||||
outputs.upToDateWhen(JsonPropertiesUpToDateSpec)
|
||||
|
||||
// super.doFirst {
|
||||
// propertiesToCheck.disallowChanges()
|
||||
// packageJsonFile.disallowChanges()
|
||||
// }
|
||||
}
|
||||
|
||||
init {
|
||||
outputs.upToDateWhen { task ->
|
||||
require(task is UpdatePackageJson) { "$task is not a UpdatePackageJson task" }
|
||||
|
||||
val currentPackageJson = task.currentPackageJson()
|
||||
val updatedPackageJson = task.updatedPackageJson(currentPackageJson)
|
||||
|
||||
val status = currentPackageJson == updatedPackageJson
|
||||
logger.lifecycle("package.json is ${if (status) "up to date" else "not up to date"}")
|
||||
status
|
||||
}
|
||||
}
|
||||
|
||||
@TaskAction
|
||||
fun exec() {
|
||||
val packageJsonFile = packageJsonFile2.asFile.get()
|
||||
val packageJsonFile = packageJsonFile.asFile.get()
|
||||
|
||||
logger.lifecycle("updating package.json ${packageJsonFile.canonicalPath}")
|
||||
|
||||
val currentPackageJson = currentPackageJson()
|
||||
val updatedPackageJson = updatedPackageJson(currentPackageJson)
|
||||
val updatedPackageJson = updatedPackageJson()
|
||||
|
||||
val packageJsonContentUpdated =
|
||||
jsonMapper
|
||||
|
|
@ -78,14 +59,18 @@ abstract class UpdatePackageJson @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
private fun currentPackageJson(): JsonObject {
|
||||
val packageJsonFile = packageJsonFile2.asFile.get()
|
||||
internal fun currentPackageJson(): JsonObject {
|
||||
val packageJsonFile = packageJsonFile.get().asFile
|
||||
return jsonMapper.parseToJsonElement(packageJsonFile.readText()).jsonObject
|
||||
}
|
||||
|
||||
private fun updatedPackageJson(currentPackageJson: JsonObject): JsonObject {
|
||||
val propertiesToCheck = propertiesToCheck.get()
|
||||
val newJsonProps = propertiesToCheck.mapValues { (_, newVal) -> JsonPrimitive(newVal) }
|
||||
internal fun updatedPackageJson(
|
||||
currentPackageJson: JsonObject = currentPackageJson(),
|
||||
): JsonObject {
|
||||
val propertiesToCheck: Map<String, String> = propertiesToCheck.get()
|
||||
val newJsonProps = propertiesToCheck.mapValues { (_, newVal) ->
|
||||
jsonMapper.parseToJsonElement(newVal)
|
||||
}
|
||||
return JsonObject(currentPackageJson + newJsonProps)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue