def run()

in scrooge-generator/src/main/scala/com/twitter/scrooge/Compiler.scala [45:123]


  def run(): Unit = {
    // 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 importer = {
      val rootImporter =
        if (config.addRootDirImporter) Importer(new File("."))
        else NullImporter
      rootImporter +: Importer(config.includePaths.toSeq)
    }

    val isJava = config.language.equals("java")
    val documentCache = new TrieMap[String, Document]

    // compile
    for (inputFile <- config.thriftFiles) {
      try {
        val parser = new ThriftParser(
          importer,
          config.strict,
          defaultOptional = isJava,
          skipIncludes = false,
          documentCache
        )
        val doc = parser.parseFile(inputFile).mapNamespaces(config.namespaceMappings)

        if (config.verbose) {
          val currentDateTime: LocalDateTime = LocalDateTime.now()
          println("+ %s Compiling %s".format(currentDateTime.format(formatter), inputFile))
        }
        val resolvedDoc = TypeResolver()(doc, Some(inputFile))
        val generator =
          GeneratorFactory(
            config.language,
            resolvedDoc,
            config.defaultNamespace,
            config.languageFlags)

        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) {
          val currentDateTime: LocalDateTime = LocalDateTime.now()
          println(
            "+ %s Generated %s"
              .format(currentDateTime.format(formatter), generatedFiles.mkString(", ")))
        }
        fileMapWriter.foreach { w =>
          generatedFiles.foreach { path => w.write(inputFile + " -> " + path + "\n") }
        }
      } catch {
        case e: Throwable => throw new FileParseException(inputFile, e)
      }
    }

    // flush and close the map file
    fileMapWriter.foreach { _.close() }
  }