Skip to content

Commit

Permalink
CLI: add instructions when version is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed May 22, 2023
1 parent cba861d commit 8c2ba7f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
24 changes: 22 additions & 2 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,35 @@ object Cli {
private val isNativeImage: Boolean =
"true" == System.getProperty("scalafmt.native-image", "false")

private def getProposedConfigVersion(options: CliOptions): String =
s"version = '$stableVersion'"

private def findRunner(
options: CliOptions
): Either[String, ScalafmtRunner] = {
// Run format using
// - `scalafmt-dynamic` if the specified `version` setting doesn't match build version.
// - `scalafmt-core` if the specified `version` setting match with build version
// (or if the `version` is not specified).
options.getVersionOpt match {
case None => Left(s"error: missing version (current $stableVersion)")
val versionOpt = options.hoconOpt.map(x => Right(x.version)).getOrElse {
Left(s"""error: missing Scalafmt configuration file.
|Consider creating '${options.getProposedConfigFile}' with the following:
|${getProposedConfigVersion(options)}
|""".stripMargin)
}
versionOpt.flatMap {
case None =>
val where = options.configStr match {
case None =>
options.canonicalConfigFile
.fold(options.getProposedConfigFile)(_.get)
.toString
case _ => "--config-str option"
}
Left(s"""error: missing Scalafmt version.
|Consider adding the following to $where:
|${getProposedConfigVersion(options)}
|""".stripMargin)
case Some(`stableVersion`) =>
options.common.debug.println(s"Using core runner [$stableVersion]")
Right(ScalafmtCoreRunner)
Expand Down
9 changes: 7 additions & 2 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,14 @@ case class CliOptions(
.fold(throw new NoSuchFileException("Config file not found"))(_.get)
}

private lazy val canonicalConfigFile: Option[Try[Path]] =
private[cli] lazy val canonicalConfigFile: Option[Try[Path]] =
gitOps.getCanonicalConfigFile(cwd, config)

private[cli] def getProposedConfigFile: Path =
canonicalConfigFile.flatMap(_.toOption).getOrElse {
gitOps.getProposedConfigFile(cwd, config).path
}

/** Parse the scalafmt configuration and try to encode it to `ScalafmtConfig`.
* If `--config-str` is specified, this will parse the configuration string
* specified by `--config-str`. Otherwise, a contents of configuration file
Expand All @@ -142,7 +147,7 @@ case class CliOptions(
def scalafmtConfig: Configured[ScalafmtConfig] =
hoconOpt.fold(Configured.ok(baseConfig))(Config.fromConf(_, baseConfig))

private lazy val hoconOpt: Option[ConfParsed] =
private[cli] lazy val hoconOpt: Option[ConfParsed] =
configStr.map(ConfParsed.fromString(_)).orElse {
canonicalConfigFile.map(
_.fold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import org.scalafmt.CompatCollections.JavaConverters._

object FileOps {

val defaultConfigFileName = ".scalafmt.conf"

def getLastModifiedMsec(file: Path): Long = {
val attributes = getAttributesNoLinks(file)
val mtime = attributes.lastModifiedTime().toMillis
Expand Down Expand Up @@ -134,7 +136,7 @@ object FileOps {
}

def tryGetConfigInDir(dir: AbsoluteFile): Option[Try[Path]] =
tryCheckConfigFile(dir / ".scalafmt.conf")
tryCheckConfigFile(dir / defaultConfigFileName)

private def tryCheckConfigFile(file: AbsoluteFile): Option[Try[Path]] =
if (!file.exists) None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ object GitOps {
def getRootConfigFile: Option[Try[Path]] =
obj.rootDir.flatMap(FileOps.tryGetConfigInDir)

def getProposedConfigFile(
cwd: AbsoluteFile,
config: Option[Path] = None
): AbsoluteFile =
config.fold {
obj.rootDir.getOrElse(cwd) / FileOps.defaultConfigFileName
}(cwd / _)

}

}
Expand Down
16 changes: 10 additions & 6 deletions scalafmt-tests/src/test/scala/org/scalafmt/cli/CliTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -813,9 +813,11 @@ class CliTest extends AbstractCliTest with CliTestBehavior {
),
ExitCode.UnsupportedVersion,
assertOut = out => {
val eol = System.lineSeparator()
val msg = s"error: missing version (current $stableVersion)$eol"
assertEquals(out, msg + msg) // two invocations
val out1 = out.substring(0, out.length / 2)
val out2 = out.substring(out1.length)
assertEquals(out1, out2) // two invocations
val msg = s"error: missing Scalafmt configuration file."
assertEquals(out1.substring(0, msg.length), msg)
}
)
}
Expand All @@ -838,9 +840,11 @@ class CliTest extends AbstractCliTest with CliTestBehavior {
),
ExitCode.UnsupportedVersion,
assertOut = out => {
val eol = System.lineSeparator()
val msg = s"error: missing version (current $stableVersion)$eol"
assertEquals(out, msg + msg) // two invocations
val out1 = out.substring(0, out.length / 2)
val out2 = out.substring(out1.length)
assertEquals(out1, out2) // two invocations
val msg = s"error: missing Scalafmt version."
assertEquals(out1.substring(0, msg.length), msg)
}
)
}
Expand Down

0 comments on commit 8c2ba7f

Please sign in to comment.