com.datadog.gradle.plugin.licenses.tasks.CheckDependencyLicensesTask.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dependency-license Show documentation
Show all versions of dependency-license Show documentation
This plugin generates the OSS licenses csv file for all dependencies.
The newest version!
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/
package com.datadog.gradle.plugin.licenses.tasks
import com.datadog.gradle.plugin.licenses.DependencyLicensesExtension
import com.datadog.gradle.plugin.licenses.internal.DependenciesLicenseProvider
import com.datadog.gradle.plugin.licenses.internal.License
import com.datadog.gradle.plugin.licenses.internal.ThirdPartyDependency
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.VerificationException
import java.io.File
/**
* Task verifying the target CSV file has up to date information about the dependency licenses
* found in all the modules of the project.
*/
open class CheckDependencyLicensesTask : DefaultTask() {
@get:Input
internal var extension: DependencyLicensesExtension =
DependencyLicensesExtension()
private val provider: DependenciesLicenseProvider =
DependenciesLicenseProvider()
init {
group = "datadog"
description = "Check all Third Party Licenses appear in the csv file"
}
// region Task
/**
* Verifies that the existing csv file matches the current dependencies of the project.
*/
@TaskAction
fun applyTask() {
val projectDependencies = provider.getThirdPartyDependencies(
project,
extension.transitiveDependencies,
extension.listDependencyOnce,
)
val listedDependencies = parseCsvFile()
checkMatchingDependencies(projectDependencies, listedDependencies, "missing")
if (extension.checkObsoleteDependencies) {
checkMatchingDependencies(listedDependencies, projectDependencies, "obsolete")
}
listedDependencies.filter { it.license is License.Empty }
.forEach {
logger.warn("License for ${it.origin} is empty")
}
listedDependencies.filter { it.license is License.Raw }
.forEach {
logger.warn("License for ${it.origin} is not valid : ${it.license}")
}
listedDependencies.filter { it.copyright == "__" }
.forEach {
logger.warn("Copyright for ${it.origin} is missing")
}
}
private fun checkMatchingDependencies(
trueDependencies: List,
testedDependencies: List,
check: String,
) {
var error = false
trueDependencies.forEach { dep ->
val known = testedDependencies.firstOrNull {
it.component == dep.component && it.origin == dep.origin
}
val knownInOtherComponent = testedDependencies.firstOrNull {
it.component != dep.component && it.origin == dep.origin
}
if (known == null && knownInOtherComponent == null) {
error = true
logger.error("- $check dependency in ${extension.csvFile.name} : ${dep.toCSV()}")
} else if (knownInOtherComponent != null) {
logger.info(
"- ${dep.origin} $check but exists in component ${knownInOtherComponent.component}",
)
}
}
if (error) {
throw VerificationException("Some dependencies are missing in ${extension.csvFile.name}")
}
}
/**
* @return the CSV input file to verify
*/
@InputFile
fun getCsvInputFile(): File {
return extension.csvFile
}
// endregion
// region Internal
@Suppress("DestructuringDeclarationWithTooManyEntries")
private fun parseCsvFile(): List {
val result = mutableListOf()
var firstLineRead = false
extension.csvFile.forEachLine {
if (firstLineRead) {
val dependency = ThirdPartyDependency.fromCSV(it)
if (dependency != null) {
result.add(dependency)
}
} else {
firstLineRead = true
}
}
return result
}
// endregion
}