Added environment variable expansion support to the server config file

- use a replaceAll of a compile time regex to expand environment variables of
the form ${ENV_VAR} in the import paths
- added a unit test for the regex
- updated docs
This commit is contained in:
Georgi Dimitrov 2016-03-15 04:58:00 -07:00
parent ce5ac2d978
commit b20857f4fd
2 changed files with 26 additions and 2 deletions

View File

@ -224,7 +224,11 @@ The server will attempt to read the file ```${XDG_CONFIG_HOME}/dcd/dcd.conf```
```dcd.conf``` on Windows in the current working directory on startup. ```dcd.conf``` on Windows in the current working directory on startup.
If it exists, each line of the file is interpreted as a path that should be If it exists, each line of the file is interpreted as a path that should be
searched when looking for module imports. Lines that start with the "#" character searched when looking for module imports. Lines that start with the "#" character
are ignored. are ignored. Lines can contain environment variables which will be expanded
during loading. The name of the environment variable needs to the enclosed in
${VAR}. For example:
${HOME}/sysroot/usr/include/dmd/phobos
Keep in mind that DCD treats import paths the same way that the compiler does. Keep in mind that DCD treats import paths the same way that the compiler does.
For example, a configuration file like this will not work as expected: For example, a configuration file like this will not work as expected:

View File

@ -376,11 +376,29 @@ void warnAboutOldConfigLocation()
} }
} }
import std.regex : ctRegex;
alias envVarRegex = ctRegex!(`\$\{([_a-zA-Z][_a-zA-Z 0-9]*)\}`);
private unittest
{
import std.regex : replaceAll;
enum input = `${HOME}/aaa/${_bb_b}/ccc`;
assert(replaceAll!(m => m[1])(input, envVarRegex) == `HOME/aaa/_bb_b/ccc`);
}
/** /**
* Loads import directories from the configuration file * Loads import directories from the configuration file
*/ */
string[] loadConfiguredImportDirs() string[] loadConfiguredImportDirs()
{ {
string expandEnvVars(string l)
{
import std.regex : replaceAll;
return replaceAll!(m => environment.get(m[1], ""))(l, envVarRegex);
}
warnAboutOldConfigLocation(); warnAboutOldConfigLocation();
immutable string configLocation = getConfigurationLocation(); immutable string configLocation = getConfigurationLocation();
if (!configLocation.exists()) if (!configLocation.exists())
@ -388,8 +406,10 @@ string[] loadConfiguredImportDirs()
info("Loading configuration from ", configLocation); info("Loading configuration from ", configLocation);
File f = File(configLocation, "rt"); File f = File(configLocation, "rt");
return f.byLine(KeepTerminator.no) return f.byLine(KeepTerminator.no)
.filter!(a => a.length > 0 && a[0] != '#' && existanceCheck(a)) .filter!(a => a.length > 0 && a[0] != '#')
.map!(a => a.idup) .map!(a => a.idup)
.map!(expandEnvVars)
.filter!(a => existanceCheck(a))
.array(); .array();
} }