From 070b20504ed7fa4bf3276db490a9e34d8b605885 Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Tue, 7 Aug 2018 08:51:30 -0700 Subject: [PATCH] Move ProtoGen out of gerrit.war Putting this in the WAR dated back to the Maven days, when adding new binary build targets was prohibitively expensive. Nobody using a distributed gerrit.war, as opposed to building locally, actually needs to run this binary. This binary has a very thin dependency stack and can build in just a few seconds, rather than having to build all of the server target, JS bundles, and other slow pieces of gerrit.war just to generate a proto file. Change-Id: If6e7d981bdd57b11536ffe990d11789afd232a46 --- java/com/google/gerrit/proto/BUILD | 13 ++++++ .../gerrit/{pgm => proto}/ProtoGen.java | 44 ++++++++++++------- resources/com/google/gerrit/proto/BUILD | 8 ++++ .../gerrit/{pgm => proto}/ProtoGenHeader.txt | 0 4 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 java/com/google/gerrit/proto/BUILD rename java/com/google/gerrit/{pgm => proto}/ProtoGen.java (67%) create mode 100644 resources/com/google/gerrit/proto/BUILD rename resources/com/google/gerrit/{pgm => proto}/ProtoGenHeader.txt (100%) diff --git a/java/com/google/gerrit/proto/BUILD b/java/com/google/gerrit/proto/BUILD new file mode 100644 index 0000000000..f53d54aea4 --- /dev/null +++ b/java/com/google/gerrit/proto/BUILD @@ -0,0 +1,13 @@ +java_binary( + name = "ProtoGen", + srcs = ["ProtoGen.java"], + resource_strip_prefix = "resources", + resources = ["//resources/com/google/gerrit/proto"], + deps = [ + "//java/com/google/gerrit/reviewdb:server", + "//lib:args4j", + "//lib:guava", + "//lib:gwtorm", + "//lib/jgit/org.eclipse.jgit:jgit", + ], +) diff --git a/java/com/google/gerrit/pgm/ProtoGen.java b/java/com/google/gerrit/proto/ProtoGen.java similarity index 67% rename from java/com/google/gerrit/pgm/ProtoGen.java rename to java/com/google/gerrit/proto/ProtoGen.java index a882412d2d..1c55a05299 100644 --- a/java/com/google/gerrit/pgm/ProtoGen.java +++ b/java/com/google/gerrit/proto/ProtoGen.java @@ -12,11 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -package com.google.gerrit.pgm; +package com.google.gerrit.proto; +import static com.google.common.base.Preconditions.checkState; import static java.nio.charset.StandardCharsets.UTF_8; -import com.google.gerrit.pgm.util.AbstractProgram; import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gwtorm.schema.java.JavaSchemaModel; import java.io.BufferedWriter; @@ -28,23 +28,37 @@ import java.io.PrintWriter; import java.nio.ByteBuffer; import org.eclipse.jgit.internal.storage.file.LockFile; import org.eclipse.jgit.util.IO; +import org.kohsuke.args4j.CmdLineException; +import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.Option; -public class ProtoGen extends AbstractProgram { +public class ProtoGen { @Option( - name = "--output", - aliases = {"-o"}, - required = true, - metaVar = "FILE", - usage = "File to write .proto into") + 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()); - if (!lock.lock()) { - throw die("Cannot lock " + file); + public static void main(String[] argv) throws Exception { + System.exit(new ProtoGen().run(argv)); + } + + private int run(String[] argv) throws Exception { + CmdLineParser parser = new CmdLineParser(this); + try { + parser.parseArgument(argv); + } catch (CmdLineException e) { + System.err.println(e.getMessage()); + System.err.println(getClass().getSimpleName() + " -o output.proto"); + parser.printUsage(System.err); + return 1; } + + LockFile lock = new LockFile(file.getAbsoluteFile()); + checkState(lock.lock(), "cannot lock %s", file); try { JavaSchemaModel jsm = new JavaSchemaModel(ReviewDb.class); try (OutputStream o = lock.getOutputStream(); @@ -61,9 +75,7 @@ public class ProtoGen extends AbstractProgram { jsm.generateProto(out); out.flush(); } - if (!lock.commit()) { - throw die("Could not write to " + file); - } + checkState(lock.commit(), "Could not write to %s", file); } finally { lock.unlock(); } diff --git a/resources/com/google/gerrit/proto/BUILD b/resources/com/google/gerrit/proto/BUILD new file mode 100644 index 0000000000..ec2e05cf2a --- /dev/null +++ b/resources/com/google/gerrit/proto/BUILD @@ -0,0 +1,8 @@ +filegroup( + name = "proto", + srcs = glob( + ["**/*"], + exclude = ["BUILD"], + ), + visibility = ["//visibility:public"], +) diff --git a/resources/com/google/gerrit/pgm/ProtoGenHeader.txt b/resources/com/google/gerrit/proto/ProtoGenHeader.txt similarity index 100% rename from resources/com/google/gerrit/pgm/ProtoGenHeader.txt rename to resources/com/google/gerrit/proto/ProtoGenHeader.txt