-
Notifications
You must be signed in to change notification settings - Fork 112
/
LicensePlugin.groovy
336 lines (278 loc) · 13.1 KB
/
LicensePlugin.groovy
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* License added by: GRADLE-LICENSE-PLUGIN
*
* Copyright (C)2011 - Jeroen van Erp <jeroen@javadude.nl>
*
* 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 nl.javadude.gradle.plugins.license
import org.gradle.api.Action
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.plugins.ReportingBasePlugin
import org.gradle.api.tasks.SourceSet
class LicensePlugin implements Plugin<Project> {
private static Logger logger = Logging.getLogger(LicensePlugin);
static final String DEFAULT_FILE_NAME_FOR_REPORTS_BY_DEPENDENCY = "dependency-license"
static final String DEFAULT_FILE_NAME_FOR_REPORTS_BY_LICENSE = "license-dependency"
static final String DEFAULT_DEPENDENCY_CONFIGURATION_TO_HANDLE = "runtime"
protected Project project
protected LicenseExtension extension
protected DownloadLicensesExtension downloadLicensesExtension
def taskBaseName = 'license'
def downloadLicenseTaskName = 'downloadLicenses'
protected Task baseCheckTask
protected Task baseFormatTask
protected Task downloadLicenseTask
void apply(Project project) {
this.project = project
project.plugins.apply(ReportingBasePlugin)
// Create a single task to run all license checks and reformattings
baseCheckTask = project.task(taskBaseName)
baseFormatTask = project.task("${taskBaseName}Format")
downloadLicenseTask = project.tasks.create(downloadLicenseTaskName, DownloadLicenses)
baseCheckTask.group = baseFormatTask.group = downloadLicenseTask.group = "License"
baseCheckTask.description = "Checks for header consistency."
baseFormatTask.description = "Applies the license found in the header file in files missing the header."
downloadLicenseTask.description = "Generates reports on your runtime dependencies."
extension = createExtension()
downloadLicensesExtension = createDownloadLicensesExtension()
project.plugins.with {
withType(JavaBasePlugin) {
configureJava()
}
['com.android.build.gradle.AppPlugin', 'com.android.build.gradle.LibraryPlugin'].each { c ->
withOptionalPlugin(c) {
configureAndroid(Class.forName(c))
}
}
}
configureTaskRule()
}
void withOptionalPlugin(String pluginClassName, Action<? extends Plugin> configureAction) {
try {
def pluginClass = Class.forName(pluginClassName)
// Will most likely throw a ClassNotFoundException
project.plugins.withType(pluginClass, configureAction)
} catch(ClassNotFoundException nfe) {
// do nothing
}
}
protected LicenseExtension createExtension() {
extension = project.extensions.create(taskBaseName, LicenseExtension)
extension.with {
// Default for extension
header = project.file("LICENSE")
headerURI = null
ignoreFailures = false
dryRun = false
skipExistingHeaders = false
useDefaultMappings = true
strictCheck = false
encoding = System.properties['file.encoding']
conventionMapping.with {
sourceSets = { [] }
}
}
logger.info("Adding license extension");
return extension
}
/**
* Create and init with defaults downloadLicense extension.
*
* @return DownloadLicensesExtension
*/
protected DownloadLicensesExtension createDownloadLicensesExtension() {
downloadLicensesExtension = project.extensions.create(downloadLicenseTaskName, DownloadLicensesExtension)
def html = new LicensesReport(enabled: true, destination: new File("${project.reporting.baseDir.path}/license"))
def xml = new LicensesReport(enabled: true, destination: new File("${project.reporting.baseDir.path}/license"))
downloadLicensesExtension.with {
// Default for extension
reportByDependency = true
reportByLicenseType = true
includeProjectDependencies = false
ignoreFatalParseErrors = false
reportByDependencyFileName = DEFAULT_FILE_NAME_FOR_REPORTS_BY_DEPENDENCY
reportByLicenseFileName = DEFAULT_FILE_NAME_FOR_REPORTS_BY_LICENSE
excludeDependencies = []
licenses = [:]
aliases = [:]
report = new DownloadLicensesReportExtension(html: html, xml: xml)
dependencyConfiguration = DEFAULT_DEPENDENCY_CONFIGURATION_TO_HANDLE
}
logger.info("Adding download licenses extension");
return downloadLicensesExtension
}
/**
* Establish defaults for extension, which will be all sourceSets
*/
protected void configureExtensionRule(Class type) {
project.plugins.withType(type) {
// Defaults to use having all the sourceSets
extension.conventionMapping.sourceSets = { project.sourceSets }
}
logger.info("Adding license extension rule");
}
/**
* We'll be creating the tasks by default based on the source sets, but users could define their
* own, and we'd still want it configured.
* TODO: Confirm that user defined tasks will get this configuration, it'd have to be lazily evaluated
* @param task
*/
protected void configureTaskRule() {
project.tasks.withType(License) { License task ->
logger.info("Applying license defaults to task: ${task.path}");
configureTaskDefaults(task)
}
project.tasks.withType(DownloadLicenses) { DownloadLicenses task ->
logger.info("Applying defaults to download task: ${task.path}");
configureTaskDefaults(task)
}
}
/**
* Configure convention mapping.
*
* @param task download license task
*/
protected void configureTaskDefaults(DownloadLicenses task) {
task.conventionMapping.with {
reportByDependency = { downloadLicensesExtension.reportByDependency }
reportByLicenseType = { downloadLicensesExtension.reportByLicenseType }
reportByDependencyFileName = { downloadLicensesExtension.reportByDependencyFileName }
reportByLicenseFileName = { downloadLicensesExtension.reportByLicenseFileName }
includeProjectDependencies = {downloadLicensesExtension.includeProjectDependencies}
ignoreFatalParseErrors = {downloadLicensesExtension.ignoreFatalParseErrors}
licenses = { downloadLicensesExtension.licenses }
aliases = {downloadLicensesExtension.aliases }
xml = { downloadLicensesExtension.report.xml.enabled }
html = { downloadLicensesExtension.report.html.enabled }
excludeDependencies = { downloadLicensesExtension.excludeDependencies }
xmlDestination = { downloadLicensesExtension.report.xml.destination }
htmlDestination = { downloadLicensesExtension.report.html.destination }
dependencyConfiguration = { downloadLicensesExtension.dependencyConfiguration }
}
}
protected void configureTaskDefaults(License task) {
// Have Task Convention lazily default back to the extension
task.conventionMapping.with {
// Defaults for task, which will delegate to project's License extension
// These can still be explicitly set by the user on the individual tasks
header = { extension.header }
headerURI = { extension.headerURI }
ignoreFailures = { extension.ignoreFailures }
dryRun = { extension.dryRun }
skipExistingHeaders = { extension.skipExistingHeaders }
useDefaultMappings = { extension.useDefaultMappings }
strictCheck = { extension.strictCheck }
inheritedProperties = { extension.ext.properties }
inheritedMappings = { extension.internalMappings }
excludes = { extension.excludePatterns }
includes = { extension.includePatterns }
encoding = { extension.encoding }
}
}
private void configureJava() {
configureExtensionRule(JavaBasePlugin)
project.afterEvaluate {
// Since we're going to look at the extension, we need to run late enough to let the user configure it
configureSourceSetRule()
}
}
/**
* Dynamically create a task for each sourceSet, and register with check
*/
private void configureSourceSetRule() {
// This follows the other check task pattern
project.plugins.withType(JavaBasePlugin) {
extension.sourceSets.all { SourceSet sourceSet ->
def sourceSetTaskName = sourceSet.getTaskName(taskBaseName, null)
logger.info("Adding license tasks for sourceSet ${sourceSetTaskName}");
License checkTask = project.tasks.create(sourceSetTaskName, License)
checkTask.check = true
configureForSourceSet(sourceSet, checkTask)
baseCheckTask.dependsOn checkTask
// Add independent license task, which will perform format
def sourceSetFormatTaskName = sourceSet.getTaskName(taskBaseName + 'Format', null)
License formatTask = project.tasks.create(sourceSetFormatTaskName, License)
formatTask.check = false
configureForSourceSet(sourceSet, formatTask)
baseFormatTask.dependsOn formatTask
// Add independent clean task to remove headers
// TODO
}
// Add license checking into check lifecycle, since its a type of code quality plugin
project.tasks[JavaBasePlugin.CHECK_TASK_NAME].dependsOn baseCheckTask
}
}
protected void configureForSourceSet(SourceSet sourceSet, License task) {
task.with {
// Explicitly set description
description = "Scanning license on ${sourceSet.name} files"
}
// Default to all source files from SourceSet
task.source = sourceSet.allSource
}
private void configureAndroid(Class pluginType) {
configureExtensionRule(pluginType)
project.afterEvaluate {
// Since we're going to look at the extension, we need to run late enough to let the user configure it
configureAndroidSourceSetRule(pluginType)
}
}
/**
* Dynamically create a task for each sourceSet, and register with check
*/
private void configureAndroidSourceSetRule(Class pluginType) {
// This follows the other check task pattern
project.plugins.withType(pluginType) {
extension.sourceSets.all { sourceSet ->
def sourceSetTaskName = "${taskBaseName}Android${sourceSet.name.capitalize()}"
logger.info("[AndroidLicensePlugin] Adding license tasks for sourceSet ${sourceSetTaskName}");
License checkTask = project.tasks.create(sourceSetTaskName, License)
checkTask.check = true
configureForAndroidSourceSet(sourceSet, checkTask)
baseCheckTask.dependsOn checkTask
// Add independent license task, which will perform format
def sourceSetFormatTaskName = "${taskBaseName}FormatAndroid${sourceSet.name.capitalize()}"
License formatTask = project.tasks.create(sourceSetFormatTaskName, License)
formatTask.check = false
configureForAndroidSourceSet(sourceSet, formatTask)
baseFormatTask.dependsOn formatTask
// Add independent clean task to remove headers
// TODO
}
// Add license checking into check lifecycle, since its a type of code quality plugin
project.tasks['check'].dependsOn baseCheckTask
}
}
protected void configureForAndroidSourceSet(sourceSet, task) {
task.with {
// Explicitly set description
description = "Scanning license on ${sourceSet.name} files"
}
sourceSet.properties.each { key, val ->
logger.debug("[AndroidLicensePlugin] sourceSet.$key:$val");
}
ArrayList androidSource = new ArrayList<File>();
for (File file : sourceSet.java.sourceFiles) {
androidSource.add(file);
}
for (File file : sourceSet.res.sourceFiles) {
androidSource.add(file);
}
task.source = project.files(androidSource)
}
}