Skip to content

Commit

Permalink
Add help for repl, scalafmt and scaladoc
Browse files Browse the repository at this point in the history
  • Loading branch information
wleczny committed Oct 24, 2022
1 parent e913882 commit 940e43f
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ final case class FmtOptions(
@HelpMessage("Saves .scalafmt.conf file if it was created or overwritten")
saveScalafmtConf: Boolean = false,

@Group("Format")
@HelpMessage("Show help for scalafmt. This is an alias for --scalafmt-arg -help")
@Name("fmtHelp")
scalafmtHelp: Boolean = false,

@Group("Format")
@Hidden
osArchSuffix: Option[String] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@ case class HelpGroupOptions(
@HelpMessage("Show options for ScalaJS")
helpJs: Boolean = false,
@HelpMessage("Show options for ScalaNative")
helpNative: Boolean = false
helpNative: Boolean = false,
@HelpMessage("Show options for Scaladoc")
@Name("scaladocHelp")
@Name("docHelp")
@Name("helpDoc")
helpScaladoc: Boolean = false,
@HelpMessage("Show options for Scala REPL")
@Name("replHelp")
helpRepl: Boolean = false,
@HelpMessage("Show options for Scalafmt")
@Name("scalafmtHelp")
@Name("fmtHelp")
@Name("helpFmt")
helpScalafmt: Boolean = false
) {

private def printHelpWithGroup(help: Help[_], helpFormat: HelpFormat, group: String): Nothing = {
Expand Down
81 changes: 78 additions & 3 deletions modules/cli/src/main/scala/scala/cli/commands/ScalaCommand.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import caseapp.core.help.{Help, HelpFormat}
import caseapp.core.parser.Parser
import caseapp.core.util.Formatter
import caseapp.core.{Arg, Error, RemainingArgs}
import dependency.*

import scala.annotation.tailrec
import scala.build.EitherCps.{either, value}
import scala.build.compiler.SimpleScalaCompiler
import scala.build.internal.Constants
import scala.build.errors.BuildException
import scala.build.internal.{Constants, Runner}
import scala.build.options.{BuildOptions, Scope}
import scala.cli.commands.util.CommandHelpers
import scala.build.{Artifacts, Positioned, ReplArtifacts}
import scala.cli.commands.util.ScalacOptionsUtil.*
import scala.cli.commands.util.SharedOptionsUtil.*
import scala.cli.commands.util.{CommandHelpers, FmtOptionsUtil}
import scala.cli.{CurrentParams, ScalaCli}
import scala.util.{Properties, Try}

Expand Down Expand Up @@ -159,6 +163,74 @@ abstract class ScalaCommand[T](implicit myParser: Parser[T], help: Help[T])
sys.exit(exitCode)
}

def maybePrintToolsHelp(options: T, buildOptions: BuildOptions): Unit =
for {
shared <- sharedOptions(options)
logger = shared.logger
artifacts <- buildOptions.artifacts(logger, Scope.Main).toOption
scalaArtifacts <- artifacts.scalaOpt
scalaParams = scalaArtifacts.params
if shared.helpGroups.helpScaladoc || shared.helpGroups.helpRepl || shared.helpGroups.helpScalafmt
} {
val exitCode: Either[BuildException, Int] = either {
val (classPath: Seq[os.Path], mainClass: String) =
if (shared.helpGroups.helpScaladoc) {
val docArtifacts = value {
Artifacts.fetch(
Positioned.none(Seq(dep"org.scala-lang::scaladoc:${scalaParams.scalaVersion}")),
buildOptions.finalRepositories,
Some(scalaParams),
logger,
buildOptions.finalCache,
None
)
}
docArtifacts.files.map(os.Path(_, os.pwd)) -> "dotty.tools.scaladoc.Main"
}
else if (shared.helpGroups.helpRepl) {
val initialBuildOptions = buildOptionsOrExit(options)
val artifacts = initialBuildOptions.artifacts(logger, Scope.Main).orExit(logger)
val replArtifacts = value {
ReplArtifacts.default(
scalaParams,
artifacts.userDependencies,
Nil,
logger,
buildOptions.finalCache,
Nil,
None
)
}
replArtifacts.replClassPath -> replArtifacts.replMainClass
}
else {
val fmtArtifacts = value {
Artifacts.fetch(
Positioned.none(Seq(
dep"${Constants.scalafmtOrganization}:${Constants.scalafmtName}:${Constants.defaultScalafmtVersion}"
)),
buildOptions.finalRepositories,
Some(scalaParams),
logger,
buildOptions.finalCache,
None
)
}
fmtArtifacts.files.map(os.Path(_, os.pwd)) -> "org.scalafmt.cli.Cli"
}
val retCode = Runner.runJvm(
buildOptions.javaHome().value.javaCommand,
Nil,
classPath,
mainClass,
Seq("-help"),
logger
).waitFor()
retCode
}
sys.exit(exitCode.orExit(logger))
}

override def helpFormat: HelpFormat =
HelpFormat.default()
.withSortedGroups(Some(Seq(
Expand Down Expand Up @@ -251,7 +323,10 @@ abstract class ScalaCommand[T](implicit myParser: Parser[T], help: Help[T])
final override def run(options: T, remainingArgs: RemainingArgs): Unit = {
verbosity(options).foreach(v => CurrentParams.verbosity = v)
maybePrintGroupHelp(options)
buildOptions(options).foreach(bo => maybePrintSimpleScalacOutput(options, bo))
buildOptions(options).foreach { bo =>
maybePrintSimpleScalacOutput(options, bo)
maybePrintToolsHelp(options, bo)
}
runCommand(options, remainingArgs)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ object FmtOptionsUtil {
def scalafmtCliOptions: List[String] =
scalafmtArg :::
(if (check && !scalafmtArg.contains("--check")) List("--check") else Nil) :::
(if (scalafmtHelp && !scalafmtArg.exists(Set("-h", "-help", "--help"))) List("--help")
else Nil) :::
(if (respectProjectFilters && !scalafmtArg.contains("--respect-project-filters"))
List("--respect-project-filters")
else Nil)
Expand Down
24 changes: 18 additions & 6 deletions website/docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,6 @@ Use project filters defined in the configuration. Turned on by default, use `--r

Saves .scalafmt.conf file if it was created or overwritten

### `--scalafmt-help`

Aliases: `--fmt-help`

Show help for scalafmt. This is an alias for --scalafmt-arg -help

### `--os-arch-suffix`

[Internal]
Expand Down Expand Up @@ -364,6 +358,24 @@ Show options for ScalaJS

Show options for ScalaNative

### `--help-scaladoc`

Aliases: `--scaladoc-help`, `--doc-help`, `--help-doc`

Show options for Scaladoc

### `--help-repl`

Aliases: `--repl-help`

Show options for Scala REPL

### `--help-scalafmt`

Aliases: `--scalafmt-help`, `--fmt-help`, `--help-fmt`

Show options for Scalafmt

## Install completions options

Available in commands:
Expand Down
24 changes: 18 additions & 6 deletions website/docs/reference/scala-command/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,6 @@ Use project filters defined in the configuration. Turned on by default, use `--r

Saves .scalafmt.conf file if it was created or overwritten

### `--scalafmt-help`

Aliases: `--fmt-help`

Show help for scalafmt. This is an alias for --scalafmt-arg -help

### `--os-arch-suffix`

[Internal]
Expand Down Expand Up @@ -314,6 +308,24 @@ Show options for ScalaJS

Show options for ScalaNative

### `--help-scaladoc`

Aliases: `--scaladoc-help`, `--doc-help`, `--help-doc`

Show options for Scaladoc

### `--help-repl`

Aliases: `--repl-help`

Show options for Scala REPL

### `--help-scalafmt`

Aliases: `--scalafmt-help`, `--fmt-help`, `--help-fmt`

Show options for Scalafmt

## Install completions options

Available in commands:
Expand Down

0 comments on commit 940e43f

Please sign in to comment.