Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Getting tasks for Gradle < 5.0 #1619

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
jdneo marked this conversation as resolved.
Show resolved Hide resolved

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<String> taskNames = new HashSet<>();
for (GradleTask existingTask : rootModel.getTasks()) {
Expand All @@ -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<String> scriptClasspaths = new ArrayList<>();

classpath.getAsFiles().forEach((file) -> {
scriptClasspaths.add(file.getAbsolutePath());
});

GradleDependencyNode node = generateDefaultGradleDependencyNode(project);
List<String> plugins = getPlugins(project);
List<GradleClosure> closures = getPluginClosures(project);
List<GradleProjectModel> subModels = new ArrayList<>();
Map<String, Project> childProjects = project.getChildProjects();
for (Project childProject : childProjects.values()) {
GradleProjectModel subModel = buildModel(rootProject, childProject);
if (subModel != null) {
subModels.add(subModel);
}
}

List<GradleTask> tasks = getGradleTasks(rootProject, project);

return new DefaultGradleProjectModel(project.getParent() == null, project.getProjectDir().getAbsolutePath(),
subModels, tasks, node, plugins, closures, scriptClasspaths);
}

private List<GradleTask> getGradleTasks(Project rootProject, Project project) {
Copy link
Member

@jdneo jdneo Nov 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just directly fetch the tasks with this approach no matter the project's gradle version is > or < 5? Since they look very similar.

// The original code is not implemented by me so I'm not aware of the history of it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for long response, as far as I understand plugin's local gradle project model does not support some lazy tasks, so this commit was made as fix #1302 in which now plugin uses gradle built in model. I couldn't reproduce this bug though so I can try to unify these methods to be compatible with everything, but it surely will need more testing

List<GradleTask> 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;
Expand Down Expand Up @@ -277,4 +340,4 @@ private boolean isDeprecated(AccessibleObject object) {
}
return false;
}
}
}