diff --git a/idea/idea-maven/idea-maven.iml b/idea/idea-maven/idea-maven.iml index d272a4c3d2e..6c9da6cf829 100644 --- a/idea/idea-maven/idea-maven.iml +++ b/idea/idea-maven/idea-maven.iml @@ -24,5 +24,6 @@ + \ No newline at end of file diff --git a/idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/KotlinMavenImporter.kt b/idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/KotlinMavenImporter.kt index 09be0b9d72e..893fb7f04bb 100644 --- a/idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/KotlinMavenImporter.kt +++ b/idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/KotlinMavenImporter.kt @@ -33,6 +33,10 @@ import org.jetbrains.idea.maven.model.MavenPlugin import org.jetbrains.idea.maven.project.* import org.jetbrains.jps.model.java.JavaSourceRootType import org.jetbrains.jps.model.module.JpsModuleSourceRootType +import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments +import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments +import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments +import org.jetbrains.kotlin.compilerRunner.ArgumentUtils import org.jetbrains.kotlin.config.CoroutineSupport import org.jetbrains.kotlin.config.JvmTarget import org.jetbrains.kotlin.config.LanguageVersion @@ -102,8 +106,36 @@ class KotlinMavenImporter : MavenImporter(KOTLIN_PLUGIN_GROUP_ID, KOTLIN_PLUGIN_ configureFacet(mavenProject, modifiableModelsProvider, module) } - private fun getCompilerArgumentsByConfigurationElement(configuration: Element) = - configuration.getChild("args")?.getChildren("arg")?.map { it.text } ?: emptyList() + private fun getCompilerArgumentsByConfigurationElement(configuration: Element, platform: TargetPlatformKind<*>): List { + val arguments = when (platform) { + is TargetPlatformKind.Jvm -> K2JVMCompilerArguments() + is TargetPlatformKind.JavaScript -> K2JSCompilerArguments() + is TargetPlatformKind.Common -> K2MetadataCompilerArguments() + } + + arguments.apiVersion = configuration.getChild("apiVersion")?.text + arguments.languageVersion = configuration.getChild("languageVersion")?.text + arguments.multiPlatform = configuration.getChild("multiPlatform")?.text?.trim()?.toBoolean() ?: false + arguments.suppressWarnings = configuration.getChild("nowarn")?.text?.trim()?.toBoolean() ?: false + when (arguments) { + is K2JVMCompilerArguments -> { + arguments.classpath = configuration.getChild("classpath")?.text + arguments.jdkHome = configuration.getChild("jdkHome")?.text + arguments.jvmTarget = configuration.getChild("jvmTarget")?.text + } + is K2JSCompilerArguments -> { + arguments.sourceMap = configuration.getChild("sourceMap")?.text?.trim()?.toBoolean() ?: false + arguments.outputFile = configuration.getChild("outputFile")?.text + arguments.metaInfo = configuration.getChild("metaInfo")?.text?.trim()?.toBoolean() ?: false + arguments.moduleKind = configuration.getChild("moduleKind")?.text + } + } + + return ArrayList().apply { + this += ArgumentUtils.convertArgumentsToStringList(arguments) + configuration.getChild("args")?.getChildren("arg")?.mapNotNullTo(this) { it.text } + } + } private val compilationGoals = listOf(PomFile.KotlinGoals.Compile, PomFile.KotlinGoals.TestCompile, @@ -118,19 +150,16 @@ class KotlinMavenImporter : MavenImporter(KOTLIN_PLUGIN_GROUP_ID, KOTLIN_PLUGIN_ val platform = detectPlatformByExecutions(mavenProject) ?: detectPlatformByLibraries(mavenProject) kotlinFacet.configureFacet(compilerVersion, CoroutineSupport.DEFAULT, platform, modifiableModelsProvider) + val configuredPlatform = kotlinFacet.configuration.settings.versionInfo.targetPlatformKind!! val configuration = mavenPlugin.configurationElement - val apiVersion = configuration?.getChild("apiVersion")?.text?.let { LanguageVersion.fromFullVersionString(it) } - val sharedArguments = configuration?.let { getCompilerArgumentsByConfigurationElement(it) } ?: emptyList() + val sharedArguments = configuration?.let { getCompilerArgumentsByConfigurationElement(it, configuredPlatform) } ?: emptyList() val executionArguments = mavenPlugin.executions?.filter { it.goals.any { it in compilationGoals } } ?.firstOrNull() - ?.configurationElement?.let { getCompilerArgumentsByConfigurationElement(it) } - ?: emptyList() - with(kotlinFacet.configuration.settings) { - versionInfo.apiLevel = apiVersion - compilerInfo.k2jvmCompilerArguments?.jvmTarget = configuration?.getChild("jvmTarget")?.text - } + ?.configurationElement?.let { getCompilerArgumentsByConfigurationElement(it, configuredPlatform) } parseCompilerArgumentsToFacet(sharedArguments, emptyList(), kotlinFacet) - parseCompilerArgumentsToFacet(executionArguments, emptyList(), kotlinFacet) + if (executionArguments != null) { + parseCompilerArgumentsToFacet(executionArguments, emptyList(), kotlinFacet, true) + } MavenProjectImportHandler.getInstances(module.project).forEach { it(kotlinFacet, mavenProject) } } diff --git a/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenImporterTest.kt b/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenImporterTest.kt index 609b55732af..f7994a0bf01 100644 --- a/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenImporterTest.kt +++ b/idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenImporterTest.kt @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.idea.maven import org.jetbrains.kotlin.config.KotlinFacetSettings +import org.jetbrains.kotlin.config.TargetPlatformKind import org.jetbrains.kotlin.idea.facet.KotlinFacet import org.junit.Assert import java.io.File @@ -387,7 +388,7 @@ class KotlinMavenImporterTest : MavenImportingTestCase() { assertTestSources("project", "src/test/java", "src/test/kotlin", "src/test/kotlin.jvm") } - fun testJvmTarget() { + fun testJvmFacetConfiguration() { createProjectSubDirs("src/main/kotlin", "src/main/kotlin.jvm", "src/test/kotlin", "src/test/kotlin.jvm") importProject(""" @@ -421,6 +422,153 @@ class KotlinMavenImporterTest : MavenImportingTestCase() { + 1.1 + 1.0 + true + true + + -Xcoroutines=enable + + 1.8 + JDK_HOME + foobar.jar + + + + + """) + + assertModules("project") + assertImporterStatePresent() + + with (facetSettings) { + Assert.assertEquals("1.1", versionInfo.languageLevel!!.versionString) + Assert.assertEquals("1.1", compilerInfo.commonCompilerArguments!!.languageVersion) + Assert.assertEquals("1.0", versionInfo.apiLevel!!.versionString) + Assert.assertEquals("1.0", compilerInfo.commonCompilerArguments!!.apiVersion) + Assert.assertEquals(true, compilerInfo.commonCompilerArguments!!.suppressWarnings) + Assert.assertEquals("enable", compilerInfo.coroutineSupport.compilerArgument) + Assert.assertEquals("JVM 1.8", versionInfo.targetPlatformKind!!.description) + Assert.assertEquals("1.8", compilerInfo.k2jvmCompilerArguments!!.jvmTarget) + Assert.assertEquals("-cp foobar.jar -jdk-home JDK_HOME -Xmulti-platform", + compilerInfo.compilerSettings!!.additionalArguments) + } + } + + fun testJsFacetConfiguration() { + createProjectSubDirs("src/main/kotlin", "src/main/kotlin.jvm", "src/test/kotlin", "src/test/kotlin.jvm") + + importProject(""" + test + project + 1.0.0 + + + + org.jetbrains.kotlin + kotlin-stdlib + $kotlinVersion + + + + + src/main/kotlin + + + + org.jetbrains.kotlin + kotlin-maven-plugin + + + + compile + compile + + js + + + + + 1.1 + 1.0 + true + true + + -Xcoroutines=enable + + true + test.js + true + commonjs + + + + + """) + + assertModules("project") + assertImporterStatePresent() + + with (facetSettings) { + Assert.assertEquals("1.1", versionInfo.languageLevel!!.versionString) + Assert.assertEquals("1.1", compilerInfo.commonCompilerArguments!!.languageVersion) + Assert.assertEquals("1.0", versionInfo.apiLevel!!.versionString) + Assert.assertEquals("1.0", compilerInfo.commonCompilerArguments!!.apiVersion) + Assert.assertEquals(true, compilerInfo.commonCompilerArguments!!.suppressWarnings) + Assert.assertEquals("enable", compilerInfo.coroutineSupport.compilerArgument) + Assert.assertTrue(versionInfo.targetPlatformKind is TargetPlatformKind.JavaScript) + Assert.assertEquals(true, compilerInfo.k2jsCompilerArguments!!.sourceMap) + Assert.assertEquals("commonjs", compilerInfo.k2jsCompilerArguments!!.moduleKind) + Assert.assertEquals("-output test.js -meta-info -Xmulti-platform", + compilerInfo.compilerSettings!!.additionalArguments) + } + } + + fun testFacetSplitConfiguration() { + createProjectSubDirs("src/main/kotlin", "src/main/kotlin.jvm", "src/test/kotlin", "src/test/kotlin.jvm") + + importProject(""" + test + project + 1.0.0 + + + + org.jetbrains.kotlin + kotlin-stdlib + $kotlinVersion + + + + + src/main/kotlin + + + + org.jetbrains.kotlin + kotlin-maven-plugin + + + + compile + compile + + compile + + + 1.1 + true + + -Xcoroutines=enable + + JDK_HOME + foobar.jar + + + + + 1.0 + true 1.8 @@ -432,8 +580,16 @@ class KotlinMavenImporterTest : MavenImportingTestCase() { assertImporterStatePresent() with (facetSettings) { + Assert.assertEquals("1.1", versionInfo.languageLevel!!.versionString) + Assert.assertEquals("1.1", compilerInfo.commonCompilerArguments!!.languageVersion) + Assert.assertEquals("1.0", versionInfo.apiLevel!!.versionString) + Assert.assertEquals("1.0", compilerInfo.commonCompilerArguments!!.apiVersion) + Assert.assertEquals(true, compilerInfo.commonCompilerArguments!!.suppressWarnings) + Assert.assertEquals("enable", compilerInfo.coroutineSupport.compilerArgument) Assert.assertEquals("JVM 1.8", versionInfo.targetPlatformKind!!.description) Assert.assertEquals("1.8", compilerInfo.k2jvmCompilerArguments!!.jvmTarget) + Assert.assertEquals("-version -cp foobar.jar -jdk-home JDK_HOME -Xmulti-platform", + compilerInfo.compilerSettings!!.additionalArguments) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt index 6c62c7a10b7..546b752dfe1 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt @@ -156,7 +156,12 @@ private val CommonCompilerArguments.exposedFields: List else -> commonExposedFields } -fun parseCompilerArgumentsToFacet(arguments: List, defaultArguments: List, kotlinFacet: KotlinFacet) { +fun parseCompilerArgumentsToFacet( + arguments: List, + defaultArguments: List, + kotlinFacet: KotlinFacet, + appendAdditionalArguments: Boolean = false +) { val argumentArray = arguments.toTypedArray() with(kotlinFacet.configuration.settings) { @@ -211,8 +216,14 @@ fun parseCompilerArgumentsToFacet(arguments: List, defaultArguments: Lis copyFieldsSatisfying(compilerArguments, this, ::exposeAsAdditionalArgument) ArgumentUtils.convertArgumentsToStringList(this).joinToString(separator = " ") } - compilerInfo.compilerSettings!!.additionalArguments = - if (additionalArgumentsString.isNotEmpty()) additionalArgumentsString else CompilerSettings.DEFAULT_ADDITIONAL_ARGUMENTS + + val newAdditionalArguments = if (additionalArgumentsString.isNotEmpty()) additionalArgumentsString else CompilerSettings.DEFAULT_ADDITIONAL_ARGUMENTS + if (appendAdditionalArguments) { + compilerInfo.compilerSettings!!.additionalArguments += " $newAdditionalArguments" + } + else { + compilerInfo.compilerSettings!!.additionalArguments = newAdditionalArguments + } with(compilerArguments.javaClass.newInstance()) { copyFieldsSatisfying(this, compilerArguments, ::exposeAsAdditionalArgument)