Search in sources :

Example 1 with ProtoFile

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

the class JavaGenerator method get.

public static JavaGenerator get(Schema schema) {
    Map<ProtoType, ClassName> nameToJavaName = new LinkedHashMap<>();
    nameToJavaName.putAll(BUILT_IN_TYPES_MAP);
    for (ProtoFile protoFile : schema.protoFiles()) {
        String javaPackage = javaPackage(protoFile);
        putAll(nameToJavaName, javaPackage, null, protoFile.types());
        for (Service service : protoFile.services()) {
            ClassName className = ClassName.get(javaPackage, service.type().simpleName());
            nameToJavaName.put(service.type(), className);
        }
    }
    return new JavaGenerator(schema, nameToJavaName, new Profile(), false, false);
}
Also used : ProtoType(com.squareup.wire.schema.ProtoType) ClassName(com.squareup.javapoet.ClassName) ProtoFile(com.squareup.wire.schema.ProtoFile) Service(com.squareup.wire.schema.Service) ByteString(okio.ByteString) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ProtoFile

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

the class ProfileLoader method load.

public Profile load() throws IOException {
    Set<Location> protoLocations = new LinkedHashSet<>();
    for (ProtoFile protoFile : schema.protoFiles()) {
        protoLocations.add(protoFile.location());
    }
    Multimap<Path, String> pathsToAttempt = pathsToAttempt(protoLocations);
    ImmutableList<ProfileFileElement> profileFiles = loadProfileFiles(pathsToAttempt);
    Profile profile = new Profile(profileFiles);
    validate(schema, profileFiles);
    return profile;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Path(java.nio.file.Path) ProfileFileElement(com.squareup.wire.java.internal.ProfileFileElement) ProtoFile(com.squareup.wire.schema.ProtoFile) Location(com.squareup.wire.schema.Location)

Example 3 with ProtoFile

use of com.squareup.wire.schema.ProtoFile 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 4 with ProtoFile

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

the class WireCompiler method compile.

void compile() throws IOException {
    SchemaLoader schemaLoader = new SchemaLoader();
    for (String protoPath : protoPaths) {
        schemaLoader.addSource(fs.getPath(protoPath));
    }
    for (String sourceFileName : sourceFileNames) {
        schemaLoader.addProto(sourceFileName);
    }
    Schema schema = schemaLoader.load();
    String profileName = emitAndroid ? "android" : "java";
    Profile profile = new ProfileLoader(profileName).schema(schema).load();
    if (!identifierSet.isEmpty()) {
        log.info("Analyzing dependencies of root types.");
        schema = schema.prune(identifierSet);
        for (String rule : identifierSet.unusedIncludes()) {
            log.info("Unused include: " + rule);
        }
        for (String rule : identifierSet.unusedExcludes()) {
            log.info("Unused exclude: " + rule);
        }
    }
    JavaGenerator javaGenerator = JavaGenerator.get(schema).withProfile(profile).withAndroid(emitAndroid).withCompact(emitCompact);
    ConcurrentLinkedQueue<Type> types = new ConcurrentLinkedQueue<>();
    for (ProtoFile protoFile : schema.protoFiles()) {
        // Check if we're skipping files not explicitly named.
        if (!sourceFileNames.isEmpty() && !sourceFileNames.contains(protoFile.location().path())) {
            if (namedFilesOnly || protoFile.location().path().equals(DESCRIPTOR_PROTO))
                continue;
        }
        types.addAll(protoFile.types());
    }
    ExecutorService executor = Executors.newCachedThreadPool();
    List<Future<Void>> futures = new ArrayList<>(MAX_WRITE_CONCURRENCY);
    for (int i = 0; i < MAX_WRITE_CONCURRENCY; ++i) {
        futures.add(i, executor.submit(new JavaFileWriter(javaGenerator, types)));
    }
    executor.shutdown();
    try {
        for (Future<Void> future : futures) {
            future.get();
        }
    } catch (ExecutionException e) {
        throw new IOException(e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : SchemaLoader(com.squareup.wire.schema.SchemaLoader) ProfileLoader(com.squareup.wire.java.ProfileLoader) Schema(com.squareup.wire.schema.Schema) ProtoFile(com.squareup.wire.schema.ProtoFile) ArrayList(java.util.ArrayList) JavaGenerator(com.squareup.wire.java.JavaGenerator) IOException(java.io.IOException) Profile(com.squareup.wire.java.Profile) Type(com.squareup.wire.schema.Type) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with ProtoFile

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

the class CodegenSample method execute.

public void execute() throws IOException {
    Schema schema = loadSchema();
    if (!identifierSet.isEmpty()) {
        schema = retainRoots(schema);
    }
    JavaGenerator javaGenerator = JavaGenerator.get(schema);
    ServiceGenerator serviceGenerator = new ServiceGenerator(javaGenerator);
    for (ProtoFile protoFile : schema.protoFiles()) {
        for (Type type : protoFile.types()) {
            Stopwatch stopwatch = Stopwatch.createStarted();
            TypeSpec typeSpec = javaGenerator.generateType(type);
            ClassName javaTypeName = (ClassName) javaGenerator.typeName(type.type());
            writeJavaFile(javaTypeName, typeSpec, type.location(), stopwatch);
        }
        for (Service service : protoFile.services()) {
            Stopwatch stopwatch = Stopwatch.createStarted();
            ClassName javaTypeName = (ClassName) javaGenerator.typeName(service.type());
            TypeSpec typeSpec = serviceGenerator.api(service);
            writeJavaFile(javaTypeName, typeSpec, service.location(), stopwatch);
        }
    }
}
Also used : Type(com.squareup.wire.schema.Type) Schema(com.squareup.wire.schema.Schema) ProtoFile(com.squareup.wire.schema.ProtoFile) Stopwatch(com.google.common.base.Stopwatch) ClassName(com.squareup.javapoet.ClassName) Service(com.squareup.wire.schema.Service) JavaGenerator(com.squareup.wire.java.JavaGenerator) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

ProtoFile (com.squareup.wire.schema.ProtoFile)5 ClassName (com.squareup.javapoet.ClassName)3 JavaGenerator (com.squareup.wire.java.JavaGenerator)3 Schema (com.squareup.wire.schema.Schema)3 Type (com.squareup.wire.schema.Type)3 Stopwatch (com.google.common.base.Stopwatch)2 TypeSpec (com.squareup.javapoet.TypeSpec)2 Profile (com.squareup.wire.java.Profile)2 Service (com.squareup.wire.schema.Service)2 IOException (java.io.IOException)2 ProfileLoader (com.squareup.wire.java.ProfileLoader)1 ProfileFileElement (com.squareup.wire.java.internal.ProfileFileElement)1 IdentifierSet (com.squareup.wire.schema.IdentifierSet)1 Location (com.squareup.wire.schema.Location)1 ProtoType (com.squareup.wire.schema.ProtoType)1 SchemaLoader (com.squareup.wire.schema.SchemaLoader)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1