Skip to content

Instantly share code, notes, and snippets.

@jmini
Created December 27, 2023 08:29
Show Gist options
  • Save jmini/a0241af9a2f13a51532d1f1448ee38e6 to your computer and use it in GitHub Desktop.
Save jmini/a0241af9a2f13a51532d1f1448ee38e6 to your computer and use it in GitHub Desktop.
Jbang script to parse ".proto" files
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS com.squareup.wire:wire-schema-jvm:4.9.3
import java.nio.file.FileSystems;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import com.squareup.wire.schema.Location;
import com.squareup.wire.schema.ProtoFile;
import com.squareup.wire.schema.ProtoType;
import com.squareup.wire.schema.Schema;
import com.squareup.wire.schema.SchemaLoader;
public class ReadProto {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("jbang " + ReadProto.class.getSimpleName() + ".java <path of proto folder>");
System.exit(1);
}
SchemaLoader loader = new SchemaLoader(FileSystems.getDefault());
List<Location> sourcePath = Collections.singletonList(Location.get(args[0]));
List<Location> protoPath = Collections.emptyList();
loader.initRoots(sourcePath, protoPath);
Schema schema = loader.loadSchema();
System.out.println("== proto files:");
List<ProtoFile> protoFiles = schema.getProtoFiles();
protoFiles.stream()
.sorted(Comparator.comparing(protoFile -> protoFile.getLocation().getPath()))
.forEach(protoFile -> {
System.out.println(protoFile);
});
System.out.println("== types:");
Set<ProtoType> types = schema.getTypes();
types.stream()
.sorted(Comparator.comparing(type -> type.toString()))
.forEach(type -> {
System.out.println(type);
});
}
}
@jmini
Copy link
Author

jmini commented Dec 27, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment