tidy up UpdatePackageJson task, tidy up some Gradle config

This commit is contained in:
Adam 2022-05-19 23:12:44 +02:00
parent e9f96fb0fc
commit 9f48270a0a
4 changed files with 58 additions and 119 deletions

View file

@ -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",
)
}

View file

@ -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
}
}
}

View file

@ -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)
}
}

View file

@ -119,6 +119,8 @@ tasks.distZip {
val publishMod by tasks.registering(FactorioModPublishTask::class) {
dependsOn(tasks.check)
distributionZip.set(tasks.distZip.flatMap { it.archiveFile })
val projectModName = project.extra.get("modName") as String
@ -156,65 +158,8 @@ distributions {
tasks.withType<Zip>().configureEach {
val projectTokensXX = projectTokensX
inputs.property("projectTokens", projectTokensXX)
// if (name.startsWith("dist")) {
// notCompatibleWithConfigurationCache("NPE on projectTokens")
// }
}
//tasks.npmInstall {
// nodeModulesOutputFilter {
// exclude("**/typed-factorio/generated/classes.d.ts")
// }
//}
//interface ServiceProvider {
// @get:Inject
// val fs: FileSystemOperations
//}
//
//val fixLink by tasks.registering {
// dependsOn(tasks.npmInstall)
//
//
// val badLink = """{@link https://lua-api.factorio.com/latest/Data-Lifecycle.html Data Lifecycle}"""
// val goodLink = """https://lua-api.factorio.com/latest/Data-Lifecycle.html"""
//
// val typedFactorioDir =
// layout.projectDirectory.dir("src/main/typescript/node_modules/typed-factorio")
// val classesDTS = typedFactorioDir.file("generated/classes.d.ts")
//
// val services = project.objects.newInstance<ServiceProvider>()
//
// inputs.file(classesDTS)
// outputs.file(classesDTS)
//
// inputs.property("badLink", badLink)
// inputs.property("goodLink", goodLink)
//
// doLast {
// logger.lifecycle("fixing link ${classesDTS.asFile.canonicalPath}")
//
// services.fs.sync {
// from(classesDTS)
// into(temporaryDir)
// filter { line ->
// when {
// line.contains(badLink) -> line.replace(badLink, goodLink)
// else -> line
// }
// }
// }
//
// services.fs.copy {
// from(temporaryDir.resolve("classes.d.ts").canonicalPath)
// into(classesDTS.asFile.parent)
// duplicatesStrategy = DuplicatesStrategy.WARN
// }
// }
//}
//
//tasks.assemble { dependsOn(fixLink) }
//val downloadFactorioApiDocs by tasks.registering {
// group = project.name
@ -259,7 +204,7 @@ tasks.updatePackageJson {
inputs.property("packageJsonName", packageJsonName)
propertiesToCheck.put("name", packageJsonName)
val pjProvider = layout.projectDirectory.file(provider { "package.json" })
packageJsonFile2.set(pjProvider)
packageJsonFile.set(pjProvider)
}
@ -296,3 +241,20 @@ tasks.assemble { dependsOn(installEventsTsSchema, tasks.updatePackageJson) }
// }
// }
//}
idea {
module {
sourceDirs = sourceDirs + file("src/main/typescript")
resourceDirs = resourceDirs + file("src/main/resources")
testSourceDirs = testSourceDirs + file("src/test/typescript")
iml {
whenMerged {
require(this is org.gradle.plugins.ide.idea.model.Module)
sourceDirs = sourceDirs + file("src/main/typescript")
resourceDirs = resourceDirs + file("src/main/resources")
testSourceDirs = testSourceDirs + file("src/test/typescript")
}
}
}
}