Skip to content

Commit

Permalink
added generation of javascript resources
Browse files Browse the repository at this point in the history
  • Loading branch information
rssh committed Nov 17, 2011
1 parent fa08ef1 commit 4494a15
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 16 deletions.
28 changes: 26 additions & 2 deletions README.md
Expand Up @@ -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

Expand Down Expand Up @@ -64,16 +64,40 @@ Or if you are using a build object extending from Build:
<td> <b>thriftOutputDir</b> </td>
<td>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'.</td>
</tr>
<tr>
<td> <b>thriftJavaOptions</b> </td>
<td>Additional options to thrift compiler for java generation.</td>
</tr>
<tr>
<td> <b>thriftJavaEnabled</b> </td>
<td> Are we want generate java source (?) Default is true.</td>
</tr>
<tr>
<td> <b>thriftJsEnabled</b> </td>
<td> Are we want generate javascript source (?) Default is false.</td>
</tr>
<tr>
<td> <b>thriftJsOutputDir</b> </td>
<td>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'.</td>
</tr>
<tr>
<td> <b>thriftJsOptions</b> </td>
<td>Additional options to thrift compiler for javascript generation.</td>
</tr>

</table>

## Tasks

<table>
<tr>
<td> <b>thrift-generate-java</b> </td>
<td> <b>thrift:generate-java</b> </td>
<td>This will run generate java sources from the thrift sources. This task is automatically executed when compile is run.</td>
</tr>
<tr>
<td> <b>thrift:generate-js</b> </td>
<td>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>
</tr>

</table>

Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Expand Up @@ -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")))

81 changes: 68 additions & 13 deletions src/main/scala/ThriftPlugin.scala
Expand Up @@ -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",
Expand All @@ -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)
<x>{cmd}</x> !
}
(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)
<x>{cmd}</x> !
}
(outputDir ** "*.%s".format(language)).get.toSeq
}


}

0 comments on commit 4494a15

Please sign in to comment.