Advertisement
mitrakov

Scala Version

Jun 17th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 5.99 KB | None | 0 0
  1. /*
  2.  * ScalaVersion.scala
  3.  * https://alvinalexander.com/java/jwarehouse/scala-2.11/compiler/scala/tools/nsc/settings/ScalaVersion.scala.shtml
  4.  * NSC -- new Scala compiler
  5.  * Copyright 2005-2013 LAMP/EPFL
  6.  * @author  James Iry
  7.  */
  8. // $Id$
  9.  
  10. package scala
  11. package tools.nsc.settings
  12.  
  13. /**
  14.  * Represents a single Scala version in a manner that
  15.  * supports easy comparison and sorting.
  16.  */
  17. sealed abstract class ScalaVersion extends Ordered[ScalaVersion] {
  18.   def unparse: String
  19. }
  20.  
  21. /**
  22.  * A scala version that sorts higher than all actual versions
  23.  */
  24. case object NoScalaVersion extends ScalaVersion {
  25.   def unparse = "none"
  26.  
  27.   def compare(that: ScalaVersion): Int = that match {
  28.     case NoScalaVersion => 0
  29.     case _ => 1
  30.   }
  31. }
  32.  
  33. /**
  34.  * A specific Scala version, not one of the magic min/max versions. An SpecificScalaVersion
  35.  * may or may not be a released version - i.e. this same class is used to represent
  36.  * final, release candidate, milestone, and development builds. The build argument is used
  37.  * to segregate builds
  38.  */
  39. case class SpecificScalaVersion(major: Int, minor: Int, rev: Int, build: ScalaBuild) extends ScalaVersion {
  40.   def unparse = s"${major}.${minor}.${rev}.${build.unparse}"
  41.  
  42.   def compare(that: ScalaVersion): Int =  that match {
  43.     case SpecificScalaVersion(thatMajor, thatMinor, thatRev, thatBuild) =>
  44.       // this could be done more cleanly by importing scala.math.Ordering.Implicits, but we have to do these
  45.       // comparisons a lot so I'm using brute force direct style code
  46.       if (major < thatMajor) -1
  47.       else if (major > thatMajor) 1
  48.       else if (minor < thatMinor) -1
  49.       else if (minor > thatMinor) 1
  50.       else if (rev < thatRev) -1
  51.       else if (rev > thatRev) 1
  52.       else build compare thatBuild
  53.     case AnyScalaVersion => 1
  54.     case NoScalaVersion => -1
  55.   }
  56. }
  57.  
  58. /**
  59.  * A Scala version that sorts lower than all actual versions
  60.  */
  61. case object AnyScalaVersion extends ScalaVersion {
  62.   def unparse = "any"
  63.  
  64.   def compare(that: ScalaVersion): Int = that match {
  65.     case AnyScalaVersion => 0
  66.     case _ => -1
  67.   }
  68. }
  69.  
  70. /**
  71.  * Factory methods for producing ScalaVersions
  72.  */
  73. object ScalaVersion {
  74.   private val dot = "\\."
  75.   private val dash = "\\-"
  76.   private def not(s:String) = s"[^${s}]"
  77.   private val R = s"((${not(dot)}*)(${dot}(${not(dot)}*)(${dot}(${not(dash)}*)(${dash}(.*))?)?)?)".r
  78.  
  79.   def apply(versionString : String, errorHandler: String => Unit): ScalaVersion = {
  80.     def errorAndValue() = {
  81.         errorHandler(
  82.           s"There was a problem parsing ${versionString}. " +
  83.           "Versions should be in the form major[.minor[.revision]] " +
  84.           "where each part is a positive number, as in 2.10.1. " +
  85.           "The minor and revision parts are optional."
  86.         )
  87.         AnyScalaVersion
  88.     }
  89.  
  90.     def toInt(s: String) = s match {
  91.       case null | "" => 0
  92.       case _ => s.toInt
  93.     }
  94.  
  95.     def isInt(s: String) = util.Try(toInt(s)).isSuccess
  96.  
  97.     def toBuild(s: String) = s match {
  98.       case null | "FINAL" => Final
  99.       case s if (s.toUpperCase.startsWith("RC") && isInt(s.substring(2))) => RC(toInt(s.substring(2)))
  100.       case s if (s.toUpperCase.startsWith("M") && isInt(s.substring(1))) => Milestone(toInt(s.substring(1)))
  101.       case _ => Development(s)
  102.     }
  103.  
  104.     try versionString match {
  105.       case "none" => NoScalaVersion
  106.       case "any" => AnyScalaVersion
  107.       case R(_, majorS, _, minorS, _, revS, _, buildS) =>
  108.         SpecificScalaVersion(toInt(majorS), toInt(minorS), toInt(revS), toBuild(buildS))
  109.       case _ =>
  110.         errorAndValue()
  111.     } catch {
  112.       case e: NumberFormatException => errorAndValue()
  113.     }
  114.   }
  115.  
  116.   def apply(versionString: String): ScalaVersion =
  117.       apply(versionString, msg => throw new NumberFormatException(msg))
  118.  
  119.   /**
  120.    * The version of the compiler running now
  121.    */
  122.   val current = apply(util.Properties.versionNumberString)
  123.  
  124.   /**
  125.    * The 2.8.0 version.
  126.    */
  127.   val twoDotEight = SpecificScalaVersion(2, 8, 0, Final)
  128. }
  129.  
  130. /**
  131.  * Represents the data after the dash in major.minor.rev-build
  132.  */
  133. abstract class ScalaBuild extends Ordered[ScalaBuild] {
  134.   /**
  135.    * Return a version of this build information that can be parsed back into the
  136.    * same ScalaBuild
  137.    */
  138.   def unparse: String
  139. }
  140. /**
  141.  * A development, test, nightly, snapshot or other "unofficial" build
  142.  */
  143. case class Development(id: String) extends ScalaBuild {
  144.   def unparse = s"-${id}"
  145.  
  146.   def compare(that: ScalaBuild) = that match {
  147.     // sorting two development builds based on id is reasonably valid for two versions created with the same schema
  148.     // otherwise it's not correct, but since it's impossible to put a total ordering on development build versions
  149.     // this is a pragmatic compromise
  150.     case Development(thatId) => id compare thatId
  151.     // assume a development build is newer than anything else, that's not really true, but good luck
  152.     // mapping development build versions to other build types
  153.     case _ => 1
  154.   }
  155. }
  156. /**
  157.  * A final final
  158.  */
  159. case object Final extends ScalaBuild {
  160.   def unparse = ""
  161.  
  162.   def compare(that: ScalaBuild) = that match {
  163.     case Final => 0
  164.     // a final is newer than anything other than a development build or another final
  165.     case Development(_) => -1
  166.     case _ => 1
  167.   }
  168. }
  169.  
  170. /**
  171.  * A candidate for final release
  172.  */
  173. case class RC(n: Int) extends ScalaBuild {
  174.   def unparse = s"-RC${n}"
  175.  
  176.   def compare(that: ScalaBuild) = that match {
  177.     // compare two rcs based on their RC numbers
  178.     case RC(thatN) => n - thatN
  179.     // an rc is older than anything other than a milestone or another rc
  180.     case Milestone(_) => 1
  181.     case _ => -1
  182.   }
  183. }
  184.  
  185. /**
  186.  * An intermediate release
  187.  */
  188. case class Milestone(n: Int) extends ScalaBuild {
  189.   def unparse = s"-M${n}"
  190.  
  191.   def compare(that: ScalaBuild) = that match {
  192.     // compare two milestones based on their milestone numbers
  193.     case Milestone(thatN) => n - thatN
  194.     // a milestone is older than anything other than another milestone
  195.     case _ => -1
  196.  
  197.   }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement