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; }
public Schema load() throws IOException { if (sources.isEmpty()) { throw new IllegalStateException("No sources added."); } try (Closer closer = Closer.create()) { // Map the physical path to the file system root. For regular directories the key and the // value are equal. For ZIP files the key is the path to the .zip, and the value is the root // of the file system within it. Map<Path, Path> directories = new LinkedHashMap<>(); for (Path source : sources) { if (Files.isRegularFile(source)) { FileSystem sourceFs = FileSystems.newFileSystem(source, getClass().getClassLoader()); closer.register(sourceFs); directories.put(source, getOnlyElement(sourceFs.getRootDirectories())); } else { directories.put(source, source); } } return loadFromDirectories(directories); } }
new SchemaLoader().addSource(ROOT).load(); System.out.println("All files linked successfully.");
/** Add directory or zip file source from which proto files will be loaded. */ public SchemaLoader addSource(File file) { return addSource(file.toPath()); }
loaded.put(DESCRIPTOR_PROTO, loadDescriptorProto()); Source source = source(entry.getValue(), proto); if (source == null) { continue;
public Schema schema() { try { return schemaLoader.load(); } catch (IOException e) { throw new RuntimeException(e); } }
public RepoBuilder add(String name, String protoFile) { if (name.endsWith(".proto")) { schemaLoader.addProto(name); } else if (!name.endsWith(".wire")) { throw new IllegalArgumentException("unexpected file extension: " + name); } Path relativePath = fs.getPath(name); try { Path resolvedPath = root.resolve(relativePath); Path parent = resolvedPath.getParent(); if (parent != null) { Files.createDirectories(parent); } Files.write(resolvedPath, protoFile.getBytes(UTF_8)); } catch (IOException e) { throw new AssertionError(e); } return this; }
@Test public void earlierSourcesTakePrecedenceOverLaterSources() throws IOException { Files.createDirectories(fileSystem.getPath("/source1")); Files.createDirectories(fileSystem.getPath("/source2")); writeFile("/source1/message.proto", "" + "message Message {\n" + " optional string a = 1;\n" + "}"); writeFile("/source2/message.proto", "" + "message Message {\n" + " optional string b = 2;\n" + "}"); Schema schema = new SchemaLoader() .addSource(fileSystem.getPath("/source1")) .addSource(fileSystem.getPath("/source2")) .load(); MessageType message = (MessageType) schema.getType("Message"); assertThat(message.field("a")).isNotNull(); }
/** Add directory or zip file source from which proto files will be loaded. */ public SchemaLoader addSource(File file) { return addSource(file.toPath()); }
loaded.put(DESCRIPTOR_PROTO, loadDescriptorProto()); Source source = source(proto, entry.getValue()); if (source == null) { continue;
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; }
@Test public void loadAllFilesWhenNoneSpecified() throws IOException { Files.createDirectories(fileSystem.getPath("/source")); writeFile("/source/message1.proto", "message Message1 {}"); writeFile("/source/message2.proto", "message Message2 {}"); writeFile("/source/readme.txt", "Here be protos!"); Schema schema = new SchemaLoader() .addSource(fileSystem.getPath("/source")) .load(); Type message1 = schema.getType("Message1"); assertThat(message1).isNotNull(); assertThat(message1.location().base()).isEqualTo("/source"); assertThat(message1.location().path()).isEqualTo("message1.proto"); Type message2 = schema.getType("Message2"); assertThat(message2).isNotNull(); assertThat(message2.location().base()).isEqualTo("/source"); assertThat(message2.location().path()).isEqualTo("message2.proto"); }
/** Add directory or zip file source from which proto files will be loaded. */ public SchemaLoader addSource(File file) { return addSource(file.toPath()); }
loaded.put(DESCRIPTOR_PROTO, loadDescriptorProto()); Source source = source(entry.getValue(), proto); if (source == null) { continue;
public Schema load() throws IOException { if (sources.isEmpty()) { throw new IllegalStateException("No sources added."); } try (Closer closer = Closer.create()) { // Map the physical path to the file system root. For regular directories the key and the // value are equal. For ZIP files the key is the path to the .zip, and the value is the root // of the file system within it. Map<Path, Path> directories = new LinkedHashMap<>(); for (Path source : sources) { if (Files.isRegularFile(source)) { FileSystem sourceFs = FileSystems.newFileSystem(source, getClass().getClassLoader()); closer.register(sourceFs); directories.put(source, getOnlyElement(sourceFs.getRootDirectories())); } else { directories.put(source, source); } } return loadFromDirectories(directories); } }
@Test public void locateInMultiplePaths() throws IOException { Files.createDirectories(fileSystem.getPath("/source1")); Files.createDirectories(fileSystem.getPath("/source2")); Files.createFile(fileSystem.getPath("/source1/file1.proto")); Files.createFile(fileSystem.getPath("/source2/file2.proto")); new SchemaLoader() .addSource(fileSystem.getPath("/source1")) .addSource(fileSystem.getPath("/source2")) .addProto("file1.proto") .addProto("file2.proto") .load(); }
@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")); }
public Schema load() throws IOException { if (sources.isEmpty()) { throw new IllegalStateException("No sources added."); } try (Closer closer = Closer.create()) { // Map the physical path to the file system root. For regular directories the key and the // value are equal. For ZIP files the key is the path to the .zip, and the value is the root // of the file system within it. Map<Path, Path> directories = new LinkedHashMap<>(); for (Path source : sources) { if (Files.isRegularFile(source)) { FileSystem sourceFs = FileSystems.newFileSystem(source, getClass().getClassLoader()); closer.register(sourceFs); for (Path root : sourceFs.getRootDirectories()) { directories.put(source, root); } } else { directories.put(source, source); } } return loadFromDirectories(directories); } }
@Test public void failLocate() throws IOException { Files.createDirectories(fileSystem.getPath("/source1")); Files.createDirectories(fileSystem.getPath("/source2")); Files.createFile(fileSystem.getPath("/source2/file2.proto")); SchemaLoader loader = new SchemaLoader() .addSource(fileSystem.getPath("/source1")) .addProto("file2.proto"); try { loader.load(); fail(); } catch (FileNotFoundException expected) { } }
@Test public void failLocateInZipFile() throws IOException { Files.createDirectories(fileSystem.getPath("/source")); Path zip = fileSystem.getPath("/source/protos.zip"); ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zip)); zipOutputStream.putNextEntry(new ZipEntry("a/b/trix.proto")); zipOutputStream.write("message Trix {}".getBytes(UTF_8)); zipOutputStream.close(); try { new SchemaLoader() .addSource(zip) .addProto("a/b/message.proto") .load(); fail(); } catch (FileNotFoundException expected) { } }