-
-
Notifications
You must be signed in to change notification settings - Fork 417
/
AboutLibrariesTask.kt
111 lines (97 loc) · 4.46 KB
/
AboutLibrariesTask.kt
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
package com.mikepenz.aboutlibraries.plugin
import com.mikepenz.aboutlibraries.plugin.mapping.Library
import com.mikepenz.aboutlibraries.plugin.mapping.License
import com.mikepenz.aboutlibraries.plugin.model.writeToDisk
import com.mikepenz.aboutlibraries.plugin.util.forLicense
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.slf4j.LoggerFactory
import java.io.File
import java.util.Locale
@CacheableTask
abstract class AboutLibrariesTask : BaseAboutLibrariesTask() {
@Input
val strictMode = extension.strictMode
@Input
val outputFileName = extension.outputFileName
@Internal
var resultDirectory: File = project.file("${project.buildDir}/generated/aboutLibraries/res/")
set(value) {
field = value
combinedLibrariesOutputFile = File(resultDirectory, outputFileName)
}
@OutputFile
var combinedLibrariesOutputFile = File(resultDirectory, outputFileName)
@TaskAction
public fun action() {
if (!resultDirectory.exists()) {
resultDirectory.mkdirs() // verify output exists
}
val result = createLibraryProcessor().gatherDependencies()
// validate found licenses match expectation
val allowedLicenses = allowedLicenses.map { it.lowercase(Locale.ENGLISH) }
if (allowedLicenses.isNotEmpty() && strictMode != StrictMode.IGNORE) {
// detect all missing licenses
val missing = mutableListOf<License>()
result.licenses.values.forEach {
val id = it.spdxId?.lowercase(Locale.ENGLISH) ?: it.hash.lowercase(Locale.ENGLISH)
val name = it.name.lowercase(Locale.ENGLISH)
val url = it.url?.lowercase(Locale.ENGLISH)
if (!(allowedLicenses.contains(id)
|| allowedLicenses.contains(name)
|| (url?.isNotEmpty() == true && allowedLicenses.contains(url)))
) {
missing.add(it)
}
}
val missingMapped = mutableMapOf<License, List<Library>>()
if (allowedLicensesMap.isNotEmpty()) {
missing.forEach {
val id = it.spdxId?.lowercase(Locale.ENGLISH) ?: it.hash.lowercase(Locale.ENGLISH)
val libsForLicense = allowedLicensesMap[id] ?: allowedLicensesMap[name]
if (libsForLicense != null) {
val notAllowed = result.libraries.forLicense(it).filter { lib ->
!(libsForLicense.contains(lib.uniqueId) || libsForLicense.contains(lib.groupId) || libsForLicense.contains(lib.artifactId))
}
if (notAllowed.isNotEmpty()) {
missingMapped[it] = notAllowed
}
} else {
missing.forEach { missingMapped[it] = result.libraries.forLicense(it) }
}
}
} else {
missing.forEach { missingMapped[it] = result.libraries.forLicense(it) }
}
if (missingMapped.isEmpty()) {
LOGGER.info("No libraries detected using a license not allowed.")
} else {
val message = StringBuilder()
repeat(2) {
message.appendLine("=======================================")
}
message.appendLine("Detected usage of not allowed licenses!")
missingMapped.forEach { (license, libraries) ->
message.appendLine("-> License: ${license.name} | ${license.spdxId ?: "-"} (${license.url}), used by:")
libraries.forEach { lib -> message.appendLine(" ${lib.uniqueId}") }
}
repeat(2) {
message.appendLine("=======================================")
}
if (strictMode == StrictMode.FAIL) {
throw IllegalStateException(message.toString())
} else {
LOGGER.warn(message.toString())
}
}
}
// write to disk
result.writeToDisk(combinedLibrariesOutputFile, excludeFields, extension.prettyPrint)
}
companion object {
private val LOGGER = LoggerFactory.getLogger(AboutLibrariesTask::class.java)
}
}