diff options
author | Christopher Vogt <oss.nsp@cvogt.org> | 2016-06-17 22:38:18 -0400 |
---|---|---|
committer | Christopher Vogt <oss.nsp@cvogt.org> | 2016-06-19 20:57:54 -0400 |
commit | 47cc41ecaa2eaf5677fcb1794db7bbba6533b559 (patch) | |
tree | 8708b8cd5954dfa03f2478b77440340c7664972b | |
parent | 12d5bad267b5759e941b7101578808aeb6351e66 (diff) | |
download | cbt-47cc41ecaa2eaf5677fcb1794db7bbba6533b559.tar.gz cbt-47cc41ecaa2eaf5677fcb1794db7bbba6533b559.tar.bz2 cbt-47cc41ecaa2eaf5677fcb1794db7bbba6533b559.zip |
refactor/simplify scalaJs plugin and example
- Restructure code as 1 library case class and 1 Build mixin trait. A library is generally easier to understand and could be re-used independently. 1 trait seems simpler than several here.
- Let not the plugin create the multi-project build, but the user project manually. I think while this adds some minor code overhead it is much simpler to understand. Fewer tasks and other moving parts needed.
- Remove verbose nested sbt-style folder structure. It's simpler without it :).
17 files changed, 122 insertions, 145 deletions
diff --git a/examples/build-scalajs/build/build/build.scala b/examples/build-scalajs/build/build/build.scala deleted file mode 100644 index 19593f9..0000000 --- a/examples/build-scalajs/build/build/build.scala +++ /dev/null @@ -1,7 +0,0 @@ -import cbt._ -class Build(val context: Context) extends BuildBuild{ - - override def dependencies = - super.dependencies :+ - DirectoryDependency(new File(context.cbtHome + "/plugins/scalajs")) -} diff --git a/examples/build-scalajs/README.md b/examples/scalajs-react-example/README.md index 74e015a..74e015a 100644 --- a/examples/build-scalajs/README.md +++ b/examples/scalajs-react-example/README.md diff --git a/examples/build-scalajs/js/src/main/scala/App.scala b/examples/scalajs-react-example/js/App.scala index 2682936..0cd170e 100644 --- a/examples/build-scalajs/js/src/main/scala/App.scala +++ b/examples/scalajs-react-example/js/App.scala @@ -3,19 +3,12 @@ package prototype import japgolly.scalajs.react.ReactDOM import org.scalajs.dom -import scala.scalajs.js import scala.scalajs.js.JSApp import scala.scalajs.js.annotation.JSExport -import scalajs.js.Dynamic.{global => g} -/** - * Created by katrin on 2016-04-10. - */ @JSExport("App") object App extends JSApp { - def main(): Unit = { - val doc = dom.document ReactDOM.render(Pictures.PictureComponent(), doc.getElementById("main")) } diff --git a/examples/build-scalajs/js/src/main/scala/Pictures.scala b/examples/scalajs-react-example/js/Pictures.scala index 88329ee..db1d7ef 100644 --- a/examples/build-scalajs/js/src/main/scala/Pictures.scala +++ b/examples/scalajs-react-example/js/Pictures.scala @@ -8,9 +8,6 @@ import japgolly.scalajs.react.vdom.all._ import scala.scalajs.js.JSON -/** - * Created by katrin on 2016-04-10. - */ object Pictures { case class State(pictures: List[Picture], favourites: List[Picture]) diff --git a/examples/build-scalajs/build/build.scala b/examples/scalajs-react-example/js/build/build.scala index 89ad030..29f1c73 100644 --- a/examples/build-scalajs/build/build.scala +++ b/examples/scalajs-react-example/js/build/build.scala @@ -1,9 +1,11 @@ import cbt._ -import java.io.File -class Build( val context: Context ) extends ScalaJsBuild { - +class Build(val context: Context) extends ScalaJsBuild{ override val projectName = "my-project" + override def sources = super.sources ++ Seq( + projectDirectory.getParentFile ++ "/shared" + ) + override def dependencies = ( super.dependencies ++ Resolver( mavenCentral ).bind( @@ -14,11 +16,7 @@ class Build( val context: Context ) extends ScalaJsBuild { ) ) - /* ++ some JVM only dependencies */ - override def jvmDependencies = Seq.empty - - override def fastOptOutput = { - projectDirectory.getAbsolutePath + "/server/public/" + new File(super.fastOptOutput).getName + override protected def fastOptJSFile = { + projectDirectory.getParentFile ++ "/server/public" ++ ("/"++super.fastOptJSFile.getName) } } - diff --git a/examples/scalajs-react-example/js/build/build/build.scala b/examples/scalajs-react-example/js/build/build/build.scala new file mode 100644 index 0000000..b30e005 --- /dev/null +++ b/examples/scalajs-react-example/js/build/build/build.scala @@ -0,0 +1,4 @@ +import cbt._ +class Build(val context: Context) extends BuildBuild{ + override def dependencies = super.dependencies :+ plugins.scalaJs +} diff --git a/examples/scalajs-react-example/jvm/build/build.scala b/examples/scalajs-react-example/jvm/build/build.scala new file mode 100644 index 0000000..327d705 --- /dev/null +++ b/examples/scalajs-react-example/jvm/build/build.scala @@ -0,0 +1,6 @@ +import cbt._ +class Build(val context: Context) extends BaseBuild{ + override def sources = super.sources ++ Seq( + projectDirectory.getParentFile ++ "/shared" + ) +} diff --git a/examples/build-scalajs/server/app.js b/examples/scalajs-react-example/server/app.js index 620c26a..620c26a 100644 --- a/examples/build-scalajs/server/app.js +++ b/examples/scalajs-react-example/server/app.js diff --git a/examples/build-scalajs/server/package.json b/examples/scalajs-react-example/server/package.json index d20ec98..d20ec98 100644 --- a/examples/build-scalajs/server/package.json +++ b/examples/scalajs-react-example/server/package.json diff --git a/examples/build-scalajs/server/public/index.html b/examples/scalajs-react-example/server/public/index.html index 08de20d..08de20d 100644 --- a/examples/build-scalajs/server/public/index.html +++ b/examples/scalajs-react-example/server/public/index.html diff --git a/examples/build-scalajs/shared/src/main/scala/prototype/Picture.scala b/examples/scalajs-react-example/shared/Picture.scala index dbb985a..dbb985a 100644 --- a/examples/build-scalajs/shared/src/main/scala/prototype/Picture.scala +++ b/examples/scalajs-react-example/shared/Picture.scala diff --git a/plugins/scalajs/ScalaJs.scala b/plugins/scalajs/ScalaJs.scala deleted file mode 100644 index 8b55044..0000000 --- a/plugins/scalajs/ScalaJs.scala +++ /dev/null @@ -1,93 +0,0 @@ -import java.io.File - -import cbt._ - - -trait ScalaJsSbtDependencyDsl extends SbtDependencyDsl { self: ScalaJsBuild => - - //Note: We make same assumption about scala version. - //In order to be able to choose different scala version, one has to use %. - implicit class ScalaJsDependencyBuilder(groupId: String){ - def %%%(artifactId: String) = new DependencyBuilder2( - groupId, artifactId + artifactIdSuffix, Some(scalaMajorVersion)) - } -} - -trait ScalaJsBuild extends BaseBuild with ScalaJsSbtDependencyDsl with ScalaJsInformation { outer => - - def sharedFolder = projectDirectory ++ "/shared" - def jvmFolder = projectDirectory ++ "/jvm" - def jsFolder = projectDirectory ++ "/js" - - private lazy val jvmBuild = new BasicBuild(outer.context){ - override def sources = Seq(sharedFolder ++ "/src/main/scala", jvmFolder ++ "/src/main/scala") - override def target = jvmFolder ++ "/target" - override def dependencies = outer.dependencies ++ jvmDependencies - } - private lazy val jsBuild = new BasicBuild(outer.context){ - override def sources = Seq(sharedFolder ++ "/src/main/scala", jsFolder ++ "/src/main/scala") - override def target = jsFolder ++ "/target" - override def dependencies = outer.dependencies :+ scalaJsLibDep - override def scalacOptions = super.scalacOptions ++ - Seq(s"-Xplugin:${scalaJsCompilerDep.jar.getAbsolutePath}", "-Xplugin-require:scalajs") - } - - override def triggerLoopFiles = super.triggerLoopFiles ++ (jvmBuild.sources ++ jsBuild.sources).distinct - - def jvmDependencies = Seq.empty[Dependency] - //TODO: implement - def jsDependencies = Seq.empty[Dependency] - def jvmCompile: Option[File] = jvmBuild.compile - def jsCompile: Option[File] = jsBuild.compile - override def compile = { - jvmCompile - jsCompile - } - - trait JsOutputMode { - def option: String - def fileSuffix: String - } - case object FastOptJS extends JsOutputMode{ - override val option = "--fastOpt" - override val fileSuffix = "fastopt" - } - case object FullOptJS extends JsOutputMode{ - override val option = "--fullOpt" - override val fileSuffix = "fullopt" - } - - private def output(mode: JsOutputMode) = s"${jsBuild.target.getAbsolutePath}/$projectName-${mode.fileSuffix}.js" - - private def link(mode: JsOutputMode, outputPath: String) = { - lib.runMain( - "org.scalajs.cli.Scalajsld", - Seq( - mode.option, - "--sourceMap", - "--stdlib", s"${scalaJsLibDep.jar.getAbsolutePath}", - "--output", outputPath - ) ++ scalaJsOptions ++ Seq( - jsBuild.target.getAbsolutePath) ++ - jsBuild.dependencies.collect{case d: BoundMavenDependency => d.jar.getAbsolutePath}, - scalaJsCliDep.classLoader(jsBuild.context.classLoaderCache) - ) - } - - def scalaJsOptions: Seq[String] = Seq() - - def fastOptJS = { - compile - link(FastOptJS, fastOptOutput, scalaJsOptions) - } - def fullOptJS = { - compile - link(FullOptJS, fastOptOutput, scalaJsOptions) - } - def fastOptOutput: String = output(FastOptJS) - def fullOptOutput: String = output(FullOptJS) -} - - - - diff --git a/plugins/scalajs/ScalaJsBuild.scala b/plugins/scalajs/ScalaJsBuild.scala new file mode 100644 index 0000000..9374f66 --- /dev/null +++ b/plugins/scalajs/ScalaJsBuild.scala @@ -0,0 +1,52 @@ +package cbt +import java.io.File +import java.net.URL + +trait ScalaJsBuild extends BaseBuild { + final protected val scalaJsLib = ScalaJsLib( + scalaJsVersion, + scalaVersion, + context.cbtHasChanged, + context.classLoaderCache, + context.paths.mavenCache + ) + import scalaJsLib.{link => _,_} + + def scalaJsVersion = "0.6.8" + final protected val scalaJsMajorVersion: String = lib.libMajorVersion(scalaJsVersion) + final protected val artifactIdSuffix = s"_sjs$scalaJsMajorVersion" + + override def dependencies = super.dependencies :+ scalaJsLibraryDependency + override def scalacOptions = super.scalacOptions ++ scalaJsLib.scalacOptions + + /** Note: We make same assumption about scala version. + In order to be able to choose different scala version, one has to use %. */ + implicit class ScalaJsDependencyBuilder(groupId: String){ + def %%%(artifactId: String) = new DependencyBuilder2( + groupId, artifactId + artifactIdSuffix, Some(scalaMajorVersion)) + } + + private def link(mode: ScalaJsOutputMode, outputPath: File) = scalaJsLib.link( + mode, outputPath, scalaJsOptions, + target +: dependencies.collect{case d: BoundMavenDependency => d.jar} + ) + + def scalaJsOptions: Seq[String] = Seq() + def scalaJsOptionsFastOpt: Seq[String] = scalaJsOptions + def scalaJsOptionsFullOpt: Seq[String] = scalaJsOptions + + private def output(mode: ScalaJsOutputMode) = target ++ s"/$projectName-${mode.fileSuffix}.js" + protected def fastOptJSFile: File = output(FastOptJS) + protected def fullOptJSFile: File = output(FullOptJS) + + def fastOptJS = { + compile + link(FastOptJS, fastOptJSFile) + fastOptJSFile + } + def fullOptJS = { + compile + link(FullOptJS, fullOptJSFile) + fullOptJSFile + } +} diff --git a/plugins/scalajs/ScalaJsInformation.scala b/plugins/scalajs/ScalaJsInformation.scala deleted file mode 100644 index ada3baf..0000000 --- a/plugins/scalajs/ScalaJsInformation.scala +++ /dev/null @@ -1,25 +0,0 @@ -import cbt._ - -trait ScalaJsInformation extends BaseBuild { outer => - - val sjsVersion = "0.6.8" - final private val sjsMajorVersion: String = lib.libMajorVersion(sjsVersion) - final protected val artifactIdSuffix = s"_sjs$sjsMajorVersion" - - final protected val scalaJsCompilerDep = - Resolver( mavenCentral ).bindOne( - // Has to be full Scala version because the compiler is incompatible between versions - MavenDependency("org.scala-js", "scalajs-compiler_2.11.8", sjsVersion) - ) - - final protected val scalaJsLibDep = - Resolver( mavenCentral ).bindOne( - ScalaDependency("org.scala-js", "scalajs-library", sjsVersion) - ) - - final protected val scalaJsCliDep = - Resolver( mavenCentral ).bindOne( - ScalaDependency("org.scala-js", "scalajs-cli", sjsVersion) - ) -} - diff --git a/plugins/scalajs/ScalaJsLib.scala b/plugins/scalajs/ScalaJsLib.scala new file mode 100644 index 0000000..0355850 --- /dev/null +++ b/plugins/scalajs/ScalaJsLib.scala @@ -0,0 +1,51 @@ +package cbt +import java.io.File + +case class ScalaJsLib( + scalaJsVersion: String, scalaVersion: String, + cbtHasChanged: Boolean, classLoaderCache: ClassLoaderCache, mavenCache: File +)(implicit logger: Logger){ + sealed trait ScalaJsOutputMode { + def option: String + def fileSuffix: String + } + case object FastOptJS extends ScalaJsOutputMode{ + override val option = "--fastOpt" + override val fileSuffix = "fastopt" + } + case object FullOptJS extends ScalaJsOutputMode{ + override val option = "--fullOpt" + override val fileSuffix = "fullopt" + } + + val lib = new Lib(logger) + def dep(artifactId: String) = MavenResolver( cbtHasChanged, mavenCache, mavenCentral ).bindOne( + MavenDependency("org.scala-js", artifactId, scalaJsVersion) + ) + + def link( + mode: ScalaJsOutputMode, outputPath: File, + scalaJsOptions: Seq[String], entriesToLink: Seq[File] + ) = { + val scalaJsCliDep = dep( "scalajs-cli_"++lib.libMajorVersion(scalaVersion) ) + lib.runMain( + "org.scalajs.cli.Scalajsld", + Seq( + mode.option, + "--sourceMap", + "--stdlib", s"${scalaJsLibraryDependency.jar.getAbsolutePath}", + "--output", outputPath.string + ) ++ scalaJsOptions ++ entriesToLink.map(_.getAbsolutePath), + scalaJsCliDep.classLoader(classLoaderCache) + ) + } + + val scalaJsLibraryDependency = dep( "scalajs-library_"++lib.libMajorVersion(scalaVersion) ) + + // Has to be full Scala version because the compiler is incompatible between versions + val scalaJsCompilerDependency = dep( "scalajs-compiler_"++scalaVersion ) + val scalacOptions = Seq( + "-Xplugin:" ++ scalaJsCompilerDependency.jar.string, + "-Xplugin-require:scalajs" + ) +} diff --git a/stage1/cbt.scala b/stage1/cbt.scala index 78015b6..8564695 100644 --- a/stage1/cbt.scala +++ b/stage1/cbt.scala @@ -16,7 +16,7 @@ object `package`{ implicit class FileExtensionMethods( file: File ){ def ++( s: String ): File = { if(s endsWith "/") throw new Exception( - """Trying to append a String that ends in "/" to a File would loose it. Use .stripSuffix("/") if you need to.""" + """Trying to append a String that ends in "/" to a File would loose the trailing "/". Use .stripSuffix("/") if you need to.""" ) new File( file.toString ++ s ) } diff --git a/stage2/BuildBuild.scala b/stage2/BuildBuild.scala index db5374c..4d9c869 100644 --- a/stage2/BuildBuild.scala +++ b/stage2/BuildBuild.scala @@ -10,6 +10,7 @@ trait BuildBuild extends BaseBuild{ object plugins{ final val scalaTest = DirectoryDependency( managedContext.cbtHome ++ "/plugins/scalatest" ) final val sbtLayout = DirectoryDependency( managedContext.cbtHome ++ "/plugins/sbt_layout" ) + final val scalaJs = DirectoryDependency( managedContext.cbtHome ++ "/plugins/scalajs" ) } override def dependencies = |