Skip to content

Commit e6f96a4

Browse files
committed
Partial port to Scala 3.1.0
1 parent 64a23d1 commit e6f96a4

File tree

7 files changed

+129
-7
lines changed

7 files changed

+129
-7
lines changed

build.sbt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ ThisBuild / publishTo :=(if (!isSnapshot.value) {
2222
Some(Opts.resolver.sonatypeSnapshots)
2323
})
2424

25+
ThisBuild / libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.10" % Test
26+
27+
val defaultScalaVersions = Seq(ScalaVersions.scala_310, ScalaVersions.scala_213, ScalaVersions.scala_212)
28+
2529
val scalaJsVersion = "1.7.0"
2630
val scalaNativeVersion = "0.4.1"
2731
val crossProjectVersion = "1.1.0"
@@ -90,21 +94,21 @@ val scalaOpts = scalacOptions ++= ((isSnapshot.value, scalaVersion.value) match
9094

9195
lazy val sbtmeta = (project in file("sbtmeta"))
9296
.settings(
93-
crossScalaVersions := Seq(ScalaVersions.scala_213, ScalaVersions.scala_212),
97+
crossScalaVersions := defaultScalaVersions,
9498
scalaVersion := crossScalaVersions.value.head,
95-
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value % Provided,
99+
libraryDependencies ++=
100+
Some("org.scala-lang" % "scala-reflect" % scalaVersion.value % Provided)
101+
.filterNot(_ => scalaVersion.value == ScalaVersions.scala_310),
96102
scalaOpts,
97103
)
98104

99105
lazy val sbtgen = (project in file("sbtgen"))
100106
.dependsOn(sbtmeta)
101107
.settings(
102-
crossScalaVersions := Seq(ScalaVersions.scala_213, ScalaVersions.scala_212),
108+
crossScalaVersions := defaultScalaVersions,
103109
scalaVersion := crossScalaVersions.value.head,
104-
// libraryDependencies += "com.github.scopt" %% "scopt" % "4.0.0-RC2",
105-
libraryDependencies += "com.github.scopt" %% "scopt" % "3.7.1",
110+
libraryDependencies += "com.github.scopt" %% "scopt" % "4.0.1",
106111
libraryDependencies += "org.scala-lang.modules" %% "scala-collection-compat" % "2.5.0",
107-
(ThisBuild / libraryDependencies) += "org.scalatest" %% "scalatest" % "3.2.10" % Test,
108112
scalacOptions ++= Seq(
109113
s"-Xmacro-settings:product-version=${version.value}",
110114
s"-Xmacro-settings:product-group=${organization.value}",

project/ScalaVersions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
object ScalaVersions {
22
val scala_212 = "2.12.15"
33
val scala_213 = "2.13.7"
4+
val scala_310 = "3.1.0"
45
}

sbtmeta/src/main/scala/izumi/sbtgen/sbtmeta/ProjectAttributeMacro.scala renamed to sbtmeta/src/main/scala-2/izumi/sbtgen/sbtmeta/ProjectAttributeMacro.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ object ProjectAttributeMacro {
1717
}
1818

1919
def extractAttrMacro(c: blackbox.Context)(name: c.Expr[String]): c.Expr[Option[String]] = {
20+
val nameStr = TreeTools.stringLiteral(c)(c.universe)(name.tree)
21+
extractAttr(c, nameStr)
22+
}
23+
24+
def extractAttrMandatoryMacro(c: blackbox.Context)(name: c.Expr[String]): c.Expr[Option[String]] = {
2025
val nameStr = TreeTools.stringLiteral(c)(c.universe)(name.tree)
2126
extractAttr(c, nameStr, force = true)
2227
}

sbtmeta/src/main/scala/izumi/sbtgen/sbtmeta/SbtgenMeta.scala renamed to sbtmeta/src/main/scala-2/izumi/sbtgen/sbtmeta/SbtgenMeta.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ object SbtgenMeta {
2323

2424
def extract(name: String): Option[String] = macro ProjectAttributeMacro.extractAttrMacro
2525

26-
def extractMandatory(name: String): Option[String] = macro ProjectAttributeMacro.extractAttrMacro
26+
def extractMandatory(name: String): Option[String] = macro ProjectAttributeMacro.extractAttrMandatoryMacro
2727
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package izumi.sbtgen.sbtmeta
2+
3+
import java.nio.file.{Path, Paths}
4+
import java.time.LocalDateTime
5+
import scala.annotation.tailrec
6+
import scala.quoted.{Expr, Quotes, quotes}
7+
import scala.util.chaining.scalaUtilChainingOps
8+
9+
object ProjectAttributeMacro {
10+
11+
def buildTimestampMacro(using Quotes): Expr[LocalDateTime] = {
12+
val time = LocalDateTime.now()
13+
'{ LocalDateTime.of(${Expr(time.getYear)}, ${Expr(time.getMonthValue)}, ${Expr(time.getDayOfMonth)}, ${Expr(time.getHour)}, ${Expr(time.getMinute)}, ${Expr(time.getSecond)}, ${Expr(time.getNano)}) }
14+
}
15+
16+
def extractAttrMacro(name: Expr[String])(using Quotes): Expr[Option[String]] = {
17+
val nameStr = name.valueOrAbort
18+
extractAttr(nameStr)
19+
}
20+
21+
def extractAttrMandatoryMacro(name: Expr[String])(using Quotes): Expr[Option[String]] = {
22+
val nameStr = name.valueOrAbort
23+
extractAttr(nameStr, force = true)
24+
}
25+
26+
def extractProjectGroupIdMacro(using Quotes): Expr[Option[String]] = {
27+
extractAttr("product-group")
28+
}
29+
30+
def extractSbtVersionMacro(using Quotes): Expr[Option[String]] = {
31+
extractAttr("sbt-version")
32+
}
33+
34+
def extractScalatestVersionMacro(using Quotes): Expr[Option[String]] = {
35+
extractAttr("scalatest-version")
36+
}
37+
38+
def extractScalaVersionsMacro(using Quotes): Expr[Option[String]] = {
39+
extractAttr("scala-versions")
40+
}
41+
42+
def extractScalaVersionMacro(using Quotes): Expr[Option[String]] = {
43+
extractAttr("scala-version")
44+
}
45+
46+
def extractProjectVersionMacro(using Quotes): Expr[Option[String]] = {
47+
extractAttr("product-version")
48+
}
49+
50+
private def extractAttr(name: String, force: Boolean = false)(using Quotes): Expr[Option[String]] = {
51+
var scala3MacroSettingsExist: Boolean = true
52+
val macroSettings: List[String] = {
53+
quotes.reflect.report.warning(
54+
"""Required implementation of -Xmacro-settings doesn't exist in Scala 3 yet.
55+
|
56+
|Please fix Dotty issue #12038 to proceed.
57+
|
58+
|See: https://github.com/lampepfl/dotty/issues/12038
59+
| - https://github.com/lampepfl/dotty/pull/12039""".stripMargin
60+
)
61+
scala3MacroSettingsExist = false
62+
Nil
63+
}
64+
val prefix = s"$name="
65+
val value = macroSettings.find(_.startsWith(prefix)).filterNot(_.isEmpty).map(_.stripPrefix(prefix))
66+
if (value.isEmpty) {
67+
s"Undefined macro parameter $name, add `-Xmacro-settings:$prefix<value>` into `scalac` options"
68+
`pipe` (if (force && scala3MacroSettingsExist) quotes.reflect.report.errorAndAbort else quotes.reflect.report.warning)
69+
}
70+
Expr[Option[String]](value)
71+
}
72+
73+
def findProjectRootMacro(using Quotes): Expr[Option[String]] = {
74+
val srcPath = quotes.reflect.Position.ofMacroExpansion.sourceFile.getJPath.getOrElse(
75+
quotes.reflect.report.errorAndAbort("Couldn't find file path of the current file")
76+
)
77+
val result = projectRoot(srcPath).map(_.toFile.getCanonicalPath)
78+
Expr[Option[String]](result)
79+
}
80+
81+
@tailrec
82+
private def projectRoot(cp: Path): Option[Path] = {
83+
if (cp.resolve("build.sbt").toFile.exists()) {
84+
Some(cp)
85+
} else {
86+
val parent = cp.getParent
87+
88+
if (parent == null || parent == cp.getRoot) {
89+
None
90+
} else {
91+
projectRoot(parent)
92+
}
93+
}
94+
}
95+
96+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package izumi.sbtgen.sbtmeta
2+
3+
import java.time.LocalDateTime
4+
5+
object SbtgenMeta {
6+
inline def buildTimestamp(): LocalDateTime = ${ ProjectAttributeMacro.buildTimestampMacro }
7+
inline def projectRoot(): Option[String] = ${ ProjectAttributeMacro.findProjectRootMacro }
8+
inline def extractSbtProjectGroupId(): Option[String] = ${ ProjectAttributeMacro.extractProjectGroupIdMacro }
9+
inline def extractSbtProjectVersion(): Option[String] = ${ ProjectAttributeMacro.extractProjectVersionMacro }
10+
inline def extractSbtVersion(): Option[String] = ${ ProjectAttributeMacro.extractSbtVersionMacro }
11+
inline def extractScalatestVersion(): Option[String] = ${ ProjectAttributeMacro.extractScalatestVersionMacro }
12+
inline def extractScalaVersion(): Option[String] = ${ ProjectAttributeMacro.extractScalaVersionMacro }
13+
inline def extractScalaVersions(): Option[String] = ${ ProjectAttributeMacro.extractScalaVersionsMacro }
14+
inline def extract(inline name: String): Option[String] = ${ ProjectAttributeMacro.extractAttrMacro('{name}) }
15+
inline def extractMandatory(inline name: String): Option[String] = ${ ProjectAttributeMacro.extractAttrMandatoryMacro('{name}) }
16+
}

0 commit comments

Comments
 (0)