diff --git a/.gitignore b/.gitignore index f7023a3..23a8bb0 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,6 @@ hs_err_pid* .idea .vscode .bsp -out \ No newline at end of file +.bloop +.metals +out diff --git a/README.md b/README.md index fc8ec81..5354535 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Universal packaging for Java application, ported from [sbt-native-packager](https://github.com/sbt/sbt-native-packager). -Currently, `mill-universal-packager` only supports universal `zip` archive with Bash start script. +Currently, `mill-universal-packager` only supports universal `stage` with Bash start script and the corresponding `zip` archive. ## Usage @@ -17,6 +17,14 @@ object example extends JavaAppPackagingModule { } ``` +Run `universalStage` command + +```bash +> mill example.universalStage +``` + +The output directory can be found at `universalStage.dest` in Mill's `out` folder. + Run `universalPackage` command ```bash diff --git a/build.sc b/build.sc index d2c4529..fce99d1 100644 --- a/build.sc +++ b/build.sc @@ -48,6 +48,7 @@ object `mill-universal-packager` extends ScalaModule with CiReleaseModule { override def compileIvyDeps = super.compileIvyDeps() ++ Agg( ivy"com.lihaoyi::mill-scalalib:${millVersion()}" ) + } object itest extends MillIntegrationTestModule { @@ -60,6 +61,7 @@ object itest extends MillIntegrationTestModule { override def testInvocations = Seq( PathRef(testBase / "example") -> Seq( + TestInvocation.Targets(Seq("universalStage")), TestInvocation.Targets(Seq("universalPackage")) ) ) diff --git a/itest/src/example/build.sc b/itest/src/example/build.sc index 696a40d..3975922 100644 --- a/itest/src/example/build.sc +++ b/itest/src/example/build.sc @@ -22,4 +22,6 @@ object example extends RootModule with ScalaModule with JavaAppPackagingModule { ivy"dev.zio::zio:2.0.19" ) + override def topLevelDirectory = T { Some("example-app") } + } diff --git a/mill-universal-packager/src/io/github/hoangmaihuy/mill/packager/universal/UniversalPackagerModule.scala b/mill-universal-packager/src/io/github/hoangmaihuy/mill/packager/universal/UniversalPackagerModule.scala index f1b146b..5896df4 100644 --- a/mill-universal-packager/src/io/github/hoangmaihuy/mill/packager/universal/UniversalPackagerModule.scala +++ b/mill-universal-packager/src/io/github/hoangmaihuy/mill/packager/universal/UniversalPackagerModule.scala @@ -2,6 +2,7 @@ package io.github.hoangmaihuy.mill.packager.universal import mill._ import io.github.hoangmaihuy.mill.packager._ +import os.Path trait UniversalPackagerModule extends PackagerModule { @@ -22,15 +23,39 @@ trait UniversalPackagerModule extends PackagerModule { } } + /** Create an zip package file. The task will run universalStage() first, and then zip the stage output directory as + * the result. + */ def universalPackage: T[PathRef] = T { + val stagePath = universalStage().path + val m2 = os.walk(stagePath).map { case p => + val targetSubPath = p.relativeTo(stagePath).asSubPath + topLevelDirectory() match { + case None => p -> targetSubPath + case Some(dir) => p -> os.sub / dir / targetSubPath + } + } val zip = T.dest / (packageName() + ".zip") - val mappings = universalMappings() - // add top level directory if defined - val m2 = topLevelDirectory().map { dir => - mappings.map { case (f, p) => f -> (os.sub / dir / p) } - } getOrElse (mappings) ZipHelper.zip(m2, zip) PathRef(zip) } + /** Create a local directory with all the files laid out as they would be in the final distribution. + * + * Note: the output "stage" directory should always have no top level directory. + */ + def universalStage: T[PathRef] = T { + universalMappings().foreach { case (f, p) => + os.copy( + from = f, + to = Path(p, T.dest), + createFolders = true, + followLinks = true, + replaceExisting = true, + mergeFolders = true + ) + } + PathRef(T.dest) + } + }