use of net.morimekta.providence.generator.Generator in project providence by morimekta.
the class BaseGenerateSourcesMojo method executeInternal.
boolean executeInternal(IncludeExcludeFileSelector includeDirs, File outputDir, IncludeExcludeFileSelector inputSelector, String defaultInputIncludes, boolean testCompile) throws MojoExecutionException, MojoFailureException {
Set<File> inputFiles = ProvidenceInput.getInputFiles(project, inputSelector, defaultInputIncludes, print_debug, getLog());
if (inputFiles.isEmpty()) {
return false;
}
if (!outputDir.exists()) {
if (!outputDir.mkdirs()) {
throw new MojoExecutionException("Unable to create target directory " + outputDir);
}
}
TreeSet<File> includes = new TreeSet<>();
File workingDir = new File(buildDir, testCompile ? "providence-test" : "providence");
File[] deleteFiles = workingDir.listFiles();
if (!workingDir.exists()) {
if (!workingDir.mkdirs()) {
throw new MojoExecutionException("Unable to create working directory " + workingDir);
}
} else if (deleteFiles != null) {
StreamSupport.<File>stream(Spliterators.spliterator(deleteFiles, Spliterator.DISTINCT | Spliterator.IMMUTABLE), false).forEach(File::delete);
}
Set<Artifact> resolvedArtifacts = new HashSet<>();
for (Dependency dep : dependencies) {
if (testCompile || !TEST.equalsIgnoreCase(dep.getScope())) {
resolveDependency(dep, includes, workingDir, resolvedArtifacts);
}
}
if (includeDirs != null) {
DirectoryScanner includeScanner = new DirectoryScanner();
includeScanner.setIncludes(includeDirs.getIncludes());
if (includeDirs.getExcludes() != null) {
includeScanner.setExcludes(includeDirs.getExcludes());
}
includeScanner.setBasedir(project.getBasedir());
includeScanner.scan();
for (String dir : includeScanner.getIncludedDirectories()) {
includes.add(new File(project.getBasedir(), dir));
}
for (String dir : includeScanner.getExcludedDirectories()) {
includes.remove(new File(project.getBasedir(), dir));
}
}
FileManager fileManager = new FileManager(outputDir);
ProgramParser parser = new ThriftProgramParser(require_field_id, require_enum_value, allow_language_reserved_names);
TypeLoader loader = new TypeLoader(includes, parser);
if (print_debug) {
inputFiles.stream().filter(Objects::nonNull).map(file -> {
try {
return file.getAbsoluteFile().getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}).sorted().forEach(f -> getLog().info("Compiling: " + f));
}
JavaOptions javaOptions = new JavaOptions();
javaOptions.android = android;
javaOptions.jackson = jackson;
javaOptions.rw_binary = rw_binary;
javaOptions.hazelcast_portable = hazelcast_portable;
javaOptions.generated_annotation_version = generated_annotation_version;
javaOptions.public_constructors = public_constructors;
GeneratorOptions generatorOptions = new GeneratorOptions();
generatorOptions.generator_program_name = "providence-maven-plugin";
generatorOptions.program_version = getVersionString();
GeneratorFactory factory = new JavaGeneratorFactory();
Generator generator = new JavaGenerator(fileManager, generatorOptions, javaOptions);
Path base = project.getBasedir().toPath().toAbsolutePath();
if (project.getParent() != null && project.getParent().getBasedir() != null) {
// Only replace with parent if parent is a parent directory of this.
Path parentBase = project.getParent().getBasedir().toPath().toAbsolutePath();
if (base.toString().startsWith(parentBase.toString())) {
base = parentBase;
}
}
for (File in : inputFiles) {
ProgramTypeRegistry registry;
try {
registry = loader.load(in);
} catch (SerializerException e) {
// ParseException is a SerializerException. And serialize exceptions can come from
// failing to make sense of constant definitions.
getLog().error(" ============ >> PROVIDENCE << ============");
getLog().error("");
Arrays.stream(e.asString().split("\r?\n")).forEach(l -> getLog().error(l));
getLog().error("");
getLog().error(" ============ << PROVIDENCE >> ============");
throw new MojoFailureException("Failed to parse thrift file: " + in.getName(), e);
} catch (IOException e) {
throw new MojoExecutionException("Failed to read thrift file: " + in.getName(), e);
}
try {
if (skipIfMissingNamespace && registry.getProgram().getNamespaceForLanguage(factory.generatorName()) == null) {
getLog().warn("Skipping (no " + factory.generatorName() + " namespace) " + base.relativize(in.toPath()));
continue;
}
generator.generate(registry);
} catch (GeneratorException e) {
throw new MojoFailureException("Failed to generate program: " + registry.getProgram().getProgramName(), e);
} catch (IOException e) {
throw new MojoExecutionException("Failed to write program file: " + registry.getProgram().getProgramName(), e);
}
}
try {
generator.generateGlobal(loader.getProgramRegistry(), inputFiles);
} catch (GeneratorException e) {
throw new MojoFailureException("Failed to generate global", e);
} catch (IOException e) {
throw new MojoExecutionException("Failed to write global file", e);
}
return compileOutput;
}
use of net.morimekta.providence.generator.Generator in project providence by morimekta.
the class FactoryLoaderTest method testLoader.
@Test
public void testLoader() {
GeneratorOptions options = new GeneratorOptions();
options.generator_program_name = "loader-test";
options.program_version = "1.0";
ResourceUtils.copyResourceTo("/test.jar", tmp.getRoot());
List<GeneratorFactory> factories = loader.getFactories(tmp.getRoot());
assertThat(factories, hasSize(1));
GeneratorFactory factory = factories.get(0);
assertThat(factory.generatorName(), is("js"));
assertThat(factory.generatorDescription(), is("Generates JavaScript (es5.1 or es6)."));
// Make sure we can instantiate the generator.
Generator generator = factory.createGenerator(new FakeFileManager(tmp.getRoot()), options, ImmutableList.of());
assertThat(generator, is(notNullValue()));
}
use of net.morimekta.providence.generator.Generator in project providence by morimekta.
the class JavaGeneratorTest method testHazelcast.
@Test
public void testHazelcast() throws IOException {
File src = tmp.newFolder("hz");
for (String res : ImmutableList.of("/hazelcast/hazelcast.thrift")) {
CProgram program = typeLoader.load(copyResourceTo(res, src)).getProgram();
programRegistry.putProgram(res, program);
programs.add(program);
}
JavaOptions options = new JavaOptions();
options.hazelcast_portable = true;
for (CProgram program : programs) {
Generator generator = new JavaGenerator(fileManager, generatorOptions, options);
generator.generate(programRegistry.registryForPath(program.getProgramFilePath()));
}
}
use of net.morimekta.providence.generator.Generator in project providence by morimekta.
the class JavaGeneratorTest method testGenerate_defaults.
@Test
public void testGenerate_defaults() throws GeneratorException, IOException {
defaultSources();
JavaOptions options = new JavaOptions();
for (CProgram program : programs) {
Generator generator = new JavaGenerator(fileManager, generatorOptions, options);
generator.generate(programRegistry.registryForPath(program.getProgramFilePath()));
}
}
use of net.morimekta.providence.generator.Generator in project providence by morimekta.
the class GeneratorOptions method getArgumentParser.
public ArgumentParser getArgumentParser(String prog, String description) throws IOException {
ArgumentOptions opts = ArgumentOptions.defaults(tty).withMaxUsageWidth(120);
ArgumentParser parser = new ArgumentParser(prog, Utils.getVersionString(), description, opts);
parser.add(new Option("--gen", "g", "generator", "Generate files for this language spec.", new GeneratorSpecParser(this::getFactories).andApply(this::setGenerator)));
parser.add(new HelpOption("--help", "h?", "Show this help or about language.", this::getFactories, this::setHelp));
parser.add(new Flag("--verbose", "V", "Show verbose output and error messages.", this::setVerbose));
parser.add(new Flag("--version", "v", "Show program version.", this::setVersion));
parser.add(new Option("--rc", null, "FILE", "Providence RC to use", file(this::setRc), "~" + File.separator + ".pvdrc"));
parser.add(new Option("--include", "I", "dir", "Allow includes of files in directory", dir(this::addInclude), null, true, false, false));
parser.add(new Option("--out", "o", "dir", "Output directory", outputDir(this::setOut), "${PWD}"));
parser.add(new Flag("--require-field-id", null, "Require all fields to have a defined ID", this::setRequireFieldId));
parser.add(new Flag("--require-enum-value", null, "Require all enum values to have a defined ID", this::setRequireEnumValue));
parser.add(new Flag("--allow-language-reserved-names", null, "Allow language-reserved words in type names", this::setAllowLanguageReservedNames, true));
parser.add(new Flag("--no-language-reserved-names", null, "Do not allow language-reserved words in type names", b -> this.setAllowLanguageReservedNames(!b)));
parser.add(new Flag("--skip-if-missing-namespace", "N", "Skip generation for files without requested namespace", this::setSkipIfMissingNamespace));
parser.add(new Option("--add-generator", null, "FILE", "Add extra generator .jar file", file(extraGenerators::add)));
parser.add(new Argument("file", "Files to compile.", file(this::addFile), null, null, true, true, false));
return parser;
}
Aggregations