Skip to content

Commit 4cb6bd7

Browse files
committed
Add docker package logging and generate bootstrap file in temp directory for docker
*Fix building docker image for scala.js
1 parent cc67ab6 commit 4cb6bd7

File tree

4 files changed

+52
-32
lines changed

4 files changed

+52
-32
lines changed

modules/build/src/main/scala/scala/build/options/BuildOptions.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ final case class BuildOptions(
214214

215215
// FIXME We'll probably need more refined rules if we start to support extra Scala.JS or Scala Native specific types
216216
def packageTypeOpt: Option[PackageType] =
217-
if (scalaJsOptions.enable) Some(PackageType.Js)
217+
if (packageOptions.isDockerEnabled) Some(PackageType.Docker)
218+
else if (scalaJsOptions.enable) Some(PackageType.Js)
218219
else if (scalaNativeOptions.enable) Some(PackageType.Native)
219220
else packageOptions.packageTypeOpt
220221

modules/build/src/main/scala/scala/build/options/PackageType.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ object PackageType {
1818
case object Native extends PackageType {
1919
override def runnable = true
2020
}
21+
case object Docker extends PackageType
2122
case object Debian extends NativePackagerType
2223
case object Dmg extends NativePackagerType
2324
case object Pkg extends NativePackagerType

modules/cli/src/main/scala/scala/cli/commands/Package.scala

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -182,44 +182,44 @@ object Package extends ScalaCommand[PackageOptions] {
182182

183183
lazy val debianSettings = DebianSettings(
184184
shared = sharedSettings,
185-
maintainer = packageOptions.maintainer.mandatory("maintainer", "debian"),
186-
description = packageOptions.description.mandatory("description", "debian"),
185+
maintainer = packageOptions.maintainer.mandatory("--maintainer", "debian"),
186+
description = packageOptions.description.mandatory("--description", "debian"),
187187
debianConflicts = packageOptions.debianOptions.conflicts,
188188
debianDependencies = packageOptions.debianOptions.dependencies,
189189
architecture = packageOptions.debianOptions.architecture.mandatory(
190-
"deb architecture",
190+
"--deb-architecture",
191191
"debian"
192192
)
193193
)
194194

195195
lazy val macOSSettings = MacOSSettings(
196196
shared = sharedSettings,
197197
identifier =
198-
packageOptions.macOSidentifier.mandatory("identifier parameter", "macOs")
198+
packageOptions.macOSidentifier.mandatory("--identifier-parameter", "macOs")
199199
)
200200

201201
lazy val redHatSettings = RedHatSettings(
202202
shared = sharedSettings,
203-
description = packageOptions.description.mandatory("description", "redHat"),
203+
description = packageOptions.description.mandatory("--description", "redHat"),
204204
license =
205-
packageOptions.redHatOptions.license.mandatory("license", "redHat"),
205+
packageOptions.redHatOptions.license.mandatory("--license", "redHat"),
206206
release =
207-
packageOptions.redHatOptions.release.mandatory("release", "redHat"),
207+
packageOptions.redHatOptions.release.mandatory("--release", "redHat"),
208208
rpmArchitecture = packageOptions.redHatOptions.architecture.mandatory(
209-
"rpm architecture",
209+
"--rpm-architecture",
210210
"redHat"
211211
)
212212
)
213213

214214
lazy val windowsSettings = WindowsSettings(
215215
shared = sharedSettings,
216-
maintainer = packageOptions.maintainer.mandatory("maintainer", "windows"),
216+
maintainer = packageOptions.maintainer.mandatory("--maintainer", "windows"),
217217
licencePath = packageOptions.windowsOptions.licensePath.mandatory(
218-
"licence path",
218+
"--licence-path",
219219
"windows"
220220
),
221221
productName = packageOptions.windowsOptions.productName.mandatory(
222-
"product name",
222+
"--product-name",
223223
"windows"
224224
),
225225
exitDialog = packageOptions.windowsOptions.exitDialog,
@@ -246,29 +246,45 @@ object Package extends ScalaCommand[PackageOptions] {
246246
imageResizerOpt = imageResizerOpt
247247
).build()
248248
}
249-
}
249+
case PackageType.Docker =>
250+
val exec = if (build.options.scalaJsOptions.enable) "node" else "sh"
251+
val from = build.options.packageOptions.dockerOptions.from match {
252+
case Some(baseImage) => baseImage
253+
case None => if (build.options.scalaJsOptions.enable) "node" else "openjdk:8-jre-slim"
254+
}
255+
val repository = build.options.packageOptions.dockerOptions.imageRepository.mandatory(
256+
"--docker-image-repository",
257+
"docker"
258+
)
259+
val tag = build.options.packageOptions.dockerOptions.imageTag.getOrElse("latest")
260+
261+
val dockerSettings = DockerSettings(
262+
from = from,
263+
registry = build.options.packageOptions.dockerOptions.imageRegistry,
264+
repository = repository,
265+
tag = Some(tag),
266+
exec = exec
267+
)
250268

251-
// build docker image
252-
if (build.options.packageOptions.isDockerEnabled) {
269+
val appPath = os.temp.dir(prefix = "scala-cli-docker") / "app"
270+
if (build.options.scalaJsOptions.enable) {
271+
val linkerConfig = build.options.scalaJsOptions.linkerConfig
272+
linkJs(build, appPath, Some(mainClass()), addTestInitializer = false, linkerConfig)
273+
}
274+
else {
275+
bootstrap(build, appPath, mainClass(), () => alreadyExistsCheck())
276+
}
253277

254-
val exec = if (build.options.scalaJsOptions.enable) "node" else "sh"
255-
val from = build.options.packageOptions.dockerOptions.from match {
256-
case Some(baseImage) => baseImage
257-
case None => if (build.options.scalaJsOptions.enable) "node" else "adoptopenjdk/openjdk8"
258-
}
278+
logger.message(
279+
"Started building docker image with your application, it would take some time"
280+
)
259281

260-
val dockerSettings = DockerSettings(
261-
from = from,
262-
registry = build.options.packageOptions.dockerOptions.imageRegistry,
263-
repository = build.options.packageOptions.dockerOptions.imageRepository.mandatory(
264-
"image repository",
265-
"docker"
266-
),
267-
tag = build.options.packageOptions.dockerOptions.imageTag,
268-
exec = exec
269-
)
282+
DockerPackage(appPath, dockerSettings).build()
270283

271-
DockerPackage(destPath, dockerSettings).build()
284+
logger.message(
285+
"Built docker image, run it with" + System.lineSeparator() +
286+
s" docker run $repository:$tag"
287+
)
272288
}
273289

274290
logger.message {
@@ -278,6 +294,8 @@ object Package extends ScalaCommand[PackageOptions] {
278294
else if (packageType == PackageType.Js)
279295
s"Wrote $dest, run it with" + System.lineSeparator() +
280296
" node " + printableDest
297+
else if (packageType == PackageType.Docker)
298+
"Wrote docker image"
281299
else
282300
s"Wrote $dest"
283301
}

modules/cli/src/main/scala/scala/cli/commands/PackagerOptions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ final case class PackagerOptions(
8686
@HelpMessage(
8787
"The image repository"
8888
)
89-
dockerImageRepository: Option[String] = None,
89+
dockerImageRepository: Option[String] = Some("latest"),
9090
@Group("Docker")
9191
@HelpMessage(
9292
"The image tag, the default tag is latest"

0 commit comments

Comments
 (0)