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
|
|
|
|
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"
|
2023-02-09 14:41:59 +01:00
|
|
|
#include "utiltypes.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:
|
2023-01-26 17:48:36 +01:00
|
|
|
Environment();
|
|
|
|
|
explicit Environment(OsType osType);
|
|
|
|
|
explicit Environment(const QStringList &env, OsType osType = HostOsInfo::hostOs());
|
|
|
|
|
explicit Environment(const NameValuePairs &nameValues);
|
|
|
|
|
explicit Environment(const NameValueDictionary &dict);
|
|
|
|
|
|
|
|
|
|
QString value(const QString &key) const;
|
|
|
|
|
QString value_or(const QString &key, const QString &defaultValue) const;
|
|
|
|
|
bool hasKey(const QString &key) const;
|
|
|
|
|
|
|
|
|
|
void set(const QString &key, const QString &value, bool enabled = true);
|
2023-03-24 10:59:30 +01:00
|
|
|
void setFallback(const QString &key, const QString &value);
|
2023-01-26 17:48:36 +01:00
|
|
|
void unset(const QString &key);
|
|
|
|
|
void modify(const NameValueItems &items);
|
2022-06-01 12:34:08 +02:00
|
|
|
|
2023-01-27 12:30:03 +01:00
|
|
|
bool hasChanges() const;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
OsType osType() const;
|
|
|
|
|
QStringList toStringList() const;
|
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
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
void prependToPath(const FilePaths &values);
|
|
|
|
|
void appendToPath(const FilePaths &values);
|
|
|
|
|
|
2021-05-20 11:57:21 +02:00
|
|
|
void setupEnglishOutput();
|
2023-03-24 10:59:30 +01:00
|
|
|
void setupSudoAskPass(const FilePath &askPass);
|
2021-05-20 11:57:21 +02:00
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath searchInPath(const QString &executable,
|
2019-12-17 14:07:53 +01:00
|
|
|
const FilePaths &additionalDirs = FilePaths(),
|
2023-02-09 14:41:59 +01:00
|
|
|
const FilePathPredicate &func = {}) 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
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
NameValueDictionary toDictionary() const; // FIXME: avoid
|
2022-06-01 12:34:08 +02:00
|
|
|
NameValueItems diff(const Environment &other, bool checkAppendPrepend = false) const; // FIXME: avoid
|
|
|
|
|
|
2023-03-02 13:26:26 +01:00
|
|
|
struct Entry { QString key; QString value; bool enabled; };
|
|
|
|
|
using FindResult = std::optional<Entry>;
|
|
|
|
|
FindResult find(const QString &name) const; // Note res->key may differ in case from name.
|
2022-06-01 12:34:08 +02:00
|
|
|
|
2023-03-01 13:50:06 +01:00
|
|
|
void forEachEntry(const std::function<void (const QString &, const QString &, bool)> &callBack) const;
|
|
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
bool operator!=(const Environment &other) const;
|
|
|
|
|
bool operator==(const Environment &other) const;
|
2022-06-01 12:34:08 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-12-20 09:08:09 +01:00
|
|
|
enum Type {
|
|
|
|
|
SetSystemEnvironment,
|
2023-01-26 16:26:39 +01:00
|
|
|
SetFixedDictionary,
|
2022-12-20 09:08:09 +01:00
|
|
|
SetValue,
|
2023-03-24 10:59:30 +01:00
|
|
|
SetFallbackValue,
|
2022-12-20 09:08:09 +01:00
|
|
|
UnsetValue,
|
2023-01-26 17:48:36 +01:00
|
|
|
PrependOrSet,
|
|
|
|
|
AppendOrSet,
|
|
|
|
|
Modify,
|
2023-03-24 10:59:30 +01:00
|
|
|
SetupEnglishOutput
|
2022-06-01 13:04:57 +02:00
|
|
|
};
|
|
|
|
|
|
2022-12-20 09:08:09 +01:00
|
|
|
using Item = std::variant<
|
2023-01-26 17:48:36 +01:00
|
|
|
std::monostate, // SetSystemEnvironment dummy
|
|
|
|
|
NameValueDictionary, // SetFixedDictionary
|
|
|
|
|
std::tuple<QString, QString, bool>, // SetValue (key, value, enabled)
|
2023-03-24 10:59:30 +01:00
|
|
|
std::tuple<QString, QString>, // SetFallbackValue (key, value)
|
2023-01-26 17:48:36 +01:00
|
|
|
QString, // UnsetValue (key)
|
|
|
|
|
std::tuple<QString, QString, QString>, // PrependOrSet (key, value, separator)
|
|
|
|
|
std::tuple<QString, QString, QString>, // AppendOrSet (key, value, separator)
|
|
|
|
|
NameValueItems, // Modify
|
2023-03-24 10:59:30 +01:00
|
|
|
std::monostate, // SetupEnglishOutput
|
|
|
|
|
FilePath // SetupSudoAskPass (file path of qtc-askpass or ssh-askpass)
|
2022-12-20 09:08:09 +01:00
|
|
|
>;
|
|
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
void addItem(const Item &item);
|
2021-08-03 15:12:24 +02:00
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
Environment appliedToEnvironment(const Environment &base) const;
|
2021-05-25 15:25:37 +02:00
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
const NameValueDictionary &resolved() const;
|
2021-05-25 15:25:37 +02:00
|
|
|
|
|
|
|
|
private:
|
2023-01-26 17:48:36 +01:00
|
|
|
mutable QList<Item> m_changeItems;
|
|
|
|
|
mutable NameValueDictionary m_dict; // Latest resolved.
|
2023-03-24 10:59:30 +01:00
|
|
|
mutable bool m_fullDict = false;
|
2021-05-25 15:25:37 +02:00
|
|
|
};
|
|
|
|
|
|
2023-01-26 17:48:36 +01:00
|
|
|
using EnviromentChange = Environment;
|
|
|
|
|
|
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)
|