Skip to content

Commit

Permalink
Customize KotlinCompilerVersion in symbol-processing-aa-embeddable
Browse files Browse the repository at this point in the history
Also renamed META-INF/compiler.version to META-INF/ksp.compiler.version

(cherry picked from commit 90e5b80)
  • Loading branch information
ting-yuan authored and KSP Auto Pick committed Mar 7, 2024
1 parent e74274e commit 28cb8ff
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
29 changes: 28 additions & 1 deletion symbol-processing-aa-embeddable/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import java.util.zip.ZipFile

evaluationDependsOn(":kotlin-analysis-api")

val kotlinBaseVersion: String by project
val signingKey: String? by project
val signingPassword: String? by project

Expand Down Expand Up @@ -68,6 +67,7 @@ tasks.withType<ShadowJar> {
relocate(f, t)
}
mergeServiceFiles()
exclude("META-INF/compiler.version")

// All bundled dependencies should be renamed.
doLast {
Expand Down Expand Up @@ -203,3 +203,30 @@ signing {
useInMemoryPgpKeys(signingKey, signingPassword)
sign(extensions.getByType<PublishingExtension>().publications)
}

abstract class WriteVersionSrcTask @Inject constructor(
@get:Input val kotlinVersion: String,
@get:OutputDirectory val outputResDir: File
) : DefaultTask() {
@TaskAction
fun generate() {
val metaInfDir = File(outputResDir, "META-INF")
metaInfDir.mkdirs()
File(metaInfDir, "ksp.compiler.version").writeText(kotlinVersion)
}
}

val kspVersionDir = File(project.buildDir, "generated/ksp-versions/META-INF")
val writeVersionSrcTask = tasks.register<WriteVersionSrcTask>(
"generateKSPVersions",
aaKotlinBaseVersion,
kspVersionDir
)

kotlin {
sourceSets {
main {
resources.srcDir(writeVersionSrcTask.map { it.outputResDir })
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

package org.jetbrains.kotlin.config;

import org.jetbrains.annotations.Nullable;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class KotlinCompilerVersion {
public static final String VERSION_FILE_PATH = "/META-INF/ksp.compiler.version";
public static final String VERSION;

// True if the latest stable language version supported by this compiler has not yet been released.
// Binaries produced by this compiler with that language version (or any future language version) are going to be marked
// as "pre-release" and will not be loaded by release versions of the compiler.
// Change this value before and after every major release
private static final boolean IS_PRE_RELEASE = true;

public static final String TEST_IS_PRE_RELEASE_SYSTEM_PROPERTY = "kotlin.test.is.pre.release";

public static boolean isPreRelease() {
String overridden = System.getProperty(TEST_IS_PRE_RELEASE_SYSTEM_PROPERTY);
if (overridden != null) {
return Boolean.parseBoolean(overridden);
}

return IS_PRE_RELEASE;
}

/**
* @return version of this compiler, or `null` if it isn't known (if VERSION is "@snapshot@")
*/
@Nullable
public static String getVersion() {
return VERSION.equals("@snapshot@") ? null : VERSION;
}

@SuppressWarnings({"TryFinallyCanBeTryWithResources", "ConstantConditions"})
private static String loadKotlinCompilerVersion() throws IOException {
BufferedReader versionReader = new BufferedReader(
new InputStreamReader(KotlinCompilerVersion.class.getResourceAsStream(VERSION_FILE_PATH)));
try {
return versionReader.readLine();
} finally {
versionReader.close();
}
}

static {
try {
VERSION = loadKotlinCompilerVersion();
}
catch (IOException e) {
throw new IllegalStateException("Failed to read compiler version from " + VERSION_FILE_PATH);
}
}
}

0 comments on commit 28cb8ff

Please sign in to comment.