Skip to content

Commit

Permalink
Merge pull request #35 from alexarchambault/topic/annotations
Browse files Browse the repository at this point in the history
Update things, and move annotations to a separate project
  • Loading branch information
alexarchambault committed May 15, 2016
2 parents 4780bca + caf5a5f commit 0ccffc8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,16 @@
package caseapp

import core.util._
import scala.annotation.StaticAnnotation

/**
* Extra name for the annotated argument
*/
case class Name(name: String) extends StaticAnnotation {
private def isShort = name.length == 1

val optionName = pascalCaseSplit(name.toList).map(_.toLowerCase).mkString("-")
def option = opt
private val opt = if (isShort) s"-$name" else s"--$optionName"
private val optEq = if (isShort) s"-$name=" else s"--$optionName="

def apply(args: List[String], isFlag: Boolean): Option[List[String]] = args match {
case Nil => None
case h :: t =>
if (h == opt)
Some(t)
else if (!isFlag && h.startsWith(optEq))
Some(h.drop(optEq.length) :: t)
else
None
}

def apply(arg: String): Either[Unit, Option[String]] =
if (arg == opt)
Right(None)
else if (arg.startsWith(optEq))
Right(Some(arg.drop(optEq.length)))
else
Left(())
}
case class Name(name: String) extends StaticAnnotation

/**
* Description of the value of the annotated argument
*/
case class ValueDescription(description: String) extends StaticAnnotation {
def message: String = s"<$description>"
}
case class ValueDescription(description: String) extends StaticAnnotation

/**
* Help message for the annotated argument
Expand Down
21 changes: 15 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@

lazy val `case-app` = project.in(file("."))
.aggregate(utilJVM, utilJS, coreJVM, coreJS, doc)
.dependsOn(utilJVM, coreJVM)
.aggregate(utilJVM, utilJS, annotationsJVM, annotationsJS, coreJVM, coreJS, doc)
.dependsOn(utilJVM, annotationsJVM, coreJVM)
.settings(commonSettings)
.settings(noPublishSettings)
.settings(
name := "case-app-root"
)

lazy val annotations = crossProject
.settings(commonSettings: _*)
.settings(
name := "case-app-annotations"
)

lazy val annotationsJVM = annotations.jvm
lazy val annotationsJS = annotations.js

lazy val util = crossProject
.settings(commonSettings: _*)
.settings(
Expand All @@ -23,7 +32,7 @@ lazy val utilJVM = util.jvm
lazy val utilJS = util.js

lazy val core = crossProject
.dependsOn(util)
.dependsOn(annotations, util)
.settings(commonSettings: _*)
.settings(
name := "case-app",
Expand Down Expand Up @@ -90,12 +99,12 @@ lazy val fullReleaseSettings = Seq(
</developer>
</developers>
},
credentials += {
credentials ++= {
Seq("SONATYPE_USER", "SONATYPE_PASS").map(sys.env.get) match {
case Seq(Some(user), Some(pass)) =>
Credentials("Sonatype Nexus Repository Manager", "oss.sonatype.org", user, pass)
Seq(Credentials("Sonatype Nexus Repository Manager", "oss.sonatype.org", user, pass))
case _ =>
Credentials(Path.userHome / ".ivy2" / ".credentials")
Seq.empty
}
}
)
Expand Down
2 changes: 0 additions & 2 deletions core/jvm/src/test/scala/caseapp/PlatformTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package caseapp

import java.util.GregorianCalendar

import caseapp.core.Parser

import org.scalatest.{ Matchers, FlatSpec }

object PlatformTests {
Expand Down
39 changes: 39 additions & 0 deletions core/shared/src/main/scala/caseapp/core/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package caseapp

package object core {

implicit class NameOps(name: Name) {

private def isShort = name.name.length == 1

private def optionName = caseapp.core.util.pascalCaseSplit(name.name.toList).map(_.toLowerCase).mkString("-")
private def optionEq = if (isShort) s"-${name.name}=" else s"--$optionName="

def option: String = if (isShort) s"-${name.name}" else s"--$optionName"

def apply(args: List[String], isFlag: Boolean): Option[List[String]] = args match {
case Nil => None
case h :: t =>
if (h == option)
Some(t)
else if (!isFlag && h.startsWith(optionEq))
Some(h.drop(optionEq.length) :: t)
else
None
}

def apply(arg: String): Either[Unit, Option[String]] =
if (arg == option)
Right(None)
else if (arg.startsWith(optionEq))
Right(Some(arg.drop(optionEq.length)))
else
Left(())
}

implicit class ValueDescriptionOps(desc: ValueDescription) {

def message: String = s"<${desc.description}>"
}

}
4 changes: 2 additions & 2 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ resolvers += Resolver.url(

addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0")
addSbtPlugin("org.tpolecat" % "tut-plugin" % "0.4.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.8")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.9")

addSbtPlugin("com.github.alexarchambault" % "coursier-sbt-plugin" % "1.0.0-M10")
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.0-M11")

0 comments on commit 0ccffc8

Please sign in to comment.