Skip to content

Commit

Permalink
Drop the dependency on Android prototype, use FQNs
Browse files Browse the repository at this point in the history
  • Loading branch information
h0tk3y committed Jun 14, 2024
1 parent 88c34bc commit 49de671
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 39 deletions.
1 change: 0 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ gradle-tooling-api = { module = "org.gradle:gradle-tooling-api", version.ref = "
gradle-declarative-dsl-core = { module = "org.gradle:gradle-declarative-dsl-core", version.ref = "declarative-dsl" }
gradle-declarative-dsl-evaluator = { module = "org.gradle:gradle-declarative-dsl-evaluator", version.ref = "declarative-dsl" }
gradle-declarative-dsl-tooling-models = { module = "org.gradle:gradle-declarative-dsl-tooling-models", version.ref = "declarative-dsl" }
gradle-prototypes-plugins-android = { module = "org.gradle.experimental:plugin-android", version.ref = "unified-prototype" }
material3WindowSizeClassMultiplatform = { module = "dev.chrisbanes.material3:material3-window-size-class-multiplatform", version = "0.3.1" }
materialKolor = { module = "com.materialkolor:material-kolor", version = "1.4.4" }
slf4j-api = { module = "org.slf4j:slf4j-api", version = "2.0.12" }
Expand Down
1 change: 0 additions & 1 deletion mutations-demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ plugins {

dependencies {
implementation(libs.gradle.declarative.dsl.core)
implementation(libs.gradle.prototypes.plugins.android)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.gradle.client.demo.mutations

import org.gradle.declarative.dsl.schema.AnalysisSchema
import org.gradle.internal.declarativedsl.dom.mutation.MutationDefinition

interface AndroidPrototypeMutationDefinition : MutationDefinition {
override fun isCompatibleWithSchema(projectAnalysisSchema: AnalysisSchema): Boolean =
projectAnalysisSchema.hasAndroidPrototype()
}
48 changes: 11 additions & 37 deletions mutations-demo/src/main/kotlin/MutationDefinitions.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package org.gradle.client.demo.mutations

import org.gradle.api.experimental.android.AndroidSoftware
import org.gradle.api.experimental.android.application.AndroidApplication
import org.gradle.api.experimental.android.extensions.testing.Testing
import org.gradle.api.experimental.android.library.AndroidLibrary
import org.gradle.declarative.dsl.schema.AnalysisSchema
import org.gradle.internal.declarativedsl.dom.mutation.*
import org.gradle.internal.declarativedsl.dom.mutation.ModelMutation.IfPresentBehavior
import org.gradle.internal.declarativedsl.schemaUtils.findTypeFor
import org.gradle.internal.declarativedsl.schemaUtils.propertyNamed
import org.gradle.internal.declarativedsl.schemaUtils.singleFunctionNamed
import org.gradle.internal.declarativedsl.schemaUtils.typeFor

object SetVersionCodeMutation : MutationDefinition {
object SetVersionCodeMutation : AndroidPrototypeMutationDefinition {
override val id: String = "org.gradle.client.demo.mutations.versionCode"
override val name: String = "Set version code"
override val description: String = "Update versionCode in androidApplication"
Expand All @@ -23,13 +17,8 @@ object SetVersionCodeMutation : MutationDefinition {
override val parameters: List<MutationParameter<*>>
get() = listOf(versionCodeParam)

override fun isCompatibleWithSchema(projectAnalysisSchema: AnalysisSchema): Boolean =
hasAndroidSoftwareType(projectAnalysisSchema)

override fun defineModelMutationSequence(projectAnalysisSchema: AnalysisSchema): List<ModelMutationRequest> =
with(projectAnalysisSchema) {
val androidApplication = typeFor<AndroidApplication>()

listOf(
ModelMutationRequest(
ScopeLocation.inAnyScope().inObjectsOfType(androidApplication),
Expand All @@ -43,7 +32,7 @@ object SetVersionCodeMutation : MutationDefinition {
}
}

object SetNamespaceMutation : MutationDefinition {
object SetNamespaceMutation : AndroidPrototypeMutationDefinition {
override val id: String = "org.gradle.client.demo.mutations.namespace"
override val name: String = "Set the library namespace"
override val description: String = "Updates the namespace in an Android library"
Expand All @@ -54,18 +43,13 @@ object SetNamespaceMutation : MutationDefinition {
override val parameters: List<MutationParameter<*>>
get() = listOf(newNamespaceParam)

override fun isCompatibleWithSchema(projectAnalysisSchema: AnalysisSchema): Boolean =
hasAndroidSoftwareType(projectAnalysisSchema)

override fun defineModelMutationSequence(projectAnalysisSchema: AnalysisSchema): List<ModelMutationRequest> =
with(projectAnalysisSchema) {
val android = typeFor<AndroidSoftware>()

listOf(
ModelMutationRequest(
ScopeLocation.inAnyScope().inObjectsOfType(android),
ScopeLocation.inAnyScope().inObjectsOfType(androidSoftware),
ModelMutation.SetPropertyValue(
android.propertyNamed("namespace"),
androidSoftware.propertyNamed("namespace"),
NewValueNodeProvider.ArgumentBased { args ->
valueFromString("\"" + args[newNamespaceParam] + "\"")!!
},
Expand All @@ -80,20 +64,16 @@ val addTestingDependencyMutation =
AddDependencyMutation(
"org.gradle.client.demo.mutations.addDependency.testing"
) {
val android = typeFor<AndroidSoftware>()

ScopeLocation.fromTopLevel()
.inObjectsOfType(android)
.inObjectsConfiguredBy(android.singleFunctionNamed("testing"))
.inObjectsConfiguredBy(typeFor<Testing>().singleFunctionNamed("dependencies"))
.inObjectsOfType(androidSoftware)
.inObjectsConfiguredBy(androidSoftware.singleFunctionNamed("testing"))
.inObjectsConfiguredBy(testing.singleFunctionNamed("dependencies"))
}

val addLibraryDependencyMutation =
AddDependencyMutation(
"org.gradle.client.demo.mutations.addDependency.topLevel.library"
) {
val androidLibrary = typeFor<AndroidLibrary>()

ScopeLocation.fromTopLevel()
.inObjectsOfType(androidLibrary)
.inObjectsConfiguredBy(androidLibrary.singleFunctionNamed("dependencies"))
Expand All @@ -103,15 +83,15 @@ val addApplicationDependencyMutation =
AddDependencyMutation(
"org.gradle.client.demo.mutations.addDependency.topLevel.application"
) {
val androidApplication = typeFor<AndroidApplication>()

ScopeLocation.fromTopLevel()
.inObjectsOfType(androidApplication)
.inObjectsConfiguredBy(androidApplication.singleFunctionNamed("dependencies"))
}

class AddDependencyMutation(override val id: String, private val scopeLocation: AnalysisSchema.() -> ScopeLocation) :
MutationDefinition {
class AddDependencyMutation(
override val id: String,
private val scopeLocation: AnalysisSchema.() -> ScopeLocation
) : AndroidPrototypeMutationDefinition {
override val name: String = "Add a dependency"
override val description: String = "Add a dependency to the dependencies block"

Expand All @@ -125,9 +105,6 @@ class AddDependencyMutation(override val id: String, private val scopeLocation:
override val parameters: List<MutationParameter<*>>
get() = listOf(dependencyCoordinatesParam)

override fun isCompatibleWithSchema(projectAnalysisSchema: AnalysisSchema): Boolean =
hasAndroidSoftwareType(projectAnalysisSchema)

override fun defineModelMutationSequence(projectAnalysisSchema: AnalysisSchema): List<ModelMutationRequest> =
listOf(
ModelMutationRequest(
Expand All @@ -140,6 +117,3 @@ class AddDependencyMutation(override val id: String, private val scopeLocation:
)
)
}

private fun hasAndroidSoftwareType(projectAnalysisSchema: AnalysisSchema) =
projectAnalysisSchema.findTypeFor<AndroidSoftware>() != null
27 changes: 27 additions & 0 deletions mutations-demo/src/main/kotlin/SchemaAccessors.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.gradle.client.demo.mutations

import org.gradle.declarative.dsl.schema.AnalysisSchema
import org.gradle.declarative.dsl.schema.DataClass

fun AnalysisSchema.hasAndroidPrototype(): Boolean =
dataClassesByFqName.keys.any { it.qualifiedName == "org.gradle.api.experimental.android.AndroidSoftware" }

val AnalysisSchema.androidApplication: DataClass
get() = dataClassesByFqName.entries.single {
it.key.qualifiedName == "org.gradle.api.experimental.android.application.AndroidApplication"
}.value

val AnalysisSchema.androidLibrary: DataClass
get() = dataClassesByFqName.entries.single {
it.key.qualifiedName == "org.gradle.api.experimental.android.library.AndroidLibrary"
}.value

val AnalysisSchema.androidSoftware: DataClass
get() = dataClassesByFqName.entries.single {
it.key.qualifiedName == "org.gradle.api.experimental.android.AndroidSoftware"
}.value

val AnalysisSchema.testing: DataClass
get() = dataClassesByFqName.entries.single {
it.key.qualifiedName == "org.gradle.api.experimental.android.extensions.testing.Testing"
}.value

0 comments on commit 49de671

Please sign in to comment.