|
| 1 | +/* |
| 2 | + * Copyright (c) 2024. |
| 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 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.modelix |
| 18 | + |
| 19 | +import org.gradle.api.Project |
| 20 | +import org.gradle.kotlin.dsl.configure |
| 21 | +import org.gradle.kotlin.dsl.get |
| 22 | +import org.gradle.kotlin.dsl.withType |
| 23 | +import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension |
| 24 | +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension |
| 25 | +import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper |
| 26 | +import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper |
| 27 | + |
| 28 | +/** |
| 29 | + * Causes code containing the Modelix version to be generated and used in a Kotlin project. |
| 30 | + * The Kotlin project can be a JVM or Multiplatform project. |
| 31 | + */ |
| 32 | +fun Project.registerVersionGenerationTask(packageName: String) { |
| 33 | + val packagePath = packageName.replace('.', '/') |
| 34 | + |
| 35 | + val generateVersionVariable = tasks.register("generateVersionVariable") { |
| 36 | + doLast { |
| 37 | + val fileContent = buildString { |
| 38 | + appendLine("package $packageName") |
| 39 | + appendLine() |
| 40 | + appendLine("""const val MODELIX_VERSION: String = "$version"""") |
| 41 | + if (project.name == "modelql-core") { |
| 42 | + appendLine("""@Deprecated("Use the new MODELIX_VERSION", replaceWith = ReplaceWith("MODELIX_VERSION"))""") |
| 43 | + appendLine("const val modelqlVersion: String = MODELIX_VERSION") |
| 44 | + } |
| 45 | + } |
| 46 | + val outputDir = layout.buildDirectory.dir("version_gen/$packagePath").get().asFile |
| 47 | + outputDir.mkdirs() |
| 48 | + outputDir.resolve("Version.kt").writeText(fileContent) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Generate version constant for Kotlin JVM projects |
| 53 | + plugins.withType<KotlinPluginWrapper> { |
| 54 | + extensions.configure<KotlinJvmProjectExtension> { |
| 55 | + sourceSets["main"].kotlin.srcDir(layout.buildDirectory.dir("version_gen")) |
| 56 | + } |
| 57 | + |
| 58 | + tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().all { |
| 59 | + dependsOn(generateVersionVariable) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Generate version constant for Kotlin Multiplatform projects |
| 64 | + plugins.withType<KotlinMultiplatformPluginWrapper> { |
| 65 | + extensions.configure<KotlinMultiplatformExtension> { |
| 66 | + sourceSets.commonMain { |
| 67 | + kotlin.srcDir(layout.buildDirectory.dir("version_gen")) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().all { |
| 72 | + dependsOn(generateVersionVariable) |
| 73 | + } |
| 74 | + |
| 75 | + tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompileCommon>().all { |
| 76 | + dependsOn(generateVersionVariable) |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments