|
| 1 | +package make; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +/** |
| 7 | + * Create the routes file to be used by Play engine and individual Scala classes that represent the specific |
| 8 | + * evolutions for different languages and approaches. |
| 9 | + */ |
| 10 | +public class Create { |
| 11 | + |
| 12 | + /** Where routes file is to be placed for play. */ |
| 13 | + static final String routes = "src" + File.separator + "main" + File.separator + "resources" + File.separator + "routes"; |
| 14 | + |
| 15 | + /** All synthesized scala build files are stored in this package. */ |
| 16 | + static final String destination = "src" + File.separator + "main" + File.separator + "scala" + File.separator + "build"; |
| 17 | + |
| 18 | + /** Known Java Variations to generate. */ |
| 19 | + static final Evolution[] standardEvolutions = { |
| 20 | + new Evolution ("M0"), |
| 21 | + new Evolution ("M1", "M0"), |
| 22 | + new Evolution ("M2", "M1"), |
| 23 | + new Evolution ("M3", "M2"), |
| 24 | + new Evolution ("M4", "M3"), |
| 25 | + new Evolution ("M5", "M4"), |
| 26 | + new Evolution ("M6", "M5"), |
| 27 | + new Evolution ("M7", "M6"), |
| 28 | + }; |
| 29 | + |
| 30 | + /** Known Java Variations to generate. */ |
| 31 | + static final Evolution[] gjEvolutions = { |
| 32 | + new Evolution ("M0"), |
| 33 | + new Evolution ("M1", "M0"), |
| 34 | + }; |
| 35 | + |
| 36 | + static Instance getJavaName = new Instance() { |
| 37 | + public String instance(String name) { |
| 38 | + if (name.charAt(0) == 'M') { |
| 39 | + return "e" + name.charAt(1); |
| 40 | + } |
| 41 | + |
| 42 | + return name.toLowerCase(); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + static Instance getCPPName = new Instance() { |
| 47 | + public String instance(String name) { |
| 48 | + if (name.charAt(0) == 'M') { |
| 49 | + return "cpp_e" + name.charAt(1); |
| 50 | + } |
| 51 | + |
| 52 | + // nothing else. |
| 53 | + return name.toLowerCase(); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + static Instance getHaskellName = new Instance() { |
| 58 | + public String instance(String name) { |
| 59 | + if (name.charAt(0) == 'M') { |
| 60 | + return "e" + name.charAt(1); |
| 61 | + } |
| 62 | + |
| 63 | + // nothing else. |
| 64 | + return name.toLowerCase(); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + static Instance getGJName = new Instance() { |
| 69 | + public String instance(String name) { |
| 70 | + if (name.charAt(0) == 'M') { |
| 71 | + return "e" + name.charAt(1); |
| 72 | + } |
| 73 | + |
| 74 | + return name.toLowerCase(); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + /** Known Java Variations to generate. */ |
| 79 | + static final Evolution[] independentEvolutions = { |
| 80 | + new Evolution("I1", "M1"), |
| 81 | + new Evolution("I2", "I1"), |
| 82 | + new Evolution("P1", "M2") |
| 83 | + }; |
| 84 | + |
| 85 | + // HACK. TODO: Has to be M3 first otherwise ordering in generated code doesn't match. |
| 86 | + static final Evolution[] mergedEvolutions = { |
| 87 | + new MergedEvolution("C1", "M3", "I2") |
| 88 | + }; |
| 89 | + |
| 90 | + static final Evolution[] shapeEvolutions = { |
| 91 | + new Evolution("S0"), |
| 92 | + new Evolution("S1", "S0") |
| 93 | + }; |
| 94 | + |
| 95 | + // each language has a number of possible variations. Names of languages are used to |
| 96 | + // construct package names, i.e., "ep.j" and "ep.haskell" |
| 97 | + static final Language lang_java = new Language("j") |
| 98 | + .addEvolutions(standardEvolutions) |
| 99 | + .addEvolutions(independentEvolutions) |
| 100 | + //.addEvolutions(shapeEvolutions) |
| 101 | + .addMapping(getJavaName) |
| 102 | + .addEvolutions(mergedEvolutions) |
| 103 | + .add("algebra", "WithDomain(MathDomain) with AlgebraGenerator with AlgebraTestGenerator") |
| 104 | + .add("extensibleVisitor", "WithDomain(MathDomain) with ExtensibleVisitorGenerator with ExtensibleVisitorTestGenerator") |
| 105 | + .add("interpreter", "WithDomain(MathDomain) with InterpreterGenerator with InterpreterTestGenerator") |
| 106 | + .add("oo", "WithDomain(MathDomain) with OOGenerator with JUnitTestGenerator") |
| 107 | + .add("trivially", "WithDomain(MathDomain) with TriviallyGenerator with JUnitTestGenerator") |
| 108 | + .add("visitor", "WithDomain(MathDomain) with VisitorGenerator with JUnitTestGenerator"); |
| 109 | + static final Language lang_haskell = new Language("haskell") |
| 110 | + .addMapping(getHaskellName) |
| 111 | + .addEvolutions(standardEvolutions) |
| 112 | + .add("alacarte", "WithDomain[MathDomain] with ALaCarteGenerator with ALaCarteTestGenerator") |
| 113 | + .add("grow", "WithDomain(MathDomain) with GrowGenerator with GrowTestGenerator") |
| 114 | + .add("straight", "WithDomain(MathDomain) with StraightGenerator with StraightTestGenerator"); |
| 115 | + static final Language lang_cpp = new Language("cpp") |
| 116 | + .addMapping(getCPPName) |
| 117 | + .addEvolutions(standardEvolutions) |
| 118 | + .add("oo", "WithDomain(MathDomain) with StraightGenerator with CPPOOTestGenerator") |
| 119 | + .add("visitor", "WithDomain(MathDomain) with CPPVisitorGenerator with CPPVisitorTestGenerator") |
| 120 | + .add("visitorTable", "WithDomain(MathDomain) with CPPVisitorTableGenerator with CPPTableTestGenerator"); |
| 121 | + static final Language lang_gj = new Language("gj") |
| 122 | + .addMapping(getGJName) |
| 123 | + .addEvolutions(gjEvolutions) |
| 124 | + .add("wadler", "WithDomain(MathDomain) with WadlerGenerator with UnitTestGenerator"); // not really anything good |
| 125 | + |
| 126 | + /** Could have used reflection, but this is simpler. */ |
| 127 | + static final Language[] allLanguages = { lang_java, lang_haskell, lang_cpp, lang_gj }; |
| 128 | + |
| 129 | + static String create(Language lang, Evolution ev, String packageStruct) { |
| 130 | + String name = ev.name; |
| 131 | + String scalaClass = "class " + name + "_Variation @Inject()(web: WebJarsUtil, app: ApplicationLifecycle) extends Foundation(web, app) {"; |
| 132 | + String evolutions = ""; |
| 133 | + |
| 134 | + // lower-case names for all evolutions. Must make sure no duplicates |
| 135 | + // LAST ONE selected is the name of the evolution to be selected. |
| 136 | + Iterator<String> past = ev.evolutions(); |
| 137 | + |
| 138 | + while (past.hasNext()) { |
| 139 | + String ps = past.next(); |
| 140 | + String trait = lang.mapping.instance(ps); |
| 141 | + if (evolutions.equals("")) { |
| 142 | + evolutions = trait; |
| 143 | + } else { |
| 144 | + evolutions = trait + " with " + evolutions; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + String override = "override val gen = new " + packageStruct.replace("[", "(").replace("]", ")") + " with " + evolutions; |
| 149 | + |
| 150 | + return scalaClass + "\n" + override + "\n}"; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Each Routes entry is of the following form: |
| 155 | + * |
| 156 | + * -> / ep.scala.oo.M0_Variation |
| 157 | + * |
| 158 | + * @param args |
| 159 | + */ |
| 160 | + public static void main(String[] args) throws Exception { |
| 161 | + File rf = new File (routes); |
| 162 | + PrintWriter routesFile = new PrintWriter(rf); |
| 163 | + |
| 164 | + File output = new File(destination); |
| 165 | + if (!output.exists()) { |
| 166 | + output.mkdir(); |
| 167 | + } |
| 168 | + |
| 169 | + // for each language |
| 170 | + for (Language lang : allLanguages) { |
| 171 | + File langDir = new File (output, lang.name); |
| 172 | + if (!langDir.exists()) { |
| 173 | + langDir.mkdir(); |
| 174 | + } |
| 175 | + |
| 176 | + // for each family |
| 177 | + for (String variation: lang) { |
| 178 | + String packageName = "build." + lang.name + "." + variation; |
| 179 | + String packageStruct = lang.constructors.get(variation); |
| 180 | + File varDir = new File (langDir, variation); |
| 181 | + if (!varDir.exists()) { |
| 182 | + varDir.mkdir(); |
| 183 | + } |
| 184 | + File build = new File (varDir, "build.scala"); |
| 185 | + System.out.println (" " + lang.name + "\t" + variation ); |
| 186 | + |
| 187 | + try { |
| 188 | + PrintWriter pw_output = new PrintWriter(build); |
| 189 | + |
| 190 | + pw_output.println("package " + packageName); |
| 191 | + pw_output.println("/* Generated: " + new Date() + " */"); |
| 192 | + pw_output.println("import ep.domain._"); |
| 193 | + pw_output.println("import ep." + lang.name + "._"); |
| 194 | + pw_output.println("import ep." + lang.name + "." + variation + "._"); |
| 195 | + pw_output.println("import javax.inject.Inject"); |
| 196 | + pw_output.println("import org.webjars.play.WebJarsUtil"); |
| 197 | + pw_output.println("import play.api.inject.ApplicationLifecycle"); |
| 198 | + |
| 199 | + //String traits = ""; |
| 200 | + routesFile.println ("# " + variation + "(" + lang.name + ") evolutions: "); |
| 201 | + for (Evolution ev : lang.evolutions) { |
| 202 | + System.out.print (ev.name + ", "); |
| 203 | + String clazzDefinition = create(lang, ev, packageStruct); |
| 204 | + |
| 205 | + pw_output.println("/* "); |
| 206 | + pw_output.println(" * " + variation + " solution in " + lang.name + " for " + ev.name); |
| 207 | + pw_output.println(" * "); |
| 208 | + pw_output.println(" * @group evolutions "); |
| 209 | + pw_output.println(" */"); |
| 210 | + |
| 211 | + pw_output.println(clazzDefinition); |
| 212 | + // output routes information |
| 213 | + routesFile.println ("->\t/\t\t " + packageName + "." + ev.name + "_Variation"); |
| 214 | + } |
| 215 | + System.out.println(); |
| 216 | + pw_output.close(); |
| 217 | + } catch (IOException ioe) { |
| 218 | + ioe.printStackTrace(); |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + routesFile.close(); |
| 224 | + System.out.println ("Generated Routes file: " + rf.getAbsoluteFile()); |
| 225 | + } |
| 226 | + |
| 227 | +} |
0 commit comments