Commit 30650521 authored by Daniel Mensinger's avatar Daniel Mensinger
Browse files

meson: Apply rewriter changes

parent 9bec87f1
......@@ -15,6 +15,7 @@ set(mesonbuilder_SRCS
rewriter/mesonactionbase.cpp
rewriter/mesonkwargsinfo.cpp
rewriter/mesonkwargsmodify.cpp
rewriter/mesonrewriterjob.cpp
settings/mesonadvancedsettings.cpp
......
......@@ -80,39 +80,22 @@ QString MesonKWARGSInfo::id() const
return m_id;
}
QJsonObject MesonKWARGSInfo::result() const
bool MesonKWARGSInfo::hasKWARG(QString kwarg) const
{
return m_result;
return m_result.contains(kwarg);
}
QString MesonKWARGSInfo::getString(QString kwarg) const
QJsonValue MesonKWARGSInfo::get(QString kwarg) const
{
if (!m_result.contains(kwarg)) {
return QString();
if (!hasKWARG(kwarg)) {
return QJsonValue();
}
return m_result[kwarg].toString();
}
// Getters for Project
QString MesonKWARGSProjectInfo::mesonVersion() const
{
return getString(QStringLiteral("meson_version"));
}
QString MesonKWARGSProjectInfo::license() const
{
return getString(QStringLiteral("license"));
}
QString MesonKWARGSProjectInfo::subprojectDir() const
{
return getString(QStringLiteral("subproject_dir"));
return m_result[kwarg];
}
QString MesonKWARGSProjectInfo::version() const
QString MesonKWARGSInfo::getString(QString kwarg) const
{
return getString(QStringLiteral("version"));
return get(kwarg).toString();
}
// Constructors
......
......@@ -21,7 +21,7 @@
#include "mesonactionbase.h"
#include <QHash>
#include <QJsonObject>
class MesonKWARGSInfo;
class MesonKWARGSProjectInfo;
......@@ -47,8 +47,9 @@ public:
Function function() const;
QString id() const;
QJsonObject result() const;
bool hasKWARG(QString kwarg) const;
QJsonValue get(QString kwarg) const;
QString getString(QString kwarg) const;
private:
......@@ -64,11 +65,6 @@ class MesonKWARGSProjectInfo : public MesonKWARGSInfo
public:
explicit MesonKWARGSProjectInfo();
virtual ~MesonKWARGSProjectInfo();
QString mesonVersion() const;
QString license() const;
QString subprojectDir() const;
QString version() const;
};
class MesonKWARGSTargetInfo : public MesonKWARGSInfo
......@@ -76,8 +72,6 @@ class MesonKWARGSTargetInfo : public MesonKWARGSInfo
public:
explicit MesonKWARGSTargetInfo(QString id);
virtual ~MesonKWARGSTargetInfo();
// TODO some accessors similar to MesonKWARGSProjectInfo
};
class MesonKWARGSDependencyInfo : public MesonKWARGSInfo
......@@ -85,6 +79,4 @@ class MesonKWARGSDependencyInfo : public MesonKWARGSInfo
public:
explicit MesonKWARGSDependencyInfo(QString id);
virtual ~MesonKWARGSDependencyInfo();
// TODO some accessors similar to MesonKWARGSProjectInfo
};
/* This file is part of KDevelop
Copyright 2019 Daniel Mensinger <daniel@mensinger-ka.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "mesonkwargsmodify.h"
#include <QStringList>
MesonKWARGSModify::MesonKWARGSModify(MesonKWARGSModify::Function fn, MesonKWARGSModify::Operation op, QString id)
: m_func(fn)
, m_op(op)
, m_id(id)
{
}
QJsonObject MesonKWARGSModify::command()
{
QJsonObject res;
auto func2str = [](Function fn) -> QString {
switch (fn) {
case PROJECT:
return QStringLiteral("project");
case TARGET:
return QStringLiteral("target");
case DEPENDENCY:
return QStringLiteral("dependency");
}
return QStringLiteral("ERROR");
};
auto op2str = [](Operation op) -> QString {
switch (op) {
case SET:
return QStringLiteral("set");
case DELETE:
return QStringLiteral("delete");
}
return QStringLiteral("ERROR");
};
res[QStringLiteral("type")] = QStringLiteral("kwargs");
res[QStringLiteral("function")] = func2str(m_func);
res[QStringLiteral("id")] = m_id;
res[QStringLiteral("operation")] = op2str(m_op);
res[QStringLiteral("kwargs")] = m_kwargs;
return res;
}
void MesonKWARGSModify::set(QString kwarg, QJsonValue value)
{
m_kwargs[kwarg] = value;
}
void MesonKWARGSModify::unset(QString kwarg)
{
if (isSet(kwarg)) {
m_kwargs.remove(kwarg);
}
}
void MesonKWARGSModify::clear()
{
auto kwargs = m_kwargs.keys();
for (auto i : kwargs) {
m_kwargs.remove(i);
}
}
bool MesonKWARGSModify::isSet(QString kwarg)
{
return m_kwargs.contains(kwarg);
}
MesonKWARGSModify::Function MesonKWARGSModify::function() const
{
return m_func;
}
MesonKWARGSModify::Operation MesonKWARGSModify::operation() const
{
return m_op;
}
QString MesonKWARGSModify::id() const
{
return m_id;
}
// Constructors
MesonKWARGSProjectModify::MesonKWARGSProjectModify(MesonKWARGSModify::Operation op)
: MesonKWARGSModify(PROJECT, op, QStringLiteral("/"))
{
}
MesonKWARGSTargetModify::MesonKWARGSTargetModify(MesonKWARGSModify::Operation op, QString id)
: MesonKWARGSModify(TARGET, op, id)
{
}
MesonKWARGSDependencyModify::MesonKWARGSDependencyModify(MesonKWARGSModify::Operation op, QString id)
: MesonKWARGSModify(DEPENDENCY, op, id)
{
}
// Destructors
MesonKWARGSModify::~MesonKWARGSModify() {}
MesonKWARGSProjectModify::~MesonKWARGSProjectModify() {}
MesonKWARGSTargetModify::~MesonKWARGSTargetModify() {}
MesonKWARGSDependencyModify::~MesonKWARGSDependencyModify() {}
/* This file is part of KDevelop
Copyright 2019 Daniel Mensinger <daniel@mensinger-ka.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#pragma once
#include "mesonactionbase.h"
class MesonKWARGSModify;
class MesonKWARGSProjectModify;
class MesonKWARGSTargetModify;
class MesonKWARGSDependencyModify;
using MesonKWARGSModifyPtr = std::shared_ptr<MesonKWARGSModify>;
using MesonKWARGSProjectModifyPtr = std::shared_ptr<MesonKWARGSProjectModify>;
using MesonKWARGSTargetModifyPtr = std::shared_ptr<MesonKWARGSTargetModify>;
using MesonKWARGSDependencyModifyPtr = std::shared_ptr<MesonKWARGSDependencyModify>;
class MesonKWARGSModify : public MesonRewriterActionBase
{
public:
enum Function { PROJECT, TARGET, DEPENDENCY };
enum Operation { SET, DELETE };
public:
explicit MesonKWARGSModify(Function fn, Operation op, QString id);
virtual ~MesonKWARGSModify();
QJsonObject command() override;
Function function() const;
Operation operation() const;
QString id() const;
void set(QString kwarg, QJsonValue value);
void unset(QString kwarg);
void clear();
bool isSet(QString kwarg);
private:
Function m_func;
Operation m_op;
QString m_id;
QJsonObject m_kwargs = QJsonObject();
};
class MesonKWARGSProjectModify : public MesonKWARGSModify
{
public:
explicit MesonKWARGSProjectModify(Operation op);
virtual ~MesonKWARGSProjectModify();
};
class MesonKWARGSTargetModify : public MesonKWARGSModify
{
public:
explicit MesonKWARGSTargetModify(Operation op, QString id);
virtual ~MesonKWARGSTargetModify();
};
class MesonKWARGSDependencyModify : public MesonKWARGSModify
{
public:
explicit MesonKWARGSDependencyModify(Operation op, QString id);
virtual ~MesonKWARGSDependencyModify();
};
......@@ -18,17 +18,22 @@
*/
#include "mesonrewriterinput.h"
#include "rewriter/mesonkwargsinfo.h"
#include "rewriter/mesonkwargsmodify.h"
#include "ui_mesonrewriterinput.h"
#include <KColorScheme>
#include <QLineEdit>
#include <debug.h>
MesonRewriterInputBase::MesonRewriterInputBase(QWidget* parent)
MesonRewriterInputBase::MesonRewriterInputBase(QString name, QString kwarg, QWidget* parent)
: QWidget(parent)
, m_name(name)
, m_kwarg(kwarg)
{
m_ui = new Ui::MesonRewriterInputBase;
m_ui->setupUi(this);
m_ui->l_name->setText(m_name + QStringLiteral(":"));
connect(this, &MesonRewriterInputBase::configChanged, this, &MesonRewriterInputBase::updateUi);
}
......@@ -58,8 +63,6 @@ void MesonRewriterInputBase::setInputWidget(QWidget* input)
void MesonRewriterInputBase::updateUi()
{
m_ui->l_name->setText(m_name + QStringLiteral(":"));
KColorScheme scheme(QPalette::Normal);
KColorScheme::ForegroundRole role;
......@@ -103,9 +106,10 @@ void MesonRewriterInputBase::add()
reset();
}
void MesonRewriterInputBase::resetWidgetBase(bool enabled)
void MesonRewriterInputBase::resetFromAction(MesonKWARGSInfo* action)
{
m_default_enabled = m_enabled = enabled;
resetValue(action->get(m_kwarg));
m_default_enabled = m_enabled = action->hasKWARG(m_kwarg);
if (m_enabled) {
add();
} else {
......@@ -113,6 +117,11 @@ void MesonRewriterInputBase::resetWidgetBase(bool enabled)
}
}
void MesonRewriterInputBase::writeToAction(MesonKWARGSModify* action)
{
action->set(m_kwarg, value());
}
bool MesonRewriterInputBase::hasChanged() const
{
return hasValueChanged() || (m_default_enabled != m_enabled);
......@@ -125,8 +134,8 @@ bool MesonRewriterInputBase::isEnabled() const
// String input class
MesonRewriterInputString::MesonRewriterInputString(QWidget* parent)
: MesonRewriterInputBase(parent)
MesonRewriterInputString::MesonRewriterInputString(QString name, QString kwarg, QWidget* parent)
: MesonRewriterInputBase(name, kwarg, parent)
{
m_lineEdit = new QLineEdit(this);
connect(m_lineEdit, &QLineEdit::textChanged, this, [=]() { emit configChanged(); });
......@@ -155,8 +164,12 @@ void MesonRewriterInputString::doReset()
m_lineEdit->setText(m_initialValue);
}
void MesonRewriterInputString::resetWidget(QString initialValue)
void MesonRewriterInputString::resetValue(QJsonValue val)
{
m_initialValue = val.toString();
}
QJsonValue MesonRewriterInputString::value()
{
m_initialValue = initialValue;
resetWidgetBase(!initialValue.isNull());
return QJsonValue(m_lineEdit->text());
}
......@@ -19,6 +19,7 @@
#pragma once
#include <QJsonValue>
#include <QWidget>
namespace Ui
......@@ -28,6 +29,9 @@ class MesonRewriterInputBase;
class QLineEdit;
class MesonKWARGSInfo;
class MesonKWARGSModify;
class MesonRewriterInputBase : public QWidget
{
Q_OBJECT
......@@ -35,10 +39,8 @@ class MesonRewriterInputBase : public QWidget
public:
enum Type { STRING };
Q_PROPERTY(QString name MEMBER m_name NOTIFY configChanged)
public:
explicit MesonRewriterInputBase(QWidget* parent);
explicit MesonRewriterInputBase(QString name, QString kwarg, QWidget* parent);
virtual ~MesonRewriterInputBase();
int nameWidth();
......@@ -48,13 +50,17 @@ public:
virtual Type type() const = 0;
void resetFromAction(MesonKWARGSInfo* action);
void writeToAction(MesonKWARGSModify* action);
protected:
void setInputWidget(QWidget* input);
void resetWidgetBase(bool enabled);
virtual bool hasValueChanged() const = 0;
virtual void doReset() = 0;
virtual QWidget* inputWidget() = 0;
virtual void resetValue(QJsonValue val) = 0;
virtual QJsonValue value() = 0;
public Q_SLOTS:
void reset();
......@@ -68,6 +74,7 @@ Q_SIGNALS:
private:
Ui::MesonRewriterInputBase* m_ui = nullptr;
QString m_name;
QString m_kwarg;
bool m_enabled = false;
bool m_default_enabled = false;
};
......@@ -77,17 +84,17 @@ class MesonRewriterInputString : public MesonRewriterInputBase
Q_OBJECT
public:
explicit MesonRewriterInputString(QWidget* parent);
explicit MesonRewriterInputString(QString name, QString kwarg, QWidget* parent);
virtual ~MesonRewriterInputString();
MesonRewriterInputBase::Type type() const override;
void resetWidget(QString initialValue);
protected:
void doReset() override;
QWidget* inputWidget() override;
bool hasValueChanged() const override;
void resetValue(QJsonValue val) override;
QJsonValue value() override;
private:
QString m_initialValue;
......
......@@ -20,9 +20,11 @@
#include "mesonrewriterpage.h"
#include "mesonconfig.h"
#include "mesonmanager.h"
#include "mesonrewriterinput.h"
#include "mintro/mesonintrospectjob.h"
#include "mintro/mesonprojectinfo.h"
#include "rewriter/mesonkwargsinfo.h"
#include "rewriter/mesonkwargsmodify.h"
#include "rewriter/mesonrewriterjob.h"
#include "ui_mesonrewriterpage.h"
......@@ -66,22 +68,43 @@ MesonRewriterPage::MesonRewriterPage(IPlugin* plugin, IProject* project, QWidget
m_ui = new Ui::MesonRewriterPage;
m_ui->setupUi(this);
m_projectKwargs = constructPojectInputs();
// Calculate the maximum name width to align all widgets
vector<int> lengths;
int maxWidth = 50;
m_projectKwargs = { m_ui->w_version, m_ui->w_license, m_ui->w_mesonVersion };
lengths.reserve(m_projectKwargs.size());
for (auto* i : m_projectKwargs) {
maxWidth = std::max(maxWidth, i->nameWidth());
}
auto transform_op = [](MesonRewriterInputBase* x) -> int { return x->nameWidth(); };
transform(begin(m_projectKwargs), end(m_projectKwargs), back_inserter(lengths), transform_op);
maxWidth = accumulate(begin(lengths), end(lengths), maxWidth, [](int a, int b) -> int { return max(a, b); });
// Initialize widgets
for (auto* i : m_projectKwargs) {
m_ui->c_project->addWidget(i);
i->setMinNameWidth(maxWidth);
connect(i, &MesonRewriterInputBase::configChanged, this, &MesonRewriterPage::emitChanged);
}
m_ui->l_dispProject->setMinimumWidth(maxWidth);
reset();
}
#define STRING_INPUT(name, id) new MesonRewriterInputString(QStringLiteral(name), QStringLiteral(id), this)
QVector<MesonRewriterInputBase*> MesonRewriterPage::constructPojectInputs()
{
return {
STRING_INPUT("Version", "version"),
STRING_INPUT("License", "license"),
STRING_INPUT("Meson version", "meson_version"),
STRING_INPUT("Subprojects directory", "subproject_dir"),
};
}
void MesonRewriterPage::setWidgetsDisabled(bool disabled)
{
m_ui->c_tabs->setDisabled(disabled);
......@@ -161,6 +184,33 @@ void MesonRewriterPage::setStatus(MesonRewriterPage::State s)
void MesonRewriterPage::apply()
{
qCDebug(KDEV_Meson) << "REWRITER GUI: APPLY";
auto projectSet = make_shared<MesonKWARGSProjectModify>(MesonKWARGSProjectModify::SET);
auto projectDel = make_shared<MesonKWARGSProjectModify>(MesonKWARGSProjectModify::DELETE);
auto writer = [](MesonRewriterInputBase* widget, MesonKWARGSModifyPtr set, MesonKWARGSModifyPtr del) {
if (!widget->hasChanged()) {
return;
}
if (widget->isEnabled()) {
widget->writeToAction(set.get());
} else {
widget->writeToAction(del.get());
}
};
for_each(begin(m_projectKwargs), end(m_projectKwargs), [&](auto* w) { writer(w, projectSet, projectDel); });
QVector<MesonRewriterActionPtr> actions = { projectSet, projectDel };
KJob* rewriterJob = new MesonRewriterJob(m_project, actions, this);
// Reload the GUI once the data has been written
connect(rewriterJob, &KJob::result, this, &MesonRewriterPage::reset);
setStatus(WRITING);
rewriterJob->start();
}
void MesonRewriterPage::reset()
......@@ -203,9 +253,10 @@ void MesonRewriterPage::reset()
m_ui->l_project->setText(QStringLiteral("<html><head/><body><h3>") + prInfo->name()
+ QStringLiteral("</h3></body></html>"));
m_ui->w_version->resetWidget(projectInfo->version());
m_ui->w_license->resetWidget(projectInfo->license());
m_ui->w_mesonVersion->resetWidget(projectInfo->mesonVersion());
auto setter = [](MesonRewriterInputBase* w, MesonKWARGSInfoPtr i) { w->resetFromAction(i.get()); };
for_each(begin(m_projectKwargs), end(m_projectKwargs), [=](auto* x) { setter(x, projectInfo); });
setStatus(READY);
return;
......
......@@ -19,9 +19,9 @@
#pragma once
#include <interfaces/configpage.h>
#include <QAbstractButton>
#include <vector>
#include <QVector>
#include <interfaces/configpage.h>
namespace KDevelop
{
......@@ -40,7 +40,7 @@ class MesonRewriterPage : public KDevelop::ConfigPage
{
Q_OBJECT
public:
enum State {START, LOADING, WRITING, READY, ERROR};
enum State { START, LOADING, WRITING, READY, ERROR };
public:
explicit MesonRewriterPage(KDevelop::IPlugin* plugin, KDevelop::IProject* project, QWidget* parent = nullptr);
......@@ -56,6 +56,8 @@ public Q_SLOTS:
void emitChanged();
QVector<MesonRewriterInputBase*> constructPojectInputs();
private:
void setWidgetsDisabled(bool disabled);
void checkStatus();
......@@ -67,5 +69,5 @@