diff options
author | Andrew Richards <richards.aj@gmail.com> | 2019-03-02 14:38:40 +0000 |
---|---|---|
committer | Tobias Roeser <le.petit.fou@web.de> | 2019-03-11 07:41:03 +0100 |
commit | c448bd3ef72d06da8ae079058e27f45d5d1be147 (patch) | |
tree | 74181c6461e0ff090203707a9be89b5523d6e0d5 /contrib | |
parent | f5458e25063c880ed13cec1a742f33cbc9a738dd (diff) | |
download | mill-c448bd3ef72d06da8ae079058e27f45d5d1be147.tar.gz mill-c448bd3ef72d06da8ae079058e27f45d5d1be147.tar.bz2 mill-c448bd3ef72d06da8ae079058e27f45d5d1be147.zip |
add static assets including WebJars
Signed-off-by: Jean Helou <jhe@codamens.fr>
Diffstat (limited to 'contrib')
5 files changed, 108 insertions, 5 deletions
diff --git a/contrib/playlib/src/mill/playlib/PlayModule.scala b/contrib/playlib/src/mill/playlib/PlayModule.scala index 2d526101..563bbd1c 100644 --- a/contrib/playlib/src/mill/playlib/PlayModule.scala +++ b/contrib/playlib/src/mill/playlib/PlayModule.scala @@ -1,7 +1,20 @@ package mill package playlib +import java.net.URI +import java.nio.file.FileSystems +import java.nio.file.FileVisitOption +import java.nio.file.FileVisitResult +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.SimpleFileVisitor +import java.nio.file.attribute.BasicFileAttributes +import java.util + +import mill.PathRef import mill.scalalib._ +import scala.collection.JavaConverters._ + import api.Versions trait PlayApiModule extends Dependencies with Router with Server{ @@ -18,6 +31,96 @@ trait PlayApiModule extends Dependencies with Router with Server{ override def sources = T.sources{ millSourcePath } } + /** + * project resources including configuration, webjars and static assets + */ + def resources = T.sources { + super.resources() :+ webJarResources() :+ staticAssets() + } + + /** + * Resource base path of packaged assets (path they will appear in in the jar) + */ + def assetsPath = T{ "public" } + + /** + * Directories to include assets from + */ + def assetSources = T.sources{ millSourcePath / 'public } + + /* + Collected static assets for the project + */ + def staticAssets = T { + val toPath = os.Path(assetsPath(), T.ctx().dest) + assetSources().foreach{ pathRef => + val fromPath = pathRef.path + if (os.isDir(fromPath)) { + os.walk(fromPath).filter(os.isFile(_)).foreach{ p => + os.copy(p, toPath / p.relativeTo(fromPath), createFolders = true) + } + } + } + PathRef(T.ctx().dest) + } + + /** + * webjar dependencies - created from transitive ivy deps + */ + def webJarDeps = T{ + transitiveIvyDeps().filter(_.dep.module.organization == "org.webjars") + } + + /** + * jar files of web jars + */ + def webJars = T{ + Lib.resolveDependencies(repositories, Lib.depToDependency(_, scalaVersion()), webJarDeps()) + } + + /** + * webjar resources extracted from their source jars with version from path removed + */ + def webJarResources = T { + extractWebJars(webJars().toSeq, os.Path(assetsPath(), T.ctx().dest) / 'lib) + PathRef(T.ctx().dest) + } + + def start(args: String*) = T.command{ run(args:_*) } + + + def extractWebJars(jars: Seq[PathRef], webJarBase: os.Path): Unit = { + val prefix = "/META-INF/resources/webjars/" + + jars.foreach{ jarRef => + val uri = s"jar:file:${jarRef.path}" + val env = Map.empty[String,String].asJava + + val zipFs = FileSystems.newFileSystem(URI.create(uri), env) + try { + for(root <- zipFs.getRootDirectories.asScala) { + Files.walkFileTree(root, util.EnumSet.noneOf(classOf[FileVisitOption]), Int.MaxValue, + new SimpleFileVisitor[Path] { + override def visitFile(file: Path, attrs: BasicFileAttributes) = { + if (file.startsWith(prefix)) { + val rel = os.RelPath(file.toString.substring(prefix.length)) + val toFile = webJarBase / os.RelPath(rel.segments(0) +: rel.segments.drop(2), 0) + //println(s"$file -> $toFile") + os.makeDir.all(toFile / os.up) + Files.copy(file, toFile.toNIO) + } + FileVisitResult.CONTINUE + } + } + ) + } + } + finally { + zipFs.close() + } + } + } + } trait PlayModule extends PlayApiModule with Twirl diff --git a/contrib/playlib/src/mill/playlib/Version.scala b/contrib/playlib/src/mill/playlib/Version.scala index 24e11666..3ec77cde 100644 --- a/contrib/playlib/src/mill/playlib/Version.scala +++ b/contrib/playlib/src/mill/playlib/Version.scala @@ -12,7 +12,7 @@ private[playlib] trait Version extends Module{ playVersion().split("\\.").take(2).mkString(".") } - private[playlib] def component(id: String): Task[Dep] = T.task { + private[playlib] def component(id: String) = T.task { ivy"com.typesafe.play::$id::${playVersion()}" } } diff --git a/contrib/playlib/test/src/mill/playlib/PlayModuleTests.scala b/contrib/playlib/test/src/mill/playlib/PlayModuleTests.scala index 398634ce..f6313589 100644 --- a/contrib/playlib/test/src/mill/playlib/PlayModuleTests.scala +++ b/contrib/playlib/test/src/mill/playlib/PlayModuleTests.scala @@ -53,7 +53,7 @@ object PlayModuleTests extends TestSuite { conf.map(_.path.relativeTo(playmulti.millSourcePath).toString()) == Seq("core/conf"), app.map(_.path.relativeTo(playmulti.millSourcePath).toString()) == Seq("core/app"), sources== app, - resources== conf, + resources.map(_.path.relativeTo(playmulti.millSourcePath).toString()).contains("core/conf"), testSources.map(_.path.relativeTo(playmulti.millSourcePath).toString()) == Seq("core/test"), testResources.map(_.path.relativeTo(playmulti.millSourcePath).toString()) == Seq("core/test/resources") ) diff --git a/contrib/playlib/test/src/mill/playlib/PlaySingleApiModuleTests.scala b/contrib/playlib/test/src/mill/playlib/PlaySingleApiModuleTests.scala index df69ca83..0171dbe9 100644 --- a/contrib/playlib/test/src/mill/playlib/PlaySingleApiModuleTests.scala +++ b/contrib/playlib/test/src/mill/playlib/PlaySingleApiModuleTests.scala @@ -50,7 +50,7 @@ object PlaySingleApiModuleTests extends TestSuite { conf.map(_.path.relativeTo(playsingleapi.millSourcePath).toString()) == Seq("conf"), app.map(_.path.relativeTo(playsingleapi.millSourcePath).toString()) == Seq("app"), sources== app, - resources== conf, + resources.map(_.path.relativeTo(playsingleapi.millSourcePath).toString()).contains("conf"), testSources.map(_.path.relativeTo(playsingleapi.millSourcePath).toString()) == Seq("test"), testResources.map(_.path.relativeTo(playsingleapi.millSourcePath).toString()) == Seq("test/resources") ) diff --git a/contrib/playlib/test/src/mill/playlib/PlaySingleModuleTests.scala b/contrib/playlib/test/src/mill/playlib/PlaySingleModuleTests.scala index 82e43930..e139ae16 100644 --- a/contrib/playlib/test/src/mill/playlib/PlaySingleModuleTests.scala +++ b/contrib/playlib/test/src/mill/playlib/PlaySingleModuleTests.scala @@ -49,8 +49,8 @@ object PlaySingleModuleTests extends TestSuite { assert( conf.map(_.path.relativeTo(playsingle.millSourcePath).toString()) == Seq("conf"), app.map(_.path.relativeTo(playsingle.millSourcePath).toString()) == Seq("app"), - sources== app, - resources== conf, + sources == app, + resources.map(_.path.relativeTo(playsingle.millSourcePath).toString()).contains("conf"), testSources.map(_.path.relativeTo(playsingle.millSourcePath).toString()) == Seq("test"), testResources.map(_.path.relativeTo(playsingle.millSourcePath).toString()) == Seq("test/resources") ) |