Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement caching for npmWritePackageJson and npmInstall
- Loading branch information
Showing
14 changed files
with
328 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Johannes Kastner <jokade@karchedon.de> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# sbt-npm | ||
# sbt-node | ||
sbt plugin for integration with Node.js / npm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
plugin/src/main/scala/de/surfice/sbtnpm/sass/SassPlugin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Project: SBT NPM | ||
// Module: | ||
// Description: | ||
package de.surfice.sbtnpm.sass | ||
|
||
import de.surfice.sbtnpm.NpmPlugin | ||
import de.surfice.sbtnpm.NpmPlugin.autoImport._ | ||
import de.surfice.sbtnpm.utils.{ExternalCommand, NodeCommand} | ||
import org.scalajs.sbtplugin.{ScalaJSPluginInternal, Stage} | ||
import sbt._ | ||
import sbt.Keys._ | ||
|
||
object SassPlugin extends AutoPlugin { | ||
|
||
override lazy val requires = NpmPlugin | ||
|
||
/** | ||
* @groupname tasks Tasks | ||
* @groupname settings Settings | ||
*/ | ||
object autoImport { | ||
/** | ||
* Version of the `node-sass` npm module. | ||
*/ | ||
val nodeSassVersion: SettingKey[String] = | ||
settingKey[String]("node-sass version") | ||
|
||
/** | ||
* Defines the node-sass command to be used | ||
*/ | ||
val nodeSassCmd: SettingKey[NodeCommand] = | ||
settingKey[NodeCommand]("node-sass command") | ||
|
||
val nodeSassTarget: SettingKey[File] = | ||
settingKey[File]("target directory for compiled sass files (may be scoped to fastOptJS and fullOptJS)") | ||
|
||
val nodeSassSourceDirectories: SettingKey[Seq[File]] = | ||
settingKey[Seq[File]]("list of source directories that contain files to be processed by sass (may be scoped to fastOptJS and fullOptJS)") | ||
|
||
val nodeSassInputs: TaskKey[Seq[Attributed[(File,String)]]] = | ||
taskKey("Contains all sass input files to be processed (may be scoped to fastOptJS and fullOptJS)") | ||
|
||
val nodeSass: TaskKey[Unit] = | ||
taskKey[Unit]("Runs the sass compiler") | ||
} | ||
|
||
import autoImport._ | ||
|
||
override lazy val projectSettings: Seq[Def.Setting[_]] = Seq( | ||
nodeSassVersion := "~4.5.2", | ||
|
||
nodeSassCmd := NodeCommand(npmTargetDir.value,"node-sass","node-sass"), | ||
|
||
defineSassSourceDirectories(Compile), | ||
|
||
defineSassTarget(Compile), | ||
|
||
defineSassInputs(Compile), | ||
|
||
defineSassTask(Compile,nodeSassTarget in Compile), | ||
|
||
npmDevDependencies += "node-sass" -> nodeSassVersion.value | ||
) ++ | ||
perScalaJSStageSettings(Stage.FullOpt) ++ | ||
perScalaJSStageSettings(Stage.FastOpt) | ||
|
||
|
||
private def perScalaJSStageSettings(stage: Stage): Seq[Def.Setting[_]] = { | ||
|
||
val stageTask = ScalaJSPluginInternal.stageKeys(stage) | ||
|
||
Seq( | ||
defineSassSourceDirectories(stageTask), | ||
defineSassTarget(stageTask), | ||
defineSassInputs(stageTask), | ||
nodeSassTarget in stageTask := (crossTarget in (Compile,stageTask)).value, | ||
defineSassTask(stageTask,nodeSassTarget in stageTask) | ||
) | ||
} | ||
|
||
private def defineSassTask(scope: Any, targetDir: SettingKey[File]) = { | ||
val (task, inputs, targetDir) = scope match { | ||
case scoped: Scoped => (nodeSass in scoped, nodeSassInputs in (Compile,scoped), nodeSassTarget in (Compile,scoped)) | ||
case config: Configuration => (nodeSass in config, nodeSassInputs in config, nodeSassTarget in config) | ||
} | ||
task := { | ||
npmInstall.value | ||
val cwd = targetDir.value | ||
val sass = nodeSassCmd.value | ||
val logger = streams.value.log | ||
// logger.info(s"compiling sass files: ${inputs.value.data.mkString(", ")}") | ||
inputs.value.foreach { f => | ||
val (src,dest) = f.data | ||
sass.run(src.getAbsolutePath, (cwd / dest).getAbsolutePath)(cwd,logger) | ||
} | ||
} | ||
} | ||
|
||
private def defineSassInputs(scope: Any) = { | ||
val (task,sourceDirs) = scope match { | ||
case scoped: Scoped => (nodeSassInputs in scoped,nodeSassSourceDirectories in (Compile,scoped)) | ||
case config: Configuration => (nodeSassInputs in config, nodeSassSourceDirectories in config) | ||
} | ||
task := { | ||
Attributed.blankSeq( sourceDirs.value flatMap { dir => | ||
val fs = (dir ** "*").get.filter{ f => | ||
val name = f.getName | ||
f.isFile && !name.startsWith("_") && name.endsWith(".scss") | ||
} | ||
fs.map{ f => | ||
val path = f.relativeTo(dir).get.getPath | ||
|
||
(f, path.substring(0,path.lastIndexOf("."))+".css" ) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
private def defineSassSourceDirectories(scope: Any) = scope match { | ||
case scoped: Scoped => | ||
nodeSassSourceDirectories in (Compile,scoped) := (resourceDirectories in (Compile,scoped)).value | ||
// nodeSassSourceDirectories in (Test,scoped) := (resourceDirectories in (Test,scoped)).value | ||
case config: Configuration => nodeSassSourceDirectories in config := (resourceDirectories in config).value | ||
} | ||
|
||
private def defineSassTarget(scope: Any) = scope match { | ||
case scoped: Scoped => | ||
nodeSassTarget in (Compile,scoped) := (crossTarget in (Compile,scoped)).value / "css" | ||
// nodeSassTarget in (Compile,scoped) := (crossTarget in (Compile,scoped)).value / "css" | ||
case config: Configuration => | ||
nodeSassTarget in config := (crossTarget in config).value / "css" | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
plugin/src/main/scala/de/surfice/sbtnpm/utils/ExternalCommand.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Project: SBT NPM | ||
// Module: | ||
// Description: | ||
package de.surfice.sbtnpm.utils | ||
|
||
import sbt._ | ||
|
||
/** | ||
* Helper for platform-independent handling of external commands. | ||
*/ | ||
trait ExternalCommand { | ||
def run(args: String*)(workingDir: File, logger: Logger): Unit = { | ||
ExternalCommand.execute(cmdPath +: args, workingDir, logger) | ||
} | ||
|
||
def cmdPath: String | ||
} | ||
|
||
|
||
object ExternalCommand { | ||
def apply(cmdName: String): ExternalCommand = new Impl(cmdName) | ||
|
||
class Impl(val cmdPath: String) extends ExternalCommand | ||
|
||
def execute(cmdLine: Seq[String], cwd: sbt.File, logger: Logger): Unit = { | ||
val ret = Process(cmdLine) ! logger | ||
|
||
if(ret != 0) | ||
sys.error(s"Non-zero exit code from command ${cmdLine.head}") | ||
|
||
} | ||
|
||
object npm extends Impl("npm") { | ||
def install(npmTargetDir: File, logger: Logger): Unit = run("install")(npmTargetDir,logger) | ||
} | ||
} |
Oops, something went wrong.