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 <christian.stenger@qt.io>
This commit is contained in:
hjk
2021-05-25 15:25:37 +02:00
parent 8a5b917d8f
commit 4e30ce160e
2 changed files with 51 additions and 0 deletions

View File

@@ -401,4 +401,35 @@ optional<EnvironmentProvider> EnvironmentProvider::provider(const QByteArray &id
return nullopt; 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 } // namespace Utils

View File

@@ -86,6 +86,26 @@ private:
QSet<FilePath> &alreadyChecked); QSet<FilePath> &alreadyChecked);
}; };
class QTCREATOR_UTILS_EXPORT EnvironmentChange final
{
public:
using Item = std::function<void(Environment &)>;
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<Item> m_changeItems;
};
class QTCREATOR_UTILS_EXPORT EnvironmentProvider class QTCREATOR_UTILS_EXPORT EnvironmentProvider
{ {
public: public: