2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "environment.h"
|
|
|
|
|
|
2017-04-19 14:47:48 +02:00
|
|
|
#include "algorithm.h"
|
2023-02-09 14:41:59 +01:00
|
|
|
#include "filepath.h"
|
2017-06-20 11:30:21 +03:00
|
|
|
#include "qtcassert.h"
|
2017-04-19 14:47:48 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDir>
|
2013-01-11 12:37:38 +01:00
|
|
|
#include <QProcessEnvironment>
|
2022-10-20 15:42:59 +02:00
|
|
|
#include <QReadWriteLock>
|
2014-03-05 11:17:10 +01:00
|
|
|
#include <QSet>
|
2012-04-30 19:01:26 +02:00
|
|
|
|
2023-05-24 09:36:35 +02:00
|
|
|
/*!
|
|
|
|
|
\class Utils::Environment
|
|
|
|
|
\inmodule QtCreator
|
|
|
|
|
|
|
|
|
|
\brief The Environment class sets \QC's system environment.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-05-24 00:40:44 +02:00
|
|
|
namespace Utils {
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2022-10-20 15:42:59 +02:00
|
|
|
static QReadWriteLock s_envMutex;
|
2022-05-24 00:40:44 +02:00
|
|
|
Q_GLOBAL_STATIC_WITH_ARGS(Environment, staticSystemEnvironment,
|
|
|
|
|
(QProcessEnvironment::systemEnvironment().toStringList()))
|
2023-07-20 11:33:30 +02:00
|
|
|
Q_GLOBAL_STATIC(QList<EnvironmentProvider>, environmentProviders)
|
2019-05-07 16:51:22 +02:00
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
Environment::Environment()
|
|
|
|
|
: m_dict(HostOsInfo::hostOs())
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
Environment::Environment(OsType osType)
|
|
|
|
|
: m_dict(osType)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
Environment::Environment(const QStringList &env, OsType osType)
|
|
|
|
|
: m_dict(osType)
|
|
|
|
|
{
|
|
|
|
|
m_changeItems.append(NameValueDictionary(env, osType));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Environment::Environment(const NameValuePairs &nameValues)
|
|
|
|
|
{
|
|
|
|
|
m_changeItems.append(NameValueDictionary(nameValues));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Environment::Environment(const NameValueDictionary &dict)
|
|
|
|
|
{
|
|
|
|
|
m_changeItems.append(dict);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 12:34:08 +02:00
|
|
|
NameValueItems Environment::diff(const Environment &other, bool checkAppendPrepend) const
|
|
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
const NameValueDictionary &dict = resolved();
|
|
|
|
|
const NameValueDictionary &otherDict = other.resolved();
|
|
|
|
|
return dict.diff(otherDict, checkAppendPrepend);
|
2022-06-01 12:34:08 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-02 13:26:26 +01:00
|
|
|
Environment::FindResult Environment::find(const QString &name) const
|
|
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
const NameValueDictionary &dict = resolved();
|
|
|
|
|
const auto it = dict.constFind(name);
|
|
|
|
|
if (it == dict.constEnd())
|
2023-03-02 13:26:26 +01:00
|
|
|
return {};
|
|
|
|
|
return Entry{it.key().name, it.value().first, it.value().second};
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 13:50:06 +01:00
|
|
|
void Environment::forEachEntry(const std::function<void(const QString &, const QString &, bool)> &callBack) const
|
|
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
const NameValueDictionary &dict = resolved();
|
|
|
|
|
for (auto it = dict.m_values.constBegin(); it != dict.m_values.constEnd(); ++it)
|
2023-03-01 13:50:06 +01:00
|
|
|
callBack(it.key().name, it.value().first, it.value().second);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
bool Environment::operator==(const Environment &other) const
|
|
|
|
|
{
|
|
|
|
|
const NameValueDictionary &dict = resolved();
|
|
|
|
|
const NameValueDictionary &otherDict = other.resolved();
|
|
|
|
|
return dict == otherDict;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Environment::operator!=(const Environment &other) const
|
|
|
|
|
{
|
|
|
|
|
const NameValueDictionary &dict = resolved();
|
|
|
|
|
const NameValueDictionary &otherDict = other.resolved();
|
|
|
|
|
return dict != otherDict;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Environment::value(const QString &key) const
|
|
|
|
|
{
|
|
|
|
|
const NameValueDictionary &dict = resolved();
|
|
|
|
|
return dict.value(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Environment::value_or(const QString &key, const QString &defaultValue) const
|
|
|
|
|
{
|
|
|
|
|
const NameValueDictionary &dict = resolved();
|
|
|
|
|
return dict.hasKey(key) ? dict.value(key) : defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Environment::hasKey(const QString &key) const
|
|
|
|
|
{
|
|
|
|
|
const NameValueDictionary &dict = resolved();
|
|
|
|
|
return dict.hasKey(key);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-27 12:30:03 +01:00
|
|
|
bool Environment::hasChanges() const
|
2022-06-01 15:19:31 +02:00
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
const NameValueDictionary &dict = resolved();
|
|
|
|
|
return dict.size() != 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OsType Environment::osType() const
|
|
|
|
|
{
|
|
|
|
|
return m_dict.m_osType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList Environment::toStringList() const
|
|
|
|
|
{
|
|
|
|
|
const NameValueDictionary &dict = resolved();
|
|
|
|
|
return dict.toStringList();
|
2022-06-01 15:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
2013-01-15 13:45:46 +01:00
|
|
|
QProcessEnvironment Environment::toProcessEnvironment() const
|
|
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
const NameValueDictionary &dict = resolved();
|
2013-01-15 13:45:46 +01:00
|
|
|
QProcessEnvironment result;
|
2023-01-26 17:48:36 +01:00
|
|
|
for (auto it = dict.m_values.constBegin(); it != dict.m_values.constEnd(); ++it) {
|
2019-07-09 17:41:30 +02:00
|
|
|
if (it.value().second)
|
2023-01-26 17:48:36 +01:00
|
|
|
result.insert(it.key().name, expandedValueForKey(dict.key(it)));
|
2019-07-09 17:41:30 +02:00
|
|
|
}
|
2013-01-15 13:45:46 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-09 18:20:14 +01:00
|
|
|
void Environment::appendOrSetPath(const FilePath &value)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
QTC_CHECK(value.osType() == m_dict.m_osType);
|
2021-11-09 18:20:14 +01:00
|
|
|
if (value.isEmpty())
|
|
|
|
|
return;
|
2023-01-26 17:48:36 +01:00
|
|
|
appendOrSet("PATH", value.nativePath(), OsSpecificAspects::pathListSeparator(osType()));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-09 18:20:14 +01:00
|
|
|
void Environment::prependOrSetPath(const FilePath &value)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
QTC_CHECK(value.osType() == m_dict.m_osType);
|
2021-11-09 18:20:14 +01:00
|
|
|
if (value.isEmpty())
|
|
|
|
|
return;
|
2023-01-26 17:48:36 +01:00
|
|
|
prependOrSet("PATH", value.nativePath(), OsSpecificAspects::pathListSeparator(osType()));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-26 12:15:28 +02:00
|
|
|
void Environment::prependOrSetPath(const QString &directories)
|
|
|
|
|
{
|
|
|
|
|
prependOrSet("PATH", directories, OsSpecificAspects::pathListSeparator(osType()));
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void Environment::appendOrSet(const QString &key, const QString &value, const QString &sep)
|
|
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
addItem(Item{std::in_place_index_t<AppendOrSet>(), key, value, sep});
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 16:51:22 +02:00
|
|
|
void Environment::prependOrSet(const QString &key, const QString &value, const QString &sep)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
addItem(Item{std::in_place_index_t<PrependOrSet>(), key, value, sep});
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-09 18:20:14 +01:00
|
|
|
void Environment::prependOrSetLibrarySearchPath(const FilePath &value)
|
2011-04-29 13:25:25 +02:00
|
|
|
{
|
2022-06-01 12:34:08 +02:00
|
|
|
QTC_CHECK(value.osType() == osType());
|
2023-04-06 18:31:47 +02:00
|
|
|
const QChar sep = OsSpecificAspects::pathListSeparator(osType());
|
2022-06-01 12:34:08 +02:00
|
|
|
switch (osType()) {
|
2013-08-15 16:08:16 +02:00
|
|
|
case OsTypeWindows: {
|
2023-01-26 17:48:36 +01:00
|
|
|
prependOrSet("PATH", value.nativePath(), sep);
|
2012-08-23 15:53:58 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2015-09-02 17:06:58 +02:00
|
|
|
case OsTypeMac: {
|
2021-11-10 16:19:25 +01:00
|
|
|
const QString nativeValue = value.nativePath();
|
2017-04-19 14:26:54 +02:00
|
|
|
prependOrSet("DYLD_LIBRARY_PATH", nativeValue, sep);
|
|
|
|
|
prependOrSet("DYLD_FRAMEWORK_PATH", nativeValue, sep);
|
2015-09-02 17:06:58 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2013-08-15 16:08:16 +02:00
|
|
|
case OsTypeLinux:
|
|
|
|
|
case OsTypeOtherUnix: {
|
2023-01-26 17:48:36 +01:00
|
|
|
prependOrSet("LD_LIBRARY_PATH", value.nativePath(), sep);
|
2012-08-23 15:53:58 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2015-09-02 17:06:58 +02:00
|
|
|
default:
|
2012-08-23 15:53:58 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2011-04-29 13:25:25 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-09 18:20:14 +01:00
|
|
|
void Environment::prependOrSetLibrarySearchPaths(const FilePaths &values)
|
2018-01-08 22:46:54 +02:00
|
|
|
{
|
2022-05-24 00:40:44 +02:00
|
|
|
reverseForeach(values, [this](const FilePath &value) {
|
2018-01-08 22:46:54 +02:00
|
|
|
prependOrSetLibrarySearchPath(value);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 09:36:35 +02:00
|
|
|
/*!
|
|
|
|
|
Returns \QC's system environment.
|
|
|
|
|
|
|
|
|
|
This can be different from the system environment that \QC started in if the
|
|
|
|
|
user changed it in \uicontrol Preferences > \uicontrol Environment >
|
|
|
|
|
\uicontrol System > \uicontrol Environment.
|
|
|
|
|
*/
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
Environment Environment::systemEnvironment()
|
|
|
|
|
{
|
2022-10-20 15:42:59 +02:00
|
|
|
QReadLocker lock(&s_envMutex);
|
2012-04-30 19:01:26 +02:00
|
|
|
return *staticSystemEnvironment();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-20 11:57:21 +02:00
|
|
|
void Environment::setupEnglishOutput()
|
2016-04-07 13:06:01 +02:00
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
addItem(Item{std::in_place_index_t<SetupEnglishOutput>()});
|
2016-04-07 13:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 14:41:59 +01:00
|
|
|
using SearchResultCallback = std::function<IterationPolicy(const FilePath &)>;
|
2017-04-19 14:47:48 +02:00
|
|
|
|
2019-08-19 14:29:14 +02:00
|
|
|
QString Environment::expandedValueForKey(const QString &key) const
|
|
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
const NameValueDictionary &dict = resolved();
|
|
|
|
|
return expandVariables(dict.value(key));
|
2019-08-19 14:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-04 10:59:41 +02:00
|
|
|
FilePath Environment::searchInPath(const QString &executable,
|
|
|
|
|
const FilePaths &additionalDirs,
|
2023-05-23 11:37:16 +02:00
|
|
|
const FilePathPredicate &filter) const
|
2021-10-04 10:59:41 +02:00
|
|
|
{
|
2023-05-23 11:37:16 +02:00
|
|
|
const FilePath exec = FilePath::fromUserInput(expandVariables(executable));
|
2023-05-30 10:09:50 +02:00
|
|
|
const FilePaths dirs = path() + additionalDirs;
|
|
|
|
|
return exec.searchInDirectories(dirs, filter, FilePath::WithAnySuffix);
|
2021-10-04 10:59:41 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-17 14:07:53 +01:00
|
|
|
FilePaths Environment::path() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2019-07-05 16:58:07 +02:00
|
|
|
return pathListValue("PATH");
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 14:07:53 +01:00
|
|
|
FilePaths Environment::pathListValue(const QString &varName) const
|
2019-07-05 16:58:07 +02:00
|
|
|
{
|
2020-08-18 13:55:38 +02:00
|
|
|
const QStringList pathComponents = expandedValueForKey(varName).split(
|
2022-06-01 12:34:08 +02:00
|
|
|
OsSpecificAspects::pathListSeparator(osType()), Qt::SkipEmptyParts);
|
2019-07-05 16:58:07 +02:00
|
|
|
return transform(pathComponents, &FilePath::fromUserInput);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 16:51:22 +02:00
|
|
|
void Environment::modifySystemEnvironment(const EnvironmentItems &list)
|
2018-08-09 11:31:41 +02:00
|
|
|
{
|
2022-10-20 15:42:59 +02:00
|
|
|
QWriteLocker lock(&s_envMutex);
|
2018-08-09 11:31:41 +02:00
|
|
|
staticSystemEnvironment->modify(list);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 16:40:12 +01:00
|
|
|
void Environment::setSystemEnvironment(const Environment &environment)
|
|
|
|
|
{
|
2022-10-20 15:42:59 +02:00
|
|
|
QWriteLocker lock(&s_envMutex);
|
2020-11-16 16:40:12 +01:00
|
|
|
*staticSystemEnvironment = environment;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-22 15:10:20 +02:00
|
|
|
/** Expand environment variables in a string.
|
|
|
|
|
*
|
|
|
|
|
* Environment variables are accepted in the following forms:
|
2010-10-19 11:10:58 +02:00
|
|
|
* $SOMEVAR, ${SOMEVAR} on Unix and %SOMEVAR% on Windows.
|
|
|
|
|
* No escapes and quoting are supported.
|
|
|
|
|
* If a variable is not found, it is not substituted.
|
2010-09-22 15:10:20 +02:00
|
|
|
*/
|
|
|
|
|
QString Environment::expandVariables(const QString &input) const
|
|
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
const NameValueDictionary &dict = resolved();
|
|
|
|
|
|
2010-10-19 11:10:58 +02:00
|
|
|
QString result = input;
|
|
|
|
|
|
2022-06-01 12:34:08 +02:00
|
|
|
if (osType() == OsTypeWindows) {
|
2012-08-23 15:53:58 +02:00
|
|
|
for (int vStart = -1, i = 0; i < result.length(); ) {
|
2017-04-19 14:26:54 +02:00
|
|
|
if (result.at(i++) == '%') {
|
2012-08-23 15:53:58 +02:00
|
|
|
if (vStart > 0) {
|
2023-01-26 17:48:36 +01:00
|
|
|
const auto it = dict.findKey(result.mid(vStart, i - vStart - 1));
|
|
|
|
|
if (it != dict.m_values.constEnd()) {
|
2019-07-09 17:41:30 +02:00
|
|
|
result.replace(vStart - 1, i - vStart + 1, it->first);
|
|
|
|
|
i = vStart - 1 + it->first.length();
|
2012-08-23 15:53:58 +02:00
|
|
|
vStart = -1;
|
|
|
|
|
} else {
|
|
|
|
|
vStart = i;
|
|
|
|
|
}
|
2010-10-19 11:10:58 +02:00
|
|
|
} else {
|
|
|
|
|
vStart = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-23 15:53:58 +02:00
|
|
|
} else {
|
|
|
|
|
enum { BASE, OPTIONALVARIABLEBRACE, VARIABLE, BRACEDVARIABLE } state = BASE;
|
|
|
|
|
int vStart = -1;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < result.length();) {
|
|
|
|
|
QChar c = result.at(i++);
|
|
|
|
|
if (state == BASE) {
|
2017-04-19 14:26:54 +02:00
|
|
|
if (c == '$')
|
2012-08-23 15:53:58 +02:00
|
|
|
state = OPTIONALVARIABLEBRACE;
|
|
|
|
|
} else if (state == OPTIONALVARIABLEBRACE) {
|
2017-04-19 14:26:54 +02:00
|
|
|
if (c == '{') {
|
2012-08-23 15:53:58 +02:00
|
|
|
state = BRACEDVARIABLE;
|
|
|
|
|
vStart = i;
|
2017-04-19 14:26:54 +02:00
|
|
|
} else if (c.isLetterOrNumber() || c == '_') {
|
2012-08-23 15:53:58 +02:00
|
|
|
state = VARIABLE;
|
|
|
|
|
vStart = i - 1;
|
|
|
|
|
} else {
|
|
|
|
|
state = BASE;
|
2010-10-19 11:10:58 +02:00
|
|
|
}
|
2012-08-23 15:53:58 +02:00
|
|
|
} else if (state == BRACEDVARIABLE) {
|
2017-04-19 14:26:54 +02:00
|
|
|
if (c == '}') {
|
2023-03-02 13:26:26 +01:00
|
|
|
const QString key = result.mid(vStart, i - 1 - vStart);
|
|
|
|
|
const Environment::FindResult res = find(key);
|
|
|
|
|
if (res) {
|
|
|
|
|
result.replace(vStart - 2, i - vStart + 2, res->value);
|
|
|
|
|
i = vStart - 2 + res->value.length();
|
2012-08-23 15:53:58 +02:00
|
|
|
}
|
|
|
|
|
state = BASE;
|
|
|
|
|
}
|
|
|
|
|
} else if (state == VARIABLE) {
|
2017-04-19 14:26:54 +02:00
|
|
|
if (!c.isLetterOrNumber() && c != '_') {
|
2023-03-02 13:26:26 +01:00
|
|
|
const QString key = result.mid(vStart, i - vStart - 1);
|
|
|
|
|
const Environment::FindResult res = find(key);
|
|
|
|
|
if (res) {
|
|
|
|
|
result.replace(vStart - 1, i - vStart, res->value);
|
|
|
|
|
i = vStart - 1 + res->value.length();
|
2012-08-23 15:53:58 +02:00
|
|
|
}
|
|
|
|
|
state = BASE;
|
2010-10-19 11:10:58 +02:00
|
|
|
}
|
2010-09-22 15:10:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
2012-08-23 15:53:58 +02:00
|
|
|
if (state == VARIABLE) {
|
2023-03-02 13:26:26 +01:00
|
|
|
const Environment::FindResult res = find(result.mid(vStart));
|
|
|
|
|
if (res)
|
|
|
|
|
result.replace(vStart - 1, result.length() - vStart + 1, res->value);
|
2012-08-23 15:53:58 +02:00
|
|
|
}
|
2010-09-22 15:10:20 +02:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2010-09-23 13:08:03 +02:00
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath Environment::expandVariables(const FilePath &variables) const
|
2019-05-15 13:59:43 +02:00
|
|
|
{
|
2019-05-28 13:49:26 +02:00
|
|
|
return FilePath::fromString(expandVariables(variables.toString()));
|
2019-05-15 13:59:43 +02:00
|
|
|
}
|
|
|
|
|
|
2010-09-23 13:08:03 +02:00
|
|
|
QStringList Environment::expandVariables(const QStringList &variables) const
|
|
|
|
|
{
|
2022-05-24 00:40:44 +02:00
|
|
|
return transform(variables, [this](const QString &i) { return expandVariables(i); });
|
2010-09-23 13:08:03 +02:00
|
|
|
}
|
2011-05-24 12:04:24 +02:00
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
NameValueDictionary Environment::toDictionary() const
|
|
|
|
|
{
|
|
|
|
|
const NameValueDictionary &dict = resolved();
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-25 13:56:46 +02:00
|
|
|
void EnvironmentProvider::addProvider(EnvironmentProvider &&provider)
|
|
|
|
|
{
|
|
|
|
|
environmentProviders->append(std::move(provider));
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 11:33:30 +02:00
|
|
|
const QList<EnvironmentProvider> EnvironmentProvider::providers()
|
2018-09-25 13:56:46 +02:00
|
|
|
{
|
|
|
|
|
return *environmentProviders;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 10:30:00 +02:00
|
|
|
std::optional<EnvironmentProvider> EnvironmentProvider::provider(const QByteArray &id)
|
2018-09-25 13:56:46 +02:00
|
|
|
{
|
|
|
|
|
const int index = indexOf(*environmentProviders, equal(&EnvironmentProvider::id, id));
|
|
|
|
|
if (index >= 0)
|
2022-08-26 10:30:00 +02:00
|
|
|
return std::make_optional(environmentProviders->at(index));
|
|
|
|
|
return std::nullopt;
|
2018-09-25 13:56:46 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
void Environment::addItem(const Item &item)
|
|
|
|
|
{
|
|
|
|
|
m_dict.clear();
|
|
|
|
|
m_changeItems.append(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Environment::set(const QString &key, const QString &value, bool enabled)
|
2021-05-25 15:25:37 +02:00
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
addItem(Item{std::in_place_index_t<SetValue>(),
|
|
|
|
|
std::tuple<QString, QString, bool>{key, value, enabled}});
|
2021-05-25 15:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-24 10:59:30 +01:00
|
|
|
void Environment::setFallback(const QString &key, const QString &value)
|
|
|
|
|
{
|
|
|
|
|
addItem(Item{std::in_place_index_t<SetFallbackValue>(),
|
|
|
|
|
std::tuple<QString, QString>{key, value}});
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
void Environment::unset(const QString &key)
|
2021-05-25 15:25:37 +02:00
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
addItem(Item{std::in_place_index_t<UnsetValue>(), key});
|
2021-05-25 15:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
void Environment::modify(const NameValueItems &items)
|
2021-05-25 15:25:37 +02:00
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
addItem(Item{std::in_place_index_t<Modify>(), items});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Environment::prependToPath(const FilePaths &values)
|
|
|
|
|
{
|
|
|
|
|
m_dict.clear();
|
2021-08-03 15:12:24 +02:00
|
|
|
for (int i = values.size(); --i >= 0; ) {
|
2021-11-09 18:20:14 +01:00
|
|
|
const FilePath value = values.at(i);
|
2023-01-26 17:48:36 +01:00
|
|
|
m_changeItems.append(Item{
|
|
|
|
|
std::in_place_index_t<PrependOrSet>(),
|
|
|
|
|
QString("PATH"),
|
|
|
|
|
value.nativePath(),
|
|
|
|
|
value.pathListSeparator()
|
|
|
|
|
});
|
2021-08-03 15:12:24 +02:00
|
|
|
}
|
2021-05-25 15:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
void Environment::appendToPath(const FilePaths &values)
|
2021-05-25 15:25:37 +02:00
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
m_dict.clear();
|
|
|
|
|
for (const FilePath &value : values) {
|
|
|
|
|
m_changeItems.append(Item{
|
|
|
|
|
std::in_place_index_t<AppendOrSet>(),
|
|
|
|
|
QString("PATH"),
|
|
|
|
|
value.nativePath(),
|
|
|
|
|
value.pathListSeparator()
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-05-25 15:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
const NameValueDictionary &Environment::resolved() const
|
2021-08-03 15:12:24 +02:00
|
|
|
{
|
2023-01-26 17:48:36 +01:00
|
|
|
if (m_dict.size() != 0)
|
|
|
|
|
return m_dict;
|
2021-08-03 15:12:24 +02:00
|
|
|
|
2023-03-24 10:59:30 +01:00
|
|
|
m_fullDict = false;
|
2022-06-01 13:04:57 +02:00
|
|
|
for (const Item &item : m_changeItems) {
|
2022-12-20 09:08:09 +01:00
|
|
|
switch (item.index()) {
|
|
|
|
|
case SetSystemEnvironment:
|
2023-01-26 17:48:36 +01:00
|
|
|
m_dict = Environment::systemEnvironment().toDictionary();
|
2023-03-24 10:59:30 +01:00
|
|
|
m_fullDict = true;
|
2022-06-01 13:04:57 +02:00
|
|
|
break;
|
2023-01-26 16:26:39 +01:00
|
|
|
case SetFixedDictionary:
|
2023-01-26 17:48:36 +01:00
|
|
|
m_dict = std::get<SetFixedDictionary>(item);
|
2023-03-24 10:59:30 +01:00
|
|
|
m_fullDict = true;
|
2022-06-01 13:04:57 +02:00
|
|
|
break;
|
2022-12-20 09:08:09 +01:00
|
|
|
case SetValue: {
|
2023-01-26 17:48:36 +01:00
|
|
|
auto [key, value, enabled] = std::get<SetValue>(item);
|
|
|
|
|
m_dict.set(key, value, enabled);
|
2022-06-01 13:04:57 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2023-03-24 10:59:30 +01:00
|
|
|
case SetFallbackValue: {
|
|
|
|
|
auto [key, value] = std::get<SetFallbackValue>(item);
|
|
|
|
|
if (m_fullDict) {
|
|
|
|
|
if (m_dict.value(key).isEmpty())
|
|
|
|
|
m_dict.set(key, value, true);
|
|
|
|
|
} else {
|
|
|
|
|
QTC_ASSERT(false, qDebug() << "operating on partial dictionary");
|
|
|
|
|
m_dict.set(key, value, true);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-12-20 09:08:09 +01:00
|
|
|
case UnsetValue:
|
2023-01-26 17:48:36 +01:00
|
|
|
m_dict.unset(std::get<UnsetValue>(item));
|
2022-06-01 13:04:57 +02:00
|
|
|
break;
|
2023-01-26 17:48:36 +01:00
|
|
|
case PrependOrSet: {
|
|
|
|
|
auto [key, value, sep] = std::get<PrependOrSet>(item);
|
|
|
|
|
QTC_ASSERT(!key.contains('='), return m_dict);
|
|
|
|
|
const auto it = m_dict.findKey(key);
|
|
|
|
|
if (it == m_dict.m_values.end()) {
|
|
|
|
|
m_dict.m_values.insert(DictKey(key, m_dict.nameCaseSensitivity()), {value, true});
|
|
|
|
|
} else {
|
|
|
|
|
// Prepend unless it is already there
|
|
|
|
|
const QString toPrepend = value + sep;
|
|
|
|
|
if (!it.value().first.startsWith(toPrepend))
|
|
|
|
|
it.value().first.prepend(toPrepend);
|
|
|
|
|
}
|
2022-06-01 13:04:57 +02:00
|
|
|
break;
|
2023-01-26 17:48:36 +01:00
|
|
|
}
|
|
|
|
|
case AppendOrSet: {
|
|
|
|
|
auto [key, value, sep] = std::get<AppendOrSet>(item);
|
|
|
|
|
QTC_ASSERT(!key.contains('='), return m_dict);
|
|
|
|
|
const auto it = m_dict.findKey(key);
|
|
|
|
|
if (it == m_dict.m_values.end()) {
|
|
|
|
|
m_dict.m_values.insert(DictKey(key, m_dict.nameCaseSensitivity()), {value, true});
|
|
|
|
|
} else {
|
|
|
|
|
// Prepend unless it is already there
|
|
|
|
|
const QString toAppend = sep + value;
|
|
|
|
|
if (!it.value().first.endsWith(toAppend))
|
|
|
|
|
it.value().first.append(toAppend);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Modify: {
|
|
|
|
|
NameValueItems items = std::get<Modify>(item);
|
|
|
|
|
m_dict.modify(items);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SetupEnglishOutput:
|
|
|
|
|
m_dict.set("LC_MESSAGES", "en_US.utf8");
|
|
|
|
|
m_dict.set("LANGUAGE", "en_US:en");
|
2022-06-01 13:04:57 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-26 17:48:36 +01:00
|
|
|
|
|
|
|
|
return m_dict;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Environment Environment::appliedToEnvironment(const Environment &base) const
|
|
|
|
|
{
|
|
|
|
|
Environment res = base;
|
|
|
|
|
res.m_dict.clear();
|
|
|
|
|
res.m_changeItems.append(m_changeItems);
|
|
|
|
|
return res;
|
2021-05-25 15:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
2022-08-23 15:42:37 +02:00
|
|
|
/*!
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-24 12:04:24 +02:00
|
|
|
} // namespace Utils
|