|
| 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 | +import org.gradle.api.Project |
| 28 | +import org.gradle.api.Task |
| 29 | +import org.gradle.api.tasks.TaskProvider |
| 30 | +import org.gradle.kotlin.dsl.named |
| 31 | +import org.gradle.kotlin.dsl.register |
| 32 | + |
| 33 | +/** |
| 34 | + * Registers a `patchGeneratedTemplateString` task in the project, which replaces |
| 35 | + * references to the legacy `io.spine.validation.TemplateString` class (and its |
| 36 | + * proto FQN `.spine.validation.TemplateString`) with `io.spine.string.TemplateString` |
| 37 | + * in the `generated/` sources. |
| 38 | + * |
| 39 | + * The task is wired to run after [upstreamTask] (the task that produces the sources |
| 40 | + * to be patched) and before `compileJava` and `kspKotlin`. |
| 41 | + * |
| 42 | + * @param upstreamTask the name of the task whose output should be patched |
| 43 | + * (e.g., `generateProto` or `launchSpineCompiler`). |
| 44 | + */ |
| 45 | +fun Project.patchGeneratedTemplateString(upstreamTask: String): TaskProvider<Task> { |
| 46 | + val patchTask = tasks.register("patchGeneratedTemplateString") { |
| 47 | + dependsOn(upstreamTask) |
| 48 | + |
| 49 | + val generatedDir = layout.projectDirectory.dir("generated") |
| 50 | + inputs.dir(generatedDir).withPropertyName("generatedSources") |
| 51 | + outputs.dir(generatedDir).withPropertyName("patchedSources") |
| 52 | + |
| 53 | + doLast { |
| 54 | + val oldClassRef = "io.spine.validation.TemplateString" |
| 55 | + val newClassRef = "io.spine.string.TemplateString" |
| 56 | + val oldProtoRef = ".spine.validation.TemplateString" |
| 57 | + val newProtoRef = ".spine.string.TemplateString" |
| 58 | + generatedDir.asFile.walkTopDown() |
| 59 | + .filter { it.isFile && (it.extension == "java" || it.extension == "kt") } |
| 60 | + .forEach { file -> |
| 61 | + val original = file.readText() |
| 62 | + if (original.contains(oldClassRef) || original.contains(oldProtoRef)) { |
| 63 | + val patched = original |
| 64 | + .replace(oldClassRef, newClassRef) |
| 65 | + .replace(oldProtoRef, newProtoRef) |
| 66 | + file.writeText(patched) |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + tasks.named("compileJava") { |
| 73 | + dependsOn(patchTask) |
| 74 | + } |
| 75 | + |
| 76 | + afterEvaluate { |
| 77 | + tasks.named("kspKotlin") { |
| 78 | + dependsOn(patchTask) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return patchTask |
| 83 | +} |
0 commit comments