From 3c1142d7d74c3a57dab7b629eb492323c9a244a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Thu, 11 Feb 2010 16:42:23 +0100 Subject: [PATCH] Expand environment variables in Generic Project file lists Environment variables will now be expanded in the Generic Project file and include path lists. They have to be written like $$(VARIABLE), like in qmake .pro files. Task-number: QTCREATORBUG-366 --- .../genericprojectmanager/genericproject.cpp | 46 +++++++++++++++---- .../genericprojectmanager/genericproject.h | 4 +- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/plugins/genericprojectmanager/genericproject.cpp b/src/plugins/genericprojectmanager/genericproject.cpp index fe58e7271e4..f9facc241d7 100644 --- a/src/plugins/genericprojectmanager/genericproject.cpp +++ b/src/plugins/genericprojectmanager/genericproject.cpp @@ -41,6 +41,7 @@ #include #include +#include #include #include @@ -170,11 +171,11 @@ void GenericProject::parseProject(RefreshOptions options) if (options & Files) { m_rawListEntries.clear(); m_rawFileList = readLines(filesFileName()); - m_files = convertToAbsoluteFiles(m_rawFileList, &m_rawListEntries); + m_files = processEntries(m_rawFileList, &m_rawListEntries); } if (options & Configuration) { - m_projectIncludePaths = convertToAbsoluteFiles(readLines(includesFileName())); + m_projectIncludePaths = processEntries(readLines(includesFileName())); // TODO: Possibly load some configuration from the project file //QSettings projectInfo(m_fileName, QSettings::IniFormat); @@ -249,22 +250,49 @@ void GenericProject::refresh(RefreshOptions options) } /** - * The \a map variable is an optional argument that will map the returned - * absolute paths back to their original \a paths. + * Expands environment variables in the given \a string when they are written + * like $$(VARIABLE). */ -QStringList GenericProject::convertToAbsoluteFiles(const QStringList &paths, - QHash *map) const +static void expandEnvironmentVariables(const QProcessEnvironment &env, QString &string) { + const static QRegExp candidate(QLatin1String("\\$\\$\\((.+)\\)")); + + int index = candidate.indexIn(string); + while (index != -1) { + const QString value = env.value(candidate.cap(1)); + + string.replace(index, candidate.matchedLength(), value); + index += value.length(); + + index = candidate.indexIn(string, index); + } +} + +/** + * Expands environment variables and converts the path from relative to the + * project to an absolute path. + * + * The \a map variable is an optional argument that will map the returned + * absolute paths back to their original \a entries. + */ +QStringList GenericProject::processEntries(const QStringList &paths, + QHash *map) const +{ + const QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); const QDir projectDir(QFileInfo(m_fileName).dir()); + QStringList absolutePaths; foreach (const QString &path, paths) { - if (path.trimmed().isEmpty()) + QString trimmedPath = path.trimmed(); + if (trimmedPath.isEmpty()) continue; - const QString absPath = QFileInfo(projectDir, path).absoluteFilePath(); + expandEnvironmentVariables(env, trimmedPath); + + const QString absPath = QFileInfo(projectDir, trimmedPath).absoluteFilePath(); absolutePaths.append(absPath); if (map) - map->insert(absPath, path); + map->insert(absPath, trimmedPath); } absolutePaths.removeDuplicates(); return absolutePaths; diff --git a/src/plugins/genericprojectmanager/genericproject.h b/src/plugins/genericprojectmanager/genericproject.h index 79fdcf0204a..ccbf9886af1 100644 --- a/src/plugins/genericprojectmanager/genericproject.h +++ b/src/plugins/genericprojectmanager/genericproject.h @@ -117,8 +117,8 @@ protected: private: bool saveRawFileList(const QStringList &rawFileList); void parseProject(RefreshOptions options); - QStringList convertToAbsoluteFiles(const QStringList &paths, - QHash *map = 0) const; + QStringList processEntries(const QStringList &paths, + QHash *map = 0) const; Manager *m_manager; QString m_fileName;