in src/scala/io/bazel/rules_scala/scrooge_support/Compiler.scala [74:158]
def run() {
// if --gen-file-map is specified, prepare the map file.
fileMapWriter = config.fileMapPath.map { path =>
val file = new File(path)
val dir = file.getParentFile
if (dir != null && !dir.exists()) {
dir.mkdirs()
}
if (config.verbose) {
println("+ Writing file mapping to %s".format(path))
}
new FileWriter(file)
}
val allJars: List[File] =
(includeJars ::: compileJars)
.map { path => (new File(path)).getCanonicalFile }
val isJava = config.language.equals("java")
val documentCache = new TrieMap[String, Document]
// compile
val allPaths = for {
jar <- compileJars.iterator
inputFullPath <- CompilerDefaults.listJar(new File(jar)).iterator
} yield inputFullPath
val rootImporter = FocusedZipImporter.forPaths(None, allJars)
// Here we only should only pass Thrift files to the compiler, other files
// (e.g. manifest files) are not accepted.
allPaths.filter(_.endsWith(".thrift")).foreach { inputFullPath =>
try {
val inputFile = Paths.get(inputFullPath).getFileName.toString
val focus = Option((new File(inputFullPath)).getParentFile)
// allow lookup either focused, or relative to the root of the repo
val importer = rootImporter.copy(focus = focus) +: rootImporter
val parser = new ThriftParser(
importer,
config.strict,
defaultOptional = isJava,
skipIncludes = false,
documentCache
)
parser.logger.setLevel(Level.OFF) // scrooge warns on file names with "/"
val doc = parser.parseFile(inputFile).mapNamespaces(config.namespaceMappings)
if (config.verbose) println("+ Compiling %s".format(inputFile))
val resolvedDoc = TypeResolver()(doc)
val generator = GeneratorFactory(
config.language,
resolvedDoc,
config.defaultNamespace,
experimentFlags)
generator match {
case g: ScalaGenerator => g.warnOnJavaNamespaceFallback = config.scalaWarnOnJavaNSFallback
case g: ApacheJavaGenerator => g.serEnumType = config.javaSerEnumType
case _ => ()
}
val generatedFiles = generator(
config.flags,
new File(config.destFolder),
config.dryRun,
config.genAdapt
).map {
_.getPath
}
if (config.verbose) {
println("+ Generated %s".format(generatedFiles.mkString(", ")))
}
fileMapWriter.foreach { w =>
generatedFiles.foreach { path =>
w.write(inputFullPath + " -> " + path + "\n")
}
}
} catch {
case e: Throwable => throw new FileParseException(inputFullPath, e)
}
}
// flush and close the map file
fileMapWriter.foreach { _.close() }
}