-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ProjectReportTask.java
219 lines (199 loc) · 8.59 KB
/
ProjectReportTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.tasks.diagnostics;
import org.apache.commons.lang.StringUtils;
import org.gradle.api.Incubating;
import org.gradle.api.Project;
import org.gradle.api.internal.project.ProjectHierarchyUtils;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.tasks.diagnostics.internal.ProjectDetails;
import org.gradle.api.tasks.diagnostics.internal.TextReportRenderer;
import org.gradle.initialization.BuildClientMetaData;
import org.gradle.internal.build.BuildStateRegistry;
import org.gradle.internal.build.IncludedBuildState;
import org.gradle.internal.graph.GraphRenderer;
import org.gradle.internal.logging.text.StyledTextOutput;
import org.gradle.util.Path;
import org.gradle.util.internal.CollectionUtils;
import org.gradle.work.DisableCachingByDefault;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static org.gradle.internal.logging.text.StyledTextOutput.Style.Description;
import static org.gradle.internal.logging.text.StyledTextOutput.Style.Info;
import static org.gradle.internal.logging.text.StyledTextOutput.Style.UserInput;
/**
* <p>Displays a list of projects in the build. An instance of this type is used when you execute the {@code projects}
* task from the command-line.</p>
*/
@DisableCachingByDefault(because = "Not worth caching")
public abstract class ProjectReportTask extends AbstractProjectBasedReportTask<ProjectReportTask.ProjectReportModel> {
private final TextReportRenderer renderer = new TextReportRenderer();
@Override
protected TextReportRenderer getRenderer() {
return renderer;
}
@Inject
public BuildStateRegistry getBuildStateRegistry() {
throw new UnsupportedOperationException();
}
/**
* Report model.
*
* @since 7.6
*/
@Incubating
public static final class ProjectReportModel {
private final ProjectDetails project;
private final List<ProjectReportModel> children;
private final boolean isRootProject;
private final String tasksTaskPath;
private final String rootProjectProjectsTaskPath;
private final List<Path> includedBuildIdentityPaths;
private ProjectReportModel(
ProjectDetails project,
List<ProjectReportModel> children,
boolean isRootProject,
String tasksTaskPath,
String rootProjectProjectsTaskPath,
List<Path> includedBuildIdentityPaths
) {
this.project = project;
this.children = children;
this.isRootProject = isRootProject;
this.tasksTaskPath = tasksTaskPath;
this.rootProjectProjectsTaskPath = rootProjectProjectsTaskPath;
this.includedBuildIdentityPaths = includedBuildIdentityPaths;
}
}
@Override
protected ProjectReportModel calculateReportModelFor(Project project) {
return new ProjectReportModel(
ProjectDetails.of(project),
calculateChildrenProjectsFor(project),
project == project.getRootProject(),
project.absoluteProjectPath(ProjectInternal.TASKS_TASK),
project.getRootProject().absoluteProjectPath(ProjectInternal.PROJECTS_TASK),
calculateIncludedBuildIdentityPaths()
);
}
private List<ProjectReportModel> calculateChildrenProjectsFor(Project project) {
List<Project> childProjects = CollectionUtils.sort(ProjectHierarchyUtils.getChildProjectsForInternalUse(project));
List<ProjectReportModel> children = new ArrayList<>(childProjects.size());
for (Project childProject : childProjects) {
children.add(calculateReportModelFor(childProject));
}
return children;
}
private List<Path> calculateIncludedBuildIdentityPaths() {
Collection<? extends IncludedBuildState> includedBuilds = getBuildStateRegistry().getIncludedBuilds();
List<Path> includedBuildIdentityPaths = new ArrayList<>(includedBuilds.size());
for (IncludedBuildState includedBuild : includedBuilds) {
includedBuildIdentityPaths.add(includedBuild.getIdentityPath());
}
return includedBuildIdentityPaths;
}
@Override
protected void generateReportFor(ProjectDetails project, ProjectReportModel model) {
renderProjectTree(model);
renderIncludedBuilds(model);
renderHelp(project, model);
}
private void renderProjectTree(ProjectReportModel model) {
StyledTextOutput textOutput = getRenderer().getTextOutput();
renderProject(model, new GraphRenderer(textOutput), true, textOutput);
if (model.children.isEmpty()) {
textOutput.withStyle(Info).text("No sub-projects");
textOutput.println();
}
}
private void renderProject(
ProjectReportModel model,
GraphRenderer renderer,
boolean lastChild,
StyledTextOutput textOutput
) {
renderer.visit(styledTextOutput -> {
styledTextOutput.text(StringUtils.capitalize(model.project.getDisplayName()));
String projectDescription = model.project.getDescription();
if (projectDescription != null && !projectDescription.isEmpty()) {
String description = projectDescription.trim();
int newlineInDescription = description.indexOf('\n');
if (newlineInDescription > 0) {
textOutput.withStyle(Description).text(" - " + description.substring(0, newlineInDescription) + "...");
} else {
textOutput.withStyle(Description).text(" - " + description);
}
}
}, lastChild);
renderer.startChildren();
for (ProjectReportModel child : model.children) {
renderProject(child, renderer, child == model.children.get(model.children.size() - 1), textOutput);
}
renderer.completeChildren();
}
private void renderIncludedBuilds(ProjectReportModel model) {
StyledTextOutput textOutput = getRenderer().getTextOutput();
if (model.isRootProject) {
int index = 0;
if (!model.includedBuildIdentityPaths.isEmpty()) {
GraphRenderer renderer = new GraphRenderer(textOutput);
textOutput.println();
textOutput.text("Included builds");
textOutput.println();
renderer.startChildren();
for (Path includedBuildIdentityPath : model.includedBuildIdentityPaths) {
renderer.visit(
text -> textOutput.text("Included build '" + includedBuildIdentityPath + "'"),
(index + 1) == model.includedBuildIdentityPaths.size()
);
index++;
}
renderer.completeChildren();
}
}
}
private void renderHelp(ProjectDetails project, ProjectReportModel model) {
BuildClientMetaData metaData = getClientMetaData();
StyledTextOutput textOutput = getRenderer().getTextOutput();
textOutput.println();
textOutput.text("To see a list of the tasks of a project, run ");
metaData.describeCommand(
textOutput.withStyle(UserInput),
"<project-path>:" + ProjectInternal.TASKS_TASK
);
textOutput.println();
textOutput.text("For example, try running ");
ProjectReportModel exampleProject = model.children.isEmpty()
? model
: model.children.get(0);
metaData.describeCommand(
textOutput.withStyle(UserInput),
exampleProject.tasksTaskPath
);
textOutput.println();
if (!model.isRootProject) {
textOutput.println();
textOutput.text("To see a list of all the projects in this build, run ");
metaData.describeCommand(
textOutput.withStyle(UserInput),
model.rootProjectProjectsTaskPath
);
textOutput.println();
}
}
}