From 8dacb72f9db46d0c2cdf8b404cc0f1fba86267f5 Mon Sep 17 00:00:00 2001 From: Miklos Martin Date: Thu, 26 Oct 2017 16:31:40 +0200 Subject: [PATCH 1/4] Fixes #3297 and #3531 Add commandName as an extension method in Command --- main-command/src/main/scala/sbt/Command.scala | 7 +++++++ sbt/src/sbt-test/actions/command-name/build.sbt | 5 +++++ sbt/src/sbt-test/actions/command-name/test | 1 + 3 files changed, 13 insertions(+) create mode 100644 sbt/src/sbt-test/actions/command-name/build.sbt create mode 100644 sbt/src/sbt-test/actions/command-name/test diff --git a/main-command/src/main/scala/sbt/Command.scala b/main-command/src/main/scala/sbt/Command.scala index f90aedc8e..05798ff55 100644 --- a/main-command/src/main/scala/sbt/Command.scala +++ b/main-command/src/main/scala/sbt/Command.scala @@ -187,6 +187,13 @@ object Command { def spacedC(name: String, c: Parser[Char]): Parser[String] = ((c & opOrIDSpaced(name)) ~ c.+) map { case (f, rem) => (f +: rem).mkString } + + implicit class CommandWithName(cmd: Command) { + def commandName: Option[String] = cmd match { + case sc: SimpleCommand => Some(sc.name) + case _ => None + } + } } trait Help { diff --git a/sbt/src/sbt-test/actions/command-name/build.sbt b/sbt/src/sbt-test/actions/command-name/build.sbt new file mode 100644 index 000000000..1a04a2dcc --- /dev/null +++ b/sbt/src/sbt-test/actions/command-name/build.sbt @@ -0,0 +1,5 @@ +val command = Command.command("noop") { s => s } + +TaskKey[Unit]("check") := { + assert(command.commandName == Some("noop"), """command.commandName should be "noop"""") +} diff --git a/sbt/src/sbt-test/actions/command-name/test b/sbt/src/sbt-test/actions/command-name/test new file mode 100644 index 000000000..a5912a391 --- /dev/null +++ b/sbt/src/sbt-test/actions/command-name/test @@ -0,0 +1 @@ +> check \ No newline at end of file From 79e75f02e5e9a42d33f226034068f48f1b5df074 Mon Sep 17 00:00:00 2001 From: Miklos Martin Date: Thu, 26 Oct 2017 16:32:54 +0200 Subject: [PATCH 2/4] add newline --- sbt/src/sbt-test/actions/command-name/test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sbt/src/sbt-test/actions/command-name/test b/sbt/src/sbt-test/actions/command-name/test index a5912a391..15675b169 100644 --- a/sbt/src/sbt-test/actions/command-name/test +++ b/sbt/src/sbt-test/actions/command-name/test @@ -1 +1 @@ -> check \ No newline at end of file +> check From 951c78a4d774a8f474df04553d83dd49dc88691f Mon Sep 17 00:00:00 2001 From: Miklos Martin Date: Fri, 27 Oct 2017 20:10:30 +0200 Subject: [PATCH 3/4] better naming + value class --- main-command/src/main/scala/sbt/Command.scala | 4 ++-- sbt/src/sbt-test/actions/command-name/build.sbt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/main-command/src/main/scala/sbt/Command.scala b/main-command/src/main/scala/sbt/Command.scala index 05798ff55..48324aaf5 100644 --- a/main-command/src/main/scala/sbt/Command.scala +++ b/main-command/src/main/scala/sbt/Command.scala @@ -188,8 +188,8 @@ object Command { def spacedC(name: String, c: Parser[Char]): Parser[String] = ((c & opOrIDSpaced(name)) ~ c.+) map { case (f, rem) => (f +: rem).mkString } - implicit class CommandWithName(cmd: Command) { - def commandName: Option[String] = cmd match { + implicit class CommandWithName(val cmd: Command) extends AnyVal { + def nameOption: Option[String] = cmd match { case sc: SimpleCommand => Some(sc.name) case _ => None } diff --git a/sbt/src/sbt-test/actions/command-name/build.sbt b/sbt/src/sbt-test/actions/command-name/build.sbt index 1a04a2dcc..374a7d3cd 100644 --- a/sbt/src/sbt-test/actions/command-name/build.sbt +++ b/sbt/src/sbt-test/actions/command-name/build.sbt @@ -1,5 +1,5 @@ val command = Command.command("noop") { s => s } TaskKey[Unit]("check") := { - assert(command.commandName == Some("noop"), """command.commandName should be "noop"""") + assert(command.nameOption == Some("noop"), """command.commandName should be "noop"""") } From 1f89e3dc021d65f6b2e6f084b827567a71da01db Mon Sep 17 00:00:00 2001 From: Miklos Martin Date: Thu, 2 Nov 2017 16:56:14 +0100 Subject: [PATCH 4/4] move nameOption to the trait --- main-command/src/main/scala/sbt/Command.scala | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/main-command/src/main/scala/sbt/Command.scala b/main-command/src/main/scala/sbt/Command.scala index 48324aaf5..53158daa4 100644 --- a/main-command/src/main/scala/sbt/Command.scala +++ b/main-command/src/main/scala/sbt/Command.scala @@ -25,6 +25,11 @@ sealed trait Command { def tags: AttributeMap def tag[T](key: AttributeKey[T], value: T): Command + + def nameOption: Option[String] = this match { + case sc: SimpleCommand => Some(sc.name) + case _ => None + } } private[sbt] final class SimpleCommand( @@ -187,13 +192,6 @@ object Command { def spacedC(name: String, c: Parser[Char]): Parser[String] = ((c & opOrIDSpaced(name)) ~ c.+) map { case (f, rem) => (f +: rem).mkString } - - implicit class CommandWithName(val cmd: Command) extends AnyVal { - def nameOption: Option[String] = cmd match { - case sc: SimpleCommand => Some(sc.name) - case _ => None - } - } } trait Help {