Skip to content

Instantly share code, notes, and snippets.

@slabuz
Created May 8, 2023 08:44
Show Gist options
  • Save slabuz/b66432d9c71dd100d193617754c79911 to your computer and use it in GitHub Desktop.
Save slabuz/b66432d9c71dd100d193617754c79911 to your computer and use it in GitHub Desktop.
Prototype protobuf generator for scala-cli
//> using scala "2.13"
//> using dep "com.thesamet.scalapb::scalapbc:0.11.13"
import scalapb.ScalaPBC
class ProtobufGenerator {
def metadataJson: String =
s"""{
| "name" : "Protobuf generator",
| "version" : "1.0.0",
| "supportedExtensions" : ["proto"],
| "isReferentiallyTransparent" : true
|}""".stripMargin
def generate(inputLocation: String, outputLocation: String): Unit = {
ScalaPBC.main(Array(s"--scala_out=${outputLocation}", inputLocation))
}
}
object Main {
def main(args: Array[String]) = {
val generator = new ProtobufGenerator()
args.toList match {
case "generate" :: output :: inputs if inputs.nonEmpty =>
inputs.foreach { input =>
println(s"Running protobuf generation from [$input] to [$output]")
generator.generate(input, output)
}
case "generator-metadata" :: "protobuf" :: Nil =>
println("Protobuf generator metadata:")
println(generator.metadataJson)
case _ =>
println("Expected usages")
println("scala-cli . -- generate <output directory> <input file>*")
println("scala-cli . -- generator-metadata protobuf")
System.exit(1)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment