Refactor gradle option generation mechanism
Ensure there's a statically checked dependency on LanguageVersion and JvmTarget, so that this information is updated automatically once a new language version or a JVM target is added
This commit is contained in:
+3
-4
@@ -25,17 +25,16 @@ import java.util.List;
|
|||||||
public abstract class CommonCompilerArguments {
|
public abstract class CommonCompilerArguments {
|
||||||
public static final String PLUGIN_OPTION_FORMAT = "plugin:<pluginId>:<optionName>=<value>";
|
public static final String PLUGIN_OPTION_FORMAT = "plugin:<pluginId>:<optionName>=<value>";
|
||||||
|
|
||||||
// todo: reuse LanguageVersion from frontend
|
@GradleOption(DefaultValues.LanguageVersions.class)
|
||||||
@GradleOption(defaultValue = "\"1.1\"", possibleValues = { "\"1.0\", \"1.1\"" })
|
|
||||||
@Argument(value = "language-version", description = "Provide source compatibility with specified language version")
|
@Argument(value = "language-version", description = "Provide source compatibility with specified language version")
|
||||||
@ValueDescription("<version>")
|
@ValueDescription("<version>")
|
||||||
public String languageVersion;
|
public String languageVersion;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "false")
|
@GradleOption(DefaultValues.BooleanFalseDefault.class)
|
||||||
@Argument(value = "nowarn", description = "Generate no warnings")
|
@Argument(value = "nowarn", description = "Generate no warnings")
|
||||||
public boolean suppressWarnings;
|
public boolean suppressWarnings;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "false")
|
@GradleOption(DefaultValues.BooleanFalseDefault.class)
|
||||||
@Argument(value = "verbose", description = "Enable verbose logging output")
|
@Argument(value = "verbose", description = "Enable verbose logging output")
|
||||||
public boolean verbose;
|
public boolean verbose;
|
||||||
|
|
||||||
|
|||||||
+55
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2016 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.cli.common.arguments
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants.CALL
|
||||||
|
import org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants.NO_CALL
|
||||||
|
import org.jetbrains.kotlin.config.JvmTarget
|
||||||
|
import org.jetbrains.kotlin.config.LanguageVersion
|
||||||
|
|
||||||
|
open class DefaultValues(val defaultValue: String, val possibleValues: List<String>? = null) {
|
||||||
|
object BooleanFalseDefault : DefaultValues("false")
|
||||||
|
|
||||||
|
object BooleanTrueDefault : DefaultValues("true")
|
||||||
|
|
||||||
|
object StringNullDefault : DefaultValues("null")
|
||||||
|
|
||||||
|
object LanguageVersions : DefaultValues(
|
||||||
|
"\"" + LanguageVersion.LATEST.versionString + "\"",
|
||||||
|
LanguageVersion.values().map { "\"${it.versionString}\"" }
|
||||||
|
)
|
||||||
|
|
||||||
|
object JvmTargetVersions : DefaultValues(
|
||||||
|
"\"" + JvmTarget.DEFAULT.string + "\"",
|
||||||
|
JvmTarget.values().map { "\"${it.string}\"" }
|
||||||
|
)
|
||||||
|
|
||||||
|
object JsEcmaVersions : DefaultValues(
|
||||||
|
"\"v5\"",
|
||||||
|
listOf("\"v5\"")
|
||||||
|
)
|
||||||
|
|
||||||
|
object JsModuleKinds : DefaultValues(
|
||||||
|
"\"plain\"",
|
||||||
|
listOf("\"plain\"", "\"amd\"", "\"commonjs\"", "\"umd\"")
|
||||||
|
)
|
||||||
|
|
||||||
|
object JsMain : DefaultValues(
|
||||||
|
"\"" + NO_CALL + "\"",
|
||||||
|
listOf("\"" + CALL + "\"", "\"" + NO_CALL + "\"")
|
||||||
|
)
|
||||||
|
}
|
||||||
+3
-2
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.cli.common.arguments
|
package org.jetbrains.kotlin.cli.common.arguments
|
||||||
|
|
||||||
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
@Retention(AnnotationRetention.RUNTIME)
|
@Retention(AnnotationRetention.RUNTIME)
|
||||||
annotation class GradleOption(val defaultValue: String,
|
annotation class GradleOption(val value: KClass<out DefaultValues> = DefaultValues::class)
|
||||||
val possibleValues: Array<String> = arrayOf())
|
|
||||||
|
|||||||
+9
-9
@@ -24,12 +24,12 @@ import static org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants.CA
|
|||||||
import static org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants.NO_CALL;
|
import static org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants.NO_CALL;
|
||||||
|
|
||||||
public class K2JSCompilerArguments extends CommonCompilerArguments {
|
public class K2JSCompilerArguments extends CommonCompilerArguments {
|
||||||
@GradleOption(defaultValue = "null")
|
@GradleOption(DefaultValues.StringNullDefault.class)
|
||||||
@Argument(value = "output", description = "Output file path")
|
@Argument(value = "output", description = "Output file path")
|
||||||
@ValueDescription("<path>")
|
@ValueDescription("<path>")
|
||||||
public String outputFile;
|
public String outputFile;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "true")
|
@GradleOption(DefaultValues.BooleanTrueDefault.class)
|
||||||
@Argument(value = "no-stdlib", description = "Don't use bundled Kotlin stdlib")
|
@Argument(value = "no-stdlib", description = "Don't use bundled Kotlin stdlib")
|
||||||
public boolean noStdlib;
|
public boolean noStdlib;
|
||||||
|
|
||||||
@@ -37,29 +37,29 @@ public class K2JSCompilerArguments extends CommonCompilerArguments {
|
|||||||
@ValueDescription("<path[,]>")
|
@ValueDescription("<path[,]>")
|
||||||
public String[] libraryFiles;
|
public String[] libraryFiles;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "false")
|
@GradleOption(DefaultValues.BooleanFalseDefault.class)
|
||||||
@Argument(value = "source-map", description = "Generate source map")
|
@Argument(value = "source-map", description = "Generate source map")
|
||||||
public boolean sourceMap;
|
public boolean sourceMap;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "true")
|
@GradleOption(DefaultValues.BooleanTrueDefault.class)
|
||||||
@Argument(value = "meta-info", description = "Generate metadata")
|
@Argument(value = "meta-info", description = "Generate metadata")
|
||||||
public boolean metaInfo;
|
public boolean metaInfo;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "true")
|
@GradleOption(DefaultValues.BooleanTrueDefault.class)
|
||||||
@Argument(value = "kjsm", description = "Generate kjsm-files (for creating libraries)")
|
@Argument(value = "kjsm", description = "Generate kjsm-files (for creating libraries)")
|
||||||
public boolean kjsm;
|
public boolean kjsm;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "\"v5\"", possibleValues = { "\"v5\"" })
|
@GradleOption(DefaultValues.JsEcmaVersions.class)
|
||||||
@Argument(value = "target", description = "Generate JS files for specific ECMA version)")
|
@Argument(value = "target", description = "Generate JS files for specific ECMA version")
|
||||||
@ValueDescription("{ v5 }")
|
@ValueDescription("{ v5 }")
|
||||||
public String target;
|
public String target;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "\"plain\"", possibleValues = { "\"plain\"", "\"amd\"", "\"commonjs\"", "\"umd\"" })
|
@GradleOption(DefaultValues.JsModuleKinds.class)
|
||||||
@Argument(value = "module-kind", description = "Kind of a module generated by compiler")
|
@Argument(value = "module-kind", description = "Kind of a module generated by compiler")
|
||||||
@ValueDescription("{ plain, amd, commonjs, umd }")
|
@ValueDescription("{ plain, amd, commonjs, umd }")
|
||||||
public String moduleKind;
|
public String moduleKind;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "\"" + NO_CALL + "\"", possibleValues = { "\"" + CALL + "\"", "\"" + NO_CALL + "\"" })
|
@GradleOption(DefaultValues.JsMain.class)
|
||||||
@Nullable
|
@Nullable
|
||||||
@Argument(value = "main", description = "Whether a main function should be called")
|
@Argument(value = "main", description = "Whether a main function should be called")
|
||||||
@ValueDescription("{" + CALL + "," + NO_CALL + "}")
|
@ValueDescription("{" + CALL + "," + NO_CALL + "}")
|
||||||
|
|||||||
+6
-6
@@ -28,24 +28,24 @@ public class K2JVMCompilerArguments extends CommonCompilerArguments {
|
|||||||
@ValueDescription("<path>")
|
@ValueDescription("<path>")
|
||||||
public String classpath;
|
public String classpath;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "false")
|
@GradleOption(DefaultValues.BooleanFalseDefault.class)
|
||||||
@Argument(value = "include-runtime", description = "Include Kotlin runtime in to resulting .jar")
|
@Argument(value = "include-runtime", description = "Include Kotlin runtime in to resulting .jar")
|
||||||
public boolean includeRuntime;
|
public boolean includeRuntime;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "null")
|
@GradleOption(DefaultValues.StringNullDefault.class)
|
||||||
@Argument(value = "jdk-home", description = "Path to JDK home directory to include into classpath, if differs from default JAVA_HOME")
|
@Argument(value = "jdk-home", description = "Path to JDK home directory to include into classpath, if differs from default JAVA_HOME")
|
||||||
@ValueDescription("<path>")
|
@ValueDescription("<path>")
|
||||||
public String jdkHome;
|
public String jdkHome;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "false")
|
@GradleOption(DefaultValues.BooleanFalseDefault.class)
|
||||||
@Argument(value = "no-jdk", description = "Don't include Java runtime into classpath")
|
@Argument(value = "no-jdk", description = "Don't include Java runtime into classpath")
|
||||||
public boolean noJdk;
|
public boolean noJdk;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "true")
|
@GradleOption(DefaultValues.BooleanTrueDefault.class)
|
||||||
@Argument(value = "no-stdlib", description = "Don't include Kotlin runtime into classpath")
|
@Argument(value = "no-stdlib", description = "Don't include Kotlin runtime into classpath")
|
||||||
public boolean noStdlib;
|
public boolean noStdlib;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "true")
|
@GradleOption(DefaultValues.BooleanTrueDefault.class)
|
||||||
@Argument(value = "no-reflect", description = "Don't include Kotlin reflection implementation into classpath")
|
@Argument(value = "no-reflect", description = "Don't include Kotlin reflection implementation into classpath")
|
||||||
public boolean noReflect;
|
public boolean noReflect;
|
||||||
|
|
||||||
@@ -67,7 +67,7 @@ public class K2JVMCompilerArguments extends CommonCompilerArguments {
|
|||||||
@Argument(value = "module-name", description = "Module name")
|
@Argument(value = "module-name", description = "Module name")
|
||||||
public String moduleName;
|
public String moduleName;
|
||||||
|
|
||||||
@GradleOption(defaultValue = "\"1.6\"", possibleValues = { "\"1.6\"", "\"1.8\"" })
|
@GradleOption(DefaultValues.JvmTargetVersions.class)
|
||||||
@Argument(value = "jvm-target", description = "Target version of the generated JVM bytecode (1.6 or 1.8), default is 1.6")
|
@Argument(value = "jvm-target", description = "Target version of the generated JVM bytecode (1.6 or 1.8), default is 1.6")
|
||||||
@ValueDescription("<version>")
|
@ValueDescription("<version>")
|
||||||
public String jvmTarget;
|
public String jvmTarget;
|
||||||
|
|||||||
Vendored
+1
-1
@@ -6,7 +6,7 @@ where possible options include:
|
|||||||
-source-map Generate source map
|
-source-map Generate source map
|
||||||
-meta-info Generate metadata
|
-meta-info Generate metadata
|
||||||
-kjsm Generate kjsm-files (for creating libraries)
|
-kjsm Generate kjsm-files (for creating libraries)
|
||||||
-target { v5 } Generate JS files for specific ECMA version)
|
-target { v5 } Generate JS files for specific ECMA version
|
||||||
-module-kind { plain, amd, commonjs, umd }
|
-module-kind { plain, amd, commonjs, umd }
|
||||||
Kind of a module generated by compiler
|
Kind of a module generated by compiler
|
||||||
-main {call,noCall} Whether a main function should be called
|
-main {call,noCall} Whether a main function should be called
|
||||||
|
|||||||
@@ -22,6 +22,9 @@ enum class JvmTarget(val string: String) {
|
|||||||
;
|
;
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@JvmField
|
||||||
|
val DEFAULT = JVM_1_6
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun fromString(string: String) = values().find { it.string == string }
|
fun fromString(string: String) = values().find { it.string == string }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.kotlin.generators.arguments
|
package org.jetbrains.kotlin.generators.arguments
|
||||||
|
|
||||||
import com.sampullara.cli.Argument
|
import com.sampullara.cli.Argument
|
||||||
|
import org.jetbrains.kotlin.cli.common.arguments.DefaultValues
|
||||||
import org.jetbrains.kotlin.cli.common.arguments.GradleOption
|
import org.jetbrains.kotlin.cli.common.arguments.GradleOption
|
||||||
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
|
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
|
||||||
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
|
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
|
||||||
@@ -24,13 +25,18 @@ import org.jetbrains.kotlin.name.FqName
|
|||||||
import org.jetbrains.kotlin.utils.Printer
|
import org.jetbrains.kotlin.utils.Printer
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.PrintStream
|
import java.io.PrintStream
|
||||||
import kotlin.reflect.*
|
import kotlin.reflect.KAnnotatedElement
|
||||||
|
import kotlin.reflect.KProperty1
|
||||||
|
import kotlin.reflect.memberProperties
|
||||||
|
|
||||||
// Additional properties that should be included in interface
|
// Additional properties that should be included in interface
|
||||||
|
@Suppress("unused")
|
||||||
interface AdditionalGradleProperties {
|
interface AdditionalGradleProperties {
|
||||||
@GradleOption(defaultValue = "emptyList()")
|
@GradleOption(EmptyList::class)
|
||||||
@Argument(description = "A list of additional compiler arguments")
|
@Argument(description = "A list of additional compiler arguments")
|
||||||
var freeCompilerArgs: List<String>
|
var freeCompilerArgs: List<String>
|
||||||
|
|
||||||
|
object EmptyList : DefaultValues("emptyList()")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
@@ -173,12 +179,12 @@ private fun Printer.generatePropertyDeclaration(property: KProperty1<*, *>, modi
|
|||||||
|
|
||||||
private fun Printer.generateDoc(property: KProperty1<*, *>) {
|
private fun Printer.generateDoc(property: KProperty1<*, *>) {
|
||||||
val description = property.findAnnotation<Argument>()!!.description
|
val description = property.findAnnotation<Argument>()!!.description
|
||||||
val possibleValues = property.gradlePossibleValues
|
val possibleValues = property.gradleValues.possibleValues
|
||||||
val defaultValue = property.gradleDefaultValue
|
val defaultValue = property.gradleDefaultValue
|
||||||
|
|
||||||
println("/**")
|
println("/**")
|
||||||
println(" * $description")
|
println(" * $description")
|
||||||
if (possibleValues.isNotEmpty()) {
|
if (possibleValues != null) {
|
||||||
println(" * Possible values: ${possibleValues.joinToString()}")
|
println(" * Possible values: ${possibleValues.joinToString()}")
|
||||||
}
|
}
|
||||||
println(" * Default value: $defaultValue")
|
println(" * Default value: $defaultValue")
|
||||||
@@ -191,11 +197,11 @@ private inline fun Printer.withIndent(fn: Printer.()->Unit) {
|
|||||||
popIndent()
|
popIndent()
|
||||||
}
|
}
|
||||||
|
|
||||||
private val KProperty1<*, *>.gradlePossibleValues: Array<String>
|
private val KProperty1<*, *>.gradleValues: DefaultValues
|
||||||
get() = findAnnotation<GradleOption>()!!.possibleValues
|
get() = findAnnotation<GradleOption>()!!.value.objectInstance!!
|
||||||
|
|
||||||
private val KProperty1<*, *>.gradleDefaultValue: String
|
private val KProperty1<*, *>.gradleDefaultValue: String
|
||||||
get() = findAnnotation<GradleOption>()!!.defaultValue
|
get() = gradleValues.defaultValue
|
||||||
|
|
||||||
private val KProperty1<*, *>.gradleReturnType: String
|
private val KProperty1<*, *>.gradleReturnType: String
|
||||||
get() {
|
get() {
|
||||||
@@ -207,4 +213,4 @@ private val KProperty1<*, *>.gradleReturnType: String
|
|||||||
}
|
}
|
||||||
|
|
||||||
private inline fun <reified T> KAnnotatedElement.findAnnotation(): T? =
|
private inline fun <reified T> KAnnotatedElement.findAnnotation(): T? =
|
||||||
annotations.filterIsInstance<T>().firstOrNull()
|
annotations.filterIsInstance<T>().firstOrNull()
|
||||||
|
|||||||
+1
-1
@@ -62,7 +62,7 @@ interface KotlinJsOptions {
|
|||||||
var suppressWarnings: kotlin.Boolean
|
var suppressWarnings: kotlin.Boolean
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate JS files for specific ECMA version)
|
* Generate JS files for specific ECMA version
|
||||||
* Possible values: "v5"
|
* Possible values: "v5"
|
||||||
* Default value: "v5"
|
* Default value: "v5"
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user