Skip to content

Commit

Permalink
Merge pull request #87 from Kevin-Lee/task/86/0-dependency
Browse files Browse the repository at this point in the history
Close #86 - Make just-semver 0-dependency project
  • Loading branch information
kevin-lee committed May 28, 2021
2 parents ec0cff6 + 20cb148 commit dd11a03
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 34 deletions.
32 changes: 15 additions & 17 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ProjectInfo._
import kevinlee.sbt.SbtCommon.crossVersionProps
import just.semver.SemVer
import SemVer.{Major, Minor}
import SemVer.Major
import kevinlee.sbt.SbtCommon.crossVersionProps
import org.scoverage.coveralls.Imports.CoverallsKeys._

ThisBuild / scalaVersion := props.ProjectScalaVersion
Expand All @@ -12,7 +12,7 @@ ThisBuild / developers := List(
"Kevin-Lee",
"Kevin Lee",
"kevin.code@kevinlee.io",
url("https://github.com/Kevin-Lee"),
url("https://github.com/Kevin-Lee")
)
)
ThisBuild / homepage := url("https://github.com/Kevin-Lee/just-semver").some
Expand All @@ -28,16 +28,16 @@ lazy val justSemVer = (project in file("."))
.settings(
name := "just-semver",
description := "Semantic Versioning (SemVer) for Scala",
Compile / unmanagedSourceDirectories ++= {
val sharedSourceDir = (ThisBuild / baseDirectory).value / "src/main"
if (isScala3(scalaVersion.value))
Seq(sharedSourceDir / "scala-3")
else if (scalaVersion.value.startsWith("2.13") || scalaVersion.value.startsWith("2.12"))
Seq(sharedSourceDir / "scala-2.12_2.13")
else
Seq(sharedSourceDir / "scala-2.10_2.11")
Compile / unmanagedSourceDirectories := {
val sharedSourceDir = baseDirectory.value / "src/main"
val moreSrcs =
if (scalaVersion.value.startsWith("2.13") || scalaVersion.value.startsWith("2.12"))
Seq(sharedSourceDir / "scala-2.12_2.13")
else
Seq.empty[File]
((Compile / unmanagedSourceDirectories).value ++ moreSrcs).distinct
},
libraryDependencies := Seq(libs.justFp) ++
libraryDependencies :=
crossVersionProps(Seq.empty[ModuleID], SemVer.parseUnsafe(scalaVersion.value)) {
case (Major(3), _, _) =>
libs.hedgehogLibs(props.hedgehogVersion) ++
Expand Down Expand Up @@ -104,7 +104,7 @@ lazy val justSemVer = (project in file("."))
case _ =>
true
}),
coverallsTokenFile := s"""${Path.userHome.absolutePath}/.coveralls-credentials""".some,
coverallsTokenFile := s"""${Path.userHome.absolutePath}/.coveralls-credentials""".some
/* } Coveralls */

)
Expand All @@ -125,7 +125,7 @@ lazy val props =
"2.11.12",
"2.12.13",
"2.13.5",
ProjectScalaVersion,
ProjectScalaVersion
).distinct

final val hedgehogVersion = "0.7.0"
Expand All @@ -138,11 +138,9 @@ lazy val libs =
def hedgehogLibs(hedgehogVersion: String): Seq[ModuleID] = Seq(
"qa.hedgehog" %% "hedgehog-core" % hedgehogVersion % Test,
"qa.hedgehog" %% "hedgehog-runner" % hedgehogVersion % Test,
"qa.hedgehog" %% "hedgehog-sbt" % hedgehogVersion % Test,
"qa.hedgehog" %% "hedgehog-sbt" % hedgehogVersion % Test
)

lazy val justFp: ModuleID = "io.kevinlee" %% "just-fp-core" % "1.6.0"

}

def isScala3(scalaVersion: String): Boolean = scalaVersion.startsWith("3.")
16 changes: 16 additions & 0 deletions src/main/scala-2.11/just/semver/Compat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package just.semver

trait Compat {
@SuppressWarnings(Array("org.wartremover.warts.ImplicitConversion"))
implicit def eitherCompat[A, B](either: Either[A, B]): Compat.EitherCompat[A, B] = new Compat.EitherCompat(either)
}
object Compat {
final class EitherCompat[A, B](val either: Either[A, B]) extends AnyVal {

@inline def map[C](f: B => C): Either[A, C] =
either.right.map(f)

@inline def flatMap[C >: A, D](f: B => Either[C, D]): Either[C, D] =
either.right.flatMap(f)
}
}
7 changes: 7 additions & 0 deletions src/main/scala-2.12_2.13/just/semver/Compat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package just.semver

/**
* @author Kevin Lee
* @since 2021-05-28
*/
trait Compat
7 changes: 7 additions & 0 deletions src/main/scala-3/just/semver/Compat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package just.semver

/**
* @author Kevin Lee
* @since 2021-05-28
*/
trait Compat
18 changes: 16 additions & 2 deletions src/main/scala/just/Common.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ package just

import scala.annotation.tailrec

import just.fp.syntax._

/**
* @author Kevin Lee
* @since 2018-12-15
*/
object Common {

@SuppressWarnings(Array("org.wartremover.warts.Equals"))
implicit final class EqualA[A](val a1: A) extends AnyVal {
@inline def ===(a2: A): Boolean = a1 == a2
@inline def !==(a2: A): Boolean = a1 != a2
}

@inline def none[A]: Option[A] = None

implicit final class Ops[A](val a: A) extends AnyVal {
@inline def some: Option[A] = Some(a)

@inline def asRight[B]: Either[B, A] = Right[B, A](a)
@inline def asLeft[B]: Either[A, B] = Left[A, B](a)
}

// $COVERAGE-OFF$
@tailrec
def compareElems[A : Ordering](x: Seq[A], y: Seq[A]): Int = {
Expand Down
12 changes: 6 additions & 6 deletions src/main/scala/just/semver/AdditionalInfo.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package just.semver

import just.fp.syntax._
import just.Common._

/**
* @author Kevin Lee
* @since 2018-10-21
*/
object AdditionalInfo {
object AdditionalInfo extends Compat {

import Anh._

Expand Down Expand Up @@ -51,21 +51,21 @@ object AdditionalInfo {
.map(_.split("\\."))
.map(_.map(Dsv.parse)) match {
case Some(preRelease) =>
preRelease.foldRight[Either[ParseError, List[Dsv]]](List.empty.right) {
preRelease.foldRight(List.empty[Dsv].asRight[ParseError]) {
(x, acc) =>
x.flatMap(validator) match {
case Right(alp) =>
acc.map(alps => alp :: alps)
case Left(error) =>
error.left
error.asLeft[List[Dsv]]
}
}
case None =>
List.empty.right
List.empty[Dsv].asRight[ParseError]
}
alphaNumHyphens.map {
case Nil =>
none
none[List[Dsv]]
case xs =>
xs.some
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/scala/just/semver/Dsv.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package just.semver

import just.Common.compareElems

import just.fp.syntax._
import just.Common._

import scala.annotation.tailrec

Expand All @@ -16,7 +14,7 @@ final case class Dsv(values: List[Anh]) extends Ordered[Dsv] {
compareElems(this.values, that.values)
}

object Dsv {
object Dsv extends Compat {

import Anh._

Expand Down
3 changes: 1 addition & 2 deletions src/test/scala/just/semver/Gens.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package just.semver

import hedgehog._

import just.Common._
import just.GenPlus

import just.fp.syntax._

import just.semver.AdditionalInfo.{BuildMetaInfo, PreRelease}
import just.semver.SemVer.{Major, Minor, Patch}

Expand Down
5 changes: 2 additions & 3 deletions src/test/scala/just/semver/SemVerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import Anh.{alphabet, hyphen, num, numFromStringUnsafe}
import hedgehog._
import hedgehog.runner._

import just.Common._
import just.semver.SemVer.{Major, Minor, Patch}

import just.fp._, syntax._

/**
* @author Kevin Lee
* @since 2018-11-04
Expand Down Expand Up @@ -493,7 +492,7 @@ object SemVerSpec extends Properties {
minor <- Gens.genMinor.log("major")
patch <- Gens.genPatch.log("major")
} yield {
SemVer.semVer(major, minor, patch).right[ParseError] ====
SemVer.semVer(major, minor, patch).asRight[ParseError] ====
SemVer.parse(s"${major.major.toString}.${minor.minor.toString}.${patch.patch.toString}")
}

Expand Down

0 comments on commit dd11a03

Please sign in to comment.