Set default jvmTarget when configuring Kotlin with Gradle
This commit is contained in:
@@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.configuration
|
package org.jetbrains.kotlin.idea.configuration
|
||||||
|
|
||||||
|
import com.intellij.openapi.projectRoots.Sdk
|
||||||
|
import org.jetbrains.kotlin.idea.versions.getDefaultJvmTarget
|
||||||
import org.jetbrains.kotlin.resolve.TargetPlatform
|
import org.jetbrains.kotlin.resolve.TargetPlatform
|
||||||
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform
|
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform
|
||||||
|
|
||||||
@@ -33,6 +35,8 @@ class KotlinGradleModuleConfigurator internal constructor() : KotlinWithGradleCo
|
|||||||
override val applyPluginDirective: String
|
override val applyPluginDirective: String
|
||||||
get() = APPLY_KOTLIN
|
get() = APPLY_KOTLIN
|
||||||
|
|
||||||
|
override fun getJvmTarget(sdk: Sdk?, version: String) = getDefaultJvmTarget(sdk, version)?.description
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val NAME = "gradle"
|
val NAME = "gradle"
|
||||||
|
|
||||||
|
|||||||
@@ -135,40 +135,44 @@ abstract class KotlinWithGradleConfigurator : KotlinProjectConfigurator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected fun addElementsToModuleFile(file: GroovyFile, version: String): Boolean {
|
protected fun addElementsToModuleFile(file: GroovyFile, version: String): Boolean {
|
||||||
var wasModified = false
|
val oldText = file.text
|
||||||
|
|
||||||
if (!containsDirective(file.text, applyPluginDirective)) {
|
if (!containsDirective(file.text, applyPluginDirective)) {
|
||||||
val apply = GroovyPsiElementFactory.getInstance(file.project).createExpressionFromText(applyPluginDirective)
|
val apply = GroovyPsiElementFactory.getInstance(file.project).createExpressionFromText(applyPluginDirective)
|
||||||
val applyStatement = getApplyStatement(file)
|
val applyStatement = getApplyStatement(file)
|
||||||
if (applyStatement != null) {
|
if (applyStatement != null) {
|
||||||
file.addAfter(apply, applyStatement)
|
file.addAfter(apply, applyStatement)
|
||||||
wasModified = true
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
val buildScript = getBlockByName(file, "buildscript")
|
val buildScript = getBlockByName(file, "buildscript")
|
||||||
if (buildScript != null) {
|
if (buildScript != null) {
|
||||||
file.addAfter(apply, buildScript.parent)
|
file.addAfter(apply, buildScript.parent)
|
||||||
wasModified = true
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
file.addAfter(apply, file.statements.lastOrNull() ?: file.firstChild)
|
file.addAfter(apply, file.statements.lastOrNull() ?: file.firstChild)
|
||||||
wasModified = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val repositoriesBlock = getRepositoriesBlock(file)
|
val repositoriesBlock = getRepositoriesBlock(file)
|
||||||
wasModified = wasModified or addRepository(repositoriesBlock, version)
|
addRepository(repositoriesBlock, version)
|
||||||
|
|
||||||
val dependenciesBlock = getDependenciesBlock(file)
|
val dependenciesBlock = getDependenciesBlock(file)
|
||||||
val sdk = ModuleUtil.findModuleForPsiElement(file)?.let { ModuleRootManager.getInstance(it).sdk }
|
val sdk = ModuleUtil.findModuleForPsiElement(file)?.let { ModuleRootManager.getInstance(it).sdk }
|
||||||
wasModified = wasModified or addExpressionInBlockIfNeeded(getDependencyDirective(sdk, version), dependenciesBlock, false)
|
addExpressionInBlockIfNeeded(getDependencyDirective(sdk, version), dependenciesBlock, false)
|
||||||
|
val jvmTarget = getJvmTarget(sdk, version)
|
||||||
|
if (jvmTarget != null) {
|
||||||
|
changeKotlinTaskParameter(file, "jvmTarget", jvmTarget, forTests = false)
|
||||||
|
changeKotlinTaskParameter(file, "jvmTarget", jvmTarget, forTests = true)
|
||||||
|
}
|
||||||
|
|
||||||
return wasModified
|
return file.text != oldText
|
||||||
}
|
}
|
||||||
|
|
||||||
protected open fun getDependencyDirective(sdk: Sdk?, version: String) = getRuntimeLibrary(sdk, version)
|
protected open fun getDependencyDirective(sdk: Sdk?, version: String) = getRuntimeLibrary(sdk, version)
|
||||||
|
|
||||||
|
protected open fun getJvmTarget(sdk: Sdk?, version: String): String? = null
|
||||||
|
|
||||||
protected abstract val applyPluginDirective: String
|
protected abstract val applyPluginDirective: String
|
||||||
|
|
||||||
protected open fun addElementsToFile(
|
protected open fun addElementsToFile(
|
||||||
@@ -299,26 +303,26 @@ abstract class KotlinWithGradleConfigurator : KotlinProjectConfigurator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun changeLanguageVersion(gradleFile: GroovyFile, languageVersion: String, forTests: Boolean): PsiElement? {
|
fun changeLanguageVersion(gradleFile: GroovyFile, languageVersion: String, forTests: Boolean): PsiElement? {
|
||||||
return changeVersion(gradleFile, languageVersion, forTests, "languageVersion")
|
return changeKotlinTaskParameter(gradleFile, "languageVersion", languageVersion, forTests)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun changeApiVersion(gradleFile: GroovyFile, apiVersion: String, forTests: Boolean): PsiElement? {
|
fun changeApiVersion(gradleFile: GroovyFile, apiVersion: String, forTests: Boolean): PsiElement? {
|
||||||
return changeVersion(gradleFile, apiVersion, forTests, "apiVersion")
|
return changeKotlinTaskParameter(gradleFile, "apiVersion", apiVersion, forTests)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun changeVersion(gradleFile: GroovyFile, version: String, forTests: Boolean, versionParameter: String): PsiElement? {
|
private fun changeKotlinTaskParameter(gradleFile: GroovyFile, parameterName: String, parameterValue: String, forTests: Boolean): PsiElement? {
|
||||||
val snippet = "$versionParameter = \"$version\""
|
val snippet = "$parameterName = \"$parameterValue\""
|
||||||
val kotlinBlock = getBlockOrCreate(gradleFile, if (forTests) "compileTestKotlin" else "compileKotlin")
|
val kotlinBlock = getBlockOrCreate(gradleFile, if (forTests) "compileTestKotlin" else "compileKotlin")
|
||||||
|
|
||||||
for (stmt in kotlinBlock.statements) {
|
for (stmt in kotlinBlock.statements) {
|
||||||
if ((stmt as? GrAssignmentExpression)?.lValue?.text == "kotlinOptions." + versionParameter) {
|
if ((stmt as? GrAssignmentExpression)?.lValue?.text == "kotlinOptions." + parameterName) {
|
||||||
return stmt.replaceWithStatementFromText("kotlinOptions." + snippet)
|
return stmt.replaceWithStatementFromText("kotlinOptions." + snippet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val kotlinOptionsBlock = getBlockOrCreate(kotlinBlock, "kotlinOptions")
|
val kotlinOptionsBlock = getBlockOrCreate(kotlinBlock, "kotlinOptions")
|
||||||
addOrReplaceExpression(kotlinOptionsBlock, snippet) { stmt ->
|
addOrReplaceExpression(kotlinOptionsBlock, snippet) { stmt ->
|
||||||
(stmt as? GrAssignmentExpression)?.lValue?.text == versionParameter
|
(stmt as? GrAssignmentExpression)?.lValue?.text == parameterName
|
||||||
}
|
}
|
||||||
return kotlinBlock.parent
|
return kotlinBlock.parent
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,3 +21,13 @@ buildscript {
|
|||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
compileKotlin {
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = "1.8"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
compileTestKotlin {
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = "1.8"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,4 +23,14 @@ sourceSets {
|
|||||||
}
|
}
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
compileKotlin {
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = "1.8"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
compileTestKotlin {
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = "1.8"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -24,3 +24,13 @@ sourceSets {
|
|||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
compileKotlin {
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = "1.8"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
compileTestKotlin {
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = "1.8"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user