Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,8 @@ object Package extends ScalaCommand[PackageOptions] with BuildCommandHelpers {
repository = repository,
tag = Some(tag),
exec = exec,
dockerExecutable = None
dockerExecutable = None,
extraDirectories = packageOptions.dockerOptions.extraDirectories.map(_.toNIO)
)

val appPath = os.temp.dir(prefix = "scala-cli-docker") / "app"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ final case class PackageOptions(
imageRepository = packager.dockerImageRepository,
imageTag = packager.dockerImageTag,
cmd = packager.dockerCmd,
isDockerEnabled = Some(docker)
isDockerEnabled = Some(docker),
extraDirectories = packager.dockerExtraDirectories.map(os.Path(_, os.pwd))
),
nativeImageOptions = NativeImageOptions(
graalvmJvmId = packager.graalvmJvmId.map(_.trim).filter(_.nonEmpty),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ final case class PackagerOptions(
)
@Tag(tags.restricted)
dockerCmd: Option[String] = None,

@Group(HelpGroup.Docker.toString)
@HelpMessage("Extra directories to be added to the docker image")
@Tag(tags.restricted)
dockerExtraDirectories: List[String] = Nil,

@Group(HelpGroup.NativeImage.toString)
@HelpMessage(s"GraalVM Java major version to use to build GraalVM native images (${Constants.defaultGraalVMJavaVersion} by default)")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package scala.build.errors

class WrongDirectoryPathError(cause: Throwable) extends BuildException(
message = s"""The directory path argument in the using directives at could not be found!
|${cause.getLocalizedMessage}""".stripMargin,
cause = cause
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import scala.build.errors.{
BuildException,
CompositeBuildException,
MalformedInputError,
ModuleFormatError
ModuleFormatError,
WrongDirectoryPathError
}
import scala.build.options.*
import scala.build.options.packaging.{DockerOptions, NativeImageOptions}
import scala.cli.commands.SpecificationLevel
import scala.util.Try

@DirectiveGroupName("Packaging")
@DirectivePrefix("packaging.")
Expand All @@ -28,6 +30,10 @@ import scala.cli.commands.SpecificationLevel
@DirectiveExamples("//> using packaging.dockerImageRepository scala-cli")
@DirectiveExamples("//> using packaging.dockerCmd sh")
@DirectiveExamples("//> using packaging.dockerCmd node")
@DirectiveExamples(
"//> using packaging.dockerExtraDirectories path/to/directory1 path/to/directory2"
)
@DirectiveExamples("//> using packaging.dockerExtraDirectory path/to/directory")
@DirectiveUsage(
"""using packaging.packageType [package type]
|using packaging.output [destination path]
Expand All @@ -38,6 +44,7 @@ import scala.cli.commands.SpecificationLevel
|using packaging.dockerImageRegistry [image registry]
|using packaging.dockerImageRepository [image repository]
|using packaging.dockerCmd [docker command]
|using packaging.dockerExtraDirectories [directories]
|""".stripMargin,
"""`//> using packaging.packageType` _package-type_
|
Expand All @@ -57,6 +64,9 @@ import scala.cli.commands.SpecificationLevel
|
|`//> using packaging.dockerCmd` _docker-command_
|
|`//> using packaging.dockerExtraDirectories` _directories_
|`//> using packaging.dockerExtraDirectory` _directory_
|
|""".stripMargin
)
@DirectiveDescription("Set parameters for packaging")
Expand All @@ -70,7 +80,10 @@ final case class Packaging(
dockerImageTag: Option[String] = None,
dockerImageRegistry: Option[String] = None,
dockerImageRepository: Option[String] = None,
dockerCmd: Option[String] = None
dockerCmd: Option[String] = None,
@DirectiveName("dockerExtraDirectory")
dockerExtraDirectories: DirectiveValueParser.WithScopePath[List[Positioned[String]]] =
DirectiveValueParser.WithScopePath.empty(Nil)
) extends HasBuildOptions {
def buildOptions: Either[BuildException, BuildOptions] = either {
val maybePackageTypeOpt = packageType
Expand Down Expand Up @@ -110,6 +123,23 @@ final case class Packaging(
.left.map(CompositeBuildException(_))
}

val cwd = dockerExtraDirectories.scopePath
val extraDirectories = value {
dockerExtraDirectories
.value
.map { posPathStr =>
val eitherRootPathOrBuildException =
Directive.osRoot(cwd, posPathStr.positions.headOption)
eitherRootPathOrBuildException.flatMap { root =>
Try(os.Path(posPathStr.value, root))
.toEither
.left.map(new WrongDirectoryPathError(_))
}
}
.sequence
.left.map(CompositeBuildException(_))
}

BuildOptions(
internal = InternalOptions(
keepResolution = provided0.nonEmpty || packageTypeOpt.contains(PackageType.Spark)
Expand All @@ -124,7 +154,8 @@ final case class Packaging(
imageRegistry = dockerImageRegistry,
imageRepository = dockerImageRepository,
imageTag = dockerImageTag,
cmd = dockerCmd
cmd = dockerCmd,
extraDirectories = extraDirectories
),
nativeImageOptions = NativeImageOptions(
graalvmArgs = graalvmArgs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1118,13 +1118,62 @@ abstract class PackageTestDefinitions extends ScalaCliSuite with TestScalaVersio
}
}

if (Properties.isLinux)
def dockerWithExtraDirsTest(): Unit = {
val codePath = os.rel / "src" / "Hello.scala"
val extraFileName = "extraFile.txt"
val extraFileDir = os.rel / "extraDir"
val extraFilePath = extraFileDir / extraFileName
val imageName = "extradir"
val expectedOutput = "hello"
val inputs = TestInputs(
codePath ->
s"""//> using toolkit default
|
|object Smth extends App {
| val content =
| os.walk(os.pwd)
| .filter(os.isFile)
| .filter(_.endsWith(os.rel / "$extraFileName"))
| .headOption
| .map(file => os.read(file).trim())
| .getOrElse("No matching files found")
| println(content)
|}
|""".stripMargin,
extraFilePath -> expectedOutput
)
inputs.fromRoot { root =>
os.proc(
TestUtil.cli,
"--power",
"package",
codePath,
"--docker",
"--docker-image-repository",
imageName,
"--docker-extra-directories",
root / extraFileDir,
"-f"
).call(cwd = root)
val output = os.proc("docker", "run", imageName).call(cwd = root).out.trim()
expect(output == expectedOutput)
}
}

if (Properties.isLinux) {
test("pass java options to docker") {
TestUtil.retryOnCi() {
javaOptionsDockerTest()
}
}

test("pass extra directory to docker") {
TestUtil.retryOnCi() {
dockerWithExtraDirsTest()
}
}
}

test("default values in help") {
TestInputs.empty.fromRoot { root =>
val res = os.proc(TestUtil.cli, "--power", "package", extraOptions, "--help").call(cwd = root)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ final case class DockerOptions(
imageRepository: Option[String] = None,
imageTag: Option[String] = None,
cmd: Option[String] = None,
isDockerEnabled: Option[Boolean] = None
isDockerEnabled: Option[Boolean] = None,
extraDirectories: Seq[os.Path] = Nil
)

object DockerOptions {
Expand Down
2 changes: 1 addition & 1 deletion project/deps/package.mill.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ object Deps {
def maxScalaNativeForTypelevelToolkit = scalaNative04
def maxScalaNativeForScalaPy = scalaNative04
def maxScalaNativeForMillExport = scalaNative05
def scalaPackager = "0.2.0"
def scalaPackager = "0.2.1"
def signingCli = "0.2.11"
def signingCliJvmVersion = Java.defaultJava
def javaSemanticdb = "0.10.0"
Expand Down
4 changes: 4 additions & 0 deletions website/docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,10 @@ The image tag; the default tag is `latest`

Allows to override the executable used to run the application in docker, otherwise it defaults to sh for the JVM platform and node for the JS platform

### `--docker-extra-directories`

Extra directories to be added to the docker image

### `--graalvm-java-version`

GraalVM Java major version to use to build GraalVM native images (17 by default)
Expand Down
7 changes: 7 additions & 0 deletions website/docs/reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ Set parameters for packaging

`//> using packaging.dockerCmd` _docker-command_

`//> using packaging.dockerExtraDirectories` _directories_
`//> using packaging.dockerExtraDirectory` _directory_



#### Examples
Expand All @@ -323,6 +326,10 @@ Set parameters for packaging

`//> using packaging.dockerCmd node`

`//> using packaging.dockerExtraDirectories path/to/directory1 path/to/directory2`

`//> using packaging.dockerExtraDirectory path/to/directory`

### Platform

Set the default platform to Scala.js or Scala Native
Expand Down