|
| 1 | +/* |
| 2 | + * Copyright 2019 Vladimir Sitnikov <[email protected]> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package com.github.vlsi.gradle.license |
| 19 | + |
| 20 | +import com.github.vlsi.gradle.license.api.License |
| 21 | +import com.github.vlsi.gradle.license.api.LicenseEquivalence |
| 22 | +import com.github.vlsi.gradle.license.api.LicenseExpression |
| 23 | +import com.github.vlsi.gradle.license.api.asExpression |
| 24 | +import org.gradle.api.Action |
| 25 | +import org.gradle.api.DefaultTask |
| 26 | +import org.gradle.api.GradleException |
| 27 | +import org.gradle.api.artifacts.component.ModuleComponentIdentifier |
| 28 | +import org.gradle.api.file.ProjectLayout |
| 29 | +import org.gradle.api.logging.LogLevel |
| 30 | +import org.gradle.api.model.ObjectFactory |
| 31 | +import org.gradle.api.tasks.Console |
| 32 | +import org.gradle.api.tasks.Input |
| 33 | +import org.gradle.api.tasks.InputFiles |
| 34 | +import org.gradle.api.tasks.OutputFile |
| 35 | +import org.gradle.api.tasks.TaskAction |
| 36 | +import org.gradle.api.tasks.options.Option |
| 37 | +import org.gradle.kotlin.dsl.invoke |
| 38 | +import org.gradle.kotlin.dsl.mapProperty |
| 39 | +import org.gradle.kotlin.dsl.property |
| 40 | +import java.util.* |
| 41 | +import javax.inject.Inject |
| 42 | + |
| 43 | +open class VerifyLicenseCompatibilityTask @Inject constructor( |
| 44 | + objectFactory: ObjectFactory, |
| 45 | + layout: ProjectLayout |
| 46 | +) : DefaultTask() { |
| 47 | + @InputFiles |
| 48 | + val metadata = objectFactory.fileCollection() |
| 49 | + |
| 50 | + @Input |
| 51 | + val failOnIncompatibleLicense = objectFactory.property<Boolean>().convention(true) |
| 52 | + |
| 53 | + @Input |
| 54 | + val resolvedCases = objectFactory.mapProperty<LicenseExpression, LicenseCompatibility>() |
| 55 | + |
| 56 | + @Input |
| 57 | + val licenseSimilarityNormalizationThreshold = |
| 58 | + objectFactory.property<Int>().convention(42) |
| 59 | + |
| 60 | + @Option(option = "print", description = "prints the verification results to console") |
| 61 | + @Console |
| 62 | + val printResults = objectFactory.property<Boolean>().convention(false) |
| 63 | + |
| 64 | + /** |
| 65 | + * Outputs the license verification results (incompatible and unknown licenses are listed first). |
| 66 | + */ |
| 67 | + @OutputFile |
| 68 | + val outputFile = objectFactory.fileProperty() |
| 69 | + .convention(layout.buildDirectory.file("verifyLicense/$name/verification_result.txt")) |
| 70 | + |
| 71 | + private fun registerResolution( |
| 72 | + licenseExpression: LicenseExpression, |
| 73 | + type: CompatibilityResult, |
| 74 | + action: Action<LicenseCompatibilityConfig>? = null |
| 75 | + ) { |
| 76 | + val reason = action?.let { |
| 77 | + object : LicenseCompatibilityConfig { |
| 78 | + var reason = "" |
| 79 | + override fun because(reason: String) { |
| 80 | + this.reason = reason |
| 81 | + } |
| 82 | + }.let { |
| 83 | + action.invoke(it) |
| 84 | + it.reason |
| 85 | + } |
| 86 | + } ?: "" |
| 87 | + resolvedCases.put(licenseExpression, LicenseCompatibility(type, reason)) |
| 88 | + } |
| 89 | + |
| 90 | + @JvmOverloads |
| 91 | + fun allow(license: License, action: Action<LicenseCompatibilityConfig>? = null) { |
| 92 | + allow(license.asExpression(), action) |
| 93 | + } |
| 94 | + |
| 95 | + @JvmOverloads |
| 96 | + fun allow( |
| 97 | + licenseExpression: LicenseExpression, |
| 98 | + action: Action<LicenseCompatibilityConfig>? = null |
| 99 | + ) { |
| 100 | + registerResolution(licenseExpression, CompatibilityResult.ALLOW, action) |
| 101 | + } |
| 102 | + |
| 103 | + @JvmOverloads |
| 104 | + fun reject(license: License, action: Action<LicenseCompatibilityConfig>? = null) { |
| 105 | + reject(license.asExpression(), action) |
| 106 | + } |
| 107 | + |
| 108 | + @JvmOverloads |
| 109 | + fun reject( |
| 110 | + licenseExpression: LicenseExpression, |
| 111 | + action: Action<LicenseCompatibilityConfig>? = null |
| 112 | + ) { |
| 113 | + registerResolution(licenseExpression, CompatibilityResult.REJECT, action) |
| 114 | + } |
| 115 | + |
| 116 | + @JvmOverloads |
| 117 | + fun unknown(license: License, action: Action<LicenseCompatibilityConfig>? = null) { |
| 118 | + unknown(license.asExpression(), action) |
| 119 | + } |
| 120 | + |
| 121 | + @JvmOverloads |
| 122 | + fun unknown( |
| 123 | + licenseExpression: LicenseExpression, |
| 124 | + action: Action<LicenseCompatibilityConfig>? = null |
| 125 | + ) { |
| 126 | + registerResolution(licenseExpression, CompatibilityResult.UNKNOWN, action) |
| 127 | + } |
| 128 | + |
| 129 | + @TaskAction |
| 130 | + fun run() { |
| 131 | + val dependencies = MetadataStore.load(metadata).dependencies |
| 132 | + |
| 133 | + val licenseNormalizer = GuessBasedNormalizer( |
| 134 | + logger, licenseSimilarityNormalizationThreshold.get().toDouble() |
| 135 | + ) |
| 136 | + val licenseCompatibilityInterpreter = LicenseCompatibilityInterpreter( |
| 137 | + // TODO: make it configurable |
| 138 | + LicenseEquivalence(), |
| 139 | + resolvedCases.get().mapKeys { |
| 140 | + licenseNormalizer.normalize(it.key) |
| 141 | + } |
| 142 | + ) |
| 143 | + |
| 144 | + val ok = StringBuilder() |
| 145 | + val ko = StringBuilder() |
| 146 | + |
| 147 | + dependencies |
| 148 | + .asSequence() |
| 149 | + .map { (component, licenseInfo) -> component to licenseInfo.license } |
| 150 | + .groupByTo(TreeMap()) { (component, license) -> |
| 151 | + licenseCompatibilityInterpreter.eval(license).also { |
| 152 | + logger.log( |
| 153 | + when (it.type) { |
| 154 | + CompatibilityResult.ALLOW -> LogLevel.DEBUG |
| 155 | + CompatibilityResult.UNKNOWN -> LogLevel.LIFECYCLE |
| 156 | + CompatibilityResult.REJECT -> LogLevel.LIFECYCLE |
| 157 | + }, |
| 158 | + "License compatibility for {}: {} -> {}", component, license, it |
| 159 | + ) |
| 160 | + } |
| 161 | + } |
| 162 | + .forEach { (licenseCompatibility, components) -> |
| 163 | + val header = |
| 164 | + "${licenseCompatibility.type}: ${licenseCompatibility.reasons.joinToString(", ")}" |
| 165 | + val sb = if (licenseCompatibility.type == CompatibilityResult.ALLOW) ok else ko |
| 166 | + if (sb.isNotEmpty()) { |
| 167 | + sb.append('\n') |
| 168 | + } |
| 169 | + sb.append(header).append('\n') |
| 170 | + sb.append("=".repeat(header.length)).append('\n') |
| 171 | + sb.appendComponents(components) |
| 172 | + } |
| 173 | + |
| 174 | + val errorMessage = ko.toString() |
| 175 | + val result = ko.apply { |
| 176 | + if (isNotEmpty() && ok.isNotEmpty()) { |
| 177 | + append('\n') |
| 178 | + } |
| 179 | + append(ok) |
| 180 | + while (endsWith('\n')) { |
| 181 | + setLength(length - 1) |
| 182 | + } |
| 183 | + }.toString() |
| 184 | + |
| 185 | + if (printResults.get()) { |
| 186 | + println(result) |
| 187 | + } |
| 188 | + |
| 189 | + outputFile.get().asFile.apply { |
| 190 | + parentFile.mkdirs() |
| 191 | + writeText(result) |
| 192 | + } |
| 193 | + |
| 194 | + if (errorMessage.isNotEmpty()) { |
| 195 | + if (failOnIncompatibleLicense.get()) { |
| 196 | + throw GradleException(errorMessage) |
| 197 | + } else { |
| 198 | + logger.warn(errorMessage) |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + private fun Appendable.appendComponents( |
| 204 | + components: List<Pair<ModuleComponentIdentifier, LicenseExpression?>> |
| 205 | + ) = |
| 206 | + components |
| 207 | + .groupByTo(TreeMap(nullsFirst(LicenseExpression.NATURAL_ORDER)), |
| 208 | + { it.second }, { it.first }) |
| 209 | + .forEach { (license, components) -> |
| 210 | + append('\n') |
| 211 | + append(license?.toString() ?: "Unknown license").append('\n') |
| 212 | + components.forEach { |
| 213 | + append("* ${it.displayName}\n") |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments