www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to add an additional config dir in DUB under source?

reply tastyminerals <tastyminerals gmail.com> writes:
I have the following project structure:

source/
   media/
     icon.png
   config/
     conf.toml

In order to access "icon.png" without explicitly providing the 
path I added in dub.json

"stringImportPaths": [
     "source/media",
     "source/config"
]

It works for "icon.png" but doesn't work for "conf.toml". The 
"icon.png" can be accessed and the code below works:

     addEntry(new EmbeddedPng!("quit.png")

but std.file: readText refuses to see "conf.toml":

     readText("conf.toml");

file.d(371): conf.toml: No such file or directory

I wonder why and what am I doing wrong?
Oct 12 2020
parent reply Mike Parker <aldacron gmail.com> writes:
On Monday, 12 October 2020 at 22:31:53 UTC, tastyminerals wrote:

 I wonder why and what am I doing wrong?
This: readText("conf.toml"); "stringImportPath" (dmd's -J command line option) is specifically for D's import expression (which is different from the import statement, e.g., `import std`): https://dlang.org/spec/expression.html#import_expressions It has no effect on arbitrary filesystem calls. And, in fact, filesystem calls cannot be called at compile-time because they ultimately depend on system calls for which the source code is not available. The very first restriction on CTFE is this: "The function source code must be available to the compiler. Functions which exist in the source code only as extern declarations cannot be executed in CTFE." So if you were to do something like, `enum foo = readText("foo.txt")`, you would get a compile time error. On Windows: "Error: CreateFileW cannot be interpreted at compile time, because it has no available source code" You're calling `readText` in a normal runtime context, so it's looking file at runtime relative to the current working directory. What you need to do is something like this: ``` string config = import("config.toml"); void loadConfig() { // parse config string here } ``` I don't know where the `EmbeddedPNG` template comes from, but it has to be using the import expression internally, else it wouldn't work.
Oct 12 2020
next sibling parent Mike Parker <aldacron gmail.com> writes:
On Tuesday, 13 October 2020 at 05:13:18 UTC, Mike Parker wrote:

 not available. The very first restriction on CTFE is this:

 "The function source code must be available to the compiler. 
 Functions which exist in the source code only as extern 
 declarations cannot be executed in CTFE."
Forgot the link: https://dlang.org/spec/function.html#interpretation
Oct 12 2020
prev sibling parent tastyminerals <tastyminerals gmail.com> writes:
On Tuesday, 13 October 2020 at 05:13:18 UTC, Mike Parker wrote:
 On Monday, 12 October 2020 at 22:31:53 UTC, tastyminerals wrote:

 [...]
This: readText("conf.toml"); [...]
Thanks. I remembered that I read about them in Ali's book but never actually used them.
Oct 13 2020