178 lines
5.8 KiB
C++
Executable File
178 lines
5.8 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* main for ExerciseManagement
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
*/
|
|
|
|
#include "StructBuilder.h"
|
|
#include "XmlReader.h"
|
|
#include "DaoGeneratorGrantlee.h"
|
|
#include "DatabaseGeneratorGrantlee.h"
|
|
#include "CppTopicGeneratorGrantlee.h"
|
|
#include "SqlGenerator.h"
|
|
|
|
#include <nglapplication/nglcoreapplication.h>
|
|
#include <QDir>
|
|
#include <NGLLogger/INGLLogger.h>
|
|
#include <LtsCommandLineParser/LtsCommandLineParser.h>
|
|
|
|
// ------------------------------------------------------------------
|
|
class CmdLineParser : public LtsCommandLineParser::LtsCommandLineParser
|
|
{
|
|
public:
|
|
CmdLineParser()
|
|
: LtsCommandLineParser(LtsCommandLineParser::LtsCommandLineParser::None)
|
|
{
|
|
addOption({ QStringList() << QStringLiteral("s") << QStringLiteral("servicefile"),
|
|
QStringLiteral("full path to the service xml-file"),
|
|
QStringLiteral("service xml-file"),
|
|
QStringLiteral("/home")
|
|
});
|
|
|
|
addOption({ QStringList() << QStringLiteral("a") << QStringLiteral("action"),
|
|
QStringLiteral("the action to execute"),
|
|
QStringLiteral("DATABASE|DATABASE_SERVER|IDL|DDS_TOPICS"),
|
|
QStringLiteral("/0")
|
|
});
|
|
|
|
addOption({ QStringList() << QStringLiteral("o") << QStringLiteral("outdir"),
|
|
QStringLiteral("the output directory where to put the generated files"),
|
|
QStringLiteral("output directory"),
|
|
QStringLiteral("/home")
|
|
});
|
|
|
|
addOption({ QStringList() << QStringLiteral("i") << QStringLiteral("idlinfo"),
|
|
QStringLiteral("the info file with all available idl service files"),
|
|
QStringLiteral("info file for idl"),
|
|
QStringLiteral("/0")
|
|
});
|
|
}
|
|
|
|
bool init()
|
|
{
|
|
if (!LtsCommandLineParser::init())
|
|
return false;
|
|
|
|
if (serviceFile().isEmpty() || action().isEmpty() || outDir().isEmpty())
|
|
{
|
|
showHelp();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
QString serviceFile() const { return value(QStringLiteral("s")); }
|
|
QString action() const { return value(QStringLiteral("a")); }
|
|
QString outDir() const { return value(QStringLiteral("o")); }
|
|
QString idlInfoFile() const { return value(QStringLiteral("i")); }
|
|
};
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
NGLCoreApplication app(argc, argv);
|
|
app.createLogger("CodeGen", "codegen_log4cxx.props", true /* don't spam while compilation of the project */);
|
|
|
|
NGL::INGLLogger &m_logger = app.baseLogger();
|
|
|
|
CmdLineParser options;
|
|
if (!options.init())
|
|
return -1;
|
|
|
|
QScopedPointer<CodeGen::XmlReader> reader(new CodeGen::XmlReader);
|
|
|
|
DEBUG_LOG(m_logger) << "ServiceFile: " << options.serviceFile();
|
|
DEBUG_LOG(m_logger) << "IdlInfoFile: " << options.idlInfoFile();
|
|
if (!reader->readServiceFile(options.serviceFile(), options.idlInfoFile()))
|
|
{
|
|
ERROR_LOG(m_logger) << "Error parsing service file '" << options.serviceFile()
|
|
<< ": " << reader->errorString();
|
|
return 2;
|
|
}
|
|
|
|
QDir dir(options.outDir());
|
|
DEBUG_LOG(m_logger) << "Directory: " << options.outDir();
|
|
if (!dir.exists() && !dir.mkpath(options.outDir()))
|
|
{
|
|
ERROR_LOG(m_logger) << "Directory '" << options.outDir()
|
|
<< "' does not exist and can not be created!";
|
|
return 3;
|
|
}
|
|
|
|
QScopedPointer<CodeGen::StructBuilder> structBuilder(new CodeGen::StructBuilder(reader.data()));
|
|
if (!structBuilder->buildStructures())
|
|
{
|
|
ERROR_LOG(m_logger) << "Error building up structures";
|
|
return 4;
|
|
}
|
|
|
|
const QString action = options.action().toLower();
|
|
//INFO_LOG(m_logger) << "action = " << action;
|
|
|
|
QScopedPointer<CodeGen::GeneratorGrantleeBase> generator;
|
|
if (action == QStringLiteral("dao"))
|
|
generator.reset(new CodeGen::DaoGeneratorGrantlee);
|
|
else if (action == QStringLiteral("database"))
|
|
generator.reset(new CodeGen::DatabaseGeneratorGrantlee(false));
|
|
else if (action == QStringLiteral("database_server"))
|
|
generator.reset(new CodeGen::DatabaseGeneratorGrantlee(true));
|
|
else if (action == QStringLiteral("dds_topics"))
|
|
generator.reset(new CodeGen::CppTopicGeneratorGrantlee);
|
|
else if (action == QStringLiteral("sql"))
|
|
{
|
|
CodeGen::SqlGenerator sql(reader.data(), structBuilder.data());
|
|
if (!sql.generateData(35))
|
|
return 8;
|
|
|
|
// .sql will be appended in method
|
|
if (!sql.writeSql(dir.absolutePath() + QLatin1Char('/'), CodeGen::ExerciseDependency::Exercise))
|
|
{
|
|
ERROR_LOG(m_logger) << "Error while writing sql information for service " << sql.getServiceAbbr();
|
|
return 10;
|
|
}
|
|
|
|
if (!sql.generateData(42))
|
|
return 8;
|
|
|
|
// .sql will be appended in method
|
|
if (!sql.writeSql(dir.absolutePath() + QLatin1Char('/'), CodeGen::ExerciseDependency::Exercise))
|
|
{
|
|
ERROR_LOG(m_logger) << "Error while writing sql information for service " << sql.getServiceAbbr();
|
|
return 10;
|
|
}
|
|
|
|
// recreate data for exercise independent statements
|
|
sql.generateData(0);
|
|
if (!sql.writeSql(dir.absolutePath() + QLatin1Char('/'), CodeGen::ExerciseDependency::Global))
|
|
{
|
|
ERROR_LOG(m_logger) << "Error while writing sql information for service " << sql.getServiceAbbr();
|
|
return 10;
|
|
}
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
ERROR_LOG(m_logger) << "Invalid action '" << options.action() << "' given!";
|
|
return 5;
|
|
}
|
|
|
|
if (!generator->init(reader.data(), structBuilder.data()))
|
|
{
|
|
ERROR_LOG(m_logger) << "Error initializing generator!";
|
|
return 6;
|
|
}
|
|
|
|
//INFO_LOG(m_logger) << "init generator ";
|
|
//INFO_LOG(m_logger) << "########## options.arguments.size(): " << options.arguments().size();
|
|
|
|
const auto posArgs = options.positionalArguments();
|
|
for (const QString &file : posArgs)
|
|
{
|
|
//INFO_LOG(m_logger) << "GenerateData for " << file;
|
|
if (!generator->generateData(file, dir.absolutePath()))
|
|
return 7;
|
|
}
|
|
return 0;
|
|
}
|