Skip to content

Commit 85129ee

Browse files
committed
Kotlin Facet: Import more configuration options from Maven
(cherry picked from commit dca696f)
1 parent cb0c276 commit 85129ee

File tree

4 files changed

+212
-15
lines changed

4 files changed

+212
-15
lines changed

idea/idea-maven/idea-maven.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
<orderEntry type="module" module-name="util" />
2525
<orderEntry type="module" module-name="cli-common" />
2626
<orderEntry type="library" name="idea-full" level="project" />
27+
<orderEntry type="module" module-name="build-common" />
2728
</component>
2829
</module>

idea/idea-maven/src/org/jetbrains/kotlin/idea/maven/KotlinMavenImporter.kt

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ import org.jetbrains.idea.maven.model.MavenPlugin
3333
import org.jetbrains.idea.maven.project.*
3434
import org.jetbrains.jps.model.java.JavaSourceRootType
3535
import org.jetbrains.jps.model.module.JpsModuleSourceRootType
36+
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
37+
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
38+
import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments
39+
import org.jetbrains.kotlin.compilerRunner.ArgumentUtils
3640
import org.jetbrains.kotlin.config.CoroutineSupport
3741
import org.jetbrains.kotlin.config.JvmTarget
3842
import org.jetbrains.kotlin.config.LanguageVersion
@@ -102,8 +106,36 @@ class KotlinMavenImporter : MavenImporter(KOTLIN_PLUGIN_GROUP_ID, KOTLIN_PLUGIN_
102106
configureFacet(mavenProject, modifiableModelsProvider, module)
103107
}
104108

105-
private fun getCompilerArgumentsByConfigurationElement(configuration: Element) =
106-
configuration.getChild("args")?.getChildren("arg")?.map { it.text } ?: emptyList()
109+
private fun getCompilerArgumentsByConfigurationElement(configuration: Element, platform: TargetPlatformKind<*>): List<String> {
110+
val arguments = when (platform) {
111+
is TargetPlatformKind.Jvm -> K2JVMCompilerArguments()
112+
is TargetPlatformKind.JavaScript -> K2JSCompilerArguments()
113+
is TargetPlatformKind.Common -> K2MetadataCompilerArguments()
114+
}
115+
116+
arguments.apiVersion = configuration.getChild("apiVersion")?.text
117+
arguments.languageVersion = configuration.getChild("languageVersion")?.text
118+
arguments.multiPlatform = configuration.getChild("multiPlatform")?.text?.trim()?.toBoolean() ?: false
119+
arguments.suppressWarnings = configuration.getChild("nowarn")?.text?.trim()?.toBoolean() ?: false
120+
when (arguments) {
121+
is K2JVMCompilerArguments -> {
122+
arguments.classpath = configuration.getChild("classpath")?.text
123+
arguments.jdkHome = configuration.getChild("jdkHome")?.text
124+
arguments.jvmTarget = configuration.getChild("jvmTarget")?.text
125+
}
126+
is K2JSCompilerArguments -> {
127+
arguments.sourceMap = configuration.getChild("sourceMap")?.text?.trim()?.toBoolean() ?: false
128+
arguments.outputFile = configuration.getChild("outputFile")?.text
129+
arguments.metaInfo = configuration.getChild("metaInfo")?.text?.trim()?.toBoolean() ?: false
130+
arguments.moduleKind = configuration.getChild("moduleKind")?.text
131+
}
132+
}
133+
134+
return ArrayList<String>().apply {
135+
this += ArgumentUtils.convertArgumentsToStringList(arguments)
136+
configuration.getChild("args")?.getChildren("arg")?.mapNotNullTo(this) { it.text }
137+
}
138+
}
107139

108140
private val compilationGoals = listOf(PomFile.KotlinGoals.Compile,
109141
PomFile.KotlinGoals.TestCompile,
@@ -118,19 +150,16 @@ class KotlinMavenImporter : MavenImporter(KOTLIN_PLUGIN_GROUP_ID, KOTLIN_PLUGIN_
118150
val platform = detectPlatformByExecutions(mavenProject) ?: detectPlatformByLibraries(mavenProject)
119151

120152
kotlinFacet.configureFacet(compilerVersion, CoroutineSupport.DEFAULT, platform, modifiableModelsProvider)
153+
val configuredPlatform = kotlinFacet.configuration.settings.versionInfo.targetPlatformKind!!
121154
val configuration = mavenPlugin.configurationElement
122-
val apiVersion = configuration?.getChild("apiVersion")?.text?.let { LanguageVersion.fromFullVersionString(it) }
123-
val sharedArguments = configuration?.let { getCompilerArgumentsByConfigurationElement(it) } ?: emptyList()
155+
val sharedArguments = configuration?.let { getCompilerArgumentsByConfigurationElement(it, configuredPlatform) } ?: emptyList()
124156
val executionArguments = mavenPlugin.executions?.filter { it.goals.any { it in compilationGoals } }
125157
?.firstOrNull()
126-
?.configurationElement?.let { getCompilerArgumentsByConfigurationElement(it) }
127-
?: emptyList()
128-
with(kotlinFacet.configuration.settings) {
129-
versionInfo.apiLevel = apiVersion
130-
compilerInfo.k2jvmCompilerArguments?.jvmTarget = configuration?.getChild("jvmTarget")?.text
131-
}
158+
?.configurationElement?.let { getCompilerArgumentsByConfigurationElement(it, configuredPlatform) }
132159
parseCompilerArgumentsToFacet(sharedArguments, emptyList(), kotlinFacet)
133-
parseCompilerArgumentsToFacet(executionArguments, emptyList(), kotlinFacet)
160+
if (executionArguments != null) {
161+
parseCompilerArgumentsToFacet(executionArguments, emptyList(), kotlinFacet, true)
162+
}
134163
MavenProjectImportHandler.getInstances(module.project).forEach { it(kotlinFacet, mavenProject) }
135164
}
136165

idea/idea-maven/test/org/jetbrains/kotlin/idea/maven/KotlinMavenImporterTest.kt

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.jetbrains.kotlin.idea.maven
1818

1919
import org.jetbrains.kotlin.config.KotlinFacetSettings
20+
import org.jetbrains.kotlin.config.TargetPlatformKind
2021
import org.jetbrains.kotlin.idea.facet.KotlinFacet
2122
import org.junit.Assert
2223
import java.io.File
@@ -387,7 +388,7 @@ class KotlinMavenImporterTest : MavenImportingTestCase() {
387388
assertTestSources("project", "src/test/java", "src/test/kotlin", "src/test/kotlin.jvm")
388389
}
389390

390-
fun testJvmTarget() {
391+
fun testJvmFacetConfiguration() {
391392
createProjectSubDirs("src/main/kotlin", "src/main/kotlin.jvm", "src/test/kotlin", "src/test/kotlin.jvm")
392393

393394
importProject("""
@@ -421,7 +422,16 @@ class KotlinMavenImporterTest : MavenImportingTestCase() {
421422
</execution>
422423
</executions>
423424
<configuration>
425+
<languageVersion>1.1</languageVersion>
426+
<apiVersion>1.0</apiVersion>
427+
<multiPlatform>true</multiPlatform>
428+
<nowarn>true</nowarn>
429+
<args>
430+
<arg>-Xcoroutines=enable</arg>
431+
</args>
424432
<jvmTarget>1.8</jvmTarget>
433+
<jdkHome>JDK_HOME</jdkHome>
434+
<classpath>foobar.jar</classpath>
425435
</configuration>
426436
</plugin>
427437
</plugins>
@@ -432,8 +442,154 @@ class KotlinMavenImporterTest : MavenImportingTestCase() {
432442
assertImporterStatePresent()
433443

434444
with (facetSettings) {
445+
Assert.assertEquals("1.1", versionInfo.languageLevel!!.versionString)
446+
Assert.assertEquals("1.1", compilerInfo.commonCompilerArguments!!.languageVersion)
447+
Assert.assertEquals("1.0", versionInfo.apiLevel!!.versionString)
448+
Assert.assertEquals("1.0", compilerInfo.commonCompilerArguments!!.apiVersion)
449+
Assert.assertEquals(true, compilerInfo.commonCompilerArguments!!.suppressWarnings)
450+
Assert.assertEquals("enable", compilerInfo.coroutineSupport.compilerArgument)
451+
Assert.assertEquals("JVM 1.8", versionInfo.targetPlatformKind!!.description)
452+
Assert.assertEquals("1.8", compilerInfo.k2jvmCompilerArguments!!.jvmTarget)
453+
Assert.assertEquals("-cp foobar.jar -jdk-home JDK_HOME -Xmulti-platform",
454+
compilerInfo.compilerSettings!!.additionalArguments)
455+
}
456+
}
457+
458+
fun testJsFacetConfiguration() {
459+
createProjectSubDirs("src/main/kotlin", "src/main/kotlin.jvm", "src/test/kotlin", "src/test/kotlin.jvm")
460+
461+
importProject("""
462+
<groupId>test</groupId>
463+
<artifactId>project</artifactId>
464+
<version>1.0.0</version>
465+
466+
<dependencies>
467+
<dependency>
468+
<groupId>org.jetbrains.kotlin</groupId>
469+
<artifactId>kotlin-stdlib</artifactId>
470+
<version>$kotlinVersion</version>
471+
</dependency>
472+
</dependencies>
473+
474+
<build>
475+
<sourceDirectory>src/main/kotlin</sourceDirectory>
476+
477+
<plugins>
478+
<plugin>
479+
<groupId>org.jetbrains.kotlin</groupId>
480+
<artifactId>kotlin-maven-plugin</artifactId>
481+
482+
<executions>
483+
<execution>
484+
<id>compile</id>
485+
<phase>compile</phase>
486+
<goals>
487+
<goal>js</goal>
488+
</goals>
489+
</execution>
490+
</executions>
491+
<configuration>
492+
<languageVersion>1.1</languageVersion>
493+
<apiVersion>1.0</apiVersion>
494+
<multiPlatform>true</multiPlatform>
495+
<nowarn>true</nowarn>
496+
<args>
497+
<arg>-Xcoroutines=enable</arg>
498+
</args>
499+
<sourceMap>true</sourceMap>
500+
<outputFile>test.js</outputFile>
501+
<metaInfo>true</metaInfo>
502+
<moduleKind>commonjs</moduleKind>
503+
</configuration>
504+
</plugin>
505+
</plugins>
506+
</build>
507+
""")
508+
509+
assertModules("project")
510+
assertImporterStatePresent()
511+
512+
with (facetSettings) {
513+
Assert.assertEquals("1.1", versionInfo.languageLevel!!.versionString)
514+
Assert.assertEquals("1.1", compilerInfo.commonCompilerArguments!!.languageVersion)
515+
Assert.assertEquals("1.0", versionInfo.apiLevel!!.versionString)
516+
Assert.assertEquals("1.0", compilerInfo.commonCompilerArguments!!.apiVersion)
517+
Assert.assertEquals(true, compilerInfo.commonCompilerArguments!!.suppressWarnings)
518+
Assert.assertEquals("enable", compilerInfo.coroutineSupport.compilerArgument)
519+
Assert.assertTrue(versionInfo.targetPlatformKind is TargetPlatformKind.JavaScript)
520+
Assert.assertEquals(true, compilerInfo.k2jsCompilerArguments!!.sourceMap)
521+
Assert.assertEquals("commonjs", compilerInfo.k2jsCompilerArguments!!.moduleKind)
522+
Assert.assertEquals("-output test.js -meta-info -Xmulti-platform",
523+
compilerInfo.compilerSettings!!.additionalArguments)
524+
}
525+
}
526+
527+
fun testFacetSplitConfiguration() {
528+
createProjectSubDirs("src/main/kotlin", "src/main/kotlin.jvm", "src/test/kotlin", "src/test/kotlin.jvm")
529+
530+
importProject("""
531+
<groupId>test</groupId>
532+
<artifactId>project</artifactId>
533+
<version>1.0.0</version>
534+
535+
<dependencies>
536+
<dependency>
537+
<groupId>org.jetbrains.kotlin</groupId>
538+
<artifactId>kotlin-stdlib</artifactId>
539+
<version>$kotlinVersion</version>
540+
</dependency>
541+
</dependencies>
542+
543+
<build>
544+
<sourceDirectory>src/main/kotlin</sourceDirectory>
545+
546+
<plugins>
547+
<plugin>
548+
<groupId>org.jetbrains.kotlin</groupId>
549+
<artifactId>kotlin-maven-plugin</artifactId>
550+
551+
<executions>
552+
<execution>
553+
<id>compile</id>
554+
<phase>compile</phase>
555+
<goals>
556+
<goal>compile</goal>
557+
</goals>
558+
<configuration>
559+
<languageVersion>1.1</languageVersion>
560+
<multiPlatform>true</multiPlatform>
561+
<args>
562+
<arg>-Xcoroutines=enable</arg>
563+
</args>
564+
<jdkHome>JDK_HOME</jdkHome>
565+
<classpath>foobar.jar</classpath>
566+
</configuration>
567+
</execution>
568+
</executions>
569+
<configuration>
570+
<apiVersion>1.0</apiVersion>
571+
<nowarn>true</nowarn>
572+
<jvmTarget>1.8</jvmTarget>
573+
</configuration>
574+
</plugin>
575+
</plugins>
576+
</build>
577+
""")
578+
579+
assertModules("project")
580+
assertImporterStatePresent()
581+
582+
with (facetSettings) {
583+
Assert.assertEquals("1.1", versionInfo.languageLevel!!.versionString)
584+
Assert.assertEquals("1.1", compilerInfo.commonCompilerArguments!!.languageVersion)
585+
Assert.assertEquals("1.0", versionInfo.apiLevel!!.versionString)
586+
Assert.assertEquals("1.0", compilerInfo.commonCompilerArguments!!.apiVersion)
587+
Assert.assertEquals(true, compilerInfo.commonCompilerArguments!!.suppressWarnings)
588+
Assert.assertEquals("enable", compilerInfo.coroutineSupport.compilerArgument)
435589
Assert.assertEquals("JVM 1.8", versionInfo.targetPlatformKind!!.description)
436590
Assert.assertEquals("1.8", compilerInfo.k2jvmCompilerArguments!!.jvmTarget)
591+
Assert.assertEquals("-version -cp foobar.jar -jdk-home JDK_HOME -Xmulti-platform",
592+
compilerInfo.compilerSettings!!.additionalArguments)
437593
}
438594
}
439595

idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ private val CommonCompilerArguments.exposedFields: List<String>
156156
else -> commonExposedFields
157157
}
158158

159-
fun parseCompilerArgumentsToFacet(arguments: List<String>, defaultArguments: List<String>, kotlinFacet: KotlinFacet) {
159+
fun parseCompilerArgumentsToFacet(
160+
arguments: List<String>,
161+
defaultArguments: List<String>,
162+
kotlinFacet: KotlinFacet,
163+
appendAdditionalArguments: Boolean = false
164+
) {
160165
val argumentArray = arguments.toTypedArray()
161166

162167
with(kotlinFacet.configuration.settings) {
@@ -211,8 +216,14 @@ fun parseCompilerArgumentsToFacet(arguments: List<String>, defaultArguments: Lis
211216
copyFieldsSatisfying(compilerArguments, this, ::exposeAsAdditionalArgument)
212217
ArgumentUtils.convertArgumentsToStringList(this).joinToString(separator = " ")
213218
}
214-
compilerInfo.compilerSettings!!.additionalArguments =
215-
if (additionalArgumentsString.isNotEmpty()) additionalArgumentsString else CompilerSettings.DEFAULT_ADDITIONAL_ARGUMENTS
219+
220+
val newAdditionalArguments = if (additionalArgumentsString.isNotEmpty()) additionalArgumentsString else CompilerSettings.DEFAULT_ADDITIONAL_ARGUMENTS
221+
if (appendAdditionalArguments) {
222+
compilerInfo.compilerSettings!!.additionalArguments += " $newAdditionalArguments"
223+
}
224+
else {
225+
compilerInfo.compilerSettings!!.additionalArguments = newAdditionalArguments
226+
}
216227

217228
with(compilerArguments.javaClass.newInstance()) {
218229
copyFieldsSatisfying(this, compilerArguments, ::exposeAsAdditionalArgument)

0 commit comments

Comments
 (0)