From 9a7ef388ffc96c98780f9f48c24d84da67a2f074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3zsef=20Bart=C3=B3k?= Date: Fri, 17 May 2024 12:47:16 +0300 Subject: [PATCH] -- wip -- --- build-action/build.gradle.kts | 1 + .../build/action/GetResolvedDomAction.java | 52 +++++++++++++++++-- .../build/model/ResolvedDomPrerequisites.java | 13 +++++ gradle-client/build.gradle.kts | 3 +- .../client/ui/connected/ConnectedComponent.kt | 2 +- .../ui/connected/actions/GetGradleBuild.kt | 9 ++++ .../ui/connected/actions/GetModelAction.kt | 7 ++- .../ui/connected/actions/GetProjectSchema.kt | 7 +-- .../ui/connected/actions/GetResolvedDom.kt | 36 ++++++++++--- 9 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 build-action/src/main/java/org/gradle/client/build/model/ResolvedDomPrerequisites.java diff --git a/build-action/build.gradle.kts b/build-action/build.gradle.kts index 6ba2caf..a38bb91 100644 --- a/build-action/build.gradle.kts +++ b/build-action/build.gradle.kts @@ -4,6 +4,7 @@ plugins { dependencies { implementation(libs.gradle.tooling.api) + implementation(libs.gradle.declarative.dsl.tooling.models) } java { diff --git a/build-action/src/main/java/org/gradle/client/build/action/GetResolvedDomAction.java b/build-action/src/main/java/org/gradle/client/build/action/GetResolvedDomAction.java index 326041f..3fc03dd 100644 --- a/build-action/src/main/java/org/gradle/client/build/action/GetResolvedDomAction.java +++ b/build-action/src/main/java/org/gradle/client/build/action/GetResolvedDomAction.java @@ -1,13 +1,59 @@ package org.gradle.client.build.action; +import org.gradle.client.build.model.ResolvedDomPrerequisites; +import org.gradle.declarative.dsl.schema.AnalysisSchema; +import org.gradle.declarative.dsl.tooling.models.DeclarativeSchemaModel; import org.gradle.tooling.BuildAction; import org.gradle.tooling.BuildController; import org.gradle.tooling.model.gradle.GradleBuild; -public class GetResolvedDomAction implements BuildAction { +import java.io.File; + +public class GetResolvedDomAction implements BuildAction { @Override - public GradleBuild execute(BuildController controller) { - return controller.getModel(GradleBuild.class); + public ResolvedDomPrerequisites execute(BuildController controller) { + AnalysisSchema projectSchema = getProjectSchema(controller); + String buildFileContent = getBuildFileContent(controller); + return new ResolvedDomPrerequisitesImpl(projectSchema, buildFileContent); + } + + private static AnalysisSchema getProjectSchema(BuildController controller) { + DeclarativeSchemaModel declarativeSchemaModel = controller.getModel(DeclarativeSchemaModel.class); + return declarativeSchemaModel.getProjectSchema(); + } + + private static String getBuildFileContent(BuildController controller) { + GradleBuild gradleBuild = controller.getModel(GradleBuild.class); + File randomProjectBuildFile = gradleBuild.getProjects().getAll().stream() + .map(p -> new File(p.getProjectDirectory(), "build.gradle.dcl")) + .filter(File::exists) + .findFirst() + .orElseThrow(() -> new RuntimeException("Declarative project file not found")); + + return randomProjectBuildFile.getAbsolutePath(); + + + } + + private static final class ResolvedDomPrerequisitesImpl implements ResolvedDomPrerequisites { + + private final AnalysisSchema analysisSchema; + private final String buildFilePath; + + public ResolvedDomPrerequisitesImpl(AnalysisSchema analysisSchema, String buildFilePath) { + this.analysisSchema = analysisSchema; + this.buildFilePath = buildFilePath; + } + + @Override + public AnalysisSchema getAnalysisSchema() { + return analysisSchema; + } + + @Override + public String getBuildFilePath() { + return buildFilePath; + } } } \ No newline at end of file diff --git a/build-action/src/main/java/org/gradle/client/build/model/ResolvedDomPrerequisites.java b/build-action/src/main/java/org/gradle/client/build/model/ResolvedDomPrerequisites.java new file mode 100644 index 0000000..78691ba --- /dev/null +++ b/build-action/src/main/java/org/gradle/client/build/model/ResolvedDomPrerequisites.java @@ -0,0 +1,13 @@ +package org.gradle.client.build.model; + +import org.gradle.declarative.dsl.schema.AnalysisSchema; + +import java.io.Serializable; + +public interface ResolvedDomPrerequisites extends Serializable { + + AnalysisSchema getAnalysisSchema(); + + String getBuildFilePath(); + +} diff --git a/gradle-client/build.gradle.kts b/gradle-client/build.gradle.kts index 6451b66..2708377 100644 --- a/gradle-client/build.gradle.kts +++ b/gradle-client/build.gradle.kts @@ -66,10 +66,9 @@ kotlin { implementation(libs.slf4j.api) implementation(libs.logback.classic) + implementation(libs.gradle.declarative.dsl.core) implementation(libs.gradle.declarative.dsl.tooling.models) -// implementation(libs.gradle.declarative.dsl.core) - runtimeOnly(libs.kotlinx.coroutines.swing) } diff --git a/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/ConnectedComponent.kt b/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/ConnectedComponent.kt index 082c3f6..7719e17 100644 --- a/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/ConnectedComponent.kt +++ b/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/ConnectedComponent.kt @@ -114,7 +114,7 @@ class ConnectedComponent( try { if (modelAction is GetModelAction.GetCompositeModelAction) { logger.atDebug().log { "Run ${modelAction.javaClass.simpleName} build action!" } - val result = connection.action(modelAction.getBuildAction()) + val result = connection.action(modelAction.buildAction) .addArguments( when (parameters.javaHomeDir) { null -> "-Dorg.gradle.java.home=${System.getenv("JAVA_HOME")}" diff --git a/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetGradleBuild.kt b/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetGradleBuild.kt index 952d3fa..6e87557 100644 --- a/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetGradleBuild.kt +++ b/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetGradleBuild.kt @@ -5,6 +5,9 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable import org.gradle.tooling.model.gradle.GradleBuild +import java.io.File +import java.io.IOException +import java.nio.file.Files class GetGradleBuild : GetModelAction { @@ -12,6 +15,12 @@ class GetGradleBuild : GetModelAction { @Composable override fun ColumnScope.ModelContent(model: GradleBuild) { + model.getProjects().forEach { + println("project dir = ${it.projectDirectory}") + println("\tbuild file = ${File(it.projectDirectory, "build.gradle.dcl")}") + println("\tbuild file exits = ${File(it.projectDirectory, "build.gradle.dcl").exists()}") + } + Text( text = "Gradle Build", style = MaterialTheme.typography.titleMedium diff --git a/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetModelAction.kt b/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetModelAction.kt index 571c498..c8baaac 100644 --- a/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetModelAction.kt +++ b/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetModelAction.kt @@ -2,6 +2,7 @@ package org.gradle.client.ui.connected.actions import androidx.compose.foundation.layout.ColumnScope import androidx.compose.runtime.Composable +import org.gradle.declarative.dsl.schema.FqName.Empty.simpleName import org.gradle.tooling.BuildAction import kotlin.reflect.KClass @@ -16,6 +17,10 @@ interface GetModelAction { fun ColumnScope.ModelContent(model: T) interface GetCompositeModelAction : GetModelAction { - fun getBuildAction() : BuildAction + + val buildAction : BuildAction + + override val displayName: String + get() = "Run ${buildAction::class.simpleName}" } } diff --git a/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetProjectSchema.kt b/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetProjectSchema.kt index ad84c68..5cd0897 100644 --- a/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetProjectSchema.kt +++ b/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetProjectSchema.kt @@ -14,18 +14,13 @@ class GetProjectSchema : GetModelAction { @Composable override fun ColumnScope.ModelContent(model: DeclarativeSchemaModel) { Text( - text = "Gradle Project", + text = "Gradle Project Schema", style = MaterialTheme.typography.titleMedium ) Text( text = "Schema: ${model.projectSchema.toHumanReadable()}", style = MaterialTheme.typography.labelSmall ) - - /*val (tree, code, codeOffset) = parse(scriptSource.resource.text) - val languageModel = DefaultLanguageTreeBuilder().build(tree, code, codeOffset, SourceIdentifier(scriptSource.fileName)) - - resolvedDocument(model.projectSchema, )*/ } private fun AnalysisSchema.toHumanReadable(): String { diff --git a/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetResolvedDom.kt b/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetResolvedDom.kt index 3a8f8fa..66f9aa6 100644 --- a/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetResolvedDom.kt +++ b/gradle-client/src/jvmMain/kotlin/org/gradle/client/ui/connected/actions/GetResolvedDom.kt @@ -1,20 +1,42 @@ package org.gradle.client.ui.connected.actions import androidx.compose.foundation.layout.ColumnScope +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text import androidx.compose.runtime.Composable import org.gradle.client.build.action.GetResolvedDomAction +import org.gradle.client.build.model.ResolvedDomPrerequisites +import org.gradle.internal.declarativedsl.analysis.analyzeEverything +import org.gradle.internal.declarativedsl.dom.resolvedDocument +import org.gradle.internal.declarativedsl.language.LanguageTreeResult +import org.gradle.internal.declarativedsl.language.SourceIdentifier +import org.gradle.internal.declarativedsl.parsing.DefaultLanguageTreeBuilder +import org.gradle.internal.declarativedsl.parsing.parse import org.gradle.tooling.BuildAction -import org.gradle.tooling.model.gradle.GradleBuild +import java.io.File -class GetResolvedDom : GetModelAction.GetCompositeModelAction { +class GetResolvedDom : GetModelAction.GetCompositeModelAction { - override val modelType = GradleBuild::class + override val modelType = ResolvedDomPrerequisites::class - override fun getBuildAction(): BuildAction = - GetResolvedDomAction() + override val buildAction: BuildAction = GetResolvedDomAction() @Composable - override fun ColumnScope.ModelContent(model: GradleBuild) { - TODO("Not yet implemented") + override fun ColumnScope.ModelContent(model: ResolvedDomPrerequisites) { + val buildFile = File(model.buildFilePath) + val buildFileContent = buildFile.readText() + val parsedLightTree = parse(buildFileContent) + val languageTreeResult = DefaultLanguageTreeBuilder().build(parsedLightTree, SourceIdentifier(buildFile.name)) + + val resolvedDocument = resolvedDocument(model.analysisSchema, languageTreeResult, analyzeEverything, true) + + Text( + text = "Gradle Resolved DOM", + style = MaterialTheme.typography.titleMedium + ) + Text( + text = "Schema: $resolvedDocument", + style = MaterialTheme.typography.labelSmall + ) } } \ No newline at end of file