From 4e30ce160e7e7ea4e2f7801d15b1659f19a27177 Mon Sep 17 00:00:00 2001 From: hjk Date: Tue, 25 May 2021 15:25:37 +0200 Subject: [PATCH] Utils: Start collecting environment changes instead of full environments We have a couple of cases where classes need to modify environments but have only the ability to provide complete environments without actually knowing the full context. Starting with systemEnvironment usually works so far, but breaks down in cases of e.g. containerized builds. This patch here introduces the ability to collect changes to environments, and postpones creating a full environment until the context is clear. As introductory example use BaseQtVersion::qmakeRunEnvironment Change-Id: Id23c0aa80fc95b0a8a01f8e3a87adc9b1a57002a Reviewed-by: Christian Stenger --- src/libs/utils/environment.cpp | 31 +++++++++++++++++++++++++++++++ src/libs/utils/environment.h | 20 ++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/libs/utils/environment.cpp b/src/libs/utils/environment.cpp index e02a407dbc9..fe1769daffa 100644 --- a/src/libs/utils/environment.cpp +++ b/src/libs/utils/environment.cpp @@ -401,4 +401,35 @@ optional EnvironmentProvider::provider(const QByteArray &id return nullopt; } +void EnvironmentChange::addSetValue(const QString &key, const QString &value) +{ + m_changeItems.append([key, value](Environment &env) { env.set(key, value); }); +} + +void EnvironmentChange::addUnsetValue(const QString &key) +{ + m_changeItems.append([key](Environment &env) { env.unset(key); }); +} + +void EnvironmentChange::addPrependToPath(const QString &value) +{ + m_changeItems.append([value](Environment &env) { env.prependOrSetPath(value); }); +} + +void EnvironmentChange::addAppendToPath(const QString &value) +{ + m_changeItems.append([value](Environment &env) { env.appendOrSetPath(value); }); +} + +void EnvironmentChange::addModify(const NameValueItems &items) +{ + m_changeItems.append([items](Environment &env) { env.modify(items); }); +} + +void EnvironmentChange::applyToEnvironment(Environment &env) const +{ + for (const Item &item : m_changeItems) + item(env); +} + } // namespace Utils diff --git a/src/libs/utils/environment.h b/src/libs/utils/environment.h index 75e7aeb4aa9..0ed2d9b613c 100644 --- a/src/libs/utils/environment.h +++ b/src/libs/utils/environment.h @@ -86,6 +86,26 @@ private: QSet &alreadyChecked); }; +class QTCREATOR_UTILS_EXPORT EnvironmentChange final +{ +public: + using Item = std::function; + + EnvironmentChange() = default; + + void applyToEnvironment(Environment &) const; + + void addSetValue(const QString &key, const QString &value); + void addUnsetValue(const QString &key); + void addPrependToPath(const QString &value); + void addAppendToPath(const QString &value); + void addModify(const NameValueItems &items); + void addChange(const Item &item) { m_changeItems.append(item); } + +private: + QList m_changeItems; +}; + class QTCREATOR_UTILS_EXPORT EnvironmentProvider { public: