1 // © 2019 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 package org.unicode.icu.tool.cldrtoicu.ant; 4 5 import main.java.org.unicode.icu.tool.cldrtoicu.CodeGenerator; 6 import main.java.org.unicode.icu.tool.cldrtoicu.generator.ResourceFallbackCodeGenerator; 7 import org.apache.tools.ant.BuildException; 8 import org.apache.tools.ant.Task; 9 10 import java.io.*; 11 import java.nio.file.Files; 12 import java.nio.file.Path; 13 import java.nio.file.Paths; 14 15 import static com.google.common.base.Preconditions.checkNotNull; 16 17 // Note: Auto-magical Ant methods are listed as "unused" by IDEs, unless the warning is suppressed. 18 public final class GenerateCodeTask extends Task { 19 private Path cldrPath; 20 private Path cOutDir; 21 private Path javaOutDir; 22 private String action; 23 24 private class GeneratedFileDef { 25 String cRelativePath; 26 String javaRelativePath; 27 CodeGenerator generator; 28 GeneratedFileDef(String cRelativePath, String javaRelativePath, CodeGenerator generator)29 public GeneratedFileDef(String cRelativePath, String javaRelativePath, CodeGenerator generator) { 30 this.cRelativePath = cRelativePath; 31 this.javaRelativePath = javaRelativePath; 32 this.generator = generator; 33 } 34 } 35 36 private GeneratedFileDef[] generatedFileDefs = { 37 new GeneratedFileDef("common/localefallback_data.h", "src/com/ibm/icu/impl/LocaleFallbackData.java", new ResourceFallbackCodeGenerator()), 38 }; 39 40 @SuppressWarnings("unused") setCldrDir(String path)41 public void setCldrDir(String path) { 42 // Use String here since on some systems Ant doesn't support automatically converting Path instances. 43 this.cldrPath = checkNotNull(Paths.get(path)); 44 } 45 46 @SuppressWarnings("unused") setCOutDir(String path)47 public void setCOutDir(String path) { 48 // Use String here since on some systems Ant doesn't support automatically converting Path instances. 49 this.cOutDir = Paths.get(path); 50 } 51 52 @SuppressWarnings("unused") setJavaOutDir(String path)53 public void setJavaOutDir(String path) { 54 // Use String here since on some systems Ant doesn't support automatically converting Path instances. 55 this.javaOutDir = Paths.get(path); 56 } 57 58 @SuppressWarnings("unused") setAction(String action)59 public void setAction(String action) { 60 // Use String here since on some systems Ant doesn't support automatically converting Path instances. 61 this.action = action; 62 } 63 64 @SuppressWarnings("unused") execute()65 public void execute() throws BuildException { 66 for (GeneratedFileDef task : generatedFileDefs) { 67 Path cOutPath = cOutDir.resolve(task.cRelativePath); 68 Path javaOutPath = javaOutDir.resolve(task.javaRelativePath); 69 70 try { 71 if (this.action != null && this.action.equals("clean")) { 72 log("Deleting " + cOutPath + " and " + javaOutPath + "..."); 73 Files.deleteIfExists(cOutPath); 74 Files.deleteIfExists(javaOutPath); 75 } else { 76 Files.createDirectories(cOutPath.getParent()); 77 Files.createDirectories(javaOutPath.getParent()); 78 79 try (PrintWriter cOut = new PrintWriter(new OutputStreamWriter(new FileOutputStream(cOutPath.toFile()))); 80 PrintWriter javaOut = new PrintWriter(new OutputStreamWriter(new FileOutputStream(javaOutPath.toFile())))) { 81 82 log("Generating " + cOutPath + " and " + javaOutPath + "..."); 83 task.generator.generateCode(cldrPath, cOut, javaOut); 84 } 85 } 86 } catch (IOException ioException) { 87 throw new BuildException("IOException: " + ioException.toString()); 88 } 89 } 90 } 91 92 } 93