Search in sources :

Example 1 with IdentifierSet

use of com.squareup.wire.schema.IdentifierSet in project wire by square.

the class WireGenerateSourcesMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    // Add the directory into which generated sources are placed as a compiled source root.
    project.addCompileSourceRoot(generatedSourceDirectory);
    try {
        List<String> directories = protoPaths != null && protoPaths.length > 0 ? Arrays.asList(protoPaths) : Collections.singletonList(protoSourceDirectory);
        List<String> protoFilesList = Arrays.asList(protoFiles);
        Schema schema = loadSchema(directories, protoFilesList);
        Profile profile = loadProfile(schema);
        IdentifierSet identifierSet = identifierSet();
        if (!identifierSet.isEmpty()) {
            schema = retainRoots(identifierSet, schema);
        }
        JavaGenerator javaGenerator = JavaGenerator.get(schema).withAndroid(emitAndroid).withCompact(emitCompact).withProfile(profile);
        for (ProtoFile protoFile : schema.protoFiles()) {
            if (!protoFilesList.isEmpty() && !protoFilesList.contains(protoFile.location().path())) {
                // Don't emit anything for files not explicitly compiled.
                continue;
            }
            for (Type type : protoFile.types()) {
                Stopwatch stopwatch = Stopwatch.createStarted();
                TypeSpec typeSpec = javaGenerator.generateType(type);
                ClassName javaTypeName = javaGenerator.generatedTypeName(type);
                writeJavaFile(javaTypeName, typeSpec, type.location().withPathOnly());
                getLog().info(String.format("Generated %s in %s", javaTypeName, stopwatch));
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Wire Plugin: Failure compiling proto sources.", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Schema(com.squareup.wire.schema.Schema) ProtoFile(com.squareup.wire.schema.ProtoFile) Stopwatch(com.google.common.base.Stopwatch) JavaGenerator(com.squareup.wire.java.JavaGenerator) IdentifierSet(com.squareup.wire.schema.IdentifierSet) Profile(com.squareup.wire.java.Profile) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Type(com.squareup.wire.schema.Type) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 2 with IdentifierSet

use of com.squareup.wire.schema.IdentifierSet in project wire by square.

the class CodegenSampleMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    project.addCompileSourceRoot(generatedSourceDirectory);
    ImmutableSet<String> protoPathsSet = ImmutableSet.copyOf(protoPaths);
    ImmutableSet<String> protoFilesSet = ImmutableSet.copyOf(protoFiles);
    IdentifierSet identifierSet = identifierSet();
    try {
        CodegenSample codeGenerator = new CodegenSample(this, protoPathsSet, protoFilesSet, generatedSourceDirectory, identifierSet);
        codeGenerator.execute();
    } catch (IOException e) {
        throw new MojoExecutionException("failed to generate sources", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) IdentifierSet(com.squareup.wire.schema.IdentifierSet)

Aggregations

IdentifierSet (com.squareup.wire.schema.IdentifierSet)2 IOException (java.io.IOException)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 Stopwatch (com.google.common.base.Stopwatch)1 ClassName (com.squareup.javapoet.ClassName)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 JavaGenerator (com.squareup.wire.java.JavaGenerator)1 Profile (com.squareup.wire.java.Profile)1 ProtoFile (com.squareup.wire.schema.ProtoFile)1 Schema (com.squareup.wire.schema.Schema)1 Type (com.squareup.wire.schema.Type)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1