Scala macro for embedding file content directly into a scala source file at compile time.
add the following to build.sbt:
libraryDependencies +="com.github.passivsystems"%"embed"%"0.0.1"
resolvers +="jitpack" at "https://jitpack.io"first import the macro:
importcom.passivsystems.embed.Embed._then can reference any file with a path relative to the current source file:
valtext:String= embed("resource.txt")By using the sEmbed, we can have similar semantics to s"" String interpolation. The embedded file can refer to any variable in scope at the point of call.
Note, that the curly braces are required for all embedded expressions. e.g. ${myVal} not $myVal
e.g.
valguard=truevaltxt= sEmbed("template.txt")where template.txt:
the guard is: ${if (guard) "TRUE" else "FALSE"}
One possible usecase is in Scala js projects to move html into their own file. e.g.
importcom.passivsystems.embed.Embed._importorg.scalajs.domdefel[T<: dom.raw.Element](id: String) =
dom.document.getElementById(id).asInstanceOf[T]
valbtnId="uniqueId"// el[dom.raw.HTMLElement]("div").innerHTML = s"""// <button id="${btnId}">Click me</button>// """
el[dom.raw.HTMLElement]("div").innerHTML = sEmbed("button.html")
el[dom.raw.HTMLButtonElement](btnId).onclick = { event: MouseEvent=> println("Clicked!") }where button.html:
<buttonid="${btnId}">Click me</button>