Get fils at compile-time

Jacob Carlborg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 14 23:56:40 PST 2016


On 2016-12-14 21:47, bauss (wtf happend to my name took some old cached 
title LOL??) wrote:
> Is there a way to get all files in a folder at compile-time.
>
> To be more specific I want to import the content of all files within a
> specific folder during compile-time. I know how to do it with a specific
> file using `import("filename")`, but what if I wanted to do it at
> compile-time for all files in a folder then loop through those files and
> handle their content.
>
> To give an example.
>
> Let's say I have a folder "styles" it has 3 files "global.stylesheet",
> "main.stylesheet", "shared.stylesheet".
>
> Now I don't know about these file's names, so I can't just do it
> manually as they could be "foo", "bar" and "baz", just like they're
> "global", "main" and "shared".

I would recommend creating a small script that iterates a directory and 
generates a D file with string imports for all the files, something like:

module foo;

immutable files = [import("global.stylesheet"), 
import("main.stylesheet"), import("shared.stylesheet")];

If you need the names of the files as well you could generate an 
associative array instead. The name of the D module and the variable in 
the module would always be the same to be able to reference it from 
other modules.

If your using Dub you can run the script automatically before 
compilation using the "preGenerateCommands" build setting. You can write 
the script in D and invoke it using "dmd -run generate_files.d". 
Remember to add -J with the path to the folder to your build script, or 
use the "importPaths" build setting if you're using Dub.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list