Environment: Add convenience wrappers similar to qEnvironmentVariable*

Adds corresponding wrappers that work on
Environment::systemEnvironment(), which can be different to the
environment that Qt Creator currently runs in, depending on the settings
in Preferences > Environment > System > Environment.

Change-Id: Ib1db37d2828b686d9a3233d170612beea9d12946
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Eike Ziller
2022-08-23 15:42:37 +02:00
parent 79949d9ae8
commit c0a0daf588
2 changed files with 61 additions and 0 deletions

View File

@@ -495,4 +495,59 @@ void EnvironmentChange::applyToEnvironment(Environment &env) const
}
}
/*!
Returns the value of \a key in \QC's modified system environment.
\sa Utils::Environment::systemEnvironment
\sa qEnvironmentVariable
*/
QString qtcEnvironmentVariable(const QString &key)
{
return Environment::systemEnvironment().value(key);
}
/*!
Returns the value of \a key in \QC's modified system environment if it is set,
or otherwise \a defaultValue.
\sa Utils::Environment::systemEnvironment
\sa qEnvironmentVariable
*/
QString qtcEnvironmentVariable(const QString &key, const QString &defaultValue)
{
if (Environment::systemEnvironment().hasKey(key))
return Environment::systemEnvironment().value(key);
return defaultValue;
}
/*!
Returns if the environment variable \a key is set \QC's modified system environment.
\sa Utils::Environment::systemEnvironment
\sa qEnvironmentVariableIsSet
*/
bool qtcEnvironmentVariableIsSet(const QString &key)
{
return Environment::systemEnvironment().hasKey(key);
}
/*!
Returns if the environment variable \a key is not set or empty in \QC's modified system
environment.
\sa Utils::Environment::systemEnvironment
\sa qEnvironmentVariableIsEmpty
*/
bool qtcEnvironmentVariableIsEmpty(const QString &key)
{
return Environment::systemEnvironment().value(key).isEmpty();
}
/*!
Returns the value of \a key in \QC's modified system environment, converted to an int.
If \a ok is not null, sets \c{*ok} to true or false depending on the success of the conversion
\sa Utils::Environment::systemEnvironment
\sa qEnvironmentVariableIntValue
*/
int qtcEnvironmentVariableIntValue(const QString &key, bool *ok)
{
return Environment::systemEnvironment().value(key).toInt(ok);
}
} // namespace Utils

View File

@@ -175,6 +175,12 @@ public:
static optional<EnvironmentProvider> provider(const QByteArray &id);
};
QString qtcEnvironmentVariable(const QString &key);
QString qtcEnvironmentVariable(const QString &key, const QString &defaultValue);
bool qtcEnvironmentVariableIsSet(const QString &key);
bool qtcEnvironmentVariableIsEmpty(const QString &key);
int qtcEnvironmentVariableIntValue(const QString &key, bool *ok = nullptr);
} // namespace Utils
Q_DECLARE_METATYPE(Utils::Environment)