Skip to content

Commit cad549f

Browse files
authored
Merge pull request #1213 from wleczny/output-option-for-using-directives
Define `output` option for `package` command with using directives
2 parents 4c6d1e2 + 7f9b020 commit cad549f

File tree

7 files changed

+66
-7
lines changed

7 files changed

+66
-7
lines changed

modules/build/src/test/scala/scala/build/tests/PackagingUsingDirectiveTests.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,21 @@ class PackagingUsingDirectiveTests extends munit.FunSuite {
3434
}
3535
}
3636

37+
test("output") {
38+
val output = "foo"
39+
val inputs = TestInputs(
40+
os.rel / "Bar.scala" ->
41+
s"""//> using packaging.output "$output"
42+
|def hello() = println("hello")
43+
|""".stripMargin
44+
)
45+
inputs.withLoadedBuild(buildOptions, buildThreads, bloopConfig) { (_, _, maybeBuild) =>
46+
val maybePackageOutput = maybeBuild.options.notForBloopOptions.packageOptions.output
47+
val packageOutputString = maybePackageOutput.getOrElse("None")
48+
val index = packageOutputString.lastIndexOf('/')
49+
val packageName = packageOutputString.drop(index + 1)
50+
expect(packageName == output)
51+
}
52+
}
53+
3754
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ object Package extends ScalaCommand[PackageOptions] {
248248
case _ => "app"
249249
}
250250

251-
val dest = outputOpt
251+
val packageOutput = build.options.notForBloopOptions.packageOptions.output
252+
val dest = outputOpt.orElse(packageOutput)
252253
.orElse {
253254
build.sources.defaultMainClass
254255
.map(n => n.drop(n.lastIndexOf('.') + 1))

modules/cli/src/main/scala/scala/cli/commands/util/PackageOptionsUtil.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ object PackageOptionsUtil {
5858
launcherApp = packager.launcherApp,
5959
maintainer = packager.maintainer,
6060
description = packager.description,
61+
output = output,
6162
packageTypeOpt = packageTypeOpt,
6263
logoPath = packager.logoPath.map(os.Path(_, os.pwd)),
6364
macOSidentifier = packager.identifier,

modules/directives/src/main/scala/scala/build/preprocessing/directives/UsingPackagingDirectiveHandler.scala

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,26 @@ case object UsingPackagingDirectiveHandler extends UsingDirectiveHandler {
1818
def description = "Set parameters for packaging"
1919
def usage =
2020
"""using packaging.packageType [package type]
21+
|using packaging.output [destination path]
2122
|using packaging.provided [module]
2223
|""".stripMargin
2324

2425
override def usageMd =
25-
"""`using packaging.packageType `"package type"
26+
"""`//> using packaging.packageType `"package type"
27+
|
28+
|`//> using packaging.output `"destination path"
29+
|
2630
|""".stripMargin
2731

2832
override def examples = Seq(
29-
"using packaging.packageType \"assembly\"",
30-
"using packaging.provided \"org.apache.spark::spark-sql\""
33+
"//> using packaging.packageType \"assembly\"",
34+
"//> using packaging.output \"foo\"",
35+
"//> using packaging.provided \"org.apache.spark::spark-sql\""
3136
)
3237

3338
def keys = Seq(
3439
"packageType",
40+
"output",
3541
"provided"
3642
).map("packaging." + _)
3743

@@ -72,6 +78,10 @@ case object UsingPackagingDirectiveHandler extends UsingDirectiveHandler {
7278
)
7379
))
7480
}
81+
case "packaging.output" =>
82+
val value0 = value(getValue)
83+
val path = os.Path(value0.value, os.pwd)
84+
PackageOptions(output = Option(path.toString))
7585
case "packaging.provided" =>
7686
val values0 = value(getValues)
7787
val modules = value {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ final case class PackageOptions(
99
launcherApp: Option[String] = None,
1010
maintainer: Option[String] = None,
1111
description: Option[String] = None,
12+
output: Option[String] = None,
1213
packageTypeOpt: Option[PackageType] = None,
1314
logoPath: Option[os.Path] = None,
1415
macOSidentifier: Option[String] = None,

website/docs/commands/package.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,27 @@ scala-cli package --msi --output path.msi Hello.scala
362362
* launcher-app
363363
* exit-dialog
364364
* logo-path
365+
366+
## Using directives
367+
368+
Instead of passing the `package` options directly from bash, it is possible to pass some of them with [using directives](/docs/guides/using-directives).
369+
370+
### packaging.packageType
371+
372+
This using directive allows to define the type of the package generated by the `package` command. For example:
373+
374+
```
375+
//> using packaging.packageType "assembly"
376+
```
377+
378+
Available types: `assembly`, `raw-assembly`, `bootstrap`, `library`, `source`, `doc`, `spark`, `js`, `native`, `docker`, `graalvm`, `deb`, `dmg`, `pkg`, `rpm`, `msi`.
379+
380+
### packaging.output
381+
382+
This using directive allows to define the destination path of the package generated by the `package` command. For example:
383+
384+
```
385+
//> using packaging.output "foo"
386+
```
387+
388+
This using directive allows to create a package named `foo` inside the current directory.

website/docs/reference/directives.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,18 @@ Specify default main class
109109

110110
Set parameters for packaging
111111

112-
`using packaging.packageType `"package type"
112+
`//> using packaging.packageType `"package type"
113+
114+
`//> using packaging.output `"destination path"
115+
113116

114117

115118
#### Examples
116-
`using packaging.packageType "assembly"`
119+
`//> using packaging.packageType "assembly"`
120+
121+
`//> using packaging.output "foo"`
117122

118-
`using packaging.provided "org.apache.spark::spark-sql"`
123+
`//> using packaging.provided "org.apache.spark::spark-sql"`
119124

120125
### Platform
121126

0 commit comments

Comments
 (0)