add Gradle task to upload the mod to the portal, and some gradle fixes
This commit is contained in:
parent
0f08eee56f
commit
b6b874d8f7
22 changed files with 723 additions and 273 deletions
|
|
@ -4,7 +4,7 @@ plugins {
|
|||
idea
|
||||
`kotlin-dsl`
|
||||
kotlin("jvm") version "1.6.21"
|
||||
`project-report`
|
||||
kotlin("plugin.serialization") version "1.6.21"
|
||||
}
|
||||
|
||||
val gradleJvmTarget = "11"
|
||||
|
|
@ -19,9 +19,12 @@ dependencies {
|
|||
implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable")
|
||||
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${libs.versions.kotlin.get()}")
|
||||
|
||||
implementation(platform("org.jetbrains.kotlinx:kotlinx-serialization-bom:${libs.versions.kotlinx.serialization.get()}"))
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json")
|
||||
implementation(platform(libs.kotlinx.coroutines.bom))
|
||||
implementation(libs.kotlinx.coroutines.core)
|
||||
|
||||
implementation(platform(libs.kotlinx.serialization.bom))
|
||||
implementation(libs.kotlinx.serialization.core)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
// implementation("com.charleskorn.kaml:kaml:0.37.0")
|
||||
|
||||
implementation("com.github.node-gradle:gradle-node-plugin:${libs.versions.gradleNodePlugin.get()}")
|
||||
|
|
@ -32,13 +35,29 @@ dependencies {
|
|||
|
||||
implementation("net.swiftzer.semver:semver:${libs.versions.semver.get()}")
|
||||
|
||||
implementation(platform(libs.okio.bom))
|
||||
implementation(libs.okio.core)
|
||||
|
||||
implementation(gradleKotlinDsl())
|
||||
|
||||
implementation(libs.kotlinx.cli)
|
||||
|
||||
implementation(platform(libs.ktor.bom))
|
||||
implementation(libs.ktor.clientAuth)
|
||||
implementation(libs.ktor.clientContentNegotiation)
|
||||
implementation(libs.ktor.clientCore)
|
||||
implementation(libs.ktor.clientEncoding)
|
||||
implementation(libs.ktor.clientLogging)
|
||||
implementation(libs.ktor.clientCIO)
|
||||
implementation(libs.ktor.clientResources)
|
||||
implementation(libs.ktor.serializationKotlinxJson)
|
||||
|
||||
// https://github.com/avast/gradle-docker-compose-plugin
|
||||
// implementation("com.avast.gradle:gradle-docker-compose-plugin:${Versions.gradleDockerComposePlugin}")
|
||||
|
||||
implementation(platform("org.http4k:http4k-bom:${libs.versions.http4k.get()}"))
|
||||
implementation("org.http4k:http4k-core")
|
||||
implementation("org.http4k:http4k-client-okhttp")
|
||||
|
||||
// implementation(platform("org.http4k:http4k-bom:${libs.versions.http4k.get()}"))
|
||||
// implementation("org.http4k:http4k-core")
|
||||
// implementation("org.http4k:http4k-client-okhttp")
|
||||
|
||||
// https://github.com/gradle/gradle/issues/15383#issuecomment-779893192
|
||||
// https://youtrack.jetbrains.com/issue/IDEA-262280#focus=Comments-27-5397040.0-0
|
||||
|
|
@ -63,14 +82,24 @@ tasks.withType<KotlinCompile>().configureEach {
|
|||
|
||||
kotlinOptions.freeCompilerArgs += listOf(
|
||||
// "-Xcontext-receivers",
|
||||
"-opt-in=kotlin.RequiresOptIn",
|
||||
"-opt-in=kotlin.ExperimentalStdlibApi",
|
||||
"-opt-in=kotlin.time.ExperimentalTime",
|
||||
"-Xopt-in=kotlin.RequiresOptIn",
|
||||
"-Xopt-in=kotlin.ExperimentalStdlibApi",
|
||||
"-Xopt-in=kotlin.time.ExperimentalTime",
|
||||
// "-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
|
||||
"-opt-in=kotlinx.serialization.ExperimentalSerializationApi",
|
||||
"-Xopt-in=kotlinx.serialization.ExperimentalSerializationApi",
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
afterEvaluate {
|
||||
tasks.withType<KotlinCompile>().configureEach {
|
||||
kotlinOptions {
|
||||
apiVersion = gradleKotlinTarget
|
||||
languageVersion = gradleKotlinTarget
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvmToolchain {
|
||||
(this as JavaToolchainSpec).languageVersion.set(JavaLanguageVersion.of(gradleJvmTarget))
|
||||
|
|
|
|||
|
|
@ -13,16 +13,10 @@ val jsonMapper = Json {
|
|||
|
||||
|
||||
object Versions {
|
||||
const val kotlinLang = "1.6"
|
||||
const val jvmTarget = "11"
|
||||
|
||||
const val node = "14.19.1"
|
||||
|
||||
// const val kotlinWrappers = "0.0.1-pre.330-kotlin-1.6.20"
|
||||
const val kotlinWrappers = "1.0.0-pre.336"
|
||||
const val junit = "5.8.2"
|
||||
const val kotest = "5.2.3"
|
||||
const val mockk = "1.12.1"
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,134 @@
|
|||
package dev.adamko.kafkatorio.factoriomod
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.client.plugins.defaultRequest
|
||||
import io.ktor.client.plugins.logging.LogLevel
|
||||
import io.ktor.client.plugins.logging.Logger
|
||||
import io.ktor.client.plugins.logging.Logging
|
||||
import io.ktor.client.plugins.logging.SIMPLE
|
||||
import io.ktor.client.request.forms.MultiPartFormDataContent
|
||||
import io.ktor.client.request.forms.formData
|
||||
import io.ktor.client.request.forms.submitForm
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.Headers
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.Parameters
|
||||
import io.ktor.http.append
|
||||
import io.ktor.http.isSuccess
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import java.io.File
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.gradle.api.internal.tasks.userinput.UserInputHandler
|
||||
import org.gradle.kotlin.dsl.support.useToRun
|
||||
import org.jetbrains.kotlin.cli.common.toBooleanLenient
|
||||
|
||||
|
||||
class FactorioModPortalPublishClient(
|
||||
private val userInputHandler: UserInputHandler,
|
||||
private val distributionZip: File,
|
||||
private val modName: String,
|
||||
private val modVersion: String,
|
||||
private val portalApiKey: String,
|
||||
private val portalUploadEndpoint: String,
|
||||
) {
|
||||
|
||||
private fun client() = HttpClient(CIO) {
|
||||
install(Logging) {
|
||||
logger = Logger.SIMPLE
|
||||
level = LogLevel.ALL
|
||||
}
|
||||
install(ContentNegotiation) {
|
||||
json(Json {
|
||||
prettyPrint = false
|
||||
isLenient = true
|
||||
})
|
||||
}
|
||||
defaultRequest {
|
||||
header(HttpHeaders.Authorization, "Bearer $portalApiKey")
|
||||
port = 443
|
||||
}
|
||||
followRedirects = false
|
||||
expectSuccess = false
|
||||
}
|
||||
|
||||
fun uploadMod() = runBlocking {
|
||||
client().useToRun {
|
||||
val initUploadResponse = initUpload()
|
||||
|
||||
val confirmation = userInputHandler
|
||||
.askQuestion("Are you sure you want to publish $modName:$modVersion?", "no")
|
||||
.toBooleanLenient() ?: false
|
||||
|
||||
if (confirmation) {
|
||||
upload(initUploadResponse)
|
||||
} else {
|
||||
println("aborting upload")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun HttpClient.initUpload(): InitUploadResponse.Success {
|
||||
|
||||
val response = submitForm(
|
||||
url = portalUploadEndpoint,
|
||||
formParameters = Parameters.build {
|
||||
append("mod", modName)
|
||||
}
|
||||
)
|
||||
|
||||
// val response = post(portalUploadEndpoint) {
|
||||
// contentType(ContentType.MultiPart.FormData)
|
||||
// setBody(Json.encodeToString(InitUploadRequest.serializer(), InitUploadRequest(modName)))
|
||||
//// setBody(InitUploadRequest(modName))
|
||||
// }
|
||||
println(response)
|
||||
val initUploadResponse: InitUploadResponse =
|
||||
Json.decodeFromString(InitUploadResponse.serializer(), response.bodyAsText())
|
||||
println(initUploadResponse)
|
||||
|
||||
require(response.status.isSuccess()) { "init upload request failed" }
|
||||
|
||||
return when (initUploadResponse) {
|
||||
is Failure -> error(initUploadResponse)
|
||||
is InitUploadResponse.Success -> initUploadResponse
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun HttpClient.upload(initUploadResponse: InitUploadResponse.Success) {
|
||||
|
||||
val requestForm = formData {
|
||||
append(
|
||||
"file",
|
||||
distributionZip.readBytes(),
|
||||
Headers.build {
|
||||
append(HttpHeaders.ContentType, ContentType.Application.Zip)
|
||||
append(HttpHeaders.ContentDisposition, "filename=${distributionZip.name}")
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
val response = post(initUploadResponse.uploadUrl) {
|
||||
setBody(MultiPartFormDataContent(requestForm))
|
||||
}
|
||||
|
||||
|
||||
// val response = submitFormWithBinaryData(initUploadResponse.uploadUrl, requestForm) {
|
||||
// header(HttpHeaders.Authorization, "Bearer $portalApiKey")
|
||||
// }
|
||||
|
||||
val submitUploadResponse =
|
||||
Json.decodeFromString(SubmitUploadResponse.serializer(), response.bodyAsText())
|
||||
|
||||
println(response)
|
||||
println(submitUploadResponse)
|
||||
require(response.status.isSuccess() && submitUploadResponse is SubmitUploadResponse.Success) { "upload request failed" }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
package dev.adamko.kafkatorio.factoriomod
|
||||
|
||||
import io.ktor.http.URLBuilder
|
||||
import io.ktor.http.takeFrom
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.file.RegularFileProperty
|
||||
import org.gradle.api.internal.tasks.userinput.UserInputHandler
|
||||
import org.gradle.api.model.ObjectFactory
|
||||
import org.gradle.api.provider.Property
|
||||
import org.gradle.api.provider.ProviderFactory
|
||||
import org.gradle.api.tasks.Input
|
||||
import org.gradle.api.tasks.InputFile
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
import org.gradle.kotlin.dsl.property
|
||||
import org.gradle.kotlin.dsl.support.get
|
||||
|
||||
|
||||
abstract class FactorioModPublishTask @Inject constructor(
|
||||
objects: ObjectFactory,
|
||||
providers: ProviderFactory,
|
||||
) : DefaultTask() {
|
||||
|
||||
@get:InputFile
|
||||
abstract val distributionZip: RegularFileProperty
|
||||
|
||||
@get:Input
|
||||
abstract val modName: Property<String>
|
||||
|
||||
@get:Input
|
||||
abstract val modVersion: Property<String>
|
||||
|
||||
// @get:Input
|
||||
// abstract val factorioVersion: Property<String>
|
||||
|
||||
/** Bearer API key */
|
||||
@get:Input
|
||||
val portalApiKey: Property<String> = objects.property<String>().convention(
|
||||
providers.gradleProperty("FACTORIO_MOD_PORTAL_API_KEY")
|
||||
)
|
||||
|
||||
@get:Input
|
||||
val portalApiUrl: Property<String> = objects.property<String>()
|
||||
.convention("https://mods.factorio.com/api/v2/")
|
||||
|
||||
@get:Input
|
||||
val portalUploadEndpoint: Property<String> = objects.property<String>()
|
||||
.convention("mods/releases/init_upload")
|
||||
|
||||
|
||||
init {
|
||||
group = "factorio"
|
||||
}
|
||||
|
||||
|
||||
@TaskAction
|
||||
fun deploy() = runBlocking {
|
||||
|
||||
|
||||
val userInputHandler = services.get<UserInputHandler>()
|
||||
|
||||
val portalUploadEndpoint = URLBuilder(portalApiUrl.get())
|
||||
.takeFrom(portalUploadEndpoint.get())
|
||||
.buildString()
|
||||
|
||||
val client = FactorioModPortalPublishClient(
|
||||
userInputHandler,
|
||||
distributionZip.get().asFile,
|
||||
modName.get(),
|
||||
modVersion.get(),
|
||||
portalApiKey.get(),
|
||||
portalUploadEndpoint,
|
||||
)
|
||||
|
||||
client.uploadMod()
|
||||
}
|
||||
|
||||
// private fun validateInfoJson() {
|
||||
// val modVersion = modVersion.get()
|
||||
// val factorioVersion = factorioVersion.get()
|
||||
//
|
||||
// val distributionZip = distributionZip.get().asFile.toOkioPath()
|
||||
//
|
||||
// val zipContents = FileSystem.SYSTEM.openZip(distributionZip)
|
||||
//
|
||||
// val infoJson = zipContents.read("info.json".toPath()) {
|
||||
// generateSequence { readUtf8Line() }
|
||||
// }.joinToString("\n")
|
||||
//
|
||||
// val info: FactorioModInfo = Json.decodeFromString(FactorioModInfo.serializer(), infoJson)
|
||||
//
|
||||
// require(info.version == modVersion) {
|
||||
// "mod version mismatch, ${info.version} vs $modVersion "
|
||||
// }
|
||||
//
|
||||
// require(info.factorioVersion == factorioVersion) {
|
||||
// "Factorio version mismatch, ${info.factorioVersion} vs $factorioVersion "
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
|
@ -0,0 +1,91 @@
|
|||
package dev.adamko.kafkatorio.factoriomod
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.JsonContentPolymorphicSerializer
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
|
||||
|
||||
//@Serializable
|
||||
//data class InitUploadRequest(
|
||||
// @SerialName("mod") val modName: String,
|
||||
//)
|
||||
|
||||
|
||||
@Serializable(with = InitUploadResponse.Serializer::class)
|
||||
sealed interface InitUploadResponse {
|
||||
|
||||
/**
|
||||
* @param[uploadUrl] URL the mod zip file should be uploaded to
|
||||
*/
|
||||
@Serializable
|
||||
data class Success(
|
||||
@SerialName("upload_url") val uploadUrl: String,
|
||||
) : InitUploadResponse
|
||||
|
||||
object Serializer :
|
||||
JsonContentPolymorphicSerializer<InitUploadResponse>(InitUploadResponse::class) {
|
||||
override fun selectDeserializer(element: JsonElement) = when {
|
||||
"upload_url" in element.jsonObject -> Success.serializer()
|
||||
else -> Failure.serializer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Serializable(with = SubmitUploadResponse.Serializer::class)
|
||||
sealed interface SubmitUploadResponse {
|
||||
|
||||
@Serializable
|
||||
object Success : SubmitUploadResponse
|
||||
|
||||
object Serializer : JsonContentPolymorphicSerializer<SubmitUploadResponse>(
|
||||
SubmitUploadResponse::class
|
||||
) {
|
||||
override fun selectDeserializer(element: JsonElement) = when {
|
||||
"success" in element.jsonObject -> Success.serializer()
|
||||
else -> Failure.serializer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Serializable
|
||||
data class Failure(
|
||||
val error: String? = null,
|
||||
val message: String? = null,
|
||||
) : InitUploadResponse, SubmitUploadResponse {
|
||||
|
||||
private val reason: String?
|
||||
get() = reasons[error]
|
||||
|
||||
override fun toString() = "Failure(error=$error, message=$message, reason=$reason)"
|
||||
|
||||
companion object {
|
||||
private val reasons: Map<String?, String> = mapOf(
|
||||
//@formatter:off
|
||||
"InvalidApiKey" to "Missing or invalid API key for the current endpoint",
|
||||
"InvalidRequest" to "Invalid request.",
|
||||
"InternalError" to "Internal error, please try again later.",
|
||||
"Forbidden" to "Insufficient permission for current endpoint",
|
||||
"Unknown" to "Unknown error, please try again later.",
|
||||
"InvalidModRelease" to "Invalid release data in info.json",
|
||||
"InvalidModUpload" to "Invalid mod data in zipfile",
|
||||
"UnknownMod" to "Mod does not exist in mod portal",
|
||||
//@formatter:on
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Serializable
|
||||
data class FactorioModInfo(
|
||||
val name: String,
|
||||
val version: String,
|
||||
val title: String,
|
||||
val description: String,
|
||||
val author: String,
|
||||
@SerialName("factorio_version") val factorioVersion: String,
|
||||
val dependencies: List<String>,
|
||||
)
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
package dev.adamko.kafkatorio.lang
|
||||
|
||||
import dev.adamko.kafkatorio.Versions
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
|
||||
plugins {
|
||||
|
|
@ -8,41 +7,45 @@ plugins {
|
|||
kotlin("jvm")
|
||||
}
|
||||
|
||||
val projectKotlinTarget = "1.6"
|
||||
val projectJvmTarget = "11"
|
||||
|
||||
|
||||
dependencies {
|
||||
|
||||
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
|
||||
// implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
|
||||
|
||||
testImplementation(platform("org.junit:junit-bom:${Versions.junit}"))
|
||||
// testImplementation(platform("org.junit:junit-bom:${Versions.junit}"))
|
||||
testImplementation("org.junit.jupiter:junit-jupiter")
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher") {
|
||||
because("Only needed to run tests in a version of IntelliJ IDEA that bundles older versions")
|
||||
}
|
||||
// testRuntimeOnly("org.junit.platform:junit-platform-launcher") {
|
||||
// because("Only needed to run tests in a version of IntelliJ IDEA that bundles older versions")
|
||||
// }
|
||||
|
||||
testImplementation(platform("io.kotest:kotest-bom:${Versions.kotest}"))
|
||||
// testImplementation(platform("io.kotest:kotest-bom:${Versions.kotest}"))
|
||||
testImplementation("io.kotest:kotest-runner-junit5")
|
||||
testImplementation("io.kotest:kotest-assertions-core")
|
||||
testImplementation("io.kotest:kotest-property")
|
||||
testImplementation("io.kotest:kotest-assertions-json")
|
||||
|
||||
testImplementation("io.mockk:mockk:${Versions.mockk}")
|
||||
|
||||
testImplementation("io.mockk:mockk")
|
||||
}
|
||||
|
||||
tasks.withType<KotlinCompile>().configureEach {
|
||||
|
||||
kotlinOptions {
|
||||
jvmTarget = Versions.jvmTarget
|
||||
apiVersion = Versions.kotlinLang
|
||||
languageVersion = Versions.kotlinLang
|
||||
freeCompilerArgs += listOf(
|
||||
jvmTarget = projectJvmTarget
|
||||
apiVersion = projectKotlinTarget
|
||||
languageVersion = projectKotlinTarget
|
||||
}
|
||||
|
||||
kotlinOptions.freeCompilerArgs += listOf(
|
||||
// "-Xcontext-receivers",
|
||||
"-opt-in=kotlin.RequiresOptIn",
|
||||
"-opt-in=kotlin.ExperimentalStdlibApi",
|
||||
"-opt-in=kotlin.time.ExperimentalTime",
|
||||
"-opt-in=kotlin.RequiresOptIn",
|
||||
"-opt-in=kotlin.ExperimentalStdlibApi",
|
||||
"-opt-in=kotlin.time.ExperimentalTime",
|
||||
// "-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
|
||||
// "-opt-in=kotlinx.serialization.ExperimentalSerializationApi",
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
tasks.compileTestKotlin {
|
||||
|
|
@ -51,10 +54,10 @@ tasks.compileTestKotlin {
|
|||
|
||||
kotlin {
|
||||
jvmToolchain {
|
||||
(this as JavaToolchainSpec).languageVersion.set(JavaLanguageVersion.of(Versions.jvmTarget))
|
||||
(this as JavaToolchainSpec).languageVersion.set(JavaLanguageVersion.of(projectJvmTarget))
|
||||
}
|
||||
}
|
||||
|
||||
tasks.withType<Test> {
|
||||
tasks.withType<Test>().configureEach {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,6 +52,6 @@ val updatePackageJson by tasks.registering(UpdatePackageJson::class) {
|
|||
|
||||
tasks.assemble { dependsOn(updatePackageJson) }
|
||||
|
||||
tasks.withType<NpmInstallTask> {
|
||||
tasks.withType<NpmInstallTask>().configureEach {
|
||||
dependsOn(updatePackageJson)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,23 +0,0 @@
|
|||
package dev.adamko.kafkatorio.task
|
||||
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.http4k.client.OkHttp
|
||||
import org.http4k.core.Method
|
||||
import org.http4k.core.Request
|
||||
|
||||
abstract class FactorioModPortalDeploy : DefaultTask() {
|
||||
|
||||
init {
|
||||
super.doFirst {
|
||||
|
||||
val client = OkHttp()
|
||||
|
||||
val request = Request(Method.GET, "https://factorio.com/login?mods=1")
|
||||
|
||||
println(client(request))
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -22,7 +22,7 @@ object JsonPropertiesUpToDateSpec : ExplainingSpec<Task> {
|
|||
|
||||
else -> {
|
||||
|
||||
val packageJsonContent = packageJsonFile.get().asFile.readText()
|
||||
val packageJsonContent = packageJsonFile2.get().asFile.readText()
|
||||
val packageJson = jsonMapper.parseToJsonElement(packageJsonContent).jsonObject
|
||||
|
||||
val outdatedProperties = propertiesToCheck.get().mapNotNull { (key, expectedVal) ->
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package dev.adamko.kafkatorio.task
|
|||
|
||||
import com.github.gradle.node.npm.task.NpmTask
|
||||
import javax.inject.Inject
|
||||
import org.gradle.api.file.ConfigurableFileTree
|
||||
import org.gradle.api.file.DirectoryProperty
|
||||
import org.gradle.api.file.FileSystemOperations
|
||||
import org.gradle.api.plugins.BasePlugin
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ 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
|
||||
|
|
@ -20,14 +21,15 @@ import org.jetbrains.kotlin.util.suffixIfNot
|
|||
|
||||
@CacheableTask
|
||||
abstract class UpdatePackageJson @Inject constructor(
|
||||
private val providers: ProviderFactory
|
||||
private val providers: ProviderFactory,
|
||||
) : DefaultTask() {
|
||||
|
||||
@get:Input
|
||||
abstract val propertiesToCheck: MapProperty<String, String>
|
||||
|
||||
@get:OutputFile
|
||||
abstract val packageJsonFile: RegularFileProperty
|
||||
// @get:OutputFile
|
||||
@get:Internal
|
||||
abstract val packageJsonFile2: RegularFileProperty
|
||||
|
||||
init {
|
||||
group = NodePlugin.NODE_GROUP
|
||||
|
|
@ -36,26 +38,37 @@ abstract class UpdatePackageJson @Inject constructor(
|
|||
|
||||
outputs.upToDateWhen(JsonPropertiesUpToDateSpec)
|
||||
|
||||
super.doFirst {
|
||||
propertiesToCheck.disallowChanges()
|
||||
packageJsonFile.disallowChanges()
|
||||
// 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 = packageJsonFile.asFile.get()
|
||||
val propertiesToCheck = propertiesToCheck.get()
|
||||
val packageJsonFile = packageJsonFile2.asFile.get()
|
||||
|
||||
logger.info("updating package.json ${packageJsonFile.canonicalPath}")
|
||||
val packageJson = jsonMapper.parseToJsonElement(packageJsonFile.readText()).jsonObject
|
||||
logger.lifecycle("updating package.json ${packageJsonFile.canonicalPath}")
|
||||
|
||||
val newJsonProps = propertiesToCheck.mapValues { (_, newVal) -> JsonPrimitive(newVal) }
|
||||
val currentPackageJson = currentPackageJson()
|
||||
val updatedPackageJson = updatedPackageJson(currentPackageJson)
|
||||
|
||||
val packageJsonUpdate = JsonObject(packageJson + newJsonProps)
|
||||
val packageJsonContentUpdated =
|
||||
jsonMapper
|
||||
.encodeToString(packageJsonUpdate)
|
||||
.encodeToString(updatedPackageJson)
|
||||
.suffixIfNot("\n")
|
||||
|
||||
logger.debug(packageJsonContentUpdated)
|
||||
|
|
@ -64,4 +77,15 @@ abstract class UpdatePackageJson @Inject constructor(
|
|||
write(packageJsonContentUpdated)
|
||||
}
|
||||
}
|
||||
|
||||
private fun currentPackageJson(): JsonObject {
|
||||
val packageJsonFile = packageJsonFile2.asFile.get()
|
||||
return jsonMapper.parseToJsonElement(packageJsonFile.readText()).jsonObject
|
||||
}
|
||||
|
||||
private fun updatedPackageJson(currentPackageJson: JsonObject): JsonObject {
|
||||
val propertiesToCheck = propertiesToCheck.get()
|
||||
val newJsonProps = propertiesToCheck.mapValues { (_, newVal) -> JsonPrimitive(newVal) }
|
||||
return JsonObject(currentPackageJson + newJsonProps)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue