Skip to content

Commit

Permalink
-- wip --
Browse files Browse the repository at this point in the history
  • Loading branch information
jbartok committed May 17, 2024
1 parent f97512f commit 9a7ef38
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 20 deletions.
1 change: 1 addition & 0 deletions build-action/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {

dependencies {
implementation(libs.gradle.tooling.api)
implementation(libs.gradle.declarative.dsl.tooling.models)
}

java {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GradleBuild> {
import java.io.File;

public class GetResolvedDomAction implements BuildAction<ResolvedDomPrerequisites> {

@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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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();

}
3 changes: 1 addition & 2 deletions gradle-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ 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<GradleBuild> {

override val modelType = GradleBuild::class

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -16,6 +17,10 @@ interface GetModelAction<T : Any> {
fun ColumnScope.ModelContent(model: T)

interface GetCompositeModelAction<T : Any> : GetModelAction<T> {
fun getBuildAction() : BuildAction<T>

val buildAction : BuildAction<T>

override val displayName: String
get() = "Run ${buildAction::class.simpleName}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@ class GetProjectSchema : GetModelAction<DeclarativeSchemaModel> {
@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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GradleBuild> {
class GetResolvedDom : GetModelAction.GetCompositeModelAction<ResolvedDomPrerequisites> {

override val modelType = GradleBuild::class
override val modelType = ResolvedDomPrerequisites::class

override fun getBuildAction(): BuildAction<GradleBuild> =
GetResolvedDomAction()
override val buildAction: BuildAction<ResolvedDomPrerequisites> = 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
)
}
}

0 comments on commit 9a7ef38

Please sign in to comment.