1 package org.unicode.cldr.tool; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.nio.file.Path; 7 import java.util.Set; 8 import org.unicode.cldr.tool.Option.Options; 9 import org.unicode.cldr.util.CLDRPaths; 10 import org.unicode.cldr.util.CLDRTool; 11 12 @CLDRTool( 13 alias = "checkout-archive", 14 description = "Checkout CLDR archive to $ARCHIVE (usually ../cldr-archive)", 15 url = "https://cldr.unicode.org/development/creating-the-archive") 16 public class CheckoutArchive { 17 enum MyOptions { 18 prune("Perform a 'git prune' first"), 19 echo("Only show commands, don't run them. (Dry run)"), 20 ; 21 22 // BOILERPLATE TO COPY 23 final Option option; 24 MyOptions(String argumentPattern, String defaultArgument, String helpText)25 private MyOptions(String argumentPattern, String defaultArgument, String helpText) { 26 option = new Option(this, argumentPattern, defaultArgument, helpText); 27 } 28 MyOptions(String helpText)29 private MyOptions(String helpText) { 30 option = new Option(this, helpText); 31 } 32 33 static Options myOptions = new Options(); 34 35 static { 36 for (MyOptions option : MyOptions.values()) { myOptions.add(option, option.option)37 myOptions.add(option, option.option); 38 } 39 } 40 parse(String[] args, boolean showArguments)41 private static Set<String> parse(String[] args, boolean showArguments) { 42 return myOptions.parse(MyOptions.values()[0], args, true); 43 } 44 } 45 main(String args[])46 public static void main(String args[]) throws IOException, InterruptedException { 47 MyOptions.parse(args, true); 48 49 Path archiveDir = new File(CLDRPaths.ARCHIVE_DIRECTORY).toPath(); 50 if (!archiveDir.toFile().isDirectory()) { 51 throw new FileNotFoundException( 52 "Archive directory " 53 + archiveDir.toString() 54 + " does not exist, please create it or change the value of -DARCHIVE="); 55 } 56 System.out.println("Setting up in $ARCHIVE " + archiveDir.toString() + " …"); 57 int skip = 0; 58 int created = 0; 59 int err = 0; 60 61 if (MyOptions.prune.option.doesOccur()) { 62 final String cmd[] = { 63 "git", "worktree", "prune", 64 }; 65 if (runCommand(cmd)) { 66 err++; 67 } 68 } 69 70 for (final String ver : ToolConstants.CLDR_VERSIONS) { 71 final Path dirName = archiveDir.resolve("cldr-" + ver); 72 if (dirName.toFile().isDirectory()) { 73 skip++; 74 System.out.println("# Skipping existing \t" + dirName.toString()); 75 } else { 76 final String tag = "release-" + ver.replaceAll("\\.", "-").replaceAll("-0$", ""); 77 final String cmd[] = {"git", "worktree", "add", dirName.toString(), tag}; 78 if (runCommand(cmd)) { 79 err++; 80 } 81 } 82 } 83 System.out.println(String.format("Created %d and skipped %d version(s)", created, skip)); 84 if (err != 0) { 85 throw new RuntimeException("Total errors: " + err); 86 } 87 } 88 89 /** 90 * Run a command 91 * 92 * @param cmd 93 * @return true on err 94 * @throws InterruptedException 95 * @throws IOException 96 */ runCommand(final String[] cmd)97 private static boolean runCommand(final String[] cmd) throws InterruptedException, IOException { 98 System.out.println("# " + String.join(" ", cmd)); 99 if (!MyOptions.echo.option.doesOccur()) { 100 int ev = 101 new ProcessBuilder(cmd) 102 .directory(new File(CLDRPaths.BASE_DIRECTORY)) 103 .inheritIO() 104 .start() 105 .waitFor(); 106 if (ev != 0) { 107 System.err.println("Error: exit value " + ev); 108 return true; 109 } 110 } 111 return false; 112 } 113 } 114