1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "fileutils.h"
29 #include "hostosinfo.h"
30 #include "utils_global.h"
31 
32 #include <QMap>
33 #include <QStringList>
34 
35 #include <functional>
36 
37 QT_FORWARD_DECLARE_CLASS(QDebug)
QT_FORWARD_DECLARE_CLASS(QProcessEnvironment)38 QT_FORWARD_DECLARE_CLASS(QProcessEnvironment)
39 
40 namespace Utils {
41 class Environment;
42 
43 class QTCREATOR_UTILS_EXPORT EnvironmentItem
44 {
45 public:
46     enum Operation { Set, Unset, Prepend, Append };
47 
48     EnvironmentItem(const QString &n, const QString &v, Operation op = Set)
49             : name(n), value(v), operation(op)
50     {}
51 
52     void apply(Environment *e) const { apply(e, operation); }
53 
54     QString name;
55     QString value;
56     Operation operation;
57 
58     bool operator==(const EnvironmentItem &other) const
59     {
60         return operation == other.operation && name == other.name && value == other.value;
61     }
62 
63     bool operator!=(const EnvironmentItem &other) const
64     {
65         return !(*this == other);
66     }
67 
68     static void sort(QList<EnvironmentItem> *list);
69     static QList<EnvironmentItem> fromStringList(const QStringList &list);
70     static QStringList toStringList(const QList<EnvironmentItem> &list);
71     static QList<EnvironmentItem> itemsFromVariantList(const QVariantList &list);
72     static QVariantList toVariantList(const QList<EnvironmentItem> &list);
73     static EnvironmentItem itemFromVariantList(const QVariantList &list);
74     static QVariantList toVariantList(const EnvironmentItem &item);
75 
76 private:
77     void apply(Environment *e, Operation op) const;
78 };
79 
80 QTCREATOR_UTILS_EXPORT QDebug operator<<(QDebug debug, const EnvironmentItem &i);
81 
82 class QTCREATOR_UTILS_EXPORT Environment
83 {
84 public:
85     typedef QMap<QString, QString>::const_iterator const_iterator;
86 
87     explicit Environment(OsType osType = HostOsInfo::hostOs()) : m_osType(osType) {}
88     explicit Environment(const QStringList &env, OsType osType = HostOsInfo::hostOs());
89     static Environment systemEnvironment();
90     static void setupEnglishOutput(Environment *environment);
91     static void setupEnglishOutput(QProcessEnvironment *environment);
92     static void setupEnglishOutput(QStringList *environment);
93 
94     QStringList toStringList() const;
95     QProcessEnvironment toProcessEnvironment() const;
96     QString value(const QString &key) const;
97     void set(const QString &key, const QString &value);
98     void unset(const QString &key);
99     void modify(const QList<EnvironmentItem> &list);
100     /// Return the Environment changes necessary to modify this into the other environment.
101     QList<EnvironmentItem> diff(const Environment &other, bool checkAppendPrepend = false) const;
102     bool hasKey(const QString &key) const;
103     OsType osType() const;
104 
105     QString userName() const;
106 
107     void appendOrSet(const QString &key, const QString &value, const QString &sep = QString());
108     void prependOrSet(const QString &key, const QString &value, const QString &sep = QString());
109 
110     void appendOrSetPath(const QString &value);
111     void prependOrSetPath(const QString &value);
112 
113     void prependOrSetLibrarySearchPath(const QString &value);
114     void prependOrSetLibrarySearchPaths(const QStringList &values);
115 
116     void clear();
117     int size() const;
118 
119     QString key(Environment::const_iterator it) const;
120     QString value(Environment::const_iterator it) const;
121 
122     Environment::const_iterator constBegin() const;
123     Environment::const_iterator constEnd() const;
124     Environment::const_iterator constFind(const QString &name) const;
125 
126     using PathFilter = std::function<bool(const FileName &)>;
127     FileName searchInPath(const QString &executable,
128                           const FileNameList &additionalDirs = FileNameList(),
129                           const PathFilter &func = PathFilter()) const;
130 
131     FileNameList path() const;
132     QStringList appendExeExtensions(const QString &executable) const;
133 
134     bool isSameExecutable(const QString &exe1, const QString &exe2) const;
135 
136     QString expandVariables(const QString &input) const;
137     QStringList expandVariables(const QStringList &input) const;
138 
139     bool operator!=(const Environment &other) const;
140     bool operator==(const Environment &other) const;
141 
142 private:
143     FileName searchInDirectory(const QStringList &execs, const FileName &directory,
144                                QSet<FileName> &alreadyChecked) const;
145     QMap<QString, QString> m_values;
146     OsType m_osType;
147 };
148 
149 } // namespace Utils
150