Skip to content

Commit

Permalink
Merge pull request #3 from weiwenweiwen/feature/add-universal-stage
Browse files Browse the repository at this point in the history
feat: Universal stage
  • Loading branch information
hoangmaihuy authored Jan 3, 2024
2 parents 28dc2b5 + 356ff94 commit 73d6d98
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ hs_err_pid*
.idea
.vscode
.bsp
out
.bloop
.metals
out
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -60,6 +61,7 @@ object itest extends MillIntegrationTestModule {

override def testInvocations = Seq(
PathRef(testBase / "example") -> Seq(
TestInvocation.Targets(Seq("universalStage")),
TestInvocation.Targets(Seq("universalPackage"))
)
)
Expand Down
2 changes: 2 additions & 0 deletions itest/src/example/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -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") }

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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)
}

}

0 comments on commit 73d6d98

Please sign in to comment.