Utils: Retain more high level information on EnvironmentChange::Item

Change-Id: I78df3cf7ae767d6baeed46f03587ee21875c8315
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
hjk
2022-06-01 13:04:57 +02:00
parent 3b02d7b32a
commit 6bbddf115c
2 changed files with 45 additions and 16 deletions

View File

@@ -433,44 +433,61 @@ optional<EnvironmentProvider> EnvironmentProvider::provider(const QByteArray &id
void EnvironmentChange::addSetValue(const QString &key, const QString &value)
{
m_changeItems.append([key, value](Environment &env) { env.set(key, value); });
m_changeItems.append({Item::SetValue, QVariant::fromValue(QPair<QString, QString>(key, value))});
}
void EnvironmentChange::addUnsetValue(const QString &key)
{
m_changeItems.append([key](Environment &env) { env.unset(key); });
m_changeItems.append({Item::UnsetValue, key});
}
void EnvironmentChange::addPrependToPath(const FilePaths &values)
{
for (int i = values.size(); --i >= 0; ) {
const FilePath value = values.at(i);
m_changeItems.append([value](Environment &env) { env.prependOrSetPath(value); });
m_changeItems.append({Item::PrependToPath, value.toVariant()});
}
}
void EnvironmentChange::addAppendToPath(const FilePaths &values)
{
for (const FilePath &value : values)
m_changeItems.append([value](Environment &env) { env.appendOrSetPath(value); });
}
void EnvironmentChange::addModify(const NameValueItems &items)
{
m_changeItems.append([items](Environment &env) { env.modify(items); });
m_changeItems.append({Item::AppendToPath, value.toVariant()});
}
EnvironmentChange EnvironmentChange::fromFixedEnvironment(const Environment &fixedEnv)
{
EnvironmentChange change;
change.m_changeItems.append([fixedEnv](Environment &env) { env = fixedEnv; });
change.m_changeItems.append({Item::SetFixedEnvironment, QVariant::fromValue(fixedEnv)});
return change;
}
void EnvironmentChange::applyToEnvironment(Environment &env) const
{
for (const Item &item : m_changeItems)
item(env);
for (const Item &item : m_changeItems) {
switch (item.type) {
case Item::SetSystemEnvironment:
env = Environment::systemEnvironment();
break;
case Item::SetFixedEnvironment:
env = item.data.value<Environment>();
break;
case Item::SetValue: {
auto data = item.data.value<QPair<QString, QString>>();
env.set(data.first, data.second);
break;
}
case Item::UnsetValue:
env.unset(item.data.toString());
break;
case Item::PrependToPath:
env.prependOrSetPath(FilePath::fromVariant(item.data));
break;
case Item::AppendToPath:
env.appendOrSetPath(FilePath::fromVariant(item.data));
break;
}
}
}
} // namespace Utils

View File

@@ -132,10 +132,24 @@ private:
class QTCREATOR_UTILS_EXPORT EnvironmentChange final
{
public:
using Item = std::function<void(Environment &)>;
EnvironmentChange() = default;
class Item final
{
public:
enum Type {
SetSystemEnvironment,
SetFixedEnvironment,
SetValue,
UnsetValue,
PrependToPath,
AppendToPath,
};
Type type;
QVariant data;
};
static EnvironmentChange fromFixedEnvironment(const Environment &fixedEnv);
void applyToEnvironment(Environment &) const;
@@ -144,8 +158,6 @@ public:
void addUnsetValue(const QString &key);
void addPrependToPath(const FilePaths &values);
void addAppendToPath(const FilePaths &values);
void addModify(const NameValueItems &items);
void addChange(const Item &item) { m_changeItems.append(item); }
private:
QList<Item> m_changeItems;