Skip to content

Commit c22748a

Browse files
authored
Add --version output. (#100)
1 parent 928599c commit c22748a

3 files changed

Lines changed: 44 additions & 30 deletions

File tree

build.sbt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
enablePlugins(JavaAppPackaging)
2+
enablePlugins(BuildInfoPlugin)
3+
enablePlugins(GitVersioning)
24

35
organization := "org.renci"
46

@@ -16,6 +18,14 @@ javaOptions += "-Xmx8G"
1618

1719
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")
1820

21+
val gitCommitString = SettingKey[String]("gitCommit")
22+
23+
gitCommitString := git.gitHeadCommit.value.getOrElse("Not Set")
24+
25+
buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion, gitCommitString)
26+
27+
buildInfoPackage := "org.renci.relationgraph"
28+
1929
val zioVersion = "1.0.13"
2030

2131
libraryDependencies ++= {

project/plugins.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.7")
2+
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.10.0")
3+
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.2")

src/main/scala/org/renci/relationgraph/ZCaseApp.scala

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import zio.console.{Console, putStrLn}
1111
import java.io.IOException
1212

1313
/**
14-
* Adapted from caseapp.cats.IOCaseApp
15-
*/
14+
* Adapted from caseapp.cats.IOCaseApp
15+
*/
1616
abstract class ZCaseApp[T](implicit val parser0: Parser[T], val messages: Help[T]) extends App {
1717

1818
private[this] def parser: Parser[T] = {
@@ -35,42 +35,44 @@ abstract class ZCaseApp[T](implicit val parser0: Parser[T], val messages: Help[T
3535
putStrLn(messages.withHelp.usage).as(ExitCode.success)
3636

3737
/**
38-
* Arguments are expanded then parsed. By default, argument expansion is the identity function.
39-
* Overriding this method allows plugging in an arbitrary argument expansion logic.
40-
*
41-
* One such expansion logic involves replacing each argument of the form '@<file>' with the
42-
* contents of that file where each line in the file becomes a distinct argument.
43-
* To enable this behavior, override this method as shown below.
44-
*
45-
* @example
46-
* {{{
47-
* import caseapp.core.parser.PlatformArgsExpander
48-
* override def expandArgs(args: List[String]): List[String]
49-
* = PlatformArgsExpander.expand(args)
50-
* }}}
51-
* @param args
52-
* @return
53-
*/
38+
* Arguments are expanded then parsed. By default, argument expansion is the identity function.
39+
* Overriding this method allows plugging in an arbitrary argument expansion logic.
40+
*
41+
* One such expansion logic involves replacing each argument of the form '@<file>' with the
42+
* contents of that file where each line in the file becomes a distinct argument.
43+
* To enable this behavior, override this method as shown below.
44+
*
45+
* @example
46+
* {{{
47+
* import caseapp.core.parser.PlatformArgsExpander
48+
* override def expandArgs(args: List[String]): List[String]
49+
* = PlatformArgsExpander.expand(args)
50+
* }}}
51+
* @param args
52+
* @return
53+
*/
5454
private[this] def expandArgs(args: List[String]): List[String] = args
5555

5656
/**
57-
* Whether to stop parsing at the first unrecognized argument.
58-
*
59-
* That is, stop parsing at the first non option (not starting with "-"), or
60-
* the first unrecognized option. The unparsed arguments are put in the `args`
61-
* argument of `run`.
62-
*/
57+
* Whether to stop parsing at the first unrecognized argument.
58+
*
59+
* That is, stop parsing at the first non option (not starting with "-"), or
60+
* the first unrecognized option. The unparsed arguments are put in the `args`
61+
* argument of `run`.
62+
*/
6363
private[this] def stopAtFirstUnrecognized: Boolean = false
6464

6565
private[this] def nameFormatter: Formatter[Name] = Formatter.DefaultNameFormatter
6666

67-
override def run(args: List[String]): ZIO[ZEnv, Nothing, ExitCode] =
68-
parser.withHelp.detailedParse(expandArgs(args), stopAtFirstUnrecognized) match {
69-
case Left(err) => error(err).orDie
70-
case Right((WithHelp(_, true, _), _)) => helpAsked.orDie
71-
case Right((WithHelp(true, _, _), _)) => usageAsked.orDie
72-
case Right((WithHelp(_, _, Left(err)), _)) => error(err).orDie
67+
override def run(args: List[String]): ZIO[ZEnv, Nothing, ExitCode] = {
68+
if (args == List("--version")) ZIO.succeed(println(org.renci.relationgraph.BuildInfo.toString)).exitCode
69+
else parser.withHelp.detailedParse(expandArgs(args), stopAtFirstUnrecognized) match {
70+
case Left(err) => error(err).orDie
71+
case Right((WithHelp(_, true, _), _)) => helpAsked.orDie
72+
case Right((WithHelp(true, _, _), _)) => usageAsked.orDie
73+
case Right((WithHelp(_, _, Left(err)), _)) => error(err).orDie
7374
case Right((WithHelp(_, _, Right(t)), remainingArgs)) => run(t, remainingArgs)
7475
}
76+
}
7577

7678
}

0 commit comments

Comments
 (0)