From 29abe839655615bfa79ad29bf4590cacac7330dd Mon Sep 17 00:00:00 2001 From: Tom Tresansky Date: Thu, 14 Mar 2024 14:42:53 -0400 Subject: [PATCH 1/3] Add support for using project dependencies in the declarative DSL with DependencyCollector --- .../project/dependencyConfigurationSchema.kt | 54 +++++++++++++------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/platforms/core-configuration/declarative-dsl-provider/src/main/kotlin/org/gradle/internal/declarativedsl/project/dependencyConfigurationSchema.kt b/platforms/core-configuration/declarative-dsl-provider/src/main/kotlin/org/gradle/internal/declarativedsl/project/dependencyConfigurationSchema.kt index 438e347e337f..ee337e94dff8 100644 --- a/platforms/core-configuration/declarative-dsl-provider/src/main/kotlin/org/gradle/internal/declarativedsl/project/dependencyConfigurationSchema.kt +++ b/platforms/core-configuration/declarative-dsl-provider/src/main/kotlin/org/gradle/internal/declarativedsl/project/dependencyConfigurationSchema.kt @@ -104,13 +104,22 @@ class ImplicitDependencyCollectorFunctionExtractor(val configurations: Dependenc .filter { function -> hasDependencyCollectorGetterSignature(function) } .map { function -> function.name.removePrefix("get").replaceFirstChar { it.lowercase(Locale.getDefault()) } } .filter { confName -> confName in configurations.configurationNames } - .map { confName -> - DataMemberFunction( - kClass.toDataTypeRef(), - confName, - listOf(DataParameter("dependency", String::class.toDataTypeRef(), false, ParameterSemantics.Unknown)), - false, - FunctionSemantics.AddAndConfigure(kClass.toDataTypeRef(), NOT_ALLOWED) + .flatMap { confName -> + listOf( + DataMemberFunction( + kClass.toDataTypeRef(), + confName, + listOf(DataParameter("dependency", String::class.toDataTypeRef(), false, ParameterSemantics.Unknown)), + false, + FunctionSemantics.AddAndConfigure(kClass.toDataTypeRef(), NOT_ALLOWED) + ), + DataMemberFunction( + kClass.toDataTypeRef(), + confName, + listOf(DataParameter("dependency", ProjectDependency::class.toDataTypeRef(), false, ParameterSemantics.Unknown)), + false, + FunctionSemantics.AddAndConfigure(kClass.toDataTypeRef(), NOT_ALLOWED) + ) ) } @@ -148,14 +157,29 @@ class ImplicitDependencyCollectorFunctionResolver(configurations: DependencyConf if (name in configurationNames) { val getterFunction = getDependencyCollectorGetter(receiverClass, name) if (getterFunction != null) { - return RuntimeFunctionResolver.Resolution.Resolved(object : DeclarativeRuntimeFunction { - override fun callBy(receiver: Any, binding: Map, hasLambda: Boolean): DeclarativeRuntimeFunction.InvocationResult { - val dependencyNotation = binding.values.single().toString() - val collector: DependencyCollector = getterFunction.call(receiver) as DependencyCollector - collector.add(dependencyNotation) - return DeclarativeRuntimeFunction.InvocationResult(Unit, null) - } - }) + val gavParam = DataParameter("dependency", String::class.toDataTypeRef(), false, ParameterSemantics.Unknown) + val projectParam = DataParameter("dependency", ProjectDependency::class.toDataTypeRef(), false, ParameterSemantics.Unknown) + if (parameterValueBinding.bindingMap.containsKey(gavParam)) { + return RuntimeFunctionResolver.Resolution.Resolved(object : DeclarativeRuntimeFunction { + override fun callBy(receiver: Any, binding: Map, hasLambda: Boolean): DeclarativeRuntimeFunction.InvocationResult { + val dependencyNotation = binding.values.single().toString() + val collector: DependencyCollector = getterFunction.call(receiver) as DependencyCollector + collector.add(dependencyNotation) + return DeclarativeRuntimeFunction.InvocationResult(Unit, null) + } + }) + } else if (parameterValueBinding.bindingMap.containsKey(projectParam)) { + return RuntimeFunctionResolver.Resolution.Resolved(object : DeclarativeRuntimeFunction { + override fun callBy(receiver: Any, binding: Map, hasLambda: Boolean): DeclarativeRuntimeFunction.InvocationResult { + val dependencyNotation = binding.values.single() as ProjectDependency + val collector: DependencyCollector = getterFunction.call(receiver) as DependencyCollector + collector.add(dependencyNotation) + return DeclarativeRuntimeFunction.InvocationResult(Unit, null) + } + }) + } else { + throw IllegalStateException() + } } } return RuntimeFunctionResolver.Resolution.Unresolved From acacc8c3de5add29703d419907c0cc78f4925cb7 Mon Sep 17 00:00:00 2001 From: Tom Tresansky Date: Tue, 26 Mar 2024 09:06:37 -0400 Subject: [PATCH 2/3] Extract common dependency variables for resuse and rename them for clarity --- .../project/dependencyConfigurationSchema.kt | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/platforms/core-configuration/declarative-dsl-provider/src/main/kotlin/org/gradle/internal/declarativedsl/project/dependencyConfigurationSchema.kt b/platforms/core-configuration/declarative-dsl-provider/src/main/kotlin/org/gradle/internal/declarativedsl/project/dependencyConfigurationSchema.kt index ee337e94dff8..3d2ca9c1b83a 100644 --- a/platforms/core-configuration/declarative-dsl-provider/src/main/kotlin/org/gradle/internal/declarativedsl/project/dependencyConfigurationSchema.kt +++ b/platforms/core-configuration/declarative-dsl-provider/src/main/kotlin/org/gradle/internal/declarativedsl/project/dependencyConfigurationSchema.kt @@ -86,7 +86,7 @@ class DependencyFunctionsExtractor(val configurations: DependencyConfigurations) DataMemberFunction( kClass.toDataTypeRef(), configurationName, - listOf(DataParameter("dependency", ProjectDependency::class.toDataTypeRef(), false, ParameterSemantics.Unknown)), + listOf(projectDependencyParam), false, FunctionSemantics.AddAndConfigure(ProjectDependency::class.toDataTypeRef(), NOT_ALLOWED) ) @@ -109,14 +109,14 @@ class ImplicitDependencyCollectorFunctionExtractor(val configurations: Dependenc DataMemberFunction( kClass.toDataTypeRef(), confName, - listOf(DataParameter("dependency", String::class.toDataTypeRef(), false, ParameterSemantics.Unknown)), + listOf(gavDependencyParam), false, FunctionSemantics.AddAndConfigure(kClass.toDataTypeRef(), NOT_ALLOWED) ), DataMemberFunction( kClass.toDataTypeRef(), confName, - listOf(DataParameter("dependency", ProjectDependency::class.toDataTypeRef(), false, ParameterSemantics.Unknown)), + listOf(projectDependencyParam), false, FunctionSemantics.AddAndConfigure(kClass.toDataTypeRef(), NOT_ALLOWED) ) @@ -157,9 +157,7 @@ class ImplicitDependencyCollectorFunctionResolver(configurations: DependencyConf if (name in configurationNames) { val getterFunction = getDependencyCollectorGetter(receiverClass, name) if (getterFunction != null) { - val gavParam = DataParameter("dependency", String::class.toDataTypeRef(), false, ParameterSemantics.Unknown) - val projectParam = DataParameter("dependency", ProjectDependency::class.toDataTypeRef(), false, ParameterSemantics.Unknown) - if (parameterValueBinding.bindingMap.containsKey(gavParam)) { + if (parameterValueBinding.bindingMap.containsKey(gavDependencyParam)) { return RuntimeFunctionResolver.Resolution.Resolved(object : DeclarativeRuntimeFunction { override fun callBy(receiver: Any, binding: Map, hasLambda: Boolean): DeclarativeRuntimeFunction.InvocationResult { val dependencyNotation = binding.values.single().toString() @@ -168,7 +166,7 @@ class ImplicitDependencyCollectorFunctionResolver(configurations: DependencyConf return DeclarativeRuntimeFunction.InvocationResult(Unit, null) } }) - } else if (parameterValueBinding.bindingMap.containsKey(projectParam)) { + } else if (parameterValueBinding.bindingMap.containsKey(projectDependencyParam)) { return RuntimeFunctionResolver.Resolution.Resolved(object : DeclarativeRuntimeFunction { override fun callBy(receiver: Any, binding: Map, hasLambda: Boolean): DeclarativeRuntimeFunction.InvocationResult { val dependencyNotation = binding.values.single() as ProjectDependency @@ -202,3 +200,11 @@ fun hasDependencyCollectorGetterSignature(function: KFunction<*>): Boolean { } return function.name.startsWith("get") && returnType == DependencyCollector::class.java && function.parameters.size == 1 } + + +private +val gavDependencyParam = DataParameter("dependency", String::class.toDataTypeRef(), false, ParameterSemantics.Unknown) + + +private +val projectDependencyParam = DataParameter("dependency", ProjectDependency::class.toDataTypeRef(), false, ParameterSemantics.Unknown) From e6f436db5d819a16359bc83f253cb2ecff8cd2f1 Mon Sep 17 00:00:00 2001 From: Tom Tresansky Date: Tue, 26 Mar 2024 09:44:01 -0400 Subject: [PATCH 3/3] Add description to exception --- .../declarativedsl/project/dependencyConfigurationSchema.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platforms/core-configuration/declarative-dsl-provider/src/main/kotlin/org/gradle/internal/declarativedsl/project/dependencyConfigurationSchema.kt b/platforms/core-configuration/declarative-dsl-provider/src/main/kotlin/org/gradle/internal/declarativedsl/project/dependencyConfigurationSchema.kt index 3d2ca9c1b83a..8b7c07825bdd 100644 --- a/platforms/core-configuration/declarative-dsl-provider/src/main/kotlin/org/gradle/internal/declarativedsl/project/dependencyConfigurationSchema.kt +++ b/platforms/core-configuration/declarative-dsl-provider/src/main/kotlin/org/gradle/internal/declarativedsl/project/dependencyConfigurationSchema.kt @@ -176,7 +176,7 @@ class ImplicitDependencyCollectorFunctionResolver(configurations: DependencyConf } }) } else { - throw IllegalStateException() + throw IllegalStateException("Unexpected parameter binding contents: ${parameterValueBinding.bindingMap.keys} for function: $name in: $receiverClass") } } }