forked from qt-creator/qt-creator
refactor VariableManager
make it provide a macroExpander() instead of a resolve() function. this detaches the actual variable management from the string manipulation.
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
#include "editormanager/ieditor.h"
|
||||
#include "editormanager/editormanager.h"
|
||||
|
||||
#include <utils/stringutils.h>
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
#include <QtCore/QFileInfo>
|
||||
@@ -41,6 +42,15 @@
|
||||
|
||||
namespace Core {
|
||||
|
||||
class VMMapExpander : public Utils::AbstractQtcMacroExpander {
|
||||
public:
|
||||
virtual bool resolveMacro(const QString &name, QString *ret)
|
||||
{
|
||||
*ret = Core::VariableManager::instance()->value(name);
|
||||
return !ret->isEmpty();
|
||||
}
|
||||
};
|
||||
|
||||
class VariableManagerPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@@ -54,7 +64,8 @@ public slots:
|
||||
void updateCurrentDocument(Core::IEditor *editor);
|
||||
|
||||
public:
|
||||
QMap<QString, QString> m_map;
|
||||
QHash<QString, QString> m_map;
|
||||
VMMapExpander m_macroExpander;
|
||||
static VariableManager *m_instance;
|
||||
};
|
||||
|
||||
@@ -144,20 +155,6 @@ QString VariableManager::value(const QString &variable, const QString &defaultVa
|
||||
return d->m_map.value(variable, defaultValue);
|
||||
}
|
||||
|
||||
QString VariableManager::resolve(const QString &stringWithVariables) const
|
||||
{
|
||||
QString result = stringWithVariables;
|
||||
QMapIterator<QString, QString> i(d->m_map);
|
||||
while (i.hasNext()) {
|
||||
i.next();
|
||||
QString key = QLatin1String("%{");
|
||||
key += i.key();
|
||||
key += QLatin1Char('}');
|
||||
result.replace(key, i.value());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void VariableManager::insert(const QString &variable, const QString &value)
|
||||
{
|
||||
d->insert(variable, value);
|
||||
@@ -178,6 +175,11 @@ bool VariableManager::remove(const QString &variable)
|
||||
return d->remove(variable);
|
||||
}
|
||||
|
||||
Utils::AbstractMacroExpander *VariableManager::macroExpander()
|
||||
{
|
||||
return &d->m_macroExpander;
|
||||
}
|
||||
|
||||
VariableManager* VariableManager::instance()
|
||||
{
|
||||
return VariableManagerPrivate::m_instance;
|
||||
|
||||
@@ -39,6 +39,10 @@ QT_BEGIN_NAMESPACE
|
||||
class QFileInfo;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
namespace Utils {
|
||||
class AbstractMacroExpander;
|
||||
}
|
||||
|
||||
namespace Core {
|
||||
class VariableManagerPrivate;
|
||||
|
||||
@@ -58,7 +62,7 @@ public:
|
||||
QString value(const QString &variable) const;
|
||||
QString value(const QString &variable, const QString &defaultValue) const;
|
||||
bool remove(const QString &variable);
|
||||
QString resolve(const QString &stringWithVariables) const;
|
||||
Utils::AbstractMacroExpander *macroExpander();
|
||||
|
||||
private:
|
||||
QScopedPointer<VariableManagerPrivate> d;
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
#include <projectexplorer/projectexplorer.h>
|
||||
#include <projectexplorer/gnumakeparser.h>
|
||||
#include <coreplugin/variablemanager.h>
|
||||
#include <utils/stringutils.h>
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
#include <QtGui/QFormLayout>
|
||||
@@ -102,9 +103,8 @@ bool GenericMakeStep::init()
|
||||
GenericBuildConfiguration *bc = genericBuildConfiguration();
|
||||
|
||||
setEnabled(true);
|
||||
Core::VariableManager *vm = Core::VariableManager::instance();
|
||||
const QString rawBuildDir = bc->buildDirectory();
|
||||
const QString buildDir = vm->resolve(rawBuildDir);
|
||||
QString buildDir = bc->buildDirectory();
|
||||
Utils::expandMacros(&buildDir, Core::VariableManager::instance()->macroExpander());
|
||||
setWorkingDirectory(buildDir);
|
||||
|
||||
setCommand(makeCommand());
|
||||
@@ -140,15 +140,17 @@ bool GenericMakeStep::fromMap(const QVariantMap &map)
|
||||
|
||||
QStringList GenericMakeStep::replacedArguments() const
|
||||
{
|
||||
Core::VariableManager *vm = Core::VariableManager::instance();
|
||||
Utils::AbstractMacroExpander *mx = Core::VariableManager::instance()->macroExpander();
|
||||
const QStringList targets = m_buildTargets;
|
||||
QStringList arguments = m_makeArguments;
|
||||
QStringList replacedArguments;
|
||||
foreach (const QString &arg, arguments) {
|
||||
replacedArguments.append(vm->resolve(arg));
|
||||
foreach (QString arg, arguments) {
|
||||
Utils::expandMacros(&arg, mx);
|
||||
replacedArguments << arg;
|
||||
}
|
||||
foreach (const QString &arg, targets) {
|
||||
replacedArguments.append(vm->resolve(arg));
|
||||
foreach (QString arg, targets) {
|
||||
Utils::expandMacros(&arg, mx);
|
||||
replacedArguments << arg;
|
||||
}
|
||||
return replacedArguments;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user