Skip to content

Instantly share code, notes, and snippets.

@antonioj-mattos
Forked from Slakah/Resources.scala
Created March 11, 2023 03:29
Show Gist options
  • Select an option

  • Save antonioj-mattos/fbb83efb96e23b95f45af4741f159c40 to your computer and use it in GitHub Desktop.

Select an option

Save antonioj-mattos/fbb83efb96e23b95f45af4741f159c40 to your computer and use it in GitHub Desktop.

Revisions

  1. @Slakah Slakah revised this gist Aug 26, 2019. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions Resources.scala
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,6 @@ object Resources {
    def readResourceImpl(c: whitebox.Context)(path: c.Expr[String]): c.Expr[String] = {
    import c.universe._
    val Literal(Constant(thePath: String)) = path.tree
    println(this.getClass.getResource("/"))
    println(this.getClass.getResource("/" + thePath).toURI)
    val resourcePath = Paths.get(this.getClass.getResource("/" + thePath).toURI)
    val body = Files.readAllLines(resourcePath).asScala.mkString("\n")

  2. @Slakah Slakah created this gist Aug 26, 2019.
    23 changes: 23 additions & 0 deletions Resources.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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
    println(this.getClass.getResource("/"))
    println(this.getClass.getResource("/" + thePath).toURI)
    val resourcePath = Paths.get(this.getClass.getResource("/" + thePath).toURI)
    val body = Files.readAllLines(resourcePath).asScala.mkString("\n")


    c.Expr[String](Literal(Constant(body)))
    }
    }