|
| 1 | +/* |
| 2 | + * Copyright (C) 2021-2025 SonarSource SA |
| 3 | + * All rights reserved |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + */ |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
| 16 | +/** |
| 17 | + * Validates that generated license files match committed license files. |
| 18 | + * Used during Maven verify phase to ensure license files are up-to-date. |
| 19 | + */ |
| 20 | +public final class LicenseValidator { |
| 21 | + |
| 22 | + private LicenseValidator() { |
| 23 | + // Utility class |
| 24 | + } |
| 25 | + |
| 26 | + public static void main(String[] args) { |
| 27 | + try { |
| 28 | + var arguments = parseArguments(args); |
| 29 | + var tempLicensesPath = Path.of(arguments.get("temp_licenses")); |
| 30 | + var committedLicensesPath = Path.of(arguments.get("committed_licenses")); |
| 31 | + |
| 32 | + validateDirectoriesExist(tempLicensesPath, committedLicensesPath); |
| 33 | + |
| 34 | + var result = compareDirectories(tempLicensesPath, committedLicensesPath); |
| 35 | + |
| 36 | + if (result.hasErrors()) { |
| 37 | + printFailureMessage(result); |
| 38 | + System.exit(1); |
| 39 | + } else { |
| 40 | + System.out.println("[SUCCESS] License validation passed - generated files match committed files"); |
| 41 | + System.exit(0); |
| 42 | + } |
| 43 | + } catch (IllegalArgumentException e) { |
| 44 | + System.err.println("Error: " + e.getMessage()); |
| 45 | + printUsage(); |
| 46 | + System.exit(1); |
| 47 | + } catch (IOException e) { |
| 48 | + System.err.println("Error: " + e.getMessage()); |
| 49 | + System.exit(1); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + static Map<String, String> parseArguments(String[] args) { |
| 54 | + var arguments = new java.util.HashMap<String, String>(); |
| 55 | + for (var arg : args) { |
| 56 | + if (arg.startsWith("--")) { |
| 57 | + var parts = arg.substring(2).split("=", 2); |
| 58 | + if (parts.length == 2) { |
| 59 | + arguments.put(parts[0], parts[1]); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (!arguments.containsKey("temp_licenses") || !arguments.containsKey("committed_licenses")) { |
| 65 | + throw new IllegalArgumentException("Missing required arguments"); |
| 66 | + } |
| 67 | + |
| 68 | + return arguments; |
| 69 | + } |
| 70 | + |
| 71 | + static void validateDirectoriesExist(Path tempLicenses, Path committedLicenses) throws IOException { |
| 72 | + if (!Files.exists(tempLicenses)) { |
| 73 | + throw new IOException( |
| 74 | + "Temporary licenses directory not found: " + tempLicenses + "\n" + |
| 75 | + "Please run: mvn clean package -PupdateLicenses" |
| 76 | + ); |
| 77 | + } |
| 78 | + if (!Files.exists(committedLicenses)) { |
| 79 | + throw new IOException( |
| 80 | + "Committed licenses directory not found: " + committedLicenses + "\n" + |
| 81 | + "Please run: mvn clean package -PupdateLicenses" |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + static ValidationResult compareDirectories(Path tempDir, Path committedDir) throws IOException { |
| 87 | + var tempFiles = buildFileMap(tempDir); |
| 88 | + var committedFiles = buildFileMap(committedDir); |
| 89 | + |
| 90 | + var newFiles = new ArrayList<String>(); |
| 91 | + var missingFiles = new ArrayList<String>(); |
| 92 | + var differentFiles = new ArrayList<String>(); |
| 93 | + |
| 94 | + // Find new files (in temp but not in committed) |
| 95 | + for (var relativePath : tempFiles.keySet()) { |
| 96 | + if (!committedFiles.containsKey(relativePath)) { |
| 97 | + newFiles.add(relativePath); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // Find missing files (in committed but not in temp) |
| 102 | + for (var relativePath : committedFiles.keySet()) { |
| 103 | + if (!tempFiles.containsKey(relativePath)) { |
| 104 | + missingFiles.add(relativePath); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Find files with different content |
| 109 | + for (var relativePath : tempFiles.keySet()) { |
| 110 | + if (committedFiles.containsKey(relativePath)) { |
| 111 | + var tempFile = tempFiles.get(relativePath); |
| 112 | + var committedFile = committedFiles.get(relativePath); |
| 113 | + if (Files.mismatch(tempFile, committedFile) != -1L) { |
| 114 | + differentFiles.add(relativePath); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + newFiles.sort(String::compareTo); |
| 120 | + missingFiles.sort(String::compareTo); |
| 121 | + differentFiles.sort(String::compareTo); |
| 122 | + |
| 123 | + return new ValidationResult(newFiles, missingFiles, differentFiles); |
| 124 | + } |
| 125 | + |
| 126 | + static Map<String, Path> buildFileMap(Path rootDir) throws IOException { |
| 127 | + try (Stream<Path> paths = Files.walk(rootDir)) { |
| 128 | + return paths |
| 129 | + .filter(Files::isRegularFile) |
| 130 | + .collect(Collectors.toMap( |
| 131 | + path -> rootDir.relativize(path).toString().replace('\\', '/'), |
| 132 | + path -> path |
| 133 | + )); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + static void printFailureMessage(ValidationResult result) { |
| 138 | + System.err.println("[FAILURE] License validation failed!"); |
| 139 | + System.err.println(); |
| 140 | + |
| 141 | + if (!result.newFiles().isEmpty()) { |
| 142 | + System.err.println("New files in generated licenses (not in committed):"); |
| 143 | + for (var file : result.newFiles()) { |
| 144 | + System.err.println(" + " + file); |
| 145 | + } |
| 146 | + System.err.println(); |
| 147 | + } |
| 148 | + |
| 149 | + if (!result.missingFiles().isEmpty()) { |
| 150 | + System.err.println("Missing files in generated licenses (present in committed):"); |
| 151 | + for (var file : result.missingFiles()) { |
| 152 | + System.err.println(" - " + file); |
| 153 | + } |
| 154 | + System.err.println(); |
| 155 | + } |
| 156 | + |
| 157 | + if (!result.differentFiles().isEmpty()) { |
| 158 | + System.err.println("Files with different content:"); |
| 159 | + for (var file : result.differentFiles()) { |
| 160 | + System.err.println(" ~ " + file); |
| 161 | + } |
| 162 | + System.err.println(); |
| 163 | + } |
| 164 | + |
| 165 | + System.err.println("To fix this, run: mvn clean package -PupdateLicenses"); |
| 166 | + } |
| 167 | + |
| 168 | + static void printUsage() { |
| 169 | + System.err.println("Usage: LicenseValidator --temp_licenses=<path> --committed_licenses=<path>"); |
| 170 | + } |
| 171 | + |
| 172 | + record ValidationResult( |
| 173 | + List<String> newFiles, |
| 174 | + List<String> missingFiles, |
| 175 | + List<String> differentFiles |
| 176 | + ) { |
| 177 | + boolean hasErrors() { |
| 178 | + return !newFiles.isEmpty() || !missingFiles.isEmpty() || !differentFiles.isEmpty(); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments