|
| 1 | +/* |
| 2 | + * Copyright 2026, TeamDev. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Redistribution and use in source and/or binary forms, with or without |
| 11 | + * modification, must retain the above copyright notice and the following |
| 12 | + * disclaimer. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 15 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 16 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 17 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 18 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +package io.spine.gradle.docs |
| 28 | + |
| 29 | +import java.io.File |
| 30 | +import org.gradle.api.DefaultTask |
| 31 | +import org.gradle.api.file.DirectoryProperty |
| 32 | +import org.gradle.api.provider.Property |
| 33 | +import org.gradle.api.tasks.Input |
| 34 | +import org.gradle.api.tasks.InputDirectory |
| 35 | +import org.gradle.api.tasks.Optional |
| 36 | +import org.gradle.api.tasks.TaskAction |
| 37 | + |
| 38 | +/** |
| 39 | + * Updates the version of a Gradle plugin in `build.gradle.kts` files. |
| 40 | + * |
| 41 | + * The task searches for plugin declarations in the format |
| 42 | + * `id("plugin-id") version "version-number"` and replaces |
| 43 | + * the version number with the one found in the version script file. |
| 44 | + * |
| 45 | + * @property directory |
| 46 | + * The directory to scan recursively for `build.gradle.kts` files. |
| 47 | + * @property version |
| 48 | + * The version number to set for the plugin. |
| 49 | + * @property pluginId |
| 50 | + * The ID of the plugin whose version should be updated. |
| 51 | + * @property kotlinVersion |
| 52 | + * Optional. If set, updates the version of the Kotlin plugin declared with |
| 53 | + * `kotlin("…") version "…"` syntax in the `plugins` block. |
| 54 | + * This option works in combination with the [version] and [pluginId] properties. |
| 55 | + */ |
| 56 | +abstract class UpdatePluginVersion : DefaultTask() { |
| 57 | + |
| 58 | + @get:InputDirectory |
| 59 | + abstract val directory: DirectoryProperty |
| 60 | + |
| 61 | + @get:Input |
| 62 | + abstract val version: Property<String> |
| 63 | + |
| 64 | + @get:Input |
| 65 | + abstract val pluginId: Property<String> |
| 66 | + |
| 67 | + @get:Input |
| 68 | + @get:Optional |
| 69 | + abstract val kotlinVersion: Property<String> |
| 70 | + |
| 71 | + /** |
| 72 | + * Updates plugin versions in build files within the path in the [directory]. |
| 73 | + */ |
| 74 | + @TaskAction |
| 75 | + fun update() { |
| 76 | + val rootDir = directory.get().asFile |
| 77 | + |
| 78 | + val kotlinVersionSet = kotlinVersion.isPresent |
| 79 | + val kotlinVer = kotlinVersion.orNull |
| 80 | + val id = pluginId.get() |
| 81 | + val ver = version.get() |
| 82 | + |
| 83 | + rootDir.walkTopDown() |
| 84 | + .filter { it.name == "build.gradle.kts" } |
| 85 | + .forEach { file -> |
| 86 | + if (kotlinVersionSet && kotlinVer != null) { |
| 87 | + updateKotlinPluginVersion(file, kotlinVer) |
| 88 | + } |
| 89 | + updatePluginVersion(file, id, ver) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private fun updatePluginVersion(file: File, id: String, version: String) { |
| 94 | + val content = file.readText() |
| 95 | + // Regex to match: id("plugin-id") version "version-number" |
| 96 | + val regex = """id\("$id"\)\s+version\s+"([^"]+)"""".toRegex() |
| 97 | + |
| 98 | + if (regex.containsMatchIn(content)) { |
| 99 | + val updatedContent = regex.replace(content) { |
| 100 | + "id(\"$id\") version \"$version\"" |
| 101 | + } |
| 102 | + if (content != updatedContent) { |
| 103 | + file.writeText(updatedContent) |
| 104 | + logger.info("Updated version of '$id' in `${file.absolutePath}`.") |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private fun updateKotlinPluginVersion(file: File, kotlinVersion: String) { |
| 110 | + val content = file.readText() |
| 111 | + // Regex to match Kotlin plugin declarations like: kotlin("jvm") version "1.9.0" |
| 112 | + val regex = """kotlin\("([^"]+)"\)\s+version\s+"([^"]+)"""".toRegex() |
| 113 | + if (regex.containsMatchIn(content)) { |
| 114 | + val updatedContent = regex.replace(content) { matchResult -> |
| 115 | + val plugin = matchResult.groupValues[1] |
| 116 | + "kotlin(\"$plugin\") version \"$kotlinVersion\"" |
| 117 | + } |
| 118 | + if (content != updatedContent) { |
| 119 | + file.writeText(updatedContent) |
| 120 | + logger.info("Updated Kotlin plugin version in `${file.absolutePath}`.") |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments