Last active
March 11, 2023 03:29
-
-
Save Slakah/2b3fea03adf84a44287c42fa535c3e94 to your computer and use it in GitHub Desktop.
Macro to read resources at compile time
This file contains hidden or 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
package fastparse.protobuf | |
import java.nio.file.{Files, Paths} | |
import scala.reflect.macros.whitebox | |
import scala.jdk.CollectionConverters._ | |
object Resources { | |
def readResource(path: String): String = macro Resources.readResourceImpl | |
def readResourceImpl(c: whitebox.Context)(path: c.Expr[String]): c.Expr[String] = { | |
import c.universe._ | |
val Literal(Constant(thePath: String)) = path.tree | |
val resourcePath = Paths.get(this.getClass.getResource("/" + thePath).toURI) | |
val body = Files.readAllLines(resourcePath).asScala.mkString("\n") | |
c.Expr[String](Literal(Constant(body))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment