2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2016-03-18 07:55:01 +01:00
|
|
|
#pragma once
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2022-05-24 00:40:44 +02:00
|
|
|
#include "utils_global.h"
|
|
|
|
|
|
|
|
|
|
#include "environmentfwd.h"
|
|
|
|
|
#include "filepath.h"
|
2019-05-07 16:51:22 +02:00
|
|
|
#include "namevaluedictionary.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2017-04-19 14:39:16 +02:00
|
|
|
#include <functional>
|
2022-08-26 10:30:00 +02:00
|
|
|
#include <optional>
|
2017-04-19 14:39:16 +02:00
|
|
|
|
2022-05-24 13:45:35 +02:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
class QProcessEnvironment;
|
|
|
|
|
QT_END_NAMESPACE
|
2013-03-18 14:47:33 +01:00
|
|
|
|
2010-09-23 10:35:23 +02:00
|
|
|
namespace Utils {
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2022-06-01 12:34:08 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT Environment final
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-09-23 10:35:23 +02:00
|
|
|
public:
|
2022-06-01 12:34:08 +02:00
|
|
|
Environment() : m_dict(HostOsInfo::hostOs()) {}
|
|
|
|
|
explicit Environment(OsType osType) : m_dict(osType) {}
|
|
|
|
|
explicit Environment(const QStringList &env, OsType osType = HostOsInfo::hostOs())
|
|
|
|
|
: m_dict(env, osType) {}
|
|
|
|
|
explicit Environment(const NameValuePairs &nameValues) : m_dict(nameValues) {}
|
|
|
|
|
explicit Environment(const NameValueDictionary &dict) : m_dict(dict) {}
|
2017-06-23 16:54:45 +02:00
|
|
|
|
2022-06-01 12:34:08 +02:00
|
|
|
QString value(const QString &key) const { return m_dict.value(key); }
|
|
|
|
|
bool hasKey(const QString &key) const { return m_dict.hasKey(key); }
|
|
|
|
|
|
|
|
|
|
void set(const QString &key, const QString &value, bool enabled = true) { m_dict.set(key, value, enabled); }
|
|
|
|
|
void unset(const QString &key) { m_dict.unset(key); }
|
|
|
|
|
void modify(const NameValueItems &items) { m_dict.modify(items); }
|
|
|
|
|
|
2022-06-01 15:19:31 +02:00
|
|
|
int isValid() const;
|
2022-06-01 12:34:08 +02:00
|
|
|
void clear() { return m_dict.clear(); }
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2022-06-01 12:34:08 +02:00
|
|
|
QStringList toStringList() const { return m_dict.toStringList(); }
|
2013-01-15 13:45:46 +01:00
|
|
|
QProcessEnvironment toProcessEnvironment() const;
|
2012-08-31 16:47:53 +02:00
|
|
|
|
2009-09-29 11:39:55 +02:00
|
|
|
void appendOrSet(const QString &key, const QString &value, const QString &sep = QString());
|
|
|
|
|
void prependOrSet(const QString &key, const QString &value, const QString &sep = QString());
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2022-05-24 00:40:44 +02:00
|
|
|
void appendOrSetPath(const FilePath &value);
|
|
|
|
|
void prependOrSetPath(const FilePath &value);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2022-05-24 00:40:44 +02:00
|
|
|
void prependOrSetLibrarySearchPath(const FilePath &value);
|
|
|
|
|
void prependOrSetLibrarySearchPaths(const FilePaths &values);
|
2011-04-29 13:25:25 +02:00
|
|
|
|
2021-05-20 11:57:21 +02:00
|
|
|
void setupEnglishOutput();
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
using PathFilter = std::function<bool(const FilePath &)>;
|
|
|
|
|
FilePath searchInPath(const QString &executable,
|
2019-12-17 14:07:53 +01:00
|
|
|
const FilePaths &additionalDirs = FilePaths(),
|
2017-04-19 14:39:16 +02:00
|
|
|
const PathFilter &func = PathFilter()) const;
|
2021-10-04 10:59:41 +02:00
|
|
|
FilePath searchInDirectories(const QString &executable,
|
|
|
|
|
const FilePaths &dirs) const;
|
2019-12-17 14:07:53 +01:00
|
|
|
FilePaths findAllInPath(const QString &executable,
|
|
|
|
|
const FilePaths &additionalDirs = FilePaths(),
|
2019-07-09 09:02:02 +02:00
|
|
|
const PathFilter &func = PathFilter()) const;
|
2016-10-04 11:29:30 +02:00
|
|
|
|
2019-12-17 14:07:53 +01:00
|
|
|
FilePaths path() const;
|
|
|
|
|
FilePaths pathListValue(const QString &varName) const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2019-08-19 14:29:14 +02:00
|
|
|
QString expandedValueForKey(const QString &key) const;
|
2011-05-24 12:04:24 +02:00
|
|
|
QString expandVariables(const QString &input) const;
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath expandVariables(const FilePath &input) const;
|
2011-05-24 12:04:24 +02:00
|
|
|
QStringList expandVariables(const QStringList &input) const;
|
2010-09-22 15:10:20 +02:00
|
|
|
|
2022-06-01 12:34:08 +02:00
|
|
|
OsType osType() const { return m_dict.osType(); }
|
|
|
|
|
QString userName() const;
|
|
|
|
|
|
|
|
|
|
using const_iterator = NameValueMap::const_iterator; // FIXME: avoid
|
|
|
|
|
NameValueDictionary toDictionary() const { return m_dict; } // FIXME: avoid
|
|
|
|
|
NameValueItems diff(const Environment &other, bool checkAppendPrepend = false) const; // FIXME: avoid
|
|
|
|
|
|
|
|
|
|
QString key(const_iterator it) const { return m_dict.key(it); } // FIXME: avoid
|
|
|
|
|
QString value(const_iterator it) const { return m_dict.value(it); } // FIXME: avoid
|
|
|
|
|
bool isEnabled(const_iterator it) const { return m_dict.isEnabled(it); } // FIXME: avoid
|
|
|
|
|
|
|
|
|
|
const_iterator constBegin() const { return m_dict.constBegin(); } // FIXME: avoid
|
|
|
|
|
const_iterator constEnd() const { return m_dict.constEnd(); } // FIXME: avoid
|
|
|
|
|
const_iterator constFind(const QString &name) const { return m_dict.constFind(name); } // FIXME: avoid
|
|
|
|
|
|
|
|
|
|
friend bool operator!=(const Environment &first, const Environment &second)
|
|
|
|
|
{
|
|
|
|
|
return first.m_dict != second.m_dict;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend bool operator==(const Environment &first, const Environment &second)
|
|
|
|
|
{
|
|
|
|
|
return first.m_dict == second.m_dict;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Environment systemEnvironment();
|
|
|
|
|
|
2019-05-07 16:51:22 +02:00
|
|
|
static void modifySystemEnvironment(const EnvironmentItems &list); // use with care!!!
|
2020-11-16 16:40:12 +01:00
|
|
|
static void setSystemEnvironment(const Environment &environment); // don't use at all!!!
|
2022-06-01 12:34:08 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
NameValueDictionary m_dict;
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
|
|
|
|
|
2021-05-25 15:25:37 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT EnvironmentChange final
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
EnvironmentChange() = default;
|
|
|
|
|
|
2022-12-20 09:08:09 +01:00
|
|
|
enum Type {
|
|
|
|
|
SetSystemEnvironment,
|
|
|
|
|
SetFixedEnvironment,
|
|
|
|
|
SetValue,
|
|
|
|
|
UnsetValue,
|
|
|
|
|
PrependToPath,
|
|
|
|
|
AppendToPath,
|
2022-06-01 13:04:57 +02:00
|
|
|
};
|
|
|
|
|
|
2022-12-20 09:08:09 +01:00
|
|
|
using Item = std::variant<
|
|
|
|
|
std::monostate, // SetSystemEnvironment dummy
|
|
|
|
|
Environment, // SetFixedEnvironment
|
|
|
|
|
QPair<QString, QString>, // SetValue
|
|
|
|
|
QString, // UnsetValue
|
|
|
|
|
FilePath, // PrependToPath
|
|
|
|
|
FilePath // AppendToPath
|
|
|
|
|
>;
|
|
|
|
|
|
2021-08-03 15:12:24 +02:00
|
|
|
static EnvironmentChange fromFixedEnvironment(const Environment &fixedEnv);
|
|
|
|
|
|
2021-05-25 15:25:37 +02:00
|
|
|
void applyToEnvironment(Environment &) const;
|
|
|
|
|
|
|
|
|
|
void addSetValue(const QString &key, const QString &value);
|
|
|
|
|
void addUnsetValue(const QString &key);
|
2022-05-24 00:40:44 +02:00
|
|
|
void addPrependToPath(const FilePaths &values);
|
|
|
|
|
void addAppendToPath(const FilePaths &values);
|
2021-05-25 15:25:37 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QList<Item> m_changeItems;
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-25 13:56:46 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT EnvironmentProvider
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
QByteArray id;
|
|
|
|
|
QString displayName;
|
|
|
|
|
std::function<Environment()> environment;
|
|
|
|
|
|
|
|
|
|
static void addProvider(EnvironmentProvider &&provider);
|
|
|
|
|
static const QVector<EnvironmentProvider> providers();
|
2022-08-26 10:30:00 +02:00
|
|
|
static std::optional<EnvironmentProvider> provider(const QByteArray &id);
|
2018-09-25 13:56:46 +02:00
|
|
|
};
|
|
|
|
|
|
2022-08-24 14:36:27 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT QString qtcEnvironmentVariable(const QString &key);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QString qtcEnvironmentVariable(const QString &key,
|
|
|
|
|
const QString &defaultValue);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT bool qtcEnvironmentVariableIsSet(const QString &key);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT bool qtcEnvironmentVariableIsEmpty(const QString &key);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT int qtcEnvironmentVariableIntValue(const QString &key, bool *ok = nullptr);
|
2022-08-23 15:42:37 +02:00
|
|
|
|
2010-09-23 10:35:23 +02:00
|
|
|
} // namespace Utils
|
2022-04-08 11:35:54 +02:00
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(Utils::Environment)
|