Permalink
Browse files

Initial commit

Contains basic stylus wrapper without Play integration.
  • Loading branch information...
0 parents commit 3c12c40a0f48f31d777ad77e39168e3465f65967 @knuton knuton committed Mar 14, 2012
Showing with 91 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +14 −0 project/Build.scala
  3. +11 −0 project/plugins.sbt
  4. +63 −0 src/main/scala/de/learnyousome/assets/StylusCompiler.scala
@@ -0,0 +1,3 @@
+target/*
+project/target/*
+project/**/target/*
@@ -0,0 +1,14 @@
+import sbt._
+import sbt.Keys._
+
+object PluginBuild extends Build {
+
+ lazy val playStylus = Project(
+ id = "play-stylus", base = file(".")
+ ).settings(
+ sbtPlugin := true,
+ name := "play-stylus",
+ libraryDependencies += "play" %% "play" % "2.0"
+ )
+
+}
@@ -0,0 +1,11 @@
+logLevel := Level.Warn
+
+//resolvers ++= Seq(
+// DefaultMavenRepository,
+// "Typesafe Repository" at "http://repo.typesafe.com/typesafe/ivy-releases/",
+//"Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
+//)
+
+resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
+
+addSbtPlugin("play" % "sbt-plugin" % "2.0")
@@ -0,0 +1,63 @@
+package de.learnyousome.assets
+
+//import sbt.PlayExceptions.AssetCompilationException
+import java.io.File
+import scala.sys.process._
+
+object StylusCompiler {
+
+ def compile(stylFile: File, options: Seq[String]): (String, Option[String], Seq[File]) = {
+ try {
+ val cssOutput = captureOutput("stylus" #< stylFile)
+ val compressedCssOutput = captureOutput("stylus -c" #< stylFile)
+
+ (cssOutput, Some(compressedCssOutput), Seq(stylFile))
+ } catch {
+ case e: StylusCompilationException => {
+ //throw AssetCompilationException(Some(source), "Compilation error", 0, 0)
+ throw new RuntimeException("Stylus compiler " + "(" + e.line + "/" + e.column + ")" + ": " + e.message)
+ }
+ }
+ }
+
+ private def captureOutput(command: ProcessBuilder): String = {
+ val err = new StringBuilder
+ val out = new StringBuilder
+
+ val capturer = ProcessLogger(
+ (output: String) => out.append(output + "\n"),
+ (error: String) => err.append(error + "\n"))
+
+ val process = command.run(capturer)
+ if (process.exitValue == 0)
+ out.toString
+ else
+ throw new StylusCompilationException(err.toString)
+ }
+
+ private val MarkedLine = """\s*>\s*(\d+)\|.*?""".r
+ private val EmptyLine = "".r
+
+ private class StylusCompilationException(stderr: String) extends RuntimeException {
+
+ val (line: Int, column: Int, message: String) = parseError(stderr)
+
+ private def parseError(error: String): (Int, Int, String) = {
+ var seen = 0
+ var line = 0
+ var column = 0
+ var message = "Unknown error, try running stylus directly"
+ for (errline: String <- augmentString(error).lines) {
+ errline match {
+ case MarkedLine(l) => { line = l.toInt; seen += 1 }
+ case EmptyLine() if (seen == 1) => { seen += 1 }
+ case other if (seen == 2) => { message = other; seen += 1 }
+ case other => seen
+ }
+ }
+
+ (line, column, message)
+ }
+ }
+
+}

0 comments on commit 3c12c40

Please sign in to comment.