|
| 1 | +/* |
| 2 | + * SonarSource Cloud Native Gradle Modules |
| 3 | + * Copyright (C) 2024-2025 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +import java.io.File |
| 18 | +import java.nio.file.Files |
| 19 | +import java.nio.file.StandardCopyOption |
| 20 | +import org.gradle.api.GradleException |
| 21 | +import org.gradle.api.logging.Logger |
| 22 | +import org.gradle.kotlin.dsl.create |
| 23 | +import org.gradle.kotlin.dsl.findByType |
| 24 | +import org.sonarsource.cloudnative.gradle.GoLicenseGenerationConfig |
| 25 | +import org.sonarsource.cloudnative.gradle.areDirectoriesEqual |
| 26 | +import org.sonarsource.cloudnative.gradle.copyDirectory |
| 27 | + |
| 28 | +/** |
| 29 | + * This plugin is used for generating license files for third-party GO runtime-dependencies into the resources folder. |
| 30 | + * It provides a validation task to ensure that the license files in the resource folder are up-to-date. |
| 31 | + * It provides a task to regenerate the license files into the resources folder. |
| 32 | + * These tasks expect the licenses to be generated by go-licenses by another task first, which is configurable. |
| 33 | + */ |
| 34 | + |
| 35 | +val goLicenseGenerationConfig = |
| 36 | + extensions.findByType<GoLicenseGenerationConfig>() ?: extensions.create<GoLicenseGenerationConfig>("goLicenseGenerationConfig") |
| 37 | +goLicenseGenerationConfig.buildGoLicenseFilesDir.convention(project.layout.buildDirectory.dir("go-licenses").get().asFile) |
| 38 | +goLicenseGenerationConfig.packagedBinaries.convention(emptySet()) |
| 39 | +goLicenseGenerationConfig.binaryLicenseFile.convention(project.rootDir.resolve("LICENSE.txt")) |
| 40 | + |
| 41 | +var resourceLicenseDir = project.layout.projectDirectory.dir("src/main/resources/licenses") |
| 42 | + |
| 43 | +val validateGoLicenses = tasks.register("validateGoLicenseFiles") { |
| 44 | + description = "Validate that generated go license files match the committed ones" |
| 45 | + group = "validation" |
| 46 | + dependsOn(generateGoLicenses) |
| 47 | + |
| 48 | + doLast { |
| 49 | + val generatedLicenses = goLicenseGenerationConfig.buildGoLicenseFilesDir.get().resolve("resources") |
| 50 | + goLicenseGenerationConfig.packagedBinaries.get().forEach { |
| 51 | + val licenseResourcesForBinary = resourceLicenseDir.dir("$it-licenses").asFile |
| 52 | + if (!areDirectoriesEqual(generatedLicenses, licenseResourcesForBinary, logger)) { |
| 53 | + val message = """ |
| 54 | + [FAILURE] License file validation failed! |
| 55 | + Generated license files differ from committed files at $resourceLicenseDir. |
| 56 | + To update the committed license files, run './gradlew generateLicenseResources' and commit the changes. |
| 57 | +
|
| 58 | + Note: This will completely regenerate all license files under $resourceLicenseDir and remove any stale ones. |
| 59 | + """ |
| 60 | + throw GradleException(message) |
| 61 | + } else { |
| 62 | + logger.lifecycle("Go license file validation succeeded: Generated go license files match the committed ones.") |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +tasks.named("validateLicenseFiles") { |
| 69 | + dependsOn(validateGoLicenses) |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * This tasks requires that the go-license generation is already done |
| 74 | + * during 'goLicenseGenerationConfig.generatingGoLicensesGradleTask.' task. |
| 75 | + */ |
| 76 | +val generateGoLicenses = tasks.register("generateGoLicenses") { |
| 77 | + // Generating the licenses with go-license tool is done during 'dockerCompileGo' task |
| 78 | + dependsOn(goLicenseGenerationConfig.generatingGoLicensesGradleTask.get()) |
| 79 | + doLast { |
| 80 | + transformGoLicenseOutputToDesiredLicenseStructure(goLicenseGenerationConfig.buildGoLicenseFilesDir.get(), logger) |
| 81 | + |
| 82 | + // copy included license |
| 83 | + val newIncludedLicensePath = goLicenseGenerationConfig.buildGoLicenseFilesDir.get().resolve("resources/LICENSE.txt").toPath() |
| 84 | + Files.copy(goLicenseGenerationConfig.binaryLicenseFile.get().toPath(), newIncludedLicensePath, StandardCopyOption.REPLACE_EXISTING) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +val generateGoLicenseResources = tasks.register("generateGoLicenseResources") { |
| 89 | + description = "Copies generated license files to the resources directory" |
| 90 | + dependsOn(generateGoLicenses) |
| 91 | + |
| 92 | + doLast { |
| 93 | + val generatedLicenses = goLicenseGenerationConfig.buildGoLicenseFilesDir.get().resolve("resources") |
| 94 | + goLicenseGenerationConfig.packagedBinaries.get().forEach { |
| 95 | + val licenseResourcesForBinary = resourceLicenseDir.dir("$it-licenses").asFile |
| 96 | + |
| 97 | + Files.createDirectories(licenseResourcesForBinary.toPath()) |
| 98 | + copyDirectory(generatedLicenses, licenseResourcesForBinary, logger) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +tasks.named("generateLicenseResources") { |
| 104 | + dependsOn(generateGoLicenseResources) |
| 105 | +} |
| 106 | + |
| 107 | +fun transformGoLicenseOutputToDesiredLicenseStructure( |
| 108 | + licenseFolder: File, |
| 109 | + logger: Logger, |
| 110 | +) { |
| 111 | + val generatedDirectory = licenseFolder.resolve("generated").toPath() |
| 112 | + val transformedDirectory = licenseFolder.resolve("resources/THIRD_PARTY_LICENSES").toPath() |
| 113 | + |
| 114 | + // 1. Validation: Ensure input exists |
| 115 | + if (!Files.isDirectory(generatedDirectory)) { |
| 116 | + throw GradleException("Error: The directory '$generatedDirectory' does not exist.") |
| 117 | + } |
| 118 | + |
| 119 | + // Create output directory if it doesn't exist |
| 120 | + if (!Files.exists(transformedDirectory)) { |
| 121 | + Files.createDirectories(transformedDirectory) |
| 122 | + } |
| 123 | + |
| 124 | + logger.lifecycle("Processing files from: $generatedDirectory") |
| 125 | + |
| 126 | + try { |
| 127 | + // 2. Walk the tree and collect all regular files |
| 128 | + val allGeneratedFiles = Files.walk(generatedDirectory) |
| 129 | + .filter { Files.isRegularFile(it) } |
| 130 | + .toList() |
| 131 | + // 3. Group files by their parent directory |
| 132 | + val filesByDirectory = allGeneratedFiles.groupBy { it.parent } |
| 133 | + |
| 134 | + // 4. Iterate through each directory group |
| 135 | + filesByDirectory.forEach { (parentDir, filesInDir) -> |
| 136 | + |
| 137 | + // Find "LICENSE", otherwise take the first existing file |
| 138 | + val selectedFile = filesInDir.find { it.toFile().name.startsWith("LICENSE") } |
| 139 | + ?: filesInDir.first() |
| 140 | + |
| 141 | + // 5. Create the new filename |
| 142 | + |
| 143 | + // Get path relative to "licenses" (e.g., "my/directory/structure/LICENSE") |
| 144 | + val relativePath = generatedDirectory.relativize(parentDir) |
| 145 | + val newFileName = relativePath.toString().replace(File.separator, ".") + "-LICENSE.txt" |
| 146 | + |
| 147 | + // 6. Copy to output |
| 148 | + val destinationPath = transformedDirectory.resolve(newFileName) |
| 149 | + |
| 150 | + Files.copy(selectedFile, destinationPath, StandardCopyOption.REPLACE_EXISTING) |
| 151 | + } |
| 152 | + } catch (e: Exception) { |
| 153 | + logger.warn("An error occurred: ${e.message}", e) |
| 154 | + throw e |
| 155 | + } |
| 156 | +} |
0 commit comments