diff --git a/README.md b/README.md index e8817b1..cd58ba7 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Add the following to your `project/plugins/build.sbt`: resolvers += "bigtoast-github" at "http://bigtoast.github.com/repo/" - libraryDependencies += "atd" %% "sbt-thrift" % "0.3" + libraryDependencies += "atd" %% "sbt-thrift" % "0.4" ## sbt-0.11.0 @@ -64,6 +64,26 @@ Or if you are using a build object extending from Build: thriftOutputDir The output dir for the generated sources. This directory will be added to sourceManaged so it will be automatically get compiled when you run compile. This defaults to 'target/generated-sources'. + + thriftJavaOptions + Additional options to thrift compiler for java generation. + + + thriftJavaEnabled + Are we want generate java source (?) Default is true. + + + thriftJsEnabled + Are we want generate javascript source (?) Default is false. + + + thriftJsOutputDir + The output dir for the generated javascript. This directory will be added to resourceManaged so it will be automatically get compiled during generation of resources. This defaults to 'target/gen-js'. + + + thriftJsOptions + Additional options to thrift compiler for javascript generation. + @@ -71,9 +91,13 @@ Or if you are using a build object extending from Build: - + + + +
thrift-generate-java thrift:generate-java This will run generate java sources from the thrift sources. This task is automatically executed when compile is run.
thrift:generate-js This will run generate javascript sources from the thrift sources. This task is automatically executed when resource are prepared (test or package) if thriftJsEnabled is set to true /td> +
diff --git a/build.sbt b/build.sbt index eebc935..a502eb6 100644 --- a/build.sbt +++ b/build.sbt @@ -5,7 +5,7 @@ organization := "atd" name := "sbt-thrift" -version := "0.3" +version := "0.4" publishTo := Some(Resolver.file("bigtoast.github.com", file(Path.userHome + "/Projects/Destroyer/bigtoast.github.com/repo"))) diff --git a/src/main/scala/ThriftPlugin.scala b/src/main/scala/ThriftPlugin.scala index 48b12b8..d34ccd2 100644 --- a/src/main/scala/ThriftPlugin.scala +++ b/src/main/scala/ThriftPlugin.scala @@ -11,9 +11,15 @@ object ThriftPlugin extends Plugin { val thriftConfig = config("thrift") val thrift = SettingKey[String]("thrift", "thrift executable") - val thriftSourceDir = SettingKey[File]("thrift-source-directory", "Source directory for thrift files. Defaults to src/main/thrift") - val thriftGenerate = TaskKey[Seq[File]]("thrift-generate-java", "Generate java sources from thrift files") - val thriftOutputDir = SettingKey[File]("thrift-output-directory", "Directory where the java files should be placed. Defaults to sourceManaged") + val thriftSourceDir = SettingKey[File]("source-directory", "Source directory for thrift files. Defaults to src/main/thrift") + val thriftGenerate = TaskKey[Seq[File]]("generate-java", "Generate java sources from thrift files") + val thriftOutputDir = SettingKey[File]("output-directory", "Directory where the java files should be placed. Defaults to sourceManaged") + val thriftJavaOptions = SettingKey[Seq[String]]("thrift-java-options", "additional options for java thrift generation") + val thriftJavaEnabled = SettingKey[Boolean]("java-enabled", "java generation is enabled. Default - yes") + val thriftGenerateJs = TaskKey[Seq[File]]("generate-js","Generate javascript sources from thrift files") + val thriftJsOutputDir = SettingKey[File]("js-output-directory","Direcotry where generated javsacript files should be placed. default target/thrift-js") + val thriftJsOptions = SettingKey[Seq[String]]("thrift-js-options", "additional options for js thrift generation") + val thriftJsEnabled = SettingKey[Boolean]("js-enabled", "javascript generation is enabled. Default - no") lazy val thriftSettings :Seq[Setting[_]] = inConfig(thriftConfig)(Seq[Setting[_]]( thrift := "thrift", @@ -22,25 +28,74 @@ object ThriftPlugin extends Plugin { thriftOutputDir <<= (sourceManaged in Compile).identity, - thriftGenerate <<= (streams, thriftSourceDir, thriftOutputDir, thrift) map { ( out, sdir, odir, tbin ) => - val schemas = (sdir ** "*.thrift").get - odir.mkdirs() - out.log("Compiling %d thrift files to %s".format(schemas.size, odir)) - schemas.foreach { schema => - val cmd = "%s -gen java -o %s %s".format(tbin, odir, schema) - out.log("Compiling schema with command: %s" format cmd) - {cmd} ! - } - (odir ** "*.java").get.toSeq + thriftJavaEnabled := true, + + thriftJavaOptions := Seq[String](), + + thriftJsOutputDir := new File("target/gen-js"), + + thriftGenerate <<= (streams, thriftSourceDir, thriftOutputDir, + thrift, thriftJavaOptions, thriftJavaEnabled) map { + (out, sdir, odir, tbin, opts, enabled ) => + if (enabled) { + compileThrift(sdir,odir,tbin,"java",opts,out.log); + }else{ + Seq[File]() + } }, + thriftJsEnabled := false, + + thriftJsOptions := Seq[String](), + + thriftGenerateJs <<= (streams, thriftSourceDir, thriftJsOutputDir, + thrift, thriftJsOptions, thriftJsEnabled) map { + ( out, sdir, odir, tbin, opts, enabled ) => + if (enabled) { + compileThrift(sdir,odir,tbin,"js",opts,out.log); + } else { + Seq[File]() + } + }, + + managedClasspath <<= (classpathTypes, update) map { (cpt, up) => Classpaths.managedJars(thriftConfig, cpt, up) + }, + + (managedResourceDirectories in Compile) <++= (thriftJsOutputDir, thriftJsEnabled) { + (out, enabled) => if (enabled) { + Seq(out) + } else { + Seq() + } } )) ++ Seq[Setting[_]]( sourceGenerators in Compile <+= thriftGenerate in thriftConfig, + resourceGenerators in Compile <+= thriftGenerateJs in thriftConfig, ivyConfigurations += thriftConfig ) + def compileThrift(sourceDir: File, + outputDir: File, + thriftBin: String, + language: String, + options: Seq[String], + logger: Logger):Seq[File] = + { + val schemas = (sourceDir ** "*.thrift").get + outputDir.mkdirs() + logger.info("Compiling %d thrift files to %s in %s".format(schemas.size, language, outputDir)) + schemas.foreach { schema => + val cmd = "%s -gen %s -o %s %s".format(thriftBin, + language + options.mkString(":",",",""), + outputDir, schema) + logger.info("Compiling schema with command: %s" format cmd) + {cmd} ! + } + (outputDir ** "*.%s".format(language)).get.toSeq + } + + }