Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #341 - add setting to configure root dir importer #345

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ package com.twitter.scrooge

import com.twitter.scrooge.ast.Document
import com.twitter.scrooge.backend.{GeneratorFactory, ScalaGenerator}
import com.twitter.scrooge.frontend.{FileParseException, TypeResolver, ThriftParser, Importer}
import com.twitter.scrooge.frontend.{FileParseException, Importer, NullImporter, ThriftParser, TypeResolver}
import com.twitter.scrooge.java_generator.ApacheJavaGenerator

import java.io.{File, FileWriter}
import scala.collection.concurrent.TrieMap

Expand All @@ -45,7 +46,7 @@ class Compiler(val config: ScroogeConfig) {
new FileWriter(file)
}

val importer = Importer(new File(".")) +: Importer(config.includePaths.toSeq)
val importer = (if(config.addRootDirImporter) Importer(new File(".")) else NullImporter) +: Importer(config.includePaths.toSeq)

val isJava = config.language.equals("java")
val documentCache = new TrieMap[String, Document]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ case class ScroogeConfig(
language: String = CompilerDefaults.language,
defaultNamespace: String = CompilerDefaults.defaultNamespace,
scalaWarnOnJavaNSFallback: Boolean = false,
javaSerEnumType: Boolean = false)
javaSerEnumType: Boolean = false,
addRootDirImporter: Boolean = true
)

object ScroogeOptionParser {

Expand Down
18 changes: 13 additions & 5 deletions scrooge-sbt-plugin/src/main/scala/com/twitter/ScroogeSBT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ object ScroogeSBT extends AutoPlugin {
if (language.endsWith("java")) "*.java" else "*.scala"

private[this] def compile(
log: Logger,
outputDir: File,
thriftFiles: Set[File],
thriftIncludes: Set[File],
Expand All @@ -22,7 +21,8 @@ object ScroogeSBT extends AutoPlugin {
flags: Set[ServiceOption],
disableStrict: Boolean,
scalaWarnOnJavaNSFallback: Boolean,
defaultNamespace: String
defaultNamespace: String,
addRootDirImporter: Boolean
): Unit = {

val originalLoader: Option[ClassLoader] =
Expand All @@ -43,7 +43,8 @@ object ScroogeSBT extends AutoPlugin {
strict = !disableStrict,
scalaWarnOnJavaNSFallback = scalaWarnOnJavaNSFallback,
defaultNamespace = defaultNamespace,
language = language.toLowerCase
language = language.toLowerCase,
addRootDirImporter = addRootDirImporter
)

try {
Expand Down Expand Up @@ -105,6 +106,11 @@ object ScroogeSBT extends AutoPlugin {
"complete list of folders to search for thrift 'include' directives"
)

val scroogeThriftIncludeRoot = SettingKey[Boolean](
"scrooge-thrift-include-root",
"If true scrooge will always search the project root for script files"
)

val scroogeThriftNamespaceMap = SettingKey[Map[String, String]](
"scrooge-thrift-namespace-map",
"namespace rewriting, to support generation of java/finagle/scrooge into the same jar"
Expand Down Expand Up @@ -187,6 +193,7 @@ object ScroogeSBT extends AutoPlugin {
scroogeThriftSources := (scroogeThriftSourceFolder.value ** "*.thrift").get,
// complete list of include directories
scroogeThriftIncludes := scroogeThriftIncludeFolders.value ++ scroogeUnpackDeps.value,
scroogeThriftIncludeRoot := true,
// unpack thrift files from all dependencies in the `thrift` configuration
//
// returns Seq[File] - directories that include thrift files
Expand Down Expand Up @@ -259,12 +266,12 @@ object ScroogeSBT extends AutoPlugin {
val disableStrict = scroogeDisableStrict.value
val warnOnFallBack = scroogeScalaWarnOnJavaNSFallback.value
val javaNamespace = scroogeDefaultJavaNamespace.value
val addRootDirImporter = scroogeThriftIncludeRoot.value
// for some reason, sbt sometimes calls us multiple times, often with no source files.
if (scroogeIsDirty.value && thriftSources.nonEmpty) {
streamValue.log.info(s"Generating scrooge thrift for ${thriftSources.mkString(", ")} ...")
scroogeLangs.foreach { language =>
compile(
streamValue.log,
outputFolder,
thriftSources.toSet,
thriftIncludes.toSet,
Expand All @@ -273,7 +280,8 @@ object ScroogeSBT extends AutoPlugin {
buildOptions.toSet,
disableStrict,
warnOnFallBack,
javaNamespace
javaNamespace,
addRootDirImporter
)
}
}
Expand Down