From 2b6515ad6c2fa51b45ff38e870c68aeb2c76c185 Mon Sep 17 00:00:00 2001 From: MrRetro Date: Sat, 2 Nov 2024 00:14:06 +0300 Subject: [PATCH 1/2] Fix Tasks for Gradle < 5.0 --- .../gradle/GradleProjectModelBuilder.java | 75 +++++++++++++++++-- 1 file changed, 69 insertions(+), 6 deletions(-) diff --git a/gradle-plugin/src/main/java/com/microsoft/gradle/GradleProjectModelBuilder.java b/gradle-plugin/src/main/java/com/microsoft/gradle/GradleProjectModelBuilder.java index 960f42e84..28b41cfca 100644 --- a/gradle-plugin/src/main/java/com/microsoft/gradle/GradleProjectModelBuilder.java +++ b/gradle-plugin/src/main/java/com/microsoft/gradle/GradleProjectModelBuilder.java @@ -63,13 +63,20 @@ public boolean canBuild(String modelName) { } public Object buildAll(String modelName, Project project) { + int majorVersion = Integer.parseInt(project.getGradle().getGradleVersion().split("\\.")[0]); + cachedTasks.clear(); - DefaultGradleProject gradleProject = (DefaultGradleProject) this.registry - .getBuilder("org.gradle.tooling.model.GradleProject").buildAll(modelName, project); - if (gradleProject == null) { - return null; + + GradleProjectModel rootModel = null; + if (majorVersion < 5) { + rootModel = buildModel(project, project); + } else { + DefaultGradleProject gradleProject = (DefaultGradleProject) this.registry + .getBuilder("org.gradle.tooling.model.GradleProject").buildAll(modelName, project); + + rootModel = buildModel(project, project.getName(), gradleProject); } - GradleProjectModel rootModel = buildModel(project, project.getName(), gradleProject); + // add task selectors for root project Set taskNames = new HashSet<>(); for (GradleTask existingTask : rootModel.getTasks()) { @@ -93,6 +100,62 @@ public Object buildAll(String modelName, Project project) { return rootModel; } + private GradleProjectModel buildModel(Project rootProject, Project project) { + if (rootProject == null || project == null) { + return null; + } + + ScriptHandler buildScript = project.getBuildscript(); + + ClassPath classpath = ((DefaultScriptHandler) buildScript).getScriptClassPath(); + + List scriptClasspaths = new ArrayList<>(); + + classpath.getAsFiles().forEach((file) -> { + scriptClasspaths.add(file.getAbsolutePath()); + }); + + GradleDependencyNode node = generateDefaultGradleDependencyNode(project); + List plugins = getPlugins(project); + List closures = getPluginClosures(project); + List subModels = new ArrayList<>(); + Map childProjects = project.getChildProjects(); + for (Project childProject : childProjects.values()) { + GradleProjectModel subModel = buildModel(rootProject, childProject); + if (subModel != null) { + subModels.add(subModel); + } + } + + List tasks = getGradleTasks(rootProject, project); + + return new DefaultGradleProjectModel(project.getParent() == null, project.getProjectDir().getAbsolutePath(), + subModels, tasks, node, plugins, closures, scriptClasspaths); + } + + private List getGradleTasks(Project rootProject, Project project) { + List tasks = new ArrayList<>(); + TaskContainer taskContainer = project.getTasks(); + + if (taskContainer instanceof TaskContainerInternal) { + TaskContainerInternal taskContainerInternal = (TaskContainerInternal) taskContainer; + taskContainerInternal.discoverTasks(); + taskContainerInternal.realize(); + taskContainerInternal.forEach(task -> { + String buildFile = task.getProject().getBuildscript().getSourceFile().getAbsolutePath(); + boolean debuggable = (task instanceof JavaExec) || (task instanceof Test); + GradleTask newTask = new DefaultGradleTask(task.getName(), task.getGroup(), task.getPath(), + task.getProject().getName(), buildFile, rootProject.getName(), task.getDescription(), + debuggable); + + tasks.add(newTask); + cachedTasks.add(newTask); + }); + } + + return tasks; + } + private GradleProjectModel buildModel(Project project, String rootProjectName, DefaultGradleProject gradleProject) { if (project == null) { return null; @@ -277,4 +340,4 @@ private boolean isDeprecated(AccessibleObject object) { } return false; } -} +} \ No newline at end of file From f3962a4c1dcda52e4a8f6970e0c115adfbcf829f Mon Sep 17 00:00:00 2001 From: MrRetro Date: Sat, 9 Nov 2024 11:43:40 +0300 Subject: [PATCH 2/2] Compare gradle versions --- .../gradle/GradleProjectModelBuilder.java | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/gradle-plugin/src/main/java/com/microsoft/gradle/GradleProjectModelBuilder.java b/gradle-plugin/src/main/java/com/microsoft/gradle/GradleProjectModelBuilder.java index 28b41cfca..aa0038bed 100644 --- a/gradle-plugin/src/main/java/com/microsoft/gradle/GradleProjectModelBuilder.java +++ b/gradle-plugin/src/main/java/com/microsoft/gradle/GradleProjectModelBuilder.java @@ -22,6 +22,7 @@ import java.util.Map; import java.util.Set; import java.util.TreeSet; +import java.util.stream.Collectors; import org.gradle.api.Project; import org.gradle.api.Task; import org.gradle.api.artifacts.Configuration; @@ -63,12 +64,12 @@ public boolean canBuild(String modelName) { } public Object buildAll(String modelName, Project project) { - int majorVersion = Integer.parseInt(project.getGradle().getGradleVersion().split("\\.")[0]); - cachedTasks.clear(); GradleProjectModel rootModel = null; - if (majorVersion < 5) { + int cmpVersion = GradleVersion.current().compareTo(GradleVersion.version("5.0.0")); + + if (cmpVersion == -1) { rootModel = buildModel(project, project); } else { DefaultGradleProject gradleProject = (DefaultGradleProject) this.registry @@ -109,23 +110,20 @@ private GradleProjectModel buildModel(Project rootProject, Project project) { ClassPath classpath = ((DefaultScriptHandler) buildScript).getScriptClassPath(); - List scriptClasspaths = new ArrayList<>(); - - classpath.getAsFiles().forEach((file) -> { - scriptClasspaths.add(file.getAbsolutePath()); - }); + List scriptClasspaths = classpath.getAsFiles().stream().map(file -> file.getAbsolutePath()) + .collect(Collectors.toList()); GradleDependencyNode node = generateDefaultGradleDependencyNode(project); + List plugins = getPlugins(project); + List closures = getPluginClosures(project); - List subModels = new ArrayList<>(); + Map childProjects = project.getChildProjects(); - for (Project childProject : childProjects.values()) { - GradleProjectModel subModel = buildModel(rootProject, childProject); - if (subModel != null) { - subModels.add(subModel); - } - } + + List subModels = childProjects.values().stream() + .map(childProject -> buildModel(rootProject, childProject)).filter(subModel -> subModel != null) + .collect(Collectors.toList()); List tasks = getGradleTasks(rootProject, project);