Commit 4c467f9e authored by Cees de Groot's avatar Cees de Groot Committed by David Trott

Introduced "generator" configuration option to be able to manipulate the thrift --gen option

parent 5232afcf
......@@ -75,6 +75,15 @@ abstract class AbstractThriftMojo extends AbstractMojo {
*/
private String thriftExecutable;
/**
* This string is passed to the {@code --gen} option of the {@code thrift} parameter. By default
* it will generate Java output. The main reason for this option is to be able to add options
* to the Java generator - if you generate something else, you're on your own.
*
* @parameter default-value="java"
*/
private String generator;
/**
* @parameter
*/
......@@ -140,6 +149,7 @@ abstract class AbstractThriftMojo extends AbstractMojo {
cleanDirectory(outputDirectory);
Thrift thrift = new Thrift.Builder(thriftExecutable, outputDirectory)
.setGenerator(generator)
.addThriftPathElement(thriftSourceRoot)
.addThriftPathElements(derivedThriftPathElements)
.addThriftPathElements(asList(additionalThriftPathElements))
......@@ -171,6 +181,7 @@ abstract class AbstractThriftMojo extends AbstractMojo {
checkNotNull(project, "project");
checkNotNull(projectHelper, "projectHelper");
checkNotNull(thriftExecutable, "thriftExecutable");
checkNotNull(generator, "generator");
final File thriftSourceRoot = getThriftSourceRoot();
checkNotNull(thriftSourceRoot);
checkArgument(!thriftSourceRoot.isFile(), "thriftSourceRoot is a file, not a diretory");
......
......@@ -33,6 +33,7 @@ final class Thrift {
final static String GENERATED_JAVA = "gen-java";
private final String executable;
private final String generator;
private final ImmutableSet<File> thriftPathElements;
private final ImmutableSet<File> thriftFiles;
private final File javaOutputDirectory;
......@@ -43,14 +44,16 @@ final class Thrift {
* Constructs a new instance. This should only be used by the {@link Builder}.
*
* @param executable The path to the {@code thrift} executable.
* @param generator The value for the {@code --gen} option.
* @param thriftPath The directories in which to search for imports.
* @param thriftFiles The thrift source files to compile.
* @param javaOutputDirectory The directory into which the java source files
* will be generated.
*/
private Thrift(String executable, ImmutableSet<File> thriftPath,
private Thrift(String executable, String generator, ImmutableSet<File> thriftPath,
ImmutableSet<File> thriftFiles, File javaOutputDirectory) {
this.executable = checkNotNull(executable, "executable");
this.generator = checkNotNull(generator, "generator");
this.thriftPathElements = checkNotNull(thriftPath, "thriftPath");
this.thriftFiles = checkNotNull(thriftFiles, "thriftFiles");
this.javaOutputDirectory = checkNotNull(javaOutputDirectory, "javaOutputDirectory");
......@@ -101,7 +104,7 @@ final class Thrift {
command.add("-o");
command.add(javaOutputDirectory.toString());
command.add("--gen");
command.add("java");
command.add(generator);
command.add(thriftFile.toString());
return ImmutableList.copyOf(command);
}
......@@ -151,6 +154,7 @@ final class Thrift {
private final File javaOutputDirectory;
private Set<File> thriftPathElements;
private Set<File> thriftFiles;
private String generator;
/**
* Constructs a new builder. The two parameters are present as they are
......@@ -191,6 +195,19 @@ final class Thrift {
return this;
}
/**
* Adds the option string for the Thrift executable's {@code --gen} parameter.
*
* @param generator
* @return The builder
* @throws NullPointerException If {@code generator} is {@code null}.
*/
public Builder setGenerator(String generator) {
checkNotNull(generator);
this.generator = generator;
return this;
}
private void checkThriftFileIsInThriftPath(File thriftFile) {
assert thriftFile.isFile();
checkState(checkThriftFileIsInThriftPathHelper(thriftFile.getParentFile()));
......@@ -250,7 +267,7 @@ final class Thrift {
*/
public Thrift build() {
checkState(!thriftFiles.isEmpty());
return new Thrift(executable, ImmutableSet.copyOf(thriftPathElements),
return new Thrift(executable, generator, ImmutableSet.copyOf(thriftPathElements),
ImmutableSet.copyOf(thriftFiles), javaOutputDirectory);
}
}
......
......@@ -36,7 +36,9 @@ public class TestThrift {
idlDir = new File(testResourceDir, "idl");
genJavaDir = new File(testRootDir, Thrift.GENERATED_JAVA);
builder = new Thrift.Builder("thrift", testRootDir);
builder.addThriftPathElement(idlDir);
builder
.setGenerator("java")
.addThriftPathElement(idlDir);
}
@Test
......@@ -59,6 +61,12 @@ public class TestThrift {
new File(testRootDir, "shared/SharedService.java").exists());
}
@Test
public void testThriftCompileWithGeneratorOption() throws Exception {
builder.setGenerator("java:private-members,hashcode");
testThriftCompile();
}
@Test
public void testThriftMultipleFileCompile() throws Exception {
final File sharedThrift = new File(idlDir, "shared.thrift");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment