blob: 4512078b244536458b798b9a3717fe949107a6ff [file] [log] [blame]
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.pgm;
import com.google.gerrit.pgm.util.AbstractProgram;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gwtorm.schema.java.JavaSchemaModel;
import org.eclipse.jgit.internal.storage.file.LockFile;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.IO;
import org.kohsuke.args4j.Option;
import java.io.BufferedWriter;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
public class ProtoGen extends AbstractProgram {
@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;
}
}