The request is very straightforward: To load a file that is in my internal project src/
directory into memory. Finally, print its content to the screen. Any help, please?
If we use File.new("path/to/file")
it opens a file in the local file system, so that's not what I'm searching for.
Greetings.
The next Crystal release 0.27.1 will include a read_file
macro. The change is already in master, but not released yet.
With this macro, you can just do {{ read_file("path/to/file") }}
and it will expand to a string containing the contents of the file (at compile time).
Until 0.27.1 is released, a typical workaround is to shell out: {{ `cat path/to/file`.stringify }}
A more sophisticated solution for compiling many files into the binary and accessing them in a virtual file system at runtime is schovi/baked_file_system.