Search in sources :

Example 1 with SchemaLoader

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

the class WireGenerateSourcesMojo method loadSchema.

private Schema loadSchema(List<String> directories, List<String> protos) throws IOException {
    Stopwatch stopwatch = Stopwatch.createStarted();
    SchemaLoader schemaLoader = new SchemaLoader();
    for (String directory : directories) {
        schemaLoader.addSource(new File(directory));
    }
    for (String proto : protos) {
        schemaLoader.addProto(proto);
    }
    Schema schema = schemaLoader.load();
    getLog().info(String.format("Loaded %s proto files in %s", schema.protoFiles().size(), stopwatch));
    return schema;
}
Also used : SchemaLoader(com.squareup.wire.schema.SchemaLoader) Schema(com.squareup.wire.schema.Schema) Stopwatch(com.google.common.base.Stopwatch) ProtoFile(com.squareup.wire.schema.ProtoFile) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile)

Example 2 with SchemaLoader

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

the class CodegenSample method loadSchema.

private Schema loadSchema() throws IOException {
    Stopwatch stopwatch = Stopwatch.createStarted();
    SchemaLoader loader = new SchemaLoader();
    for (String source : sources) {
        loader.addSource(new File(source));
    }
    for (String proto : protos) {
        loader.addProto(proto);
    }
    Schema schema = loader.load();
    log.info("Loaded %s proto files in %s", schema.protoFiles().size(), stopwatch);
    return schema;
}
Also used : SchemaLoader(com.squareup.wire.schema.SchemaLoader) Schema(com.squareup.wire.schema.Schema) Stopwatch(com.google.common.base.Stopwatch) ProtoFile(com.squareup.wire.schema.ProtoFile) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile)

Example 3 with SchemaLoader

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

the class ServiceGeneratorTest method schema.

private Schema schema(Map<String, String> fileToProto) throws IOException {
    SchemaLoader schemaLoader = new SchemaLoader();
    schemaLoader.addSource(temporaryFolder.getRoot());
    for (Map.Entry<String, String> entry : fileToProto.entrySet()) {
        File file = new File(temporaryFolder.getRoot(), entry.getKey());
        file.getParentFile().mkdirs();
        try (BufferedSink out = Okio.buffer(Okio.sink(file))) {
            out.writeUtf8(entry.getValue());
        }
        schemaLoader.addProto(entry.getKey());
    }
    return schemaLoader.load();
}
Also used : SchemaLoader(com.squareup.wire.schema.SchemaLoader) BufferedSink(okio.BufferedSink) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile)

Example 4 with SchemaLoader

use of com.squareup.wire.schema.SchemaLoader 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 SchemaLoader

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

the class ProfileLoaderTest method profileInZip.

@Test
public void profileInZip() throws IOException {
    FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
    Files.createDirectories(fileSystem.getPath("/source"));
    Path zip = fileSystem.getPath("/source/protos.zip");
    ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zip));
    writeFile(zipOutputStream, "a/b/message.proto", "" + "package a.b;\n" + "message Message {" + "}");
    writeFile(zipOutputStream, "a/b/android.wire", "" + "syntax = \"wire2\";\n" + "package a.b;\n" + "import \"a/b/message.proto\";\n" + "type a.b.Message {\n" + "  target java.lang.Object using com.example.Message#ADAPTER;\n" + "}");
    zipOutputStream.close();
    Schema schema = new SchemaLoader().addSource(zip).load();
    Profile profile = new ProfileLoader(fileSystem, "android").schema(schema).load();
    ProtoType message = ProtoType.get("a.b.Message");
    assertThat(profile.getTarget(message)).isEqualTo(ClassName.OBJECT);
    assertThat(profile.getAdapter(message)).isEqualTo(new AdapterConstant("com.example.Message#ADAPTER"));
}
Also used : Path(java.nio.file.Path) ProtoType(com.squareup.wire.schema.ProtoType) SchemaLoader(com.squareup.wire.schema.SchemaLoader) ZipOutputStream(java.util.zip.ZipOutputStream) FileSystem(java.nio.file.FileSystem) Schema(com.squareup.wire.schema.Schema) Test(org.junit.Test)

Aggregations

SchemaLoader (com.squareup.wire.schema.SchemaLoader)5 Schema (com.squareup.wire.schema.Schema)4 JavaFile (com.squareup.javapoet.JavaFile)3 ProtoFile (com.squareup.wire.schema.ProtoFile)3 File (java.io.File)3 Stopwatch (com.google.common.base.Stopwatch)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 JavaGenerator (com.squareup.wire.java.JavaGenerator)1 Profile (com.squareup.wire.java.Profile)1 ProfileLoader (com.squareup.wire.java.ProfileLoader)1 ProtoType (com.squareup.wire.schema.ProtoType)1 Type (com.squareup.wire.schema.Type)1 IOException (java.io.IOException)1 FileSystem (java.nio.file.FileSystem)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1