-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
AbstractReportTask.java
151 lines (133 loc) · 4.46 KB
/
AbstractReportTask.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
/*
* 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.gradle.api.Project;
import org.gradle.api.internal.ConventionTask;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.diagnostics.internal.ReportGenerator;
import org.gradle.api.tasks.diagnostics.internal.ReportRenderer;
import org.gradle.initialization.BuildClientMetaData;
import org.gradle.internal.logging.ConsoleRenderer;
import org.gradle.internal.logging.text.StyledTextOutputFactory;
import org.gradle.work.DisableCachingByDefault;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
/**
* The base class for all project report tasks.
*
* Preserved for backward compatibility.
*
* @deprecated Use {@link AbstractProjectBasedReportTask} instead.
*/
@Deprecated
@DisableCachingByDefault(because = "Abstract super-class, not to be instantiated directly")
public abstract class AbstractReportTask extends ConventionTask {
private File outputFile;
// todo annotate as required
private Set<Project> projects;
protected AbstractReportTask() {
getOutputs().upToDateWhen(element -> false);
projects = new HashSet<>();
projects.add(getProject());
}
@Inject
protected BuildClientMetaData getClientMetaData() {
throw new UnsupportedOperationException();
}
@Inject
protected StyledTextOutputFactory getTextOutputFactory() {
throw new UnsupportedOperationException();
}
@TaskAction
public void generate() {
reportGenerator().generateReport(
new TreeSet<>(getProjects()),
project -> {
generate(project);
logClickableOutputFileUrl();
}
);
}
ReportGenerator reportGenerator() {
return new ReportGenerator(
getRenderer(),
getClientMetaData(),
getOutputFile(),
getTextOutputFactory()
);
}
void logClickableOutputFileUrl() {
if (shouldCreateReportFile()) {
getLogger().lifecycle("See the report at: {}", clickableOutputFileUrl());
}
}
String clickableOutputFileUrl() {
return new ConsoleRenderer().asClickableFileUrl(getOutputFile());
}
boolean shouldCreateReportFile() {
return getOutputFile() != null;
}
@Internal
protected abstract ReportRenderer getRenderer();
protected abstract void generate(Project project) throws IOException;
/**
* Returns the file which the report will be written to. When set to {@code null}, the report is written to {@code System.out}.
* Defaults to {@code null}.
*
* @return The output file. May be null.
*/
@Nullable
@Optional
@OutputFile
public File getOutputFile() {
return outputFile;
}
/**
* Sets the file which the report will be written to. Set this to {@code null} to write the report to {@code System.out}.
*
* @param outputFile The output file. May be null.
*/
public void setOutputFile(@Nullable File outputFile) {
this.outputFile = outputFile;
}
/**
* Returns the set of project to generate this report for. By default, the report is generated for the task's
* containing project.
*
* @return The set of files.
*/
@Internal
// TODO:LPTR Have the paths of the projects serve as @Input maybe?
public Set<Project> getProjects() {
return projects;
}
/**
* Specifies the set of projects to generate this report for.
*
* @param projects The set of projects. Must not be null.
*/
public void setProjects(Set<Project> projects) {
this.projects = projects;
}
}