Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ val licenseGenerationConfig =
licenseGenerationConfig.projectLicenseFile.convention(
project.layout.projectDirectory.asFile.parentFile.resolve("LICENSE.txt")
)
licenseGenerationConfig.dependencyLicenseOverrides.convention(emptyMap())

var buildLicenseReportDirectory = project.layout.buildDirectory.dir("reports/dependency-license")
var buildLicenseOutputToCopyDir = buildLicenseReportDirectory.get().dir("licenses")
var resourceLicenseDir = project.layout.projectDirectory.dir("src/main/resources/licenses")
var resourceThirdPartyDir = resourceLicenseDir.dir("THIRD_PARTY_LICENSES")

licenseReport {
renderers = arrayOf<ReportRenderer>(AnalyzerLicensingPackagingRenderer(buildLicenseReportDirectory.get().asFile.toPath()))
renderers = arrayOf<ReportRenderer>(
AnalyzerLicensingPackagingRenderer(
buildLicenseReportDirectory.get().asFile.toPath(),
licenseGenerationConfig.dependencyLicenseOverrides
)
)
excludeGroups = arrayOf(project.group.toString(), project.group.toString().replace("com.sonarsource", "org.sonarsource"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import java.nio.file.Path
import java.nio.file.StandardCopyOption
import java.nio.file.StandardOpenOption
import java.util.ArrayList
import org.gradle.api.provider.Provider

private const val APACHE_LICENSE_FILE_NAME: String = "Apache-2.0.txt"
private const val MIT_FILE_NAME: String = "MIT.txt"
Expand All @@ -53,6 +54,7 @@ val LICENSE_TITLE_TO_RESOURCE_FILE: Map<String, String> = buildMap {

class AnalyzerLicensingPackagingRenderer(
private val buildOutputDir: Path,
private val dependencyLicenseOverrides: Provider<Map<String, java.io.File>>,
) : ReportRenderer {
private lateinit var generatedLicenseResourcesDirectory: Path
private val dependenciesWithUnusableLicenseFileInside: Set<String> = setOf(
Expand Down Expand Up @@ -94,6 +96,11 @@ class AnalyzerLicensingPackagingRenderer(
*/
@Throws(IOException::class, URISyntaxException::class)
private fun generateDependencyFile(data: ModuleData) {
val copyOverrideLicenseFile = copyOverriddenLicense(data)
if (copyOverrideLicenseFile.success) {
return
}

val copyIncludedLicenseFile = copyIncludedLicenseFromDependency(data)
if (copyIncludedLicenseFile.success) {
return
Expand All @@ -104,10 +111,19 @@ class AnalyzerLicensingPackagingRenderer(
return
}

exceptions.add("${data.group}.${data.name}: ${copyOverrideLicenseFile.message}")
exceptions.add("${data.group}.${data.name}: ${copyIncludedLicenseFile.message}")
exceptions.add("${data.group}.${data.name}: ${copyFromResources.message}")
}

@Throws(IOException::class)
private fun copyOverriddenLicense(data: ModuleData): Status {
val dependencyKey = "${data.group}:${data.name}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the : as separator, most probably we should adapt values in dependenciesWithUnusableLicenseFileInside and logic in copyIncludedLicenseFromDependency()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in ba3de29

val overrideFile = dependencyLicenseOverrides.getOrElse(emptyMap())[dependencyKey]
?: return Status.failure("No override configured.")
return copyLicenseFile(data, overrideFile.toPath())
}

@Throws(IOException::class)
private fun copyIncludedLicenseFromDependency(data: ModuleData): Status {
if (dependenciesWithUnusableLicenseFileInside.contains("${data.group}.${data.name}")) {
Expand Down Expand Up @@ -161,8 +177,16 @@ class AnalyzerLicensingPackagingRenderer(
): Status {
val licenseResourceFileName = LICENSE_TITLE_TO_RESOURCE_FILE[licenseName]
?: return Status.failure("License file '$licenseName' could not be found.")
val resourceAsStream = AnalyzerLicensingPackagingRenderer::class.java.getResourceAsStream("/licenses/$licenseResourceFileName")
?: throw IOException("Resource not found for license: $licenseName")
return copyLicenseResourceByFileName(data, licenseResourceFileName)
}

@Throws(IOException::class)
private fun copyLicenseResourceByFileName(
data: ModuleData,
resourceFileName: String,
): Status {
val resourceAsStream = AnalyzerLicensingPackagingRenderer::class.java.getResourceAsStream("/licenses/$resourceFileName")
?: throw IOException("Resource not found for license: $resourceFileName")
Files.copy(resourceAsStream, generateLicensePath(data), StandardCopyOption.REPLACE_EXISTING)
return Status.success
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@
package org.sonarsource.cloudnative.gradle

import java.io.File
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property

interface LicenseGenerationConfig {
/** The project's own license file (defaults to LICENSE.txt one level above the project directory). */
val projectLicenseFile: Property<File>

/**
* Per-dependency override of the license file to copy.
* Keys use the "group:name" format and values are files provided by the consuming project.
*/
val dependencyLicenseOverrides: MapProperty<String, File>
}
Loading