1
votes

I would like to generate scala classes based on some files. Placement of the files doesn't matter (it can be resources folder or files can be placed just near my scala source files).

How can I read them when scala macro is executed? (I use StaticAnnotation's inline def apply(defn: Any): Any method)

When I try to use resources, they can't be found (I suppose those resources can't be found because it's compile time and project is not compiled yet) I would like to read files placed right near my scala sources, but how can I get source file path when macro is executed?

1
What have you already tried? Why not rather using source generator (SBT)? - cchantep
@cchantep I'm designing a library. So code generation better be available for sbt and maven. If i got it right, enabling macro generator in maven is straightforward - just enable paradise plugin. I'm considering different options currently - trying to figure out what's better - Evgeny Nacu
Macro is a language feature, code generation is a developer tool, none is better, it depends on the case (there I won't use macro) - cchantep
yes, I see the difference ) Trying to figure out what will be better for my case. Currently it seems macro won't work good (paradise plugin is experimental and seems to be abandoned and won't be available in future versions of scala) according to this discussion: contributors.scala-lang.org/t/annotation-macros/1211/34 - Evgeny Nacu
@LukeEller I ended up with code generation - Evgeny Nacu

1 Answers

0
votes

your project dir

val projectDir = System.getProperty("user.dir")

now you can access source file

val file = new File(s"$projectDir/src/resources/<SOURCES FILE>")