@@ -33,20 +33,16 @@ package io.bazel.rules_scala.scrooge_support
3333
3434import com .twitter .scrooge ._
3535import com .twitter .scrooge .ast .Document
36- import com .twitter .scrooge .backend .{GeneratorFactory , ScalaGenerator , ServiceOption }
37- import com .twitter .scrooge .frontend .{FileParseException , TypeResolver , ThriftParser , Importer , MultiImporter , ZipImporter }
38- import java .io .{File , FileWriter }
36+ import com .twitter .scrooge .backend .{ GeneratorFactory , ScalaGenerator }
37+ import com .twitter .scrooge .frontend .{ FileParseException , TypeResolver , ThriftParser }
38+ import com .twitter .scrooge .java_generator .ApacheJavaGenerator
39+ import java .io .{ File , FileWriter }
3940import java .nio .file .Paths
4041import java .util .jar .{ JarFile , JarEntry }
4142import java .util .logging .Level
4243import scala .collection .concurrent .TrieMap
43- import scala .collection .mutable
44- import scala .collection .JavaConverters ._
4544
4645object CompilerDefaults {
47- var language : String = " scala"
48- var defaultNamespace : String = " thrift"
49-
5046 def listJar (_jar : File ): List [String ] =
5147 try {
5248 val files = List .newBuilder[String ]
@@ -66,46 +62,34 @@ object CompilerDefaults {
6662 }
6763}
6864
69- class Compiler {
70- val defaultDestFolder = " ."
71- var destFolder : String = defaultDestFolder
65+ class Compiler (val config : ScroogeConfig ) {
7266 // These are jars we are including, but are not compiling
73- val includeJars = new mutable. ListBuffer [ String ]
67+ val includeJars = config.includePaths
7468 // these are the jars we want to compile into scala source jars
75- val compileJars = new mutable.ListBuffer [String ]
76- val flags = new mutable.HashSet [ServiceOption ]
77- val namespaceMappings = new mutable.HashMap [String , String ]
78- var verbose = false
79- var strict = true
80- var skipUnchanged = false
81- var experimentFlags = new mutable.ListBuffer [String ]
82- var fileMapPath : scala.Option [String ] = None
69+ val compileJars = config.thriftFiles
70+ val experimentFlags = config.languageFlags
8371 var fileMapWriter : scala.Option [FileWriter ] = None
84- var dryRun : Boolean = false
85- var language : String = CompilerDefaults .language
86- var defaultNamespace : String = CompilerDefaults .defaultNamespace
87- var scalaWarnOnJavaNSFallback : Boolean = false
8872
8973
9074 def run () {
9175 // if --gen-file-map is specified, prepare the map file.
92- fileMapWriter = fileMapPath.map { path =>
76+ fileMapWriter = config. fileMapPath.map { path =>
9377 val file = new File (path)
9478 val dir = file.getParentFile
9579 if (dir != null && ! dir.exists()) {
9680 dir.mkdirs()
9781 }
98- if (verbose) {
82+ if (config. verbose) {
9983 println(" + Writing file mapping to %s" .format(path))
10084 }
10185 new FileWriter (file)
10286 }
10387
10488 val allJars : List [File ] =
105- (( includeJars.toList) ::: ( compileJars.toList) )
89+ (includeJars ::: compileJars)
10690 .map { path => (new File (path)).getCanonicalFile }
10791
108- val isJava = language.equals(" java" )
92+ val isJava = config. language.equals(" java" )
10993 val documentCache = new TrieMap [String , Document ]
11094
11195 // compile
@@ -126,36 +110,37 @@ class Compiler {
126110 val importer = rootImporter.copy(focus = focus) +: rootImporter
127111 val parser = new ThriftParser (
128112 importer,
129- strict,
113+ config. strict,
130114 defaultOptional = isJava,
131115 skipIncludes = false ,
132116 documentCache
133117 )
134118 parser.logger.setLevel(Level .OFF ) // scrooge warns on file names with "/"
135- val doc = parser.parseFile(inputFile).mapNamespaces(namespaceMappings.toMap )
119+ val doc = parser.parseFile(inputFile).mapNamespaces(config.namespaceMappings )
136120
137- if (verbose) println(" + Compiling %s" .format(inputFile))
121+ if (config. verbose) println(" + Compiling %s" .format(inputFile))
138122 val resolvedDoc = TypeResolver ()(doc)
139123 val generator = GeneratorFactory (
140- language,
124+ config. language,
141125 resolvedDoc,
142- defaultNamespace,
143- experimentFlags.toList
144- )
126+ config.defaultNamespace,
127+ experimentFlags)
145128
146129 generator match {
147- case g : ScalaGenerator => g.warnOnJavaNamespaceFallback = scalaWarnOnJavaNSFallback
130+ case g : ScalaGenerator => g.warnOnJavaNamespaceFallback = config.scalaWarnOnJavaNSFallback
131+ case g : ApacheJavaGenerator => g.serEnumType = config.javaSerEnumType
148132 case _ => ()
149133 }
150134
151135 val generatedFiles = generator(
152- flags.toSet,
153- new File (destFolder),
154- dryRun
136+ config.flags,
137+ new File (config.destFolder),
138+ config.dryRun,
139+ config.genAdapt
155140 ).map {
156141 _.getPath
157142 }
158- if (verbose) {
143+ if (config. verbose) {
159144 println(" + Generated %s" .format(generatedFiles.mkString(" , " )))
160145 }
161146 fileMapWriter.foreach { w =>
0 commit comments