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 "utils_global.h"
29 
30 #include <functional>
31 
32 #include <QList>
33 #include <QVector>
34 #include <QCoreApplication>
35 
36 namespace Utils {
37 
38 namespace Internal { class MacroExpanderPrivate; }
39 
40 class FilePath;
41 class MacroExpander;
42 using MacroExpanderProvider = std::function<MacroExpander *()>;
43 using MacroExpanderProviders = QVector<MacroExpanderProvider>;
44 
45 class QTCREATOR_UTILS_EXPORT MacroExpander
46 {
47     Q_DECLARE_TR_FUNCTIONS(Utils::MacroExpander)
48     Q_DISABLE_COPY(MacroExpander)
49 
50 public:
51     explicit MacroExpander();
52     ~MacroExpander();
53 
54     bool resolveMacro(const QString &name, QString *ret) const;
55 
56     QString value(const QByteArray &variable, bool *found = nullptr) const;
57 
58     QString expand(const QString &stringWithVariables) const;
59     FilePath expand(const FilePath &fileNameWithVariables) const;
60     QByteArray expand(const QByteArray &stringWithVariables) const;
61     QVariant expandVariant(const QVariant &v) const;
62 
63     QString expandProcessArgs(const QString &argsWithVariables) const;
64 
65     using PrefixFunction = std::function<QString(QString)>;
66     using ResolverFunction = std::function<bool(QString, QString *)>;
67     using StringFunction = std::function<QString()>;
68     using FileFunction = std::function<FilePath()>;
69     using IntFunction = std::function<int()>;
70 
71     void registerPrefix(const QByteArray &prefix,
72         const QString &description, const PrefixFunction &value, bool visible = true);
73 
74     void registerVariable(const QByteArray &variable,
75         const QString &description, const StringFunction &value,
76         bool visibleInChooser = true);
77 
78     void registerIntVariable(const QByteArray &variable,
79         const QString &description, const IntFunction &value);
80 
81     void registerFileVariables(const QByteArray &prefix,
82         const QString &heading, const FileFunction &value,
83         bool visibleInChooser = true);
84 
85     void registerExtraResolver(const ResolverFunction &value);
86 
87     QList<QByteArray> visibleVariables() const;
88     QString variableDescription(const QByteArray &variable) const;
89     bool isPrefixVariable(const QByteArray &variable) const;
90 
91     MacroExpanderProviders subProviders() const;
92 
93     QString displayName() const;
94     void setDisplayName(const QString &displayName);
95 
96     void registerSubProvider(const MacroExpanderProvider &provider);
97 
98     bool isAccumulating() const;
99     void setAccumulating(bool on);
100 
101 private:
102     friend class Internal::MacroExpanderPrivate;
103     Internal::MacroExpanderPrivate *d;
104 };
105 
106 QTCREATOR_UTILS_EXPORT MacroExpander *globalMacroExpander();
107 
108 } // namespace Utils
109