|
| 1 | +/* |
| 2 | + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. |
| 3 | + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. |
| 4 | + */ |
| 5 | + |
| 6 | +package org.jetbrains.kotlin.config; |
| 7 | + |
| 8 | +import org.jetbrains.annotations.Nullable; |
| 9 | + |
| 10 | +import java.io.BufferedReader; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStreamReader; |
| 13 | + |
| 14 | +public class KotlinCompilerVersion { |
| 15 | + public static final String VERSION_FILE_PATH = "/META-INF/ksp.compiler.version"; |
| 16 | + public static final String VERSION; |
| 17 | + |
| 18 | + // True if the latest stable language version supported by this compiler has not yet been released. |
| 19 | + // Binaries produced by this compiler with that language version (or any future language version) are going to be marked |
| 20 | + // as "pre-release" and will not be loaded by release versions of the compiler. |
| 21 | + // Change this value before and after every major release |
| 22 | + private static final boolean IS_PRE_RELEASE = true; |
| 23 | + |
| 24 | + public static final String TEST_IS_PRE_RELEASE_SYSTEM_PROPERTY = "kotlin.test.is.pre.release"; |
| 25 | + |
| 26 | + public static boolean isPreRelease() { |
| 27 | + String overridden = System.getProperty(TEST_IS_PRE_RELEASE_SYSTEM_PROPERTY); |
| 28 | + if (overridden != null) { |
| 29 | + return Boolean.parseBoolean(overridden); |
| 30 | + } |
| 31 | + |
| 32 | + return IS_PRE_RELEASE; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @return version of this compiler, or `null` if it isn't known (if VERSION is "@snapshot@") |
| 37 | + */ |
| 38 | + @Nullable |
| 39 | + public static String getVersion() { |
| 40 | + return VERSION.equals("@snapshot@") ? null : VERSION; |
| 41 | + } |
| 42 | + |
| 43 | + @SuppressWarnings({"TryFinallyCanBeTryWithResources", "ConstantConditions"}) |
| 44 | + private static String loadKotlinCompilerVersion() throws IOException { |
| 45 | + BufferedReader versionReader = new BufferedReader( |
| 46 | + new InputStreamReader(KotlinCompilerVersion.class.getResourceAsStream(VERSION_FILE_PATH))); |
| 47 | + try { |
| 48 | + return versionReader.readLine(); |
| 49 | + } finally { |
| 50 | + versionReader.close(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + static { |
| 55 | + try { |
| 56 | + VERSION = loadKotlinCompilerVersion(); |
| 57 | + } |
| 58 | + catch (IOException e) { |
| 59 | + throw new IllegalStateException("Failed to read compiler version from " + VERSION_FILE_PATH); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments