-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Take macro implementations from izumi Subsumes #181
- Loading branch information
Showing
11 changed files
with
126 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
...btgen/sbtmeta/ProjectAttributeMacro.scala → ...btgen/sbtmeta/ProjectAttributeMacro.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
sbtmeta/src/main/scala-2/izumi/sbtgen/sbtmeta/SbtgenMeta.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package izumi.sbtgen.sbtmeta | ||
|
||
import scala.language.experimental.macros | ||
|
||
object SbtgenMeta { | ||
def projectRoot(): Option[String] = macro ProjectAttributeMacro.findProjectRootMacro | ||
|
||
def extractSbtProjectVersion(): Option[String] = macro ProjectAttributeMacro.extractProjectVersionMacro | ||
|
||
def extractScalaVersions(): Option[String] = macro ProjectAttributeMacro.extractScalaVersionsMacro | ||
|
||
def extractMandatory(name: String): Option[String] = macro ProjectAttributeMacro.extractAttrMacro | ||
} |
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
sbtmeta/src/main/scala-3/izumi.sbtgen.sbtmeta/BuildAttributes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package izumi.sbtgen.sbtmeta | ||
|
||
import java.time.LocalDateTime | ||
|
||
object BuildAttributes { | ||
|
||
inline def sbtProjectRoot(): Option[String] = ${ BuildAttributesImpl.sbtProjectRoot() } | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
sbtmeta/src/main/scala-3/izumi.sbtgen.sbtmeta/BuildAttributesImpl.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package izumi.sbtgen.sbtmeta | ||
|
||
import java.nio.file.Path | ||
import java.time.LocalDateTime | ||
|
||
import scala.annotation.tailrec | ||
import scala.quoted.{Expr, Quotes} | ||
|
||
object BuildAttributesImpl { | ||
|
||
def sbtProjectRoot()(using quotes: Quotes): Expr[Option[String]] = { | ||
import quotes.reflect.* | ||
|
||
val result = SourceFile.current.getJPath | ||
.flatMap(findProjectRoot) | ||
.map(_.toFile.getCanonicalPath) | ||
|
||
Expr(result) | ||
} | ||
|
||
@tailrec | ||
private def findProjectRoot(cp: Path): Option[Path] = { | ||
if (cp.resolve("build.sbt").toFile.exists()) { | ||
Some(cp) | ||
} else { | ||
val parent = cp.getParent | ||
|
||
if (parent == null || parent == cp.getRoot) { | ||
None | ||
} else { | ||
findProjectRoot(parent) | ||
} | ||
} | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
sbtmeta/src/main/scala-3/izumi.sbtgen.sbtmeta/MacroParameters.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package izumi.sbtgen.sbtmeta | ||
|
||
import scala.quoted.{Expr, Quotes, Type} | ||
|
||
object MacroParameters { | ||
|
||
inline def scalaCrossVersions(): Option[String] = macroSetting("scala-versions") | ||
|
||
inline def artifactVersion(): Option[String] = macroSetting("product-version") | ||
|
||
inline def macroSetting(inline name: String): Option[String] = { | ||
${ MacroParametersImpl.extractString('{ name }) } | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
sbtmeta/src/main/scala-3/izumi.sbtgen.sbtmeta/MacroParametersImpl.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package izumi.sbtgen.sbtmeta | ||
|
||
import scala.annotation.experimental | ||
import scala.quoted.{Expr, Quotes, Type} | ||
|
||
@experimental | ||
object MacroParametersImpl { | ||
|
||
def extractString(name: Expr[String])(using quotes: Quotes): Expr[Option[String]] = { | ||
Expr(extract(name.valueOrAbort)) | ||
} | ||
|
||
def extractBool(name: Expr[String])(using quotes: Quotes): Expr[Option[Boolean]] = { | ||
val value = extract(name.valueOrAbort) | ||
val isTrue = value.map(_.toLowerCase).map(v => v == "true" || v == "1") | ||
Expr(isTrue) | ||
} | ||
|
||
private def extract(name: String)(using quotes: Quotes): Option[String] = { | ||
import quotes.reflect.* | ||
val prefix = s"$name=" | ||
val value = CompilationInfo.XmacroSettings.filter(_.startsWith(prefix)).map(_.stripPrefix(prefix)).lastOption | ||
if (value.isEmpty) { | ||
report.info(s"Undefined macro parameter $name, add `-Xmacro-settings:$prefix<value>` into `scalac` options") | ||
} | ||
value | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
sbtmeta/src/main/scala-3/izumi.sbtgen.sbtmeta/SbtgenMeta.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package izumi.sbtgen.sbtmeta | ||
|
||
object SbtgenMeta { | ||
inline def projectRoot(): Option[String] = ${ BuildAttributesImpl.sbtProjectRoot() } | ||
|
||
inline def extractSbtProjectVersion(): Option[String] = MacroParameters.artifactVersion() | ||
|
||
inline def extractScalaVersions(): Option[String] = MacroParameters.scalaCrossVersions() | ||
|
||
inline def extractMandatory(inline name: String): Option[String] = MacroParameters.macroSetting(name) | ||
} |
27 changes: 0 additions & 27 deletions
27
sbtmeta/src/main/scala/izumi/sbtgen/sbtmeta/SbtgenMeta.scala
This file was deleted.
Oops, something went wrong.