From b20857f4fd51f9a627ea28b3e0401251f2c8d893 Mon Sep 17 00:00:00 2001 From: Georgi Dimitrov Date: Tue, 15 Mar 2016 04:58:00 -0700 Subject: [PATCH] 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 --- README.md | 6 +++++- src/server/server.d | 22 +++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 42e420c..bc2e64e 100644 --- a/README.md +++ b/README.md @@ -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. 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 -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. For example, a configuration file like this will not work as expected: diff --git a/src/server/server.d b/src/server/server.d index f0eac2c..d13abdb 100644 --- a/src/server/server.d +++ b/src/server/server.d @@ -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 */ string[] loadConfiguredImportDirs() { + string expandEnvVars(string l) + { + import std.regex : replaceAll; + return replaceAll!(m => environment.get(m[1], ""))(l, envVarRegex); + } + warnAboutOldConfigLocation(); immutable string configLocation = getConfigurationLocation(); if (!configLocation.exists()) @@ -388,8 +406,10 @@ string[] loadConfiguredImportDirs() info("Loading configuration from ", configLocation); File f = File(configLocation, "rt"); 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!(expandEnvVars) + .filter!(a => existanceCheck(a)) .array(); }