Gradle: Inherit project SDK when no explicit one is specified

#KT-27145 Fixed
This commit is contained in:
Alexey Sedunov
2018-09-26 01:29:53 +03:00
parent 60758b86ee
commit c1b3e72ae8
2 changed files with 24 additions and 9 deletions
@@ -223,6 +223,10 @@ class ModuleInfo(
) )
} }
} }
if (rootModel.sdk == null) {
projectInfo.messageCollector.report("Module '${module.name}': No SDK defined")
}
} }
private fun checkDependencyScope(library: ExportableOrderEntry, scope: DependencyScope) { private fun checkDependencyScope(library: ExportableOrderEntry, scope: DependencyScope) {
@@ -24,6 +24,7 @@ import com.intellij.openapi.projectRoots.ProjectJdkTable
import com.intellij.openapi.roots.ExternalProjectSystemRegistry import com.intellij.openapi.roots.ExternalProjectSystemRegistry
import com.intellij.openapi.roots.ModuleRootManager import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.roots.ModuleRootModel import com.intellij.openapi.roots.ModuleRootModel
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.util.io.FileUtil import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.text.StringUtil import com.intellij.openapi.util.text.StringUtil
import org.jetbrains.kotlin.cli.common.arguments.* import org.jetbrains.kotlin.cli.common.arguments.*
@@ -228,22 +229,32 @@ private val CommonCompilerArguments.ignoredFields: List<String>
} }
private fun Module.configureSdkIfPossible(compilerArguments: CommonCompilerArguments, modelsProvider: IdeModifiableModelsProvider) { private fun Module.configureSdkIfPossible(compilerArguments: CommonCompilerArguments, modelsProvider: IdeModifiableModelsProvider) {
val projectSdk = ProjectRootManager.getInstance(project).projectSdk
val allSdks = ProjectJdkTable.getInstance().allJdks val allSdks = ProjectJdkTable.getInstance().allJdks
val sdk = if (compilerArguments is K2JVMCompilerArguments) { val sdk = if (compilerArguments is K2JVMCompilerArguments) {
val jdkHome = compilerArguments.jdkHome val jdkHome = compilerArguments.jdkHome
allSdks.firstOrNull { it.sdkType is JavaSdk && FileUtil.comparePaths(it.homePath, jdkHome) == 0 } when {
jdkHome != null -> allSdks.firstOrNull { it.sdkType is JavaSdk && FileUtil.comparePaths(it.homePath, jdkHome) == 0 }
projectSdk != null && projectSdk.sdkType is JavaSdk -> projectSdk
else -> allSdks.firstOrNull { it.sdkType is JavaSdk }
}
} else { } else {
allSdks.firstOrNull { it.sdkType is KotlinSdkType } allSdks.firstOrNull { it.sdkType is KotlinSdkType }
?: modelsProvider ?: modelsProvider
.modifiableModuleModel .modifiableModuleModel
.modules .modules
.asSequence() .asSequence()
.mapNotNull { modelsProvider.getModifiableRootModel(it).sdk } .mapNotNull { modelsProvider.getModifiableRootModel(it).sdk }
.firstOrNull { it.sdkType is KotlinSdkType } .firstOrNull { it.sdkType is KotlinSdkType }
?: KotlinSdkType.INSTANCE.createSdkWithUniqueName(allSdks.toList()) ?: KotlinSdkType.INSTANCE.createSdkWithUniqueName(allSdks.toList())
} }
modelsProvider.getModifiableRootModel(this).sdk = sdk val rootModel = modelsProvider.getModifiableRootModel(this)
if (sdk == null || sdk == projectSdk) {
rootModel.inheritSdk()
} else {
rootModel.sdk = sdk
}
} }
fun parseCompilerArgumentsToFacet( fun parseCompilerArgumentsToFacet(