From f3ec25bccab8ee94ad30d86b56d7cc3c3ca610a4 Mon Sep 17 00:00:00 2001 From: Jim Lawson Date: Tue, 9 Oct 2018 10:16:59 -0700 Subject: Add a filter parameter (default true for all files) to createJar(). Add createJar() filter test. --- main/src/mill/modules/Jvm.scala | 6 ++++-- main/test/src/mill/eval/JavaCompileJarTests.scala | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'main') diff --git a/main/src/mill/modules/Jvm.scala b/main/src/mill/modules/Jvm.scala index f0c0d8a9..b4f7fa94 100644 --- a/main/src/mill/modules/Jvm.scala +++ b/main/src/mill/modules/Jvm.scala @@ -209,7 +209,9 @@ object Jvm { m } - def createJar(inputPaths: Agg[Path], mainClass: Option[String] = None) + def createJar(inputPaths: Agg[Path], + mainClass: Option[String] = None, + fileFilter: (Path, RelPath) => Boolean = (p: Path, r: RelPath) => true) (implicit ctx: Ctx.Dest): PathRef = { val outputPath = ctx.dest / "out.jar" rm(outputPath) @@ -228,7 +230,7 @@ object Jvm { (file, mapping) <- if (p.isFile) Iterator(p -> empty/p.last) else ls.rec(p).filter(_.isFile).map(sub => sub -> sub.relativeTo(p)) - if !seen(mapping) + if !seen(mapping) && fileFilter(p, mapping) } { seen.add(mapping) val entry = new JarEntry(mapping.toString) diff --git a/main/test/src/mill/eval/JavaCompileJarTests.scala b/main/test/src/mill/eval/JavaCompileJarTests.scala index 37e4c119..e6d1c10a 100644 --- a/main/test/src/mill/eval/JavaCompileJarTests.scala +++ b/main/test/src/mill/eval/JavaCompileJarTests.scala @@ -39,6 +39,8 @@ object JavaCompileJarTests extends TestSuite{ def allSources = T{ sourceRoot().flatMap(p => ls.rec(p.path)).map(PathRef(_)) } def classFiles = T{ compileAll(allSources()) } def jar = T{ Jvm.createJar(Loose.Agg(classFiles().path) ++ resourceRoot().map(_.path)) } + // Test createJar() with optional file filter. + def filterJar(fileFilter: (Path, RelPath) => Boolean) = T{ Jvm.createJar(Loose.Agg(classFiles().path) ++ resourceRoot().map(_.path), None, fileFilter) } def run(mainClsName: String) = T.command{ %%('java, "-Duser.language=en", "-cp", classFiles().path, mainClsName) @@ -116,6 +118,13 @@ object JavaCompileJarTests extends TestSuite{ |""".stripMargin assert(jarContents.lines.toSeq == expectedJarContents.lines.toSeq) + // Create the Jar again, but this time, filter out the Foo files. + def noFoos(s: String) = !s.contains("Foo") + val filterFunc = (p: Path, r: RelPath) => noFoos(r.last) + filterJar(filterFunc) + val filteredJarContents = %%('jar, "-tf", evaluator.outPath/'filterJar/'dest/"out.jar")(evaluator.outPath).out.string + assert(filteredJarContents.lines.toSeq == expectedJarContents.lines.filter(noFoos(_)).toSeq) + val executed = %%('java, "-cp", evaluator.outPath/'jar/'dest/"out.jar", "test.Foo")(evaluator.outPath).out.string assert(executed == (31337 + 271828) + System.lineSeparator) -- cgit v1.2.3