|
| 1 | +/// usr/bin/env jbang "$0" "$@" ; exit $? |
| 2 | +//JAVA 17+ |
| 3 | +//DEPS info.picocli:picocli:4.7.7 |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.net.URI; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.nio.file.FileSystem; |
| 10 | +import java.nio.file.FileSystemNotFoundException; |
| 11 | +import java.nio.file.FileSystems; |
| 12 | +import java.nio.file.FileVisitResult; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.nio.file.SimpleFileVisitor; |
| 16 | +import java.nio.file.StandardOpenOption; |
| 17 | +import java.nio.file.attribute.BasicFileAttributes; |
| 18 | +import java.security.DigestInputStream; |
| 19 | +import java.security.MessageDigest; |
| 20 | +import java.security.NoSuchAlgorithmException; |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.concurrent.Callable; |
| 24 | + |
| 25 | +import picocli.AutoComplete; |
| 26 | +import picocli.CommandLine; |
| 27 | +import picocli.CommandLine.Command; |
| 28 | +import picocli.CommandLine.Option; |
| 29 | +import picocli.CommandLine.Parameters; |
| 30 | + |
| 31 | +/** |
| 32 | + * This is used to hack around the missing md5 and sha1 checksum files required by Maven Central. Specifically for the |
| 33 | + * POM's build by the wildfly-bom-builder-maven-plugin. |
| 34 | + * |
| 35 | + * @author <a href="mailto:jperkins@ibm.com">James R. Perkins</a> |
| 36 | + */ |
| 37 | +@Command(name = "checksum-updater", mixinStandardHelpOptions = true, version = "checksum-updater 0.1", |
| 38 | + description = "Creates missing checksums for POM files.", |
| 39 | + showDefaultValues = true, subcommands = AutoComplete.GenerateCompletion.class) |
| 40 | +public class ChecksumUpdater implements Callable<Integer> { |
| 41 | + |
| 42 | + private static final char[] table = { |
| 43 | + '0', '1', '2', '3', '4', '5', '6', '7', |
| 44 | + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
| 45 | + }; |
| 46 | + |
| 47 | + @Option(names = {"-a", "--algorithm"}, description = "The algorithm(s) for the generated checksum", split = ",", defaultValue = "md5,sha1") |
| 48 | + private String[] algorithms; |
| 49 | + |
| 50 | + @Parameters(arity = "1..*", description = "The directories which contain the file to create the checksums for if they are missing") |
| 51 | + private Path[] dirs; |
| 52 | + |
| 53 | + @CommandLine.Spec |
| 54 | + private CommandLine.Model.CommandSpec spec; |
| 55 | + |
| 56 | + public static void main(String... args) { |
| 57 | + final int exitCode = new CommandLine(new ChecksumUpdater()).execute(args); |
| 58 | + System.exit(exitCode); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Integer call() throws Exception { |
| 63 | + for (Path dir : dirs) { |
| 64 | + if (!Files.isDirectory(dir)) { |
| 65 | + spec.commandLine().getErr().printf("%s must be a directory%n", dir); |
| 66 | + return -1; |
| 67 | + } |
| 68 | + // Walk the ZIP tree looking for missing checksum files and add them |
| 69 | + Files.walkFileTree(dir, new SimpleFileVisitor<>() { |
| 70 | + @Override |
| 71 | + public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { |
| 72 | + if (file.toString().endsWith(".pom")) { |
| 73 | + for (String algorithm : algorithms) { |
| 74 | + final Path checksumFile = Path.of(file.toAbsolutePath() + "." + algorithm.toLowerCase(Locale.ROOT)); |
| 75 | + if (Files.notExists(checksumFile)) { |
| 76 | + try { |
| 77 | + final var md = MessageDigest.getInstance(algorithm.toUpperCase(Locale.ROOT)); |
| 78 | + try (DigestInputStream dis = new DigestInputStream(Files.newInputStream(file), md)) { |
| 79 | + final byte[] buffer = new byte[1024]; |
| 80 | + while (dis.read(buffer) > 0) { |
| 81 | + // Just gathering |
| 82 | + } |
| 83 | + final byte[] checksum = md.digest(); |
| 84 | + final String checksumString = bytesToHexString(checksum); |
| 85 | + if (Files.exists(checksumFile)) { |
| 86 | + Files.delete(checksumFile); |
| 87 | + } |
| 88 | + Files.writeString(checksumFile, checksumString, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW); |
| 89 | + } |
| 90 | + } catch (NoSuchAlgorithmException e) { |
| 91 | + spec.commandLine() |
| 92 | + .getErr() |
| 93 | + .printf("Algorithm %s not found: %s%n", algorithm, e.getMessage()); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + return FileVisitResult.CONTINUE; |
| 99 | + } |
| 100 | + }); |
| 101 | + } |
| 102 | + return 0; |
| 103 | + } |
| 104 | + |
| 105 | + private static String bytesToHexString(final byte[] bytes) { |
| 106 | + final StringBuilder builder = new StringBuilder(bytes.length * 2); |
| 107 | + for (byte b : bytes) { |
| 108 | + builder.append(table[b >> 4 & 0x0f]).append(table[b & 0x0f]); |
| 109 | + } |
| 110 | + return builder.toString(); |
| 111 | + } |
| 112 | +} |
0 commit comments