www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get fils at compile-time

reply bauss (wtf happend to my name took some old cached title LOL??) writes:
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".

There could even be more files, so eventually I'd have to load 
all files in somehow.

Ex.
enum string[] compileTimeFiles = loadFilesSomehowHere("styles");

Now if it was possible to just get the file names into 
`compileTimeFiles` then I could use "import" on each file name, 
but how would I even get them into that, if it's even possible?

Tbh. it's a very reasonable use-case if you ask me. Especially if 
it comes to dynamic generation of either code related to UI 
applications, web applications, websites etc.
Dec 14 2016
next sibling parent bauss <jj_1337 live.dk> writes:
On Wednesday, 14 December 2016 at 20:47:23 UTC, 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.

 [...]
Also excuse me for the title. It's supposed to be "files"
Dec 14 2016
prev sibling next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
I did a lot of work in this area.
There are two solutions:

1) Use a package file and public import all modules via it. From this do 
some form of 'registration' in a module constructor. Using 
__traits(allMembers, retrieve the imports and with that the other modules.
2) Before compilation create a file that contains a list of all the 
module names.

I prefer 2 now days.
Dec 14 2016
parent reply Bauss <jj_1337 live.dk> writes:
On Thursday, 15 December 2016 at 02:58:11 UTC, rikki cattermole 
wrote:
 I did a lot of work in this area.
 There are two solutions:

 1) Use a package file and public import all modules via it. 
 From this do some form of 'registration' in a module 
 constructor. Using __traits(allMembers, retrieve the imports 
 and with that the other modules.
 2) Before compilation create a file that contains a list of all 
 the module names.

 I prefer 2 now days.
It's not D modules. I'm talking about loading dynamic file content from dynamically created files. The first one won't work, because it only imports modules and you still have to give DMD each module that you compile. The second one won't work either, because again the files are not D modules. They're regular files that I need to handle at compile-time. Besides your suggestion still requires manual work, which is what I need to avoid. I know how I can do it by specifying the files, but that's exactly what I want to avoid. I don't want to specify which files I need to handle, I just need to be able to handle all files in a folder without having to specify them somewhere after the creation.
Dec 14 2016
parent rikki cattermole <rikki cattermole.co.nz> writes:
On 15/12/2016 8:11 PM, Bauss wrote:
 On Thursday, 15 December 2016 at 02:58:11 UTC, rikki cattermole wrote:
 I did a lot of work in this area.
 There are two solutions:

 1) Use a package file and public import all modules via it. From this
 do some form of 'registration' in a module constructor. Using
 __traits(allMembers, retrieve the imports and with that the other
 modules.
 2) Before compilation create a file that contains a list of all the
 module names.

 I prefer 2 now days.
It's not D modules. I'm talking about loading dynamic file content from dynamically created files. The first one won't work, because it only imports modules and you still have to give DMD each module that you compile. The second one won't work either, because again the files are not D modules. They're regular files that I need to handle at compile-time. Besides your suggestion still requires manual work, which is what I need to avoid. I know how I can do it by specifying the files, but that's exactly what I want to avoid. I don't want to specify which files I need to handle, I just need to be able to handle all files in a folder without having to specify them somewhere after the creation.
The second will still work, that file that contains a list of modules names can just as easily be a list of files to string import. Or is the files themselves it is after all generated by an external program before compilation. An example of this would be bin2d (looks like I never bothered to register it on code.dlang.org)[0]. [0] https://github.com/rikkimax/Bin2D
Dec 14 2016
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
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
Dec 14 2016
parent Bauss <jj_1337 live.dk> writes:
On Thursday, 15 December 2016 at 07:56:40 UTC, Jacob Carlborg 
wrote:
 On 2016-12-14 21:47, bauss (wtf happend to my name took some 
 old cached title LOL??) wrote:
 [...]
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: [...]
Yeah it was something like that I thought I might had to do. Shame that you can't just do it through D itself. Thanks.
Dec 15 2016