Permalink
Browse files

refactor to remove dupication

  • Loading branch information...
1 parent fa77e5d commit d37ea647bc107efe8a036e0d37136f04343fdf6b Ryan King committed Aug 12, 2011
Showing with 29 additions and 65 deletions.
  1. +3 −0 .gitignore
  2. +26 −65 src/main/scala/com/twitter/sbt/CompileThrift.scala
View
@@ -8,3 +8,6 @@ src/main/scala/com/twitter/sbt/BuildInfo.scala
/standard-project.tmproj
*.iml
.idea
+.ensime
+.ensime.msg.counter
+ensime_port
@@ -6,7 +6,7 @@ import java.io.File
import scala.collection.jcl
import java.io.{File, FileOutputStream, BufferedOutputStream}
-
+// TODO support multiple thrift versions
object CompileThrift {
var cachedFinaglePath: Option[String] = None
var cachedVanillaPath: Option[String] = None
@@ -15,28 +15,27 @@ object CompileThrift {
trait CompileThrift extends DefaultProject with GeneratedSources {
import CompileThrift._
- private[this] val _thriftBinFinagle = CompileThrift.synchronized {
- if (!cachedFinaglePath.isDefined) {
- // TODO: we don't discriminate between versions here (which we need to..).
- val binPath = System.getProperty("os.name") match {
- case "Mac OS X" => "thrift-finagle.osx10.6"
- case "Linux" => System.getProperty("os.arch") match {
- case "i386" => "thrift-finagle.linux32"
- case "amd64" => "thrift-finagle.linux64"
- case arch => throw new Exception(
- "No thrift linux binary for %s, talk to william@twitter.com".format(arch))
- }
- case "FreeBSD" => System.getProperty("os.arch") match {
- case "amd64" => "thrift-finagle.bsd64"
- case arch => throw new Exception(
- "No thrift BSD binary for %s, talk to brandon@twitter.com".format(arch))
- }
- case unknown => throw new Exception(
- "No thrift binary for %s, talk to marius@twitter.com".format(unknown))
- }
+ private[this] val platform = System.getProperty("os.name") match {
+ case "Mac OS X" => "osx10.6"
+ case "Linux" => System.getProperty("os.arch") match {
+ case "i386" => "linux32"
+ case "amd64" => "linux64"
+ case arch => throw new Exception(
+ "No thrift linux binary for %s, talk to william@twitter.com".format(arch))
+ }
+ case "FreeBSD" => System.getProperty("os.arch") match {
+ case "amd64" => "bsd64"
+ case arch => throw new Exception(
+ "No thrift BSD binary for %s, talk to brandon@twitter.com".format(arch))
+ }
+ case unknown => throw new Exception(
+ "No thrift binary for %s, talk to marius@twitter.com".format(unknown))
+ }
+ private[this] def extractBinary(binPath: String) = {
+ println("/thrift/%s".format(binPath))
val stream = getClass.getResourceAsStream("/thrift/%s".format(binPath))
- val file = File.createTempFile("thrift-finagle", "scala")
+ val file = File.createTempFile(binPath, "")
file.deleteOnExit()
val fos = new BufferedOutputStream(new FileOutputStream(file), 1<<20)
try {
@@ -58,8 +57,12 @@ trait CompileThrift extends DefaultProject with GeneratedSources {
import Process._
val path = file.getAbsolutePath()
(execTask { "chmod 0500 %s".format(path) }).run
+ path
+ }
- cachedFinaglePath = Some(path)
+ private[this] val _thriftBinFinagle = CompileThrift.synchronized {
+ if (!cachedFinaglePath.isDefined) {
+ cachedFinaglePath = Some(extractBinary("thrift-finagle." + platform))
}
cachedFinaglePath.get
@@ -69,48 +72,7 @@ trait CompileThrift extends DefaultProject with GeneratedSources {
private[this] val _thriftBinVanilla = CompileThrift.synchronized {
if (!cachedVanillaPath.isDefined) {
- // TODO: we don't discriminate between versions here (which we need to..).
- val binPath = System.getProperty("os.name") match {
- case "Mac OS X" => "thrift.osx10.6"
- case "Linux" => System.getProperty("os.arch") match {
- case "i386" => "thrift.linux32"
- case "amd64" => "thrift.linux64"
- case arch => throw new Exception(
- "No thrift linux binary for %s, talk to william@twitter.com".format(arch))
- }
- case "FreeBSD" => System.getProperty("os.arch") match {
- case arch => throw new Exception(
- "No thrift BSD binary for %s, talk to brandon@twitter.com".format(arch))
- }
- case unknown => throw new Exception(
- "No thrift binary for %s, talk to marius@twitter.com".format(unknown))
- }
-
- val stream = getClass.getResourceAsStream("/thrift/%s".format(binPath))
- val file = File.createTempFile("thrift", "scala")
- file.deleteOnExit()
- val fos = new BufferedOutputStream(new FileOutputStream(file), 1<<20)
- try {
- // TODO(oliver): upgrade to 2.8 so that i can declare @scala.annotation.tailrec
- def copy(out: java.io.OutputStream, in: java.io.InputStream) {
- val buf = new Array[Byte](4096)
- val len = in.read(buf)
- if(len > 0) {
- out.write(buf, 0, len)
- copy(out, in)
- }
- }
-
- copy(fos, stream)
- } finally {
- fos.close()
- }
-
- import Process._
- val path = file.getAbsolutePath()
- (execTask { "chmod 0500 %s".format(path) }).run
-
- cachedVanillaPath = Some(path)
+ cachedVanillaPath = Some(extractBinary("thrift." + platform))
}
cachedVanillaPath.get
@@ -139,5 +101,4 @@ trait CompileThrift extends DefaultProject with GeneratedSources {
}
if (tasks.isEmpty) None else tasks.reduceLeft { _ && _ }.run
} describedAs("Compile thrift into %s".format(lang))
-
}

0 comments on commit d37ea64

Please sign in to comment.