From 9eb8034998fb73e904a48b3e06df0db5a0e52b1b Mon Sep 17 00:00:00 2001 From: Ting-Yuan Huang Date: Fri, 13 Aug 2021 00:35:36 -0700 Subject: blockOtherPlugins: avoid referencing task in execution phase --- .../com/google/devtools/ksp/gradle/KspSubplugin.kt | 38 +++++++++++++++------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'gradle-plugin') diff --git a/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt b/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt index cbf45439..71c82713 100644 --- a/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt +++ b/gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt @@ -28,6 +28,7 @@ import org.gradle.api.artifacts.Configuration import org.gradle.api.attributes.Attribute import org.gradle.api.file.ConfigurableFileCollection import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.FileCollection import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.ListProperty import org.gradle.api.provider.MapProperty @@ -282,7 +283,6 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool ) kspTask.destination = kspOutputDir kspTask.blockOtherCompilerPlugins = kspExtension.blockOtherCompilerPlugins - kspTask.pluginConfigurationName = kotlinCompilation.pluginConfigurationName kspTask.apOptions.value(kspExtension.arguments).disallowChanges() kspTask.kspCacheDir.fileValue(getKspCachesDir(project, sourceSetName)).disallowChanges() @@ -294,6 +294,17 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool nonEmptyKspConfigurations.forEach { kspTask.dependsOn(it.buildDependencies) } + + if (kspExtension.blockOtherCompilerPlugins) { + // FIXME: ask upstream to provide an API to make this not implementation-dependent. + val cfg = project.configurations.getByName(kotlinCompilation.pluginConfigurationName) + kspTask.overridePluginClasspath.value( + kspTask.project.provider { + val dep = cfg.dependencies.single { it.name == KSP_ARTIFACT_NAME } + cfg.fileCollection(dep) + } + ) + } } fun configureAsAbstractCompile(kspTask: AbstractCompile) { @@ -415,8 +426,10 @@ interface KspTask : Task { @get:OutputDirectory var destination: File - @get:Internal - var pluginConfigurationName: String + @get:Optional + @get:PathSensitive(PathSensitivity.RELATIVE) + @get:InputFiles + val overridePluginClasspath: Property @get:Input var blockOtherCompilerPlugins: Boolean @@ -568,7 +581,7 @@ abstract class KspTaskJvm : KotlinCompile(KotlinJvmOptionsImpl()), KspTask { ignoreClasspathResolutionErrors )) if (blockOtherCompilerPlugins) { - args.blockOtherPlugins(project, pluginConfigurationName) + args.blockOtherPlugins(overridePluginClasspath.get()) } args.addPluginOptions(options.get()) args.destinationAsFile = destination @@ -639,7 +652,7 @@ abstract class KspTaskJS @Inject constructor( ignoreClasspathResolutionErrors )) if (blockOtherCompilerPlugins) { - args.blockOtherPlugins(project, pluginConfigurationName) + args.blockOtherPlugins(overridePluginClasspath.get()) } args.addPluginOptions(options.get()) args.outputFile = File(destination, "dummyOutput.js").canonicalPath @@ -696,7 +709,7 @@ abstract class KspTaskMetadata : KotlinCompileCommon(KotlinMultiplatformCommonOp ignoreClasspathResolutionErrors )) if (blockOtherCompilerPlugins) { - args.blockOtherPlugins(project, pluginConfigurationName) + args.blockOtherPlugins(overridePluginClasspath.get()) } args.addPluginOptions(options.get()) args.destination = destination.canonicalPath @@ -728,6 +741,12 @@ abstract class KspTaskNative @Inject constructor( return super.buildCompilerArgs() + kspOptions } + override fun buildCommonArgs(defaultsOnly: Boolean): List { + if (blockOtherCompilerPlugins) + compilerPluginClasspath = overridePluginClasspath.get() + return super.buildCommonArgs(defaultsOnly) + } + override fun configureCompilation( kotlinCompilation: KotlinCompilationData<*>, kotlinCompile: AbstractKotlinCompile<*>, @@ -781,11 +800,8 @@ fun CommonCompilerArguments.addChangedFiles(changedFiles: ChangedFiles) { } } -private fun CommonCompilerArguments.blockOtherPlugins(project: Project, pluginConfigurationName: String) { - // FIXME: ask upstream to provide an API to make this not implementation-dependent. - val cfg = project.configurations.getByName(pluginConfigurationName) - val dep = cfg.dependencies.single { it.name == KspGradleSubplugin.KSP_ARTIFACT_NAME } - pluginClasspaths = cfg.files(dep).map { it.canonicalPath }.toTypedArray() +private fun CommonCompilerArguments.blockOtherPlugins(kspPluginClasspath: FileCollection) { + pluginClasspaths = kspPluginClasspath.map { it.canonicalPath }.toTypedArray() pluginOptions = arrayOf() } -- cgit v1.2.3