Skip to content

Commit

Permalink
Finalized support for Play 2.3 (release 0.4.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlitola committed Jul 28, 2014
1 parent fbbc402 commit 2b1f3ed
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 43 deletions.
67 changes: 52 additions & 15 deletions README.md
Expand Up @@ -21,6 +21,36 @@ You can verify that `sass` has been installed by following command:
Installation
------------

Plugin versions are linked to different Play versions. Each Play-version has some differences in how plugin is enabled. Please select suitable instructions below.

After following the instructions `*.sass` and `*.scss` files in `app/assets`
directories will then be automatically compiled to `*.css` files. Files starting with
`_`-character will be left out from compilation as per Play convention.


Play 2.3
--------

Add following to your projects `project/plugins.sbt`

resolvers += "Sonatype OSS Releases" at "https://oss.sonatype.org/content/repositories/releases"

addSbtPlugin("net.litola" % "play-sass" % "0.4.0")

After that you'll need to enable plugin in `build.sbt`.

lazy val root = (project in file(".")).enablePlugins(PlayScala, net.litola.SassPlugin)

If you would like to pass your own command line arguments to Sass call, you can
do it with `.settings` call. For example to use Compass you should append following after previous line.

.settings(
sassOptions := Seq("--compass")
)

Play 2.2
--------

Add following to your projects `project/plugins.sbt`

resolvers += "Sonatype OSS Releases" at "https://oss.sonatype.org/content/repositories/releases"
Expand All @@ -35,30 +65,37 @@ SassPlugin settings.

play.Project.playScalaSettings ++ SassPlugin.sassSettings

On Play 2.1 and Play 2.0 you should do following changes to `project/Build.scala`.
If you would like to pass your own command line arguments to Sass call, you can
do it by overriding `Sassplugin.sassOptions`. For example to use Compass you can use
following:

import net.litola.SassPlugin
play.Project.playScalaSettings ++ SassPlugin.sassSettings ++ Seq(SassPlugin.sassOptions := Seq("--compass"))

val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings( SassPlugin.sassSettings:_* )

This adds Sass asset compiler to Play project. `*.sass` and `*.scss` files in `app/assets`
directories will then be automatically compiled to `*.css` files. Files starting with
`_`-character will be left out from compilation as per Play convention.
Play 2.0 & 2.1
--------------

Customizing
-----------
Add following to your projects `project/plugins.sbt`:

If you would like to pass your own command line arguments to Sass call, you can
do it by overriding `Sassplugin.sassOptions`. For example to use Compass you can use
following:
resolvers += "Sonatype OSS Releases" at "https://oss.sonatype.org/content/repositories/releases"

addSbtPlugin("net.litola" % "play-sass" % "0.2.0")

For 2.0 use version 0.1.3.

After that you should do following changes to `project/Build.scala`.

import net.litola.SassPlugin

val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings( SassPlugin.sassSettings:_* )

play.Project.playScalaSettings ++ SassPlugin.sassSettings ++ Seq(SassPlugin.sassOptions := Seq("--compass", "-r", "compass"))

Versions
--------

The newest version only support Play 2.2. If you need support for older versions, please use
0.2.x or 0.1.x series.
The newest version only supports Play 2.3. If you need support for older Play versions, please use earlier plugin versions.

* **0.4.0** [2014-07-28] Supports Play 2.3 (Thanks to guofengzh and hrlqn)

* **0.3.0** [2013-09-25] Supports Play 2.2 (Thanks to Nilanjan Raychaudhuri and
Zarkus13)
Expand All @@ -81,7 +118,7 @@ Stylus assets.
License
-------

Copyright (c) 2012-2013 Juha Litola
Copyright (c) 2012-2014 Juha Litola

MIT-style license, see details from LICENSE file.

Expand Down
25 changes: 14 additions & 11 deletions src/main/scala/net/litola/SassCompiler.scala
Expand Up @@ -12,14 +12,15 @@ object SassCompiler {
// be proper solution
// See: https://groups.google.com/d/topic/play-framework/VbhJUfVl-xE/discussion
val options = opts.filter { _ != "rjs" }

try {
val parentPath = sassFile.getParentFile.getAbsolutePath
val (cssOutput, dependencies) = runCompiler(
Seq(sassCommand, "-l", "-I", parentPath) ++ options ++ Seq(sassFile.getAbsolutePath)
)
)
val (compressedCssOutput, ignored) = runCompiler(
Seq(sassCommand, "-t", "compressed", "-I", parentPath) ++ options ++ Seq(sassFile.getAbsolutePath)
)
)

(cssOutput, Some(compressedCssOutput), dependencies.map { new File(_) } )
} catch {
Expand All @@ -30,9 +31,9 @@ object SassCompiler {
}

private def sassCommand = if (isWindows) "sass.bat" else "sass"
private val isWindows = System.getProperty("os.name").toLowerCase().indexOf("win") >= 0

private val isWindows = System.getProperty("os.name").toLowerCase.indexOf("win") >= 0

private val DependencyLine = """^/\* line \d+, (.*) \*/$""".r

private def runCompiler(command: ProcessBuilder): (String, Seq[String]) = {
Expand All @@ -46,8 +47,8 @@ object SassCompiler {
val process = command.run(capturer)
if (process.exitValue == 0) {
val dependencies = out.lines.collect {
case DependencyLine(f) => f
}
case DependencyLine(f) => f
}

(out.mkString, dependencies.toList.distinct)
} else
Expand All @@ -69,10 +70,12 @@ object SassCompiler {

for (errline: String <- augmentString(error).lines) {
errline match {
case LocationLine(l, f) => line = l.toInt
file = Some(new File(f))
case other if seen == 0 => message = other
seen += 1
case LocationLine(l, f) =>
line = l.toInt
file = Some(new File(f))
case other if seen == 0 =>
message = other
seen += 1
case _ => // do nothing
}
}
Expand Down
44 changes: 27 additions & 17 deletions src/main/scala/net/litola/SassPlugin.scala
Expand Up @@ -4,25 +4,35 @@ import play.PlayAssetsCompiler
import sbt.Keys._
import sbt._

object SassPlugin extends Plugin with PlayAssetsCompiler {
val sassEntryPoints = SettingKey[PathFinder]("play-sass-entry-points")
val sassOptions = SettingKey[Seq[String]]("play-sass-options")
object SassPlugin extends AutoPlugin with PlayAssetsCompiler {
override def requires = sbt.plugins.JvmPlugin

override def trigger = allRequirements

object autoImport {
val sassEntryPoints = settingKey[PathFinder]("Paths to Sass files to be compiled")
val sassOptions = settingKey[Seq[String]]("Command line options for the sass command")

val sassWatcher = AssetsCompiler("sass",
{ file => (file ** "*.sass") +++ (file ** "*.scss") },
sassEntryPoints,
{ (name, min) =>
name
.replace(".sass", if (min) ".min.css" else ".css")
.replace(".scss", if (min) ".min.css" else ".css")
},
{ (file, options) => SassCompiler.compile(file, options) },
sassOptions
{ file => (file ** "*.sass") +++ (file ** "*.scss") },
sassEntryPoints,
{ (name, min) =>
name
.replace(".sass", if (min) ".min.css" else ".css")
.replace(".scss", if (min) ".min.css" else ".css")
},
{ (file, options) => SassCompiler.compile(file, options) },
sassOptions
)

val sassSettings = Seq(
sassEntryPoints <<= (sourceDirectory in Compile)(base => ((base / "assets" ** "*.sass") +++ (base / "assets" ** "*.scss") --- base / "assets" ** "_*")),
sassOptions := Seq.empty[String],
resourceGenerators in Compile <+= sassWatcher
lazy val baseSassSettings: Seq[Def.Setting[_]] = Seq(
sassEntryPoints <<= (sourceDirectory in Compile)(base => ((base / "assets" ** "*.sass") +++ (base / "assets" ** "*.scss") --- base / "assets" ** "_*")),
sassOptions := Seq.empty[String],
resourceGenerators in Compile <+= sassWatcher
)
}
}

import autoImport._

override val projectSettings = baseSassSettings
}

0 comments on commit 2b1f3ed

Please sign in to comment.