@Option(name = "--output", aliases = {"-o"}, required = true, metaVar = "FILE", usage = "File to write .proto into")
private File file;
@Override
public int run() throws Exception {
LockFile lock = new LockFile(file.getAbsoluteFile(), FS.DETECTED);
if (!lock.lock()) {
throw die("Cannot lock " + file);
}
try {
JavaSchemaModel jsm = new JavaSchemaModel(ReviewDb.class);
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(lock.getOutputStream(), "UTF-8")));
try {
String header;
InputStream in = getClass().getResourceAsStream("ProtoGenHeader.txt");
try {
ByteBuffer buf = IO.readWholeStream(in, 1024);
int ptr = buf.arrayOffset() + buf.position();
int len = buf.remaining();
header = new String(buf.array(), ptr, len, "UTF-8");
} finally {
in.close();
}
String version = com.google.gerrit.common.Version.getVersion();
out.write(header.replace("@@VERSION@@", version));
jsm.generateProto(out);
out.flush();
} finally {
out.close();
}
if (!lock.commit()) {
throw die("Could not write to " + file);
}
} finally {
lock.unlock();
}
System.out.println("Created " + file.getPath());
return 0;
}