forked from qt-creator/qt-creator
VariableManager: Allow variables that are triggered by a prefix
E.g. "Env:<some environment var>". Remove the special handling for the "Env:" varibale that was coded into the VaribaleManager. Change-Id: If8b074b66eeaa97903b41634f9a3c86dd73087d4 Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
This commit is contained in:
@@ -58,6 +58,7 @@ class VariableManagerPrivate
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
QHash<QByteArray, VariableManager::StringFunction> m_map;
|
QHash<QByteArray, VariableManager::StringFunction> m_map;
|
||||||
|
QHash<QByteArray, VariableManager::PrefixFunction> m_prefixMap;
|
||||||
VMMapExpander m_macroExpander;
|
VMMapExpander m_macroExpander;
|
||||||
QMap<QByteArray, QString> m_descriptions;
|
QMap<QByteArray, QString> m_descriptions;
|
||||||
};
|
};
|
||||||
@@ -177,6 +178,10 @@ VariableManager::VariableManager()
|
|||||||
{
|
{
|
||||||
d = new VariableManagerPrivate;
|
d = new VariableManagerPrivate;
|
||||||
variableManagerInstance = this;
|
variableManagerInstance = this;
|
||||||
|
|
||||||
|
registerPrefix("Env", QCoreApplication::translate("Core::VariableManager", "Access environment variables."),
|
||||||
|
[](const QString &value)
|
||||||
|
{ return QString::fromLocal8Bit(qgetenv(value.toLocal8Bit())); });
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -194,16 +199,25 @@ VariableManager::~VariableManager()
|
|||||||
*/
|
*/
|
||||||
QString VariableManager::value(const QByteArray &variable, bool *found)
|
QString VariableManager::value(const QByteArray &variable, bool *found)
|
||||||
{
|
{
|
||||||
if (variable.startsWith("Env:")) {
|
StringFunction sf = d->m_map.value(variable);
|
||||||
QByteArray ba = qgetenv(variable.data() + 4);
|
if (sf) {
|
||||||
if (found)
|
if (found)
|
||||||
*found = !ba.isNull();
|
*found = true;
|
||||||
return QString::fromLocal8Bit(ba);
|
return sf();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto it = d->m_prefixMap.constBegin(); it != d->m_prefixMap.constEnd(); ++it) {
|
||||||
|
if (variable.startsWith(it.key())) {
|
||||||
|
PrefixFunction pf = it.value();
|
||||||
|
if (found)
|
||||||
|
*found = true;
|
||||||
|
return pf(QString::fromUtf8(variable.mid(it.key().count())));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (found)
|
if (found)
|
||||||
*found = d->m_map.contains(variable);
|
*found = false;
|
||||||
StringFunction f = d->m_map.value(variable);
|
|
||||||
return f ? f() : QString();
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -231,11 +245,30 @@ Utils::AbstractMacroExpander *VariableManager::macroExpander()
|
|||||||
return &d->m_macroExpander;
|
return &d->m_macroExpander;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Makes the given string-valued \a prefix known to the variable manager,
|
||||||
|
* together with a localized \a description.
|
||||||
|
*
|
||||||
|
* The \a value PrefixFunction will be called and gets the full variable name
|
||||||
|
* with the prefix stripped as input.
|
||||||
|
*
|
||||||
|
* \sa registerVariables(), registerIntVariable(), registerFileVariables()
|
||||||
|
*/
|
||||||
|
void VariableManager::registerPrefix(const QByteArray &prefix, const QString &description,
|
||||||
|
const VariableManager::PrefixFunction &value)
|
||||||
|
{
|
||||||
|
QByteArray tmp = prefix;
|
||||||
|
if (!tmp.endsWith(':'))
|
||||||
|
tmp.append(':');
|
||||||
|
d->m_descriptions.insert(tmp + "<value>", description);
|
||||||
|
d->m_prefixMap.insert(tmp, value);
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Makes the given string-valued \a variable known to the variable manager,
|
* Makes the given string-valued \a variable known to the variable manager,
|
||||||
* together with a localized \a description.
|
* together with a localized \a description.
|
||||||
*
|
*
|
||||||
* \sa registerFileVariables(), registerIntVariable()
|
* \sa registerFileVariables(), registerIntVariable(), registerPrefix()
|
||||||
*/
|
*/
|
||||||
void VariableManager::registerVariable(const QByteArray &variable,
|
void VariableManager::registerVariable(const QByteArray &variable,
|
||||||
const QString &description, const StringFunction &value)
|
const QString &description, const StringFunction &value)
|
||||||
@@ -248,7 +281,7 @@ void VariableManager::registerVariable(const QByteArray &variable,
|
|||||||
* Makes the given integral-valued \a variable known to the variable manager,
|
* Makes the given integral-valued \a variable known to the variable manager,
|
||||||
* together with a localized \a description.
|
* together with a localized \a description.
|
||||||
*
|
*
|
||||||
* \sa registerVariable(), registerFileVariables()
|
* \sa registerVariable(), registerFileVariables(), registerPrefix()
|
||||||
*/
|
*/
|
||||||
void VariableManager::registerIntVariable(const QByteArray &variable,
|
void VariableManager::registerIntVariable(const QByteArray &variable,
|
||||||
const QString &description, const VariableManager::IntFunction &value)
|
const QString &description, const VariableManager::IntFunction &value)
|
||||||
@@ -265,6 +298,8 @@ void VariableManager::registerIntVariable(const QByteArray &variable,
|
|||||||
* For example \c{registerFileVariables("CurrentDocument", tr("Current Document"))} registers
|
* For example \c{registerFileVariables("CurrentDocument", tr("Current Document"))} registers
|
||||||
* variables such as \c{CurrentDocument:FilePath} with description
|
* variables such as \c{CurrentDocument:FilePath} with description
|
||||||
* "Current Document: Full path including file name."
|
* "Current Document: Full path including file name."
|
||||||
|
*
|
||||||
|
* \sa registerVariable(), registerIntVariable(), registerPrefix()
|
||||||
*/
|
*/
|
||||||
void VariableManager::registerFileVariables(const QByteArray &prefix,
|
void VariableManager::registerFileVariables(const QByteArray &prefix,
|
||||||
const QString &heading, const StringFunction &base)
|
const QString &heading, const StringFunction &base)
|
||||||
|
|||||||
@@ -51,10 +51,13 @@ public:
|
|||||||
static QString expandedString(const QString &stringWithVariables);
|
static QString expandedString(const QString &stringWithVariables);
|
||||||
static Utils::AbstractMacroExpander *macroExpander();
|
static Utils::AbstractMacroExpander *macroExpander();
|
||||||
|
|
||||||
|
typedef std::function<QString(QString)> PrefixFunction;
|
||||||
typedef std::function<QString()> StringFunction;
|
typedef std::function<QString()> StringFunction;
|
||||||
typedef std::function<int()> IntFunction;
|
typedef std::function<int()> IntFunction;
|
||||||
|
|
||||||
|
static void registerPrefix(const QByteArray &prefix,
|
||||||
|
const QString &description, const PrefixFunction &value);
|
||||||
|
|
||||||
static void registerVariable(const QByteArray &variable,
|
static void registerVariable(const QByteArray &variable,
|
||||||
const QString &description, const StringFunction &value);
|
const QString &description, const StringFunction &value);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user