diff options
author | Ting-Yuan Huang <laszio@google.com> | 2021-08-13 00:35:36 -0700 |
---|---|---|
committer | laszio <ting-yuan@users.noreply.github.com> | 2021-08-13 11:24:31 -0700 |
commit | 9eb8034998fb73e904a48b3e06df0db5a0e52b1b (patch) | |
tree | 0778cb9c97363a148ecaff1f35d995fb65ef2b21 | |
parent | bf6a4014a19a295482e7d222730b44c592101335 (diff) | |
download | ksp-9eb8034998fb73e904a48b3e06df0db5a0e52b1b.tar.gz |
blockOtherPlugins: avoid referencing task in execution phase
-rw-r--r-- | gradle-plugin/src/main/kotlin/com/google/devtools/ksp/gradle/KspSubplugin.kt | 38 |
1 files changed, 27 insertions, 11 deletions
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<FileCollection> @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<String> { + 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() } |