Skip to content

Commit 43df186

Browse files
author
joeretro
committed
Command line tool for generating Matchers
1 parent 31ff0da commit 43df186

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

src/generator/org/hamcrest/generator/config/XmlConfigurator.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.hamcrest.generator.config;
22

3+
import org.hamcrest.generator.HamcrestFactoryWriter;
4+
import org.hamcrest.generator.QuickReferenceWriter;
35
import org.hamcrest.generator.ReflectiveFactoryReader;
46
import org.hamcrest.generator.SugarConfiguration;
57
import org.hamcrest.generator.SugarGenerator;
@@ -11,6 +13,8 @@
1113
import javax.xml.parsers.ParserConfigurationException;
1214
import javax.xml.parsers.SAXParser;
1315
import javax.xml.parsers.SAXParserFactory;
16+
import java.io.File;
17+
import java.io.FileWriter;
1418
import java.io.IOException;
1519

1620
public class XmlConfigurator {
@@ -47,8 +51,54 @@ private void addClass(String className) throws ClassNotFoundException {
4751
sugarConfiguration.addFactoryMethods(new ReflectiveFactoryReader(cls));
4852
}
4953

54+
5055
public static void main(String[] args) throws Exception {
51-
XmlConfigurator config = new XmlConfigurator(new SugarGenerator(), XmlConfigurator.class.getClassLoader());
52-
config.load(new InputSource("matchers.xml"));
56+
57+
if (args.length != 3) {
58+
System.err.println("Args: config-file generated-class output-dir");
59+
System.err.println("");
60+
System.err.println(" config-file : Path to config file listing matchers to generate sugar for.");
61+
System.err.println(" e.g. path/to/matchers.xml");
62+
System.err.println("");
63+
System.err.println("generated-class : Full name of class to generate.");
64+
System.err.println(" e.g. org.myproject.MyMatchers");
65+
System.err.println("");
66+
System.err.println(" output-dir : Where to output generated code (package subdirs will be");
67+
System.err.println(" automatically created).");
68+
System.err.println(" e.g. build/generated-code");
69+
System.exit(-1);
70+
}
71+
72+
String configFile = args[0];
73+
String fullClassName = args[1];
74+
File outputDir = new File(args[2]);
75+
76+
String fileName = fullClassName.replaceAll("\\.", File.separator) + ".java";
77+
int dotIndex = fullClassName.lastIndexOf(".");
78+
String packageName = dotIndex == -1 ? "" : fullClassName.substring(0, dotIndex);
79+
String shortClassName = fullClassName.substring(dotIndex + 1);
80+
81+
if (!outputDir.isDirectory()) {
82+
System.err.println("Output directory not found : " + outputDir.getAbsolutePath());
83+
System.exit(-1);
84+
}
85+
86+
File outputFile = new File(outputDir, fileName);
87+
outputFile.getParentFile().mkdirs();
88+
89+
SugarGenerator sugarGenerator = new SugarGenerator();
90+
try {
91+
sugarGenerator.addWriter(new HamcrestFactoryWriter(
92+
packageName, shortClassName, new FileWriter(outputFile)));
93+
sugarGenerator.addWriter(new QuickReferenceWriter(System.out));
94+
95+
XmlConfigurator xmlConfigurator = new XmlConfigurator(sugarGenerator, XmlConfigurator.class.getClassLoader());
96+
xmlConfigurator.load(new InputSource(configFile));
97+
98+
System.out.println("Generating " + fullClassName);
99+
sugarGenerator.generate();
100+
} finally {
101+
sugarGenerator.close();
102+
}
53103
}
54104
}

0 commit comments

Comments
 (0)