Last active
July 20, 2022 13:42
-
-
Save tomonacci/4491f8216addbaadf4a114d808c5c8c0 to your computer and use it in GitHub Desktop.
Using Twitter Scrooge with Mill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalalib._ | |
import mill._ | |
import mill.define.Sources | |
import mill.api.PathRef | |
import $ivy.`com.twitter::scrooge-generator:19.11.0` | |
object example extends Cross[ExampleModule]("2.12.8") | |
class ExampleModule(val crossScalaVersion: String) extends CrossSbtModule { | |
// Must match the version from scrooge-generator imported above | |
val scroogeVersion = "19.11.0" | |
// The structure loosely follows that of https://github.com/lihaoyi/mill/blob/master/contrib/scalapblib/src/ScalaPBModule.scala albeit with a lot of inlining | |
override def generatedSources = T { super.generatedSources() :+ compileScrooge() } | |
def scroogeSources: Sources = T.sources { | |
millSourcePath / 'src / 'main / 'thrift | |
} | |
def compileScrooge: T[PathRef] = T { | |
val dest = T.ctx().dest | |
def compileDir(inputDir: os.Path) { | |
// ls throws if the path doesn't exist | |
if (inputDir.toIO.exists) { | |
val thriftPaths = os.walk(inputDir).filter(_.last.matches(".*\\.thrift")).map(_.toIO.getCanonicalPath) | |
com.twitter.scrooge.Main.main(Array("-d", dest.toIO.getCanonicalPath, "-l", "scala", "--finagle") ++ thriftPaths) | |
} | |
} | |
scroogeSources().map(_.path).foreach(compileDir) | |
mill.api.Result.Success(PathRef(dest)) | |
} | |
def ivyDeps = Agg( | |
ivy"com.twitter::scrooge-core:${scroogeVersion}", | |
ivy"com.twitter::scrooge-serializer:${scroogeVersion}", | |
ivy"com.twitter::finagle-thrift:${scroogeVersion}", | |
ivy"org.apache.thrift:libthrift:0.10.0", | |
) | |
} | |
// Sample directory structure: | |
// | |
// example/src/main/thrift/Example.thrift: | |
// | |
// namespace java com.example | |
// | |
// struct Example { | |
// 1: optional string example | |
// } | |
// | |
// example/src/main/scala/com/example/Main.scala: | |
// | |
// package com.example | |
// | |
// object Main { | |
// def main(args: Array[String]) { | |
// println(com.twitter.scrooge.BinaryThriftStructSerializer(Example).toString(Example(Some("mill-scrooge")))) | |
// } | |
// } | |
// | |
// Then running mill will yield | |
// | |
// % mill 'example[2.12.8].run' | |
// [41/41] example[2.12.8].run | |
// CwABAAAADG1pbGwtc2Nyb29nZQA= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment