[Gradle] Implement IdeCompilerArgumentsResolver service
KTIJ-24976
This commit is contained in:
committed by
Space Team
parent
36ff6531ea
commit
0a3eb68934
+31
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.gradle.plugin.ide
|
||||||
|
|
||||||
|
import org.gradle.api.Project
|
||||||
|
import org.gradle.api.logging.Logger
|
||||||
|
import org.gradle.api.logging.Logging
|
||||||
|
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.extraProperties
|
||||||
|
import org.jetbrains.kotlin.gradle.utils.getOrPut
|
||||||
|
|
||||||
|
interface IdeCompilerArgumentsResolver {
|
||||||
|
fun resolveCompilerArguments(any: Any): List<String>?
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
internal val logger: Logger = Logging.getLogger(IdeMultiplatformImport::class.java)
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun instance(project: Project): IdeCompilerArgumentsResolver {
|
||||||
|
return project.extraProperties.getOrPut(IdeCompilerArgumentsResolver::class.java.name) {
|
||||||
|
IdeCompilerArgumentsResolverImpl(project.kotlinExtension)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal val Project.kotlinIdeCompilerArgumentsResolver: IdeCompilerArgumentsResolver get() = IdeCompilerArgumentsResolver.instance(project)
|
||||||
|
|
||||||
+48
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.gradle.plugin.ide
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.compilerRunner.ArgumentUtils
|
||||||
|
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
|
||||||
|
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.CreateCompilerArgumentsContext
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerArgumentsProducer
|
||||||
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
|
import org.jetbrains.kotlin.gradle.utils.hasAndroidPlugin
|
||||||
|
|
||||||
|
internal class IdeCompilerArgumentsResolverImpl(
|
||||||
|
private val extension: KotlinProjectExtension
|
||||||
|
) : IdeCompilerArgumentsResolver {
|
||||||
|
|
||||||
|
override fun resolveCompilerArguments(any: Any): List<String>? {
|
||||||
|
if (any is KotlinCompilerArgumentsProducer) {
|
||||||
|
return resolveCompilerArguments(any)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (any is KotlinCompilation<*>) {
|
||||||
|
return resolveCompilerArguments(any.compileTaskProvider.orNull ?: return null)
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun resolveCompilerArguments(producer: KotlinCompilerArgumentsProducer): List<String> {
|
||||||
|
val compilerArguments = producer.createCompilerArguments(
|
||||||
|
CreateCompilerArgumentsContext(
|
||||||
|
isLenient = true,
|
||||||
|
includeArgumentTypes = setOfNotNull(
|
||||||
|
KotlinCompilerArgumentsProducer.ArgumentType.Primitive,
|
||||||
|
KotlinCompilerArgumentsProducer.ArgumentType.DependencyClasspath
|
||||||
|
.takeIf { extension !is KotlinMultiplatformExtension }
|
||||||
|
.takeIf { producer is KotlinCompile }
|
||||||
|
.takeIf { !extension.project.hasAndroidPlugin }
|
||||||
|
),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return ArgumentUtils.convertArgumentsToStringList(compilerArguments)
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-1
@@ -10,4 +10,6 @@ import org.gradle.api.Project
|
|||||||
|
|
||||||
internal val Project.androidExtensionOrNull: BaseExtension? get() = extensions.findByName("android")?.let { it as? BaseExtension }
|
internal val Project.androidExtensionOrNull: BaseExtension? get() = extensions.findByName("android")?.let { it as? BaseExtension }
|
||||||
|
|
||||||
internal val Project.androidExtension: BaseExtension get() = extensions.getByName("android") as BaseExtension
|
internal val Project.androidExtension: BaseExtension get() = extensions.getByName("android") as BaseExtension
|
||||||
|
|
||||||
|
internal val Project.hasAndroidPlugin : Boolean get() = extensions.findByName("android") != null
|
||||||
Reference in New Issue
Block a user