Commit cff7e7c3 authored by Wouter's avatar Wouter

Make introspector more configurable on CLI

- Exposes configuration to main application
- Keep project id/name/version if already set, when loading files
- Allow saving config file (for saving the default settings)
parent f0dafc13
......@@ -23,8 +23,11 @@
#include <QtCore/QFile>
#include <QtCore/QDir>
#include <QtCore/QtDebug>
#include <QtCore/QCommandLineParser>
#include <QtCore/QCommandLineOption>
#include "lib/introspector.h"
#include <lib/config.h>
using namespace QJsonIntrospect;
......@@ -34,28 +37,42 @@ IntrospectorCli::IntrospectorCli(QObject* parent): QObject(parent)
void IntrospectorCli::execute()
{
Introspector i;
QCommandLineParser parser;
parser.setApplicationDescription(tr("JSON-RPC Code Generator"));
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument(tr("file.json"), tr("JSON-RPC schema definition."));
parser.addPositionalArgument(tr("outdir"), tr("Output directory to write code to."), tr("[outdir]"));
QCommandLineOption projectName(QStringList() << tr("projectName") << tr("n"), tr("Override the project name"), "projectName");
QCommandLineOption projectId(QStringList() << tr("projectId") << tr("i"), tr("Override the project id"), "projectId");
QCommandLineOption projectVersion(QStringList() << tr("projectVersion"), tr("Override the project version"), "projectVersion");
QCommandLineOption config(QStringList() << tr("config") << tr("c"), tr("Configuration file to use."), "config");
QCommandLineOption writeConfig(QStringList() << tr("writeConfig") << tr("w"), tr("Save configuration to file and exit."), "writeConfig");
parser.addOptions(QList<QCommandLineOption>() << projectName << projectId << projectVersion << config << writeConfig);
if (qApp->arguments().size() < 2) {
qFatal("Usage: %s file.json [outdir]", qPrintable(qApp->arguments().at(0)));
qApp->exit(1);
parser.process(*qApp);
if (parser.positionalArguments().isEmpty() && !parser.isSet(writeConfig)) {
parser.showHelp(1);
return;
}
QString file = qApp->arguments().at(1);
if (file.isEmpty() || ((!QFile::exists(file)) && file!= "-")) {
qFatal("File not found: %s", qPrintable(file));
qApp->exit(1);
if (parser.positionalArguments().size() > 2) {
parser.showHelp(1);
return;
}
QString outDir;
if (qApp->arguments().size() > 2) {
outDir = qApp->arguments().at(2);
if (parser.positionalArguments().size() == 2) {
outDir = parser.positionalArguments().at(1);
QDir dirInfo(outDir);
if (!dirInfo.exists()) {
qFatal("Output directory not found: %s", qPrintable(outDir));
qApp->exit(1);
qApp->exit(3);
return;
}
}
......@@ -63,6 +80,35 @@ void IntrospectorCli::execute()
outDir = QDir::currentPath();
}
Introspector i;
if (parser.isSet(projectName)) {
i.setProjectName(parser.value(projectName));
}
if (parser.isSet(projectId)) {
i.setProjectId(parser.value(projectId));
}
if (parser.isSet(projectVersion)) {
i.setProjectVersion(parser.value(projectVersion));
}
if (parser.isSet(config)) {
i.configuration()->loadFromFile(parser.value(config));
}
if (parser.isSet(writeConfig)) {
i.configuration()->saveToFile(parser.value(writeConfig));
qApp->exit(0);
return;
}
// Must have file argument here (not writing config)
QString file = parser.positionalArguments().at(0);
if (file.isEmpty() || ((!QFile::exists(file)) && file != "-")) {
qFatal("File not found: %s", qPrintable(file));
qApp->exit(2);
return;
}
if (!i.load(file)) {
qApp->exit(2);
}
......
#include "lib/config.h"
#include <QtCore/QSettings>
#include <QtCore/QtDebug>
using namespace QJsonIntrospect;
......@@ -232,6 +233,7 @@ void Config::loadFromFile(const QString& file)
QSettings f(file, QSettings::IniFormat);
f.beginGroup("Introspector");
setMainClassName(f.value("MainClassName", "Main").toString());
setOutputNamespace(f.value("Namespace", "Introspected").toString());
setOutputDirectory(f.value("OutputDirectory", "output").toString());
f.endGroup();
......@@ -240,29 +242,29 @@ void Config::loadFromFile(const QString& file)
m_globalTypeHeaderMap.clear();
m_jsonTypeMap.clear();
f.beginGroup("Output Language");
f.beginGroup("Output_Language");
m_reservedNames = f.value("keywords").toStringList().toSet();
f.endGroup();
f.beginGroup("Local Headers");
f.beginGroup("Local_Headers");
Q_FOREACH (const QString &type, f.childKeys()) {
m_localTypeHeaderMap.insert(type, f.value(type).toString());
}
f.endGroup();
f.beginGroup("Global Headers");
f.beginGroup("Global_Headers");
Q_FOREACH (const QString &type, f.childKeys()) {
m_globalTypeHeaderMap.insert(type, f.value(type).toString());
}
f.endGroup();
f.beginGroup("Json Typemap");
f.beginGroup("Json_Typemap");
Q_FOREACH (const QString &type, f.childKeys()) {
m_jsonTypeMap.insert(type, f.value(type).toString());
}
f.endGroup();
f.beginGroup("Header Dependencies");
f.beginGroup("Header_Dependencies");
Q_FOREACH (const QString &item, f.allKeys()) {
Q_FOREACH(const QString type, f.value(item).toStringList()) {
m_headerDependencies.insert(item, type);
......@@ -270,7 +272,7 @@ void Config::loadFromFile(const QString& file)
}
f.endGroup();
f.beginGroup("Implementation Dependencies");
f.beginGroup("Implementation_Dependencies");
Q_FOREACH (const QString &item, f.allKeys()) {
Q_FOREACH(const QString type, f.value(item).toStringList()) {
m_implementationDependencies.insert(item, type);
......@@ -279,6 +281,55 @@ void Config::loadFromFile(const QString& file)
f.endGroup();
}
void Config::saveToFile(const QString& file)
{
QSettings f(file, QSettings::IniFormat);
f.beginGroup("Introspector");
f.setValue("MainClassName", mainClassName());
f.setValue("Namespace", outputNamespace());
f.setValue("OutputDirectory", outputDirectory().path());
f.endGroup();
f.beginGroup("Output_Language");
f.setValue("keywords", QStringList(m_reservedNames.toList()));
f.endGroup();
f.beginGroup("Local_Headers");
f.remove("");
for (QMap<QString, QString>::const_iterator i = m_localTypeHeaderMap.constBegin(); i != m_localTypeHeaderMap.constEnd(); ++i) {
f.setValue(i.key(), i.value());
}
f.endGroup();
f.beginGroup("Global_Headers");
f.remove("");
for (QMap<QString, QString>::const_iterator i = m_globalTypeHeaderMap.constBegin(); i != m_globalTypeHeaderMap.constEnd(); ++i) {
f.setValue(i.key(), i.value());
}
f.endGroup();
f.beginGroup("Json_Typemap");
f.remove("");
for (QMap<QString, QString>::const_iterator i = m_jsonTypeMap.constBegin(); i != m_jsonTypeMap.constEnd(); ++i) {
f.setValue(i.key(), i.value());
}
f.endGroup();
f.beginGroup("Header_Dependencies");
f.remove("");
Q_FOREACH(const QString& key, m_headerDependencies.keys()) {
f.setValue(key, QStringList(m_headerDependencies.values(key)));
}
f.endGroup();
f.beginGroup("Implementation_Dependencies");
f.remove("");
Q_FOREACH(const QString& key, m_implementationDependencies.keys()) {
f.setValue(key, QStringList(m_implementationDependencies.values(key)));
}
f.endGroup();
}
Config::~Config()
{
}
......
......@@ -34,6 +34,7 @@ public slots:
void removeReservedName(const QString &reservedName);
void loadFromFile(const QString &file);
void saveToFile(const QString &file);
public:
explicit Config(QObject* parent = 0);
......
......@@ -157,21 +157,50 @@ QString Introspector::projectId() const
return m_projectId;
}
void Introspector::setProjectId(const QString& projectId)
{
if (projectId != m_projectId) {
m_projectId = projectId;
emit(projectIdChanged(m_projectId));
}
}
QString Introspector::projectName() const
{
return m_projectName;
}
void Introspector::setProjectName(const QString& projectName)
{
if (projectName != m_projectName) {
m_projectName = projectName;
emit(projectIdChanged(m_projectName));
}
}
QString Introspector::projectVersion() const
{
return m_projectVersion;
}
void Introspector::setProjectVersion(const QString& projectVersion)
{
if (projectVersion != m_projectVersion) {
m_projectVersion = projectVersion;
emit(projectIdChanged(m_projectVersion));
}
}
QVariantMap Introspector::originalData() const
{
return m_originalData;
}
Config* Introspector::configuration() const
{
return m_config;
}
bool Introspector::load(const QString& file)
{
// TODO Split up to better support other forms of API descriptors:
......@@ -213,9 +242,16 @@ bool Introspector::load(const QString& file)
QVariantMap data = doc.toVariant().toMap();
m_projectId = data.contains("id") ? data.value("id").toString() : "";
m_projectName = data.contains("description") ? data.value("description").toString() : "";
m_projectVersion = data.contains("version") ? data.value("version").toString() : "";
if (projectId().isEmpty()) {
setProjectId(data.contains("id") ? data.value("id").toString() : "");
}
if (projectName().isEmpty()) {
setProjectName(data.contains("description") ? data.value("description").toString() : "");
}
if (projectVersion().isEmpty()) {
setProjectVersion(data.contains("version") ? data.value("version").toString() : "");
}
m_originalData = data;
readData(data);
QStringList itemIds = sortedProcessList();
......
......@@ -26,9 +26,12 @@ class Property;
class Introspector : public QObject
{
Q_OBJECT
Q_PROPERTY(QString projectId READ projectId)
Q_PROPERTY(QString projectName READ projectName)
Q_PROPERTY(QString projectVersion READ projectVersion)
Q_PROPERTY(Config* configuration READ configuration)
Q_PROPERTY(QString projectId READ projectId WRITE setProjectId NOTIFY projectIdChanged)
Q_PROPERTY(QString projectName READ projectName WRITE setProjectName NOTIFY projectNameChanged)
Q_PROPERTY(QString projectVersion READ projectVersion WRITE setProjectVersion NOTIFY projectVersionChanged)
Q_PROPERTY(QVariantMap originalData READ originalData)
......@@ -183,6 +186,12 @@ public slots:
bool load(const QString& file = QString());
void writeAll(const QString& outputDirectory = QString());
void setProjectId(const QString& projectId);
void setProjectName(const QString& projectName);
void setProjectVersion(const QString& projectVersion);
Config* configuration() const;
public:
Introspector(QObject *parent = 0);
virtual ~Introspector();
......@@ -194,6 +203,11 @@ public:
QString projectName() const;
QString projectVersion() const;
TypeRegistry *typeRegistry() const;
signals:
void projectIdChanged(const QString& projectId) const;
void projectNameChanged(const QString& projectName) const;
void projectVersionChanged(const QString& projectVersion) const;
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment