Elm Version: 0.19.1
Is there a way to use project-metadata-utils
(or some other tool / solution) to do some amount of metaprogramming within an Elm project? Simple stuff like "get own file name", "get own file directory" which I would then use within my elm application.
What I'd like is something like this:
moduleName : String
moduleName = MetaData.moduleName
Fundamentally, Elm doesn't provide access to that kind of information (there are not Reflection APIs), and so there is no single good way to do this purely within Elm. You could try to use Ports and JS, but if you are talking about file names of the Elm source, it is unlikely to still be data that is there in the compiled code.
Realistically, the way to do this right now would probably be to implement it as a code generation build step in whatever your build process is, and generate some simple Elm files that just contain the constants you need.
For example, one of my projects does this using a simple NPM prebuild
script that just does a simple sed
command to insert the version (found from an environment variable set by the build environment) into an Elm file. This is obviously very simple and fragile, but you could create a more robust system to provide more metadata if you wanted.
Obviously the downside is that there is always the possibility of running the Elm build without updating the metadata. There is no way to avoid this that I know of, as the elm compiler doesn't current offer any hooks.