Skip to content

Commit 23f6949

Browse files
authored
Merge pull request #3257 from Gedochao/maintenance/deprecate-source
Deprecate the `--source` command line option for the `package` sub-command
2 parents 72c23a5 + 404763c commit 23f6949

File tree

7 files changed

+60
-16
lines changed

7 files changed

+60
-16
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package scala.cli.commands
33
import caseapp.Name
44
import caseapp.core.app.Command
55
import caseapp.core.parser.Parser
6-
import caseapp.core.util.Formatter
6+
import caseapp.core.util.{CaseUtil, Formatter}
77
import caseapp.core.{Arg, Error}
88

99
import scala.build.Logger
1010
import scala.build.input.ScalaCliInvokeData
1111
import scala.build.internal.util.WarningMessages
12-
import scala.build.internals.FeatureType
12+
import scala.build.internals.{ConsoleUtils, FeatureType}
1313
import scala.cli.ScalaCli
1414
import scala.cli.util.ArgHelpers.*
1515

@@ -57,6 +57,16 @@ object RestrictedCommandsParser {
5757
if arg.isExperimental && !shouldSuppressExperimentalWarnings =>
5858
logger.experimentalWarning(passedOption, FeatureType.Option)
5959
r
60+
case (r @ Right(Some(_, arg: Arg, _)), passedOption :: _)
61+
if arg.isDeprecated =>
62+
// TODO implement proper deprecation logic: https://github.com/VirtusLab/scala-cli/issues/3258
63+
arg.deprecatedOptionAliases.find(_ == passedOption)
64+
.foreach { deprecatedAlias =>
65+
logger.message(
66+
s"""[${Console.YELLOW}warn${Console.RESET}] The $deprecatedAlias option alias has been deprecated and may be removed in a future version."""
67+
)
68+
}
69+
r
6070
case (other, _) =>
6171
other
6272
}

modules/cli/src/main/scala/scala/cli/commands/package0/PackageOptions.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@ final case class PackageOptions(
4848
library: Boolean = false,
4949
@Group(HelpGroup.Package.toString)
5050
@HelpMessage("Generate a source JAR rather than an executable JAR")
51+
@Name("sourcesJar")
52+
@Name("jarSources")
5153
@Name("sources")
52-
@Name("src")
54+
@Name("source")
55+
@Tag(tags.deprecated("source")) // alias to be removed in 1.6.x
5356
@Tag(tags.restricted)
5457
@Tag(tags.inShortHelp)
55-
source: Boolean = false,
58+
src: Boolean = false,
5659
@Group(HelpGroup.Package.toString)
5760
@HelpMessage("Generate a scaladoc JAR rather than an executable JAR")
5861
@ExtraName("scaladoc")
@@ -144,7 +147,7 @@ final case class PackageOptions(
144147
def packageTypeOpt: Option[PackageType] =
145148
forcedPackageTypeOpt.orElse {
146149
if (library) Some(PackageType.LibraryJar)
147-
else if (source) Some(PackageType.SourceJar)
150+
else if (src) Some(PackageType.SourceJar)
148151
else if (assembly) Some(
149152
PackageType.Assembly(
150153
addPreamble = preamble,

modules/cli/src/main/scala/scala/cli/util/ArgHelpers.scala

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scala.cli.util
22

33
import caseapp.core.Arg
44
import caseapp.core.help.HelpFormat
5+
import caseapp.core.util.CaseUtil
56

67
import scala.build.input.ScalaCliInvokeData
78
import scala.build.internal.util.WarningMessages
@@ -12,8 +13,24 @@ import scala.cli.commands.{SpecificationLevel, tags}
1213
object ArgHelpers {
1314
extension (arg: Arg) {
1415
private def hasTag(tag: String): Boolean = arg.tags.exists(_.name == tag)
15-
def isExperimental: Boolean = arg.hasTag(tags.experimental)
16-
def isRestricted: Boolean = arg.hasTag(tags.restricted)
16+
private def hasTagPrefix(tagPrefix: String): Boolean =
17+
arg.tags.exists(_.name.startsWith(tagPrefix))
18+
def isExperimental: Boolean = arg.hasTag(tags.experimental)
19+
def isRestricted: Boolean = arg.hasTag(tags.restricted)
20+
def isDeprecated: Boolean = arg.hasTagPrefix(tags.deprecatedPrefix)
21+
22+
def deprecatedNames: List[String] = arg.tags
23+
.filter(_.name.startsWith(tags.deprecatedPrefix))
24+
.map(_.name.stripPrefix(s"${tags.deprecatedPrefix}${tags.valueSeparator}"))
25+
.toList
26+
27+
def deprecatedOptionAliases: List[String] = arg.deprecatedNames.map {
28+
case name if name.startsWith("-") => name
29+
case name if name.length == 1 => "-" + name
30+
case name => "--" + CaseUtil.pascalCaseSplit(name.toCharArray.toList).map(
31+
_.toLowerCase
32+
).mkString("-")
33+
}
1734

1835
def isExperimentalOrRestricted: Boolean = arg.isRestricted || arg.isExperimental
1936

modules/generate-reference-doc/src/main/scala/scala/cli/doc/GenerateReferenceDoc.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@ object GenerateReferenceDoc extends CaseApp[InternalDocOptions] {
207207
names
208208
.tail
209209
.sortBy(_.dropWhile(_ == '-'))
210-
.map(n => s"`$n`")
210+
.map {
211+
case name if arg.deprecatedOptionAliases.contains(name) =>
212+
s"[deprecated] `$name`"
213+
case name => s"`$name`"
214+
}
211215
.mkString("Aliases: ", ", ", "\n\n")
212216
)
213217

modules/integration/src/test/scala/scala/cli/integration/PackageTestDefinitions.scala

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -859,11 +859,16 @@ abstract class PackageTestDefinitions extends ScalaCliSuite with TestScalaVersio
859859
test("source JAR") {
860860
val dest = os.rel / "sources.jar"
861861
simpleInputWithScalaAndSc.fromRoot { root =>
862-
os.proc(TestUtil.cli, "--power", "package", extraOptions, ".", "-o", dest, "--source").call(
863-
cwd = root,
864-
stdin = os.Inherit,
865-
stdout = os.Inherit
866-
)
862+
val r =
863+
os.proc(TestUtil.cli, "--power", "package", extraOptions, ".", "-o", dest, "--source").call(
864+
cwd = root,
865+
stdin = os.Inherit,
866+
stdout = os.Inherit,
867+
stderr = os.Pipe
868+
)
869+
expect(r.err.trim().contains(
870+
"The --source option alias has been deprecated and may be removed in a future version"
871+
))
867872

868873
expect(os.isFile(root / dest))
869874

modules/specification-level/src/main/scala/scala/cli/commands/SpecificationLevel.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ object SpecificationLevel {
5858
}
5959

6060
object tags {
61+
val valueSeparator: String = ":" // separates values in a tag
62+
6163
// specification level tags
6264
val experimental: String = SpecificationLevel.EXPERIMENTAL.toString
6365
val restricted: String = SpecificationLevel.RESTRICTED.toString
@@ -68,7 +70,10 @@ object tags {
6870
// other tags
6971
// the `inShortHelp` tag whitelists options to be included in --help
7072
// this is in contrast to blacklisting options in --help with the @Hidden annotation
71-
val inShortHelp: String = "inShortHelp" // included in --help by default
73+
val inShortHelp: String = "inShortHelp" // included in --help by default
74+
val deprecatedPrefix: String = "deprecated"
75+
def deprecated(name: String): String =
76+
s"$deprecatedPrefix$valueSeparator$name" // produces a deprecated warning for the given name
7277

7378
def levelFor(name: String): Option[SpecificationLevel] = name match {
7479
case `experimental` => Some(SpecificationLevel.EXPERIMENTAL)

website/docs/reference/cli-options.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,9 +749,9 @@ Overwrite the destination file, if it exists
749749

750750
Generate a library JAR rather than an executable JAR
751751

752-
### `--source`
752+
### `--src`
753753

754-
Aliases: `--sources`, `--src`
754+
Aliases: `--jar-sources`, [deprecated] `--source`, `--sources`, `--sources-jar`
755755

756756
Generate a source JAR rather than an executable JAR
757757

0 commit comments

Comments
 (0)