Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jokade/sbt-node
Browse files Browse the repository at this point in the history
  • Loading branch information
jokade committed Jul 29, 2017
2 parents 784fd35 + 21ed13d commit a2c623a
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.sbt
@@ -1,4 +1,4 @@
version in ThisBuild := "0.0.2"
version in ThisBuild := "0.0.3"

scalaVersion in ThisBuild := "2.10.6"

Expand Down
58 changes: 58 additions & 0 deletions plugin/src/main/scala/de/surfice/sbtnpm/assets/AssetsPlugin.scala
@@ -0,0 +1,58 @@
// Project: sbt-node
// Module:
// Description:
package de.surfice.sbtnpm.assets

import sbt.{Def, _}
import Keys._
import de.surfice.sbtnpm.NpmPlugin
import org.scalajs.sbtplugin.{ScalaJSPluginInternal, Stage}

object AssetsPlugin extends AutoPlugin {
override def requires: Plugins = NpmPlugin

object autoImport {
val NodeAssets: Configuration = config("nodeAssets") describedAs("Configuration for sbt-node assets pipeline")

val nodeAssetsStage: TaskKey[File] =
taskKey[File]("Runs the node assest pipeline for the current scope (fastOptJS or fullOptJS)")
}

import autoImport._
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._

override def projectSettings: Seq[Def.Setting[_]] = Seq(
resourceDirectories in (NodeAssets,fastOptJS) := Seq( sourceDirectory.value / "main" / "public" ),
resourceDirectories in (NodeAssets,fullOptJS) := Seq( sourceDirectory.value / "main" / "public" ),

crossTarget in (NodeAssets,fastOptJS) := (crossTarget in fastOptJS).value / "fastopt",
crossTarget in (NodeAssets,fullOptJS) := (crossTarget in fastOptJS).value / "fullopt",

crossTarget in (Compile,fastOptJS) := (crossTarget in (NodeAssets,fastOptJS)).value,
crossTarget in (Compile,fullOptJS) := (crossTarget in (NodeAssets,fullOptJS)).value
) ++
perScalaJSStageSettings(Stage.FullOpt) ++
perScalaJSStageSettings(Stage.FastOpt)


private def perScalaJSStageSettings(stage: Stage): Seq[Def.Setting[_]] = {

val stageTask = ScalaJSPluginInternal.stageKeys(stage)

Seq(
defineNodeAssetsStage(stageTask)
)
}

def defineNodeAssetsStage(scoped: Scoped) =
nodeAssetsStage in scoped := {
val targetDir = (crossTarget in (NodeAssets,scoped)).value
val resourceDirs = (resourceDirectories in (NodeAssets,scoped)).value

for(dir <- resourceDirs)
IO.copyDirectory(dir,targetDir)

targetDir
}

}
Expand Up @@ -5,13 +5,15 @@ package de.surfice.sbtnpm.liteserver

import sbt._
import Keys._
import de.surfice.sbtnpm.assets.AssetsPlugin
import de.surfice.sbtnpm.{NpmPlugin, utils}
import de.surfice.sbtnpm.utils.{FileWithLastrun, JsonNode, NodeCommand}
import org.scalajs.sbtplugin.{ScalaJSPluginInternal, Stage}

object LiteServerPlugin extends AutoPlugin {

override lazy val requires = NpmPlugin

override lazy val requires = NpmPlugin && AssetsPlugin


/**
Expand All @@ -29,7 +31,7 @@ object LiteServerPlugin extends AutoPlugin {
settingKey("Base directory from which files are served (scoped to fastOptJS or fullOptJS)")

val liteServerIndexFile: SettingKey[File] =
settingKey("Path to the index.html file (scoped to fastOptJS or fullOptJS")
settingKey("Path to the index.html file (scoped to fastOptJS or fullOptJS)")

val liteServerRoutes: SettingKey[Iterable[(String,String)]] =
settingKey("Entries to be put in the lite-server config 'routes' object (scoped to fastOptJS or fullOptJS)")
Expand All @@ -40,28 +42,55 @@ object LiteServerPlugin extends AutoPlugin {
val liteServerWriteConfigFile: TaskKey[FileWithLastrun] =
taskKey("Create the lite-server config file for the current stage (fastOptJS or fullOptJS)")

val liteServerPrepare: TaskKey[Unit] =
taskKey[Unit]("Prepare config, compile, and run asset pipeline (scoped to fastOptJS and fullOptJS)")

val liteServerStart: TaskKey[Unit] =
taskKey("Start the node lite-server for the current stage (fastOptJS or fullOptJS")

val liteServerStop: TaskKey[Unit] =
taskKey("Stops the node lite-server for the current stage (fastOptJS or fullOptJS")

val liteServerWriteIndexFile: TaskKey[File] =
taskKey("Creates the index.html file for the current stage (fastOptJS or fullOptJS")
// val liteServerWriteIndexFile: TaskKey[File] =
// taskKey("Creates the index.html file for the current stage (fastOptJS or fullOptJS")

val liteServerRun: TaskKey[Unit] =
taskKey("Compiles the project and runs the node lite-server for the current stage (fastOptJS or fullOptJS")
}

import autoImport._
import NpmPlugin.autoImport._
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._
import AssetsPlugin.autoImport._

override lazy val projectSettings: Seq[Def.Setting[_]] = Seq(
liteServerVersion := de.surfice.sbtnpm.Versions.liteServer,

liteServerCmd := NodeCommand(npmNodeModulesDir.value,"lite-server","lite-server"),

npmDevDependencies += "lite-server" -> liteServerVersion.value
npmDevDependencies += "lite-server" -> liteServerVersion.value,

(liteServerRun in fastOptJS) := {
val _ = (liteServerPrepare in fastOptJS).value

val cmd = npmCmd.value
cmd.start("run-script","fastOptJS")(streams.value.log,waitAndKillOnInput = true)
},
(liteServerRun in fullOptJS) := {
val _ = (liteServerPrepare in fullOptJS).value

val cmd = npmCmd.value
cmd.start("run-script","fullOptJS")(streams.value.log,waitAndKillOnInput = true)
}
) ++
perScalaJSStageSettings(Stage.FullOpt) ++
perScalaJSStageSettings(Stage.FastOpt)
perScalaJSStageSettings(Stage.FastOpt) ++
Seq(
liteServerPrepare in fastOptJS := (liteServerPrepare in fastOptJS)
.dependsOn(npmInstall,fastOptJS in Compile,nodeAssetsStage in fastOptJS,liteServerWriteConfigFile in fastOptJS).value,
liteServerPrepare in fullOptJS := (liteServerPrepare in fullOptJS)
.dependsOn(npmInstall,fullOptJS in Compile,nodeAssetsStage in fullOptJS, liteServerWriteConfigFile in fullOptJS).value
)


private def perScalaJSStageSettings(stage: Stage): Seq[Def.Setting[_]] = {
Expand All @@ -74,21 +103,22 @@ object LiteServerPlugin extends AutoPlugin {
defineLiteServerIndexFile(stageTask),
defineLiteServerRoutes(stageTask),
defineLiteServerWriteConfigFile(stageTask),
defineLiteServerWriteIndexFile(stageTask),
defineLiteServerPrepare(stageTask),
defineLiteServerStart(stageTask),
defineLiteServerStop(stageTask)
defineLiteServerStop(stageTask),
addNpmScript(stageTask)
)
}

private def defineLiteServerConfigFile(scoped: Scoped) =
liteServerConfigFile in scoped := utils.fileWithScalaJSStageSuffix( (crossTarget in (Compile,scoped)).value,"bs-config-",scoped,".json")
liteServerConfigFile in scoped := utils.fileWithScalaJSStageSuffix( (crossTarget in Compile).value,"bs-config-",scoped,".json")

private def defineLiteServerBaseDir(scoped: Scoped) =
liteServerBaseDir in scoped := (crossTarget in (Compile,scoped)).value
liteServerBaseDir in scoped := (crossTarget in (NodeAssets,scoped)).value

private def defineLiteServerIndexFile(scoped: Scoped) =
liteServerIndexFile in scoped := {
val baseDir = (resourceDirectory in (Compile,scoped)).value
val baseDir = (liteServerBaseDir in scoped).value
if(scoped.key.label == "fastOptJS")
utils.fileWithScalaJSStageSuffix(baseDir,"index-",scoped,".html")
else
Expand All @@ -98,24 +128,42 @@ object LiteServerPlugin extends AutoPlugin {
private def defineLiteServerRoutes(scoped: Scoped) =
liteServerRoutes in scoped := Seq("/node_modules/" -> npmNodeModulesDir.value.getAbsolutePath)

private def defineLiteServerPrepare(scoped: Scoped) =
liteServerPrepare in scoped := {
val log = streams.value.log
val stageName = scoped.key.label
// log.info(s"preparing lite-server environment for $stageName")

// val srcIndexFile = (liteServerIndexFile in scoped).value
// val tgtDir = (crossTarget in (NodeAssets,scoped)).value
// val tgtIndexFile = tgtDir / "index.html"
val indexFile = (liteServerIndexFile in scoped).value

if(!indexFile.exists())
log.warn(s"File $indexFile defined by (liteServerIndexFile in $stageName) does not exist - lite-server configuration will probably fail")
// else
// IO.copyFile(srcIndexFile,tgtIndexFile)
}

private def defineLiteServerWriteConfigFile(scoped: Scoped) =
liteServerWriteConfigFile in scoped := {
val lastrun = (liteServerWriteConfigFile in scoped).previous
val file = (liteServerConfigFile in scoped).value
val baseDir = (liteServerBaseDir in scoped).value
val indexFile = (liteServerWriteIndexFile in scoped).value
val indexPath = indexFile.relativeTo(baseDir) match {
case Some(p) => p.getPath()
case None =>
streams.value.log.error(s"index file $indexFile must be a child of base directory $baseDir")
""
}
// val indexFile = baseDir / "index.html"
// val indexFile = (liteServerWriteIndexFile in scoped).value
// val indexPath = indexFile.relativeTo(baseDir) match {
// case Some(p) => p.getPath()
// case None =>
// streams.value.log.error(s"index file $indexFile must be a child of base directory $baseDir")
// ""
// }


if(lastrun.isEmpty || lastrun.get.needsUpdateComparedToConfig(baseDirectory.value)) {
writeConfigFile(file,
baseDir.getAbsolutePath,
indexPath,
"./index.html",
(liteServerRoutes in scoped).value)
FileWithLastrun(file)
}
Expand All @@ -125,7 +173,7 @@ object LiteServerPlugin extends AutoPlugin {
private def defineLiteServerStart(scoped: Scoped) =
liteServerStart in scoped := {
npmInstall.value
(liteServerWriteConfigFile in scoped).value
// (liteServerWriteConfigFile in scoped).value
val cmd = liteServerCmd.value
val configFile = (liteServerConfigFile in scoped).value

Expand All @@ -137,14 +185,23 @@ object LiteServerPlugin extends AutoPlugin {
liteServerCmd.value.destroy(scoped,streams.value.log)
}

private def defineLiteServerWriteIndexFile(scoped: Scoped) =
liteServerWriteIndexFile in scoped := {
val src = (liteServerIndexFile in scoped).value
val basedir = (liteServerBaseDir in scoped).value
val dest = basedir / src.getName
// private def defineLiteServerWriteIndexFile(scoped: Scoped) =
// liteServerWriteIndexFile in scoped := {
// val src = (liteServerIndexFile in scoped).value
// val basedir = (liteServerBaseDir in scoped).value
// val dest = basedir / src.getName
// val dest = utils.fileWithScalaJSStageSuffix((liteServerBaseDir in scoped).value,"index-",scoped,".html")
IO.copy(Seq((src,dest)),overwrite = true)
dest
// if(src.exists)
// IO.copy(Seq((src,dest)),overwrite = true)
// else
// streams.value.log.warn(s"File ${src} defined by (liteServerIndexFile in ${scoped.key.label}) does not exist - lite-server configuration will probably fail")
// dest
// }

private def addNpmScript(scoped: Scoped) =
npmScripts += {
val lsConfigFile = (liteServerConfigFile in scoped).value
(scoped.key.label, "lite-server --config="+lsConfigFile.getAbsolutePath)
}

// private def defineLiteServer(stageTask: TaskKey[Attributed[File]]) =
Expand All @@ -162,6 +219,7 @@ object LiteServerPlugin extends AutoPlugin {
//
// }


private def writeConfigFile(file: File, baseDir: String, indexFile: String, routes: Iterable[(String,String)]) = {
import JsonNode._
val config = Obj(
Expand Down
Expand Up @@ -6,13 +6,16 @@ package de.surfice.sbtnpm.systemjs
import sbt._
import Keys._
import Cache._
import de.surfice.sbtnpm.assets.AssetsPlugin
import de.surfice.sbtnpm.liteserver.LiteServerPlugin
import de.surfice.sbtnpm.{NpmPlugin, utils}
import de.surfice.sbtnpm.utils.FileWithLastrun
import org.scalajs.sbtplugin.{ScalaJSPluginInternal, Stage}

object SystemJSPlugin extends AutoPlugin {

override lazy val requires = NpmPlugin

override lazy val requires = NpmPlugin && AssetsPlugin && LiteServerPlugin

/**
* @groupname tasks Tasks
Expand All @@ -36,12 +39,21 @@ object SystemJSPlugin extends AutoPlugin {
}

import autoImport._
import NpmPlugin.autoImport._
import AssetsPlugin.autoImport._
import LiteServerPlugin.autoImport._
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._

override lazy val projectSettings: Seq[Def.Setting[_]] = Seq(
) ++
perScalaJSStageSettings(Stage.FullOpt) ++
perScalaJSStageSettings(Stage.FastOpt)
perScalaJSStageSettings(Stage.FastOpt) ++
Seq(

(fastOptJS in Compile) := (fastOptJS in Compile).dependsOn(systemJS in fastOptJS).value,
(fullOptJS in Compile) := (fullOptJS in Compile).dependsOn(systemJS in fullOptJS).value
// liteServerPrepare in fastOptJS := (liteServerPrepare in fastOptJS).dependsOn(systemJS in fastOptJS).value,
// liteServerPrepare in fullOptJS := (liteServerPrepare in fullOptJS).dependsOn(systemJS in fullOptJS).value
)


private def perScalaJSStageSettings(stage: Stage): Seq[Def.Setting[_]] = {
Expand All @@ -59,7 +71,7 @@ object SystemJSPlugin extends AutoPlugin {

private def defineSystemJSFile(scope: Any) = scope match {
case scoped: Scoped =>
systemJSFile in scoped := utils.fileWithScalaJSStageSuffix((crossTarget in (Compile,scoped)).value,"systemjs-",scoped,".config.js")
systemJSFile in scoped := utils.fileWithScalaJSStageSuffix((crossTarget in (NodeAssets,scoped)).value,"systemjs-",scoped,".config.js")
}

private def defineSystemJSPaths(scoped: Scoped) =
Expand All @@ -84,6 +96,7 @@ object SystemJSPlugin extends AutoPlugin {
val file = (systemJSFile in scoped).value

if(lastrun.isEmpty || lastrun.get.needsUpdateComparedToConfig(baseDirectory.value)) {
streams.value.log.info(s"Writing System.js configuration for ${scoped.key.label}")
writeSystemJSFile(
file = file,
paths = (systemJSPaths in scoped).value,
Expand Down

0 comments on commit a2c623a

Please sign in to comment.