Skip to content

Commit

Permalink
add args parser
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-skrodzka committed Nov 14, 2024
1 parent 9a2d1c6 commit 950aced
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
15 changes: 15 additions & 0 deletions scalafix/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("//rules:scala.bzl", "scala_binary")

scala_binary(
name = "scalafmt",
args = [
"--toolchain=",
"--opts=:--verbose:--config",
],
main_class = "rules_scala3.scalafix.src.Main",
scala = "//scala:bootstrap_3",
visibility = ["//visibility:public"],
deps = [
"@rules_scala3//3rdparty/jvm/com/github/scopt",
],
)
Empty file added scalafix/run_scalafix.bzl
Empty file.
43 changes: 43 additions & 0 deletions scalafix/src/ArgsConfig.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package rules_scala3.scalafix.src

import scopt.OParser

case class ArgsConfig(
toolchain: Option[String] = None,
scalafixOpts: Option[String] = None,
targets: Seq[String] = Seq.empty,
excludedTargets: Seq[String] = Seq.empty
)

object ArgsConfig:
private val builder = OParser.builder[ArgsConfig]
import builder.*

private val parser = OParser.sequence(
programName("scalafix-runner"),
head("scalafix-runner", "1.0"),

help('h', "help").text("Displays this usage text"),

opt[String]('t', "toolchain")
.required()
.action((value, config) => config.copy(toolchain = Some(value)))
.text("Required. Specifies the toolchain with which targets will be built"),

opt[String]('o', "opts")
.action((value, config) => config.copy(scalafixOpts = Some(value)))
.text("Options passed to scalafix as a colon-separated string (e.g., :--verbose:--config)"),

arg[String]("<target>...")
.unbounded()
.optional()
.action((target, config) => config.copy(targets = config.targets :+ target))
.text("Specifies the Bazel targets to be included"),

checkConfig { config =>
val (excluded, included) = config.targets.partition(_.startsWith("-"))
Right(config.copy(excludedTargets = excluded.map(_.drop(1)), targets = included))
}
)

def parse(args: Seq[String]): Option[ArgsConfig] = OParser.parse(parser, args, ArgsConfig())
5 changes: 5 additions & 0 deletions scalafix/src/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package rules_scala3.scalafix.src

object Main:
def main(args: Array[String]): Unit =
given Vars = ArgsConfig(args.toIndexedSeq).getOrElse(sys.exit(2))

0 comments on commit 950aced

Please sign in to comment.