Skip to content

Commit 37b636c

Browse files
committed
Add warnings for -save & -nosave legacy scala runner options instead of failing
1 parent 26bd940 commit 37b636c

File tree

4 files changed

+104
-3
lines changed

4 files changed

+104
-3
lines changed

modules/cli/src/main/scala/scala/cli/commands/default/Default.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Default(
5252
sys.exit(0)
5353
}
5454

55-
override def runCommand(options: DefaultOptions, args: RemainingArgs, logger: Logger): Unit = {
55+
override def runCommand(options: DefaultOptions, args: RemainingArgs, logger: Logger): Unit =
5656
if options.version then println(Version.versionInfo)
5757
else
5858
{
@@ -62,9 +62,8 @@ class Default(
6262
options.shared.snippet.executeMarkdown.nonEmpty ||
6363
(options.shared.extraJarsAndClassPath.nonEmpty && options.sharedRun.mainClass.mainClass.nonEmpty)
6464
if shouldDefaultToRun then RunOptions.parser else ReplOptions.parser
65-
}.parse(rawArgs) match
65+
}.parse(options.legacyScala.filterNonDeprecatedArgs(rawArgs, progName, logger)) match
6666
case Left(e) => error(e)
6767
case Right((replOptions: ReplOptions, _)) => Repl.runCommand(replOptions, args, logger)
6868
case Right((runOptions: RunOptions, _)) => Run.runCommand(runOptions, args, logger)
69-
}
7069
}

modules/cli/src/main/scala/scala/cli/commands/default/DefaultOptions.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ case class DefaultOptions(
1414
sharedRun: SharedRunOptions = SharedRunOptions(),
1515
@Recurse
1616
sharedRepl: SharedReplOptions = SharedReplOptions(),
17+
@Recurse
18+
legacyScala: LegacyScalaOptions = LegacyScalaOptions(),
1719
@Name("-version")
1820
version: Boolean = false
1921
) extends HasSharedOptions
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package scala.cli.commands.default
2+
3+
import caseapp.*
4+
import caseapp.core.Indexed
5+
6+
import scala.build.Logger
7+
import scala.cli.ScalaCli
8+
import scala.cli.commands.package0.Package
9+
import scala.cli.commands.tags
10+
11+
/** Options covering backwards compatibility with the old scala runner.
12+
*/
13+
// format: off
14+
case class LegacyScalaOptions(
15+
@Group("Scala")
16+
@HelpMessage("Ignored legacy option. Deprecated equivalent of running a subsequent `package` command.")
17+
@Tag(tags.must)
18+
@Name("-save")
19+
save: Option[Indexed[Boolean]] = None,
20+
@Group("Scala")
21+
@HelpMessage("Ignored legacy option. Deprecated override canceling the `-nosave` option.")
22+
@Tag(tags.must)
23+
@Name("-nosave")
24+
nosave: Option[Indexed[Boolean]] = None,
25+
) {
26+
// format: on
27+
28+
extension [T](indexedOption: Option[Indexed[T]]) {
29+
private def findArg(args: Array[String]): Option[String] =
30+
indexedOption.flatMap(io => args.lift(io.index))
31+
}
32+
33+
def filterNonDeprecatedArgs(
34+
args: Array[String],
35+
progName: String,
36+
logger: Logger
37+
): Array[String] = {
38+
val saveOptionString = save.findArg(args)
39+
val noSaveOptionString = nosave.findArg(args)
40+
val deprecatedArgs = Seq(saveOptionString, noSaveOptionString).flatten
41+
val filteredArgs = args.filterNot(deprecatedArgs.contains)
42+
val filteredArgsString = filteredArgs.mkString(" ")
43+
val powerString = if ScalaCli.allowRestrictedFeatures then "" else "--power "
44+
saveOptionString.foreach { s =>
45+
logger.message(
46+
s"""Deprecated option '$s' is ignored.
47+
|The compiled project files will be saved in the '.scala-build' directory in the project root folder.
48+
|If an actual jar file is necessary to be saved, run the 'package' sub-command after this one:
49+
| ${Console.BOLD}$progName $powerString${Package.name} --library $filteredArgsString${Console.RESET}""".stripMargin
50+
)
51+
}
52+
noSaveOptionString.foreach { ns =>
53+
logger.message(
54+
s"""Deprecated option '$ns' is ignored.
55+
|A jar file is not saved unless the '$powerString${Package.name}' sub-command is called.""".stripMargin
56+
)
57+
}
58+
filteredArgs
59+
}
60+
}
61+
object LegacyScalaOptions {
62+
implicit lazy val parser: Parser[LegacyScalaOptions] = Parser.derive
63+
implicit lazy val help: Help[LegacyScalaOptions] = Help.derive
64+
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,42 @@ class DefaultTests extends WithWarmUpScalaCliSuite {
183183
}
184184
}
185185

186+
test("ensure -save/--save works with the default command") {
187+
val msg = "Hello world"
188+
TestInputs(os.rel / "s.sc" -> s"""println("$msg")""").fromRoot { root =>
189+
val legacySaveOption = "-save"
190+
val res1 =
191+
os.proc(TestUtil.cli, ".", legacySaveOption, TestUtil.extraOptions)
192+
.call(cwd = root, stderr = os.Pipe)
193+
expect(res1.out.trim() == msg)
194+
expect(res1.err.trim().contains(s"Deprecated option '$legacySaveOption' is ignored"))
195+
val doubleDashSaveOption = "--save"
196+
val res2 =
197+
os.proc(TestUtil.cli, ".", doubleDashSaveOption, TestUtil.extraOptions)
198+
.call(cwd = root, stderr = os.Pipe)
199+
expect(res2.out.trim() == msg)
200+
expect(res2.err.trim().contains(s"Deprecated option '$doubleDashSaveOption' is ignored"))
201+
}
202+
}
203+
204+
test("ensure -nosave/--nosave works with the default command") {
205+
val msg = "Hello world"
206+
TestInputs(os.rel / "s.sc" -> s"""println("$msg")""").fromRoot { root =>
207+
val legacyNoSaveOption = "-nosave"
208+
val res1 =
209+
os.proc(TestUtil.cli, ".", legacyNoSaveOption, TestUtil.extraOptions)
210+
.call(cwd = root, stderr = os.Pipe)
211+
expect(res1.out.trim() == msg)
212+
expect(res1.err.trim().contains(s"Deprecated option '$legacyNoSaveOption' is ignored"))
213+
val doubleDashNoSaveOption = "--nosave"
214+
val res2 =
215+
os.proc(TestUtil.cli, ".", doubleDashNoSaveOption, TestUtil.extraOptions)
216+
.call(cwd = root, stderr = os.Pipe)
217+
expect(res2.out.trim() == msg)
218+
expect(res2.err.trim().contains(s"Deprecated option '$doubleDashNoSaveOption' is ignored"))
219+
}
220+
}
221+
186222
private def unrecognizedArgMessage(argName: String) =
187223
s"""
188224
|Unrecognized argument: $argName

0 commit comments

Comments
 (0)