Skip to content

Commit

Permalink
Add proper logging in the commands parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Gedochao committed Mar 17, 2023
1 parent 38a5592 commit a32d333
Show file tree
Hide file tree
Showing 42 changed files with 135 additions and 198 deletions.
Expand Up @@ -3,12 +3,16 @@ package scala.cli.commands
import caseapp.core.app.Command
import caseapp.core.parser.Parser

import scala.build.Logger
import scala.cli.commands.shared.HasGlobalOptions

trait RestrictableCommand[T](implicit myParser: Parser[T]) {
self: Command[T] =>

def shouldSuppressExperimentalFeatureWarnings: Boolean
def logger: Logger
override def parser: Parser[T] =
RestrictedCommandsParser(myParser, shouldSuppressExperimentalFeatureWarnings)
RestrictedCommandsParser(myParser, logger, shouldSuppressExperimentalFeatureWarnings)

final def isRestricted: Boolean = scalaSpecificationLevel == SpecificationLevel.RESTRICTED

Expand Down
Expand Up @@ -6,12 +6,17 @@ import caseapp.core.parser.Parser
import caseapp.core.util.Formatter
import caseapp.core.{Arg, Error}

import scala.build.Logger
import scala.build.internal.util.WarningMessages
import scala.cli.ScalaCli
import scala.cli.util.ArgHelpers.*

object RestrictedCommandsParser {
def apply[T](parser: Parser[T], shouldSuppressExperimentalWarnings: Boolean): Parser[T] =
def apply[T](
parser: Parser[T],
logger: Logger,
shouldSuppressExperimentalWarnings: Boolean
): Parser[T] =
new Parser[T] {

type D = parser.D
Expand All @@ -29,6 +34,7 @@ object RestrictedCommandsParser {
def withDefaultOrigin(origin: String): caseapp.core.parser.Parser[T] =
RestrictedCommandsParser(
parser.withDefaultOrigin(origin),
logger,
shouldSuppressExperimentalWarnings
)

Expand All @@ -52,7 +58,7 @@ object RestrictedCommandsParser {
))
case (r @ Right(Some(_, arg: Arg, _)), passedOption :: _)
if arg.isExperimental && !shouldSuppressExperimentalWarnings =>
System.err.println(WarningMessages.experimentalOptionUsed(passedOption))
logger.message(WarningMessages.experimentalOptionUsed(passedOption))
r
case (other, _) =>
other
Expand Down
47 changes: 22 additions & 25 deletions modules/cli/src/main/scala/scala/cli/commands/ScalaCommand.scala
Expand Up @@ -10,7 +10,7 @@ import caseapp.{HelpMessage, Name}
import coursier.core.{Repository, Version}
import dependency.*

import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.{AtomicBoolean, AtomicReference}

import scala.annotation.tailrec
import scala.build.EitherCps.{either, value}
Expand All @@ -22,14 +22,7 @@ import scala.build.internal.{Constants, Runner}
import scala.build.options.{BuildOptions, ScalacOpt, Scope}
import scala.build.{Artifacts, Directories, Logger, Positioned, ReplArtifacts}
import scala.cli.commands.default.LegacyScalaOptions
import scala.cli.commands.shared.{
GlobalSuppressWarningOptions,
HasGlobalOptions,
HelpMessages,
ScalaCliHelp,
ScalacOptions,
SharedOptions
}
import scala.cli.commands.shared._
import scala.cli.commands.util.CommandHelpers
import scala.cli.commands.util.ScalacOptionsUtil.*
import scala.cli.config.{ConfigDb, Keys}
Expand Down Expand Up @@ -167,7 +160,7 @@ abstract class ScalaCommand[T <: HasGlobalOptions](implicit myParser: Parser[T],

private def maybePrintWarnings(options: T): Unit = {
import scala.cli.commands.shared.ScalacOptions.YScriptRunnerOption
val logger = options.logging.logger
val logger = options.global.logging.logger
sharedOptions(options).foreach { so =>
val scalacOpts = so.scalac.scalacOption.toScalacOptShadowingSeq
if scalacOpts.keys.contains(ScalacOpt(YScriptRunnerOption)) then
Expand Down Expand Up @@ -319,20 +312,25 @@ abstract class ScalaCommand[T <: HasGlobalOptions](implicit myParser: Parser[T],
sys.exit(1)
}

private val shouldSuppressExperimentalFeatureWarningsAtomic: AtomicBoolean =
new AtomicBoolean(false)
private val globalOptionsAtomic: AtomicReference[Option[GlobalOptions]] =
new AtomicReference(None)
private def globalOptions: GlobalOptions = globalOptionsAtomic.get() match
case Some(opts) => opts
case None => // should never happen
System.err.println("Failed to initialize the global options.")
sys.exit(1)
override def shouldSuppressExperimentalFeatureWarnings: Boolean =
shouldSuppressExperimentalFeatureWarningsAtomic.get()
final override def main(progName: String, args: Array[String]): Unit = {
shouldSuppressExperimentalFeatureWarningsAtomic
.set {
GlobalSuppressWarningOptions.shouldSuppressExperimentalFeatureWarning(args.toList)
.orElse {
configDb.toOption
.flatMap(_.getOpt(Keys.suppressExperimentalFeatureWarning))
}
.getOrElse(false)
globalOptions.globalSuppress.suppressExperimentalFeatureWarning
.orElse {
configDb.toOption
.flatMap(_.getOpt(Keys.suppressExperimentalFeatureWarning))
}
.getOrElse(false)

override def logger: Logger = globalOptions.logging.logger

final override def main(progName: String, args: Array[String]): Unit = {
globalOptionsAtomic.set(GlobalOptions.get(args.toList))
super.main(progName, args)
}

Expand All @@ -349,8 +347,7 @@ abstract class ScalaCommand[T <: HasGlobalOptions](implicit myParser: Parser[T],
* start of running every [[ScalaCommand]].
*/
final override def run(options: T, remainingArgs: RemainingArgs): Unit = {
CurrentParams.verbosity = options.logging.verbosity
val logger = options.logging.logger
CurrentParams.verbosity = options.global.logging.verbosity
if shouldExcludeInSip then
logger.error(HelpMessages.powerCommandUsedInSip(scalaSpecificationLevel))
sys.exit(1)
Expand All @@ -362,6 +359,6 @@ abstract class ScalaCommand[T <: HasGlobalOptions](implicit myParser: Parser[T],
maybePrintSimpleScalacOutput(options, bo)
maybePrintToolsHelp(options, bo)
}
runCommand(options, remainingArgs, options.logging.logger)
runCommand(options, remainingArgs, options.global.logging.logger)
}
}
Expand Up @@ -2,16 +2,14 @@ package scala.cli.commands.addpath

import caseapp.*

import scala.cli.commands.shared.{GlobalSuppressWarningOptions, HasGlobalOptions, LoggingOptions}
import scala.cli.commands.shared.{GlobalOptions, HasGlobalOptions}
import scala.cli.commands.tags

// format: off
@HelpMessage("Add entries to the PATH environment variable.")
final case class AddPathOptions(
@Recurse
logging: LoggingOptions = LoggingOptions(),
@Recurse
globalSuppressWarning: GlobalSuppressWarningOptions = GlobalSuppressWarningOptions(),
global: GlobalOptions = GlobalOptions(),
@Tag(tags.restricted)
title: String = ""
) extends HasGlobalOptions
Expand Down
Expand Up @@ -28,12 +28,12 @@ object Bloop extends ScalaCommand[BloopOptions] {
// directly.

val sharedOptions = SharedOptions(
logging = opts.logging,
logging = opts.global.logging,
compilationServer = opts.compilationServer,
jvm = opts.jvm,
coursier = opts.coursier
)
val options = sharedOptions.buildOptions(false, None).orExit(opts.logging.logger)
val options = sharedOptions.buildOptions(false, None).orExit(opts.global.logging.logger)
lazy val defaultJvmCmd =
sharedOptions.downloadJvm(OsLibc.baseDefaultJvm(OsLibc.jvmIndexOs, "17"), options)
val javaCmd = opts.compilationServer.bloopJvm
Expand All @@ -48,9 +48,9 @@ object Bloop extends ScalaCommand[BloopOptions] {
.getOrElse(defaultJvmCmd)

opts.compilationServer.bloopRifleConfig(
opts.logging.logger,
opts.global.logging.logger,
sharedOptions.coursierCache,
opts.logging.verbosity,
opts.global.logging.verbosity,
javaCmd,
Directories.directories,
Some(17)
Expand Down
Expand Up @@ -17,9 +17,9 @@ object BloopExit extends ScalaCommand[BloopExitOptions] {
private def mkBloopRifleConfig(opts: BloopExitOptions): BloopRifleConfig = {
import opts.*
compilationServer.bloopRifleConfig(
logging.logger,
coursier.coursierCache(logging.logger.coursierLogger("Downloading Bloop")),
logging.verbosity,
global.logging.logger,
coursier.coursierCache(global.logging.logger.coursierLogger("Downloading Bloop")),
global.logging.verbosity,
"java", // shouldn't be used…
Directories.directories
)
Expand All @@ -36,7 +36,7 @@ object BloopExit extends ScalaCommand[BloopExitOptions] {
if (ret == 0)
logger.message("Stopped Bloop server.")
else {
if (options.logging.verbosity >= 0)
if (options.global.logging.verbosity >= 0)
System.err.println(s"Error running bloop exit command (return code $ret)")
sys.exit(1)
}
Expand Down
Expand Up @@ -2,7 +2,7 @@ package scala.cli.commands.bloop

import caseapp.*

import scala.cli.commands.shared.{CoursierOptions, GlobalSuppressWarningOptions, HasGlobalOptions, HelpMessages, LoggingOptions, SharedCompilationServerOptions}
import scala.cli.commands.shared.{CoursierOptions, GlobalOptions, HasGlobalOptions, HelpMessages, SharedCompilationServerOptions}


// format: off
Expand All @@ -12,9 +12,7 @@ import scala.cli.commands.shared.{CoursierOptions, GlobalSuppressWarningOptions,
|${HelpMessages.bloopInfo}""".stripMargin)
final case class BloopExitOptions(
@Recurse
logging: LoggingOptions = LoggingOptions(),
@Recurse
globalSuppressWarning: GlobalSuppressWarningOptions = GlobalSuppressWarningOptions(),
global: GlobalOptions = GlobalOptions(),
@Recurse
compilationServer: SharedCompilationServerOptions = SharedCompilationServerOptions(),
@Recurse
Expand Down
Expand Up @@ -9,9 +9,7 @@ import scala.cli.commands.tags
@HelpMessage(BloopOptions.helpMessage, "", BloopOptions.detailedHelpMessage)
final case class BloopOptions(
@Recurse
logging: LoggingOptions = LoggingOptions(),
@Recurse
globalSuppressWarning: GlobalSuppressWarningOptions = GlobalSuppressWarningOptions(),
global: GlobalOptions = GlobalOptions(),
@Recurse
compilationServer: SharedCompilationServerOptions = SharedCompilationServerOptions(),
@Recurse
Expand Down
Expand Up @@ -21,7 +21,7 @@ object BloopOutput extends ScalaCommand[BloopOutputOptions] {
val bloopRifleConfig = options.compilationServer.bloopRifleConfig(
logger,
CoursierOptions().coursierCache(logger.coursierLogger("Downloading Bloop")), // unused here
options.logging.verbosity,
options.global.logging.verbosity,
"unused-java", // unused here
Directories.directories
)
Expand All @@ -31,14 +31,14 @@ object BloopOutput extends ScalaCommand[BloopOutputOptions] {
logger.debug(s"Bloop server output path: ${s.outputPath}")
os.Path(s.outputPath, os.pwd)
case tcp: BloopRifleConfig.Address.Tcp =>
if (options.logging.verbosity >= 0)
if (options.global.logging.verbosity >= 0)
System.err.println(
s"Error: Bloop server is listening on TCP at ${tcp.render}, output not available."
)
sys.exit(1)
}
if (!os.isFile(outputFile)) {
if (options.logging.verbosity >= 0)
if (options.global.logging.verbosity >= 0)
System.err.println(s"Error: $outputFile not found")
sys.exit(1)
}
Expand Down
Expand Up @@ -2,7 +2,7 @@ package scala.cli.commands.bloop

import caseapp.*

import scala.cli.commands.shared.{GlobalSuppressWarningOptions, HasGlobalOptions, HelpMessages, LoggingOptions, SharedCompilationServerOptions}
import scala.cli.commands.shared.{GlobalOptions, GlobalSuppressWarningOptions, HasGlobalOptions, HelpMessages, LoggingOptions, SharedCompilationServerOptions}

// format: off
@HelpMessage(
Expand All @@ -11,9 +11,7 @@ import scala.cli.commands.shared.{GlobalSuppressWarningOptions, HasGlobalOptions
|${HelpMessages.bloopInfo}""".stripMargin)
final case class BloopOutputOptions(
@Recurse
logging: LoggingOptions = LoggingOptions(),
@Recurse
globalSuppressWarning: GlobalSuppressWarningOptions = GlobalSuppressWarningOptions(),
global: GlobalOptions = GlobalOptions(),
@Recurse
compilationServer: SharedCompilationServerOptions = SharedCompilationServerOptions(),
) extends HasGlobalOptions
Expand Down
Expand Up @@ -23,16 +23,16 @@ object BloopStart extends ScalaCommand[BloopStartOptions] {
private def mkBloopRifleConfig(opts: BloopStartOptions): BloopRifleConfig = {
import opts.*
val buildOptions = BuildOptions(
javaOptions = JvmUtils.javaOptions(jvm).orExit(logging.logger),
javaOptions = JvmUtils.javaOptions(jvm).orExit(global.logging.logger),
internal = InternalOptions(
cache = Some(coursier.coursierCache(logging.logger.coursierLogger("")))
cache = Some(coursier.coursierCache(global.logging.logger.coursierLogger("")))
)
)

compilationServer.bloopRifleConfig(
logging.logger,
coursier.coursierCache(logging.logger.coursierLogger("Downloading Bloop")),
logging.verbosity,
global.logging.logger,
coursier.coursierCache(global.logging.logger.coursierLogger("Downloading Bloop")),
global.logging.verbosity,
buildOptions.javaHome().value.javaCommand,
Directories.directories
)
Expand All @@ -51,7 +51,7 @@ object BloopStart extends ScalaCommand[BloopStartOptions] {
if (ret == 0)
logger.message("Stopped Bloop server.")
else {
if (options.logging.verbosity >= 0)
if (options.global.logging.verbosity >= 0)
System.err.println(s"Error running bloop exit command (return code $ret)")
sys.exit(1)
}
Expand Down
Expand Up @@ -12,9 +12,7 @@ import scala.cli.commands.tags
|${HelpMessages.bloopInfo}""".stripMargin)
final case class BloopStartOptions(
@Recurse
logging: LoggingOptions = LoggingOptions(),
@Recurse
globalSuppressWarning: GlobalSuppressWarningOptions = GlobalSuppressWarningOptions(),
global: GlobalOptions = GlobalOptions(),
@Recurse
compilationServer: SharedCompilationServerOptions = SharedCompilationServerOptions(),
@Recurse
Expand Down
Expand Up @@ -3,22 +3,13 @@ package scala.cli.commands.clean
import caseapp.*

import scala.cli.ScalaCli.fullRunnerName
import scala.cli.commands.shared.{
GlobalSuppressWarningOptions,
HasGlobalOptions,
HelpMessages,
LoggingOptions,
SharedBspFileOptions,
SharedWorkspaceOptions
}
import scala.cli.commands.shared._

// format: off
@HelpMessage(CleanOptions.helpMessage, "", CleanOptions.detailedHelpMessage)
final case class CleanOptions(
@Recurse
logging: LoggingOptions = LoggingOptions(),
@Recurse
globalSuppressWarning: GlobalSuppressWarningOptions = GlobalSuppressWarningOptions(),
global: GlobalOptions = GlobalOptions(),
@Recurse
bspFile: SharedBspFileOptions = SharedBspFileOptions(),
@Recurse
Expand Down
Expand Up @@ -13,9 +13,7 @@ import scala.cli.config.{Key, Keys}
@HelpMessage(ConfigOptions.helpMessage, "", ConfigOptions.detailedHelpMessage)
final case class ConfigOptions(
@Recurse
logging: LoggingOptions = LoggingOptions(),
@Recurse
globalSuppressWarning: GlobalSuppressWarningOptions = GlobalSuppressWarningOptions(),
global: GlobalOptions = GlobalOptions(),
@Recurse
coursier: CoursierOptions = CoursierOptions(),
@Recurse
Expand Down
Expand Up @@ -47,7 +47,7 @@ class Default(

override def runCommand(options: DefaultOptions, args: RemainingArgs, logger: Logger): Unit =
// can't fully re-parse and redirect to Version because of --cli-version and --scala-version clashing
if options.version then Version.runCommand(VersionOptions(options.shared.logging), args, logger)
if options.version then Version.runCommand(VersionOptions(options.shared.global), args, logger)
else
{
val shouldDefaultToRun =
Expand Down

0 comments on commit a32d333

Please sign in to comment.