|
| 1 | +package third_party.dependency_analyzer.src.main.io.bazel.rulesscala.dependencyanalyzer |
| 2 | + |
| 3 | +import scala.language.experimental.macros |
| 4 | +import scala.reflect.macros.blackbox |
| 5 | + |
| 6 | +object ScalaVersion { |
| 7 | + val Current: ScalaVersion = ScalaVersion(util.Properties.versionNumberString) |
| 8 | + |
| 9 | + def apply(versionString: String): ScalaVersion = { |
| 10 | + versionString.split("\\.") match { |
| 11 | + case Array(superMajor, major, minor) => |
| 12 | + new ScalaVersion(superMajor.toInt, major.toInt, minor.toInt) |
| 13 | + case _ => |
| 14 | + throw new Exception(s"Failed to parse version $versionString") |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Runs [code] only if minVersion and maxVersion constraints are met. |
| 20 | + * |
| 21 | + * NOTE: This method should be used only rarely. Most of the time |
| 22 | + * just comparing versions in code should be enough. This is needed |
| 23 | + * only when the code we want to run can't compile under certain |
| 24 | + * versions. The reason to use this rarely is the API's inflexibility |
| 25 | + * and the difficulty in debugging this code. |
| 26 | + * |
| 27 | + * Each of minVersionOpt and maxVersionOpt can either be None |
| 28 | + * to signify that there is no restriction on this bound, |
| 29 | + * or it can be a string of a full version number such as "2.12.10". |
| 30 | + * |
| 31 | + * When set to a version number, the bounds are inclusive. |
| 32 | + * For example, a maxVersion of "2.12.10" will accept version "2.12.10". |
| 33 | + * |
| 34 | + * Note only literal strings are accepted and inlined variables are accepted. |
| 35 | + * If any non-inlined variables are passed the code will fail to compile. |
| 36 | + * Inlined variables are generally those declared final on an object which |
| 37 | + * do not have a type attached. |
| 38 | + * |
| 39 | + * valid: |
| 40 | + * conditional(Some("2.12.4"), None, "foo()") |
| 41 | + * invalid: |
| 42 | + * conditional(MinVersionForFoo, None, "foo()") |
| 43 | + */ |
| 44 | + def conditional( |
| 45 | + minVersionOpt: Option[String], |
| 46 | + maxVersionOpt: Option[String], |
| 47 | + code: String |
| 48 | + ): Unit = |
| 49 | + macro conditionalImpl |
| 50 | + |
| 51 | + def conditionalImpl( |
| 52 | + c: blackbox.Context |
| 53 | + )( |
| 54 | + minVersionOpt: c.Expr[Option[String]], |
| 55 | + maxVersionOpt: c.Expr[Option[String]], |
| 56 | + code: c.Expr[String] |
| 57 | + ): c.Tree = { |
| 58 | + import c.{universe => u} |
| 59 | + |
| 60 | + // Due to non-deterministic code generation of quasiquotes, we do |
| 61 | + // not use them |
| 62 | + // See https://github.com/scala/bug/issues/11008 |
| 63 | + // Eventually once we stop supporting all versions which don't have |
| 64 | + // the bugfix, we can use quasiquotes as desired |
| 65 | + |
| 66 | + def extractStringFromTree(tree: c.Tree): Option[String] = { |
| 67 | + tree match { |
| 68 | + case u.Literal(u.Constant(s: String)) => |
| 69 | + Some(s) |
| 70 | + case _ => |
| 71 | + None |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + def extractStringOption(expr: c.Expr[Option[String]]): Option[String] = { |
| 76 | + expr.tree match { |
| 77 | + case u.Apply( |
| 78 | + u.TypeApply( |
| 79 | + u.Select(u.Select(u.Ident(u.TermName("scala")), u.TermName("Some")), u.TermName("apply")), |
| 80 | + List(u.TypeTree())), |
| 81 | + str :: Nil |
| 82 | + ) if extractStringFromTree(str).nonEmpty => |
| 83 | + extractStringFromTree(str) |
| 84 | + case u.Select(u.Ident(u.TermName("scala")), u.TermName("None")) => |
| 85 | + None |
| 86 | + case _ => |
| 87 | + c.error( |
| 88 | + expr.tree.pos, |
| 89 | + "Parameter must be passed as an Option[String] literal such as " + |
| 90 | + "Some(\"2.12.10\") or None") |
| 91 | + None |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + def extractString(expr: c.Expr[String]): String = { |
| 96 | + extractStringFromTree(expr.tree).getOrElse { |
| 97 | + c.error( |
| 98 | + expr.tree.pos, |
| 99 | + "Parameter must be passed as a string literal such as \"2.12.10\"") |
| 100 | + "" |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + val meetsMinVersionRequirement = { |
| 105 | + val minVersionOptValue = extractStringOption(minVersionOpt) |
| 106 | + |
| 107 | + // Note: Unit tests do not test that this bound is inclusive rather |
| 108 | + // than exclusive so be careful when changing this code not to |
| 109 | + // accidentally make this an exclusive bound (see ScalaVersionTest for |
| 110 | + // details) |
| 111 | + minVersionOptValue.forall(version => Current >= ScalaVersion(version)) |
| 112 | + } |
| 113 | + |
| 114 | + val meetsMaxVersionRequirement = { |
| 115 | + val maxVersionOptValue = extractStringOption(maxVersionOpt) |
| 116 | + // Note: Unit tests do not test that this bound is inclusive rather |
| 117 | + // than exclusive so be careful when changing this code not to |
| 118 | + // accidentally make this an exclusive bound (see ScalaVersionTest for |
| 119 | + // details) |
| 120 | + maxVersionOptValue.forall(version => Current <= ScalaVersion(version)) |
| 121 | + } |
| 122 | + |
| 123 | + if (meetsMinVersionRequirement && meetsMaxVersionRequirement) { |
| 124 | + c.parse(extractString(code)) |
| 125 | + } else { |
| 126 | + u.EmptyTree |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +class ScalaVersion private( |
| 132 | + private val superMajor: Int, |
| 133 | + private val major: Int, |
| 134 | + private val minor: Int |
| 135 | +) extends Ordered[ScalaVersion] { |
| 136 | + override def compare(that: ScalaVersion): Int = { |
| 137 | + if (this.superMajor != that.superMajor) { |
| 138 | + this.superMajor.compareTo(that.superMajor) |
| 139 | + } else if (this.major != that.major) { |
| 140 | + this.major.compareTo(that.major) |
| 141 | + } else { |
| 142 | + this.minor.compareTo(that.minor) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + override def equals(obj: Any): Boolean = { |
| 147 | + obj match { |
| 148 | + case that: ScalaVersion => |
| 149 | + compare(that) == 0 |
| 150 | + case _ => |
| 151 | + false |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + override def toString: String = { |
| 156 | + s"$superMajor.$major.$minor" |
| 157 | + } |
| 158 | +} |
0 commit comments