forked from qt-creator/qt-creator
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:
@@ -401,4 +401,35 @@ optional<EnvironmentProvider> 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
|
||||
|
||||
@@ -86,6 +86,26 @@ private:
|
||||
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
|
||||
{
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user