<commit_before>/* Copyright (c) 2013, <NAME>
 * All rights reserved.
 *
 * This software may be used and distributed according to the terms of
 * the New BSD License, which is incorporated herein by reference.
 */

#include "mongoobjgenerator.h"
#include "global.h"
#include "projectfilegenerator.h"
#include "filewriter.h"
#include <QPair>
#include <QRegExp>
#include <unistd.h>

#define MONGOOBJECT_HEADER_TEMPLATE                                     \
    "#ifndef %1OBJECT_H\n"                                              \
    "#define %1OBJECT_H\n"                                              \
    "\n"                                                                \
    "#include <TMongoObject>\n"                                         \
    "#include <QSharedData>\n"                                          \
    "\n\n"                                                              \
    "class T_MODEL_EXPORT %2Object : public TMongoObject, public QSharedData\n" \
    "{\n"                                                               \
    "public:\n"                                                         \
    "%3"                                                                \
    "\n"                                                                \
    "    enum PropertyIndex {\n"                                        \
    "%4"                                                                \
    "    };\n"                                                          \
    "\n"                                                                \
    "    virtual QString collectionName() const { return \"%5\"; }\n"   \
    "    virtual QString objectId() const { return _id; }\n"            \
    "    virtual QString &objectId() { return _id; }\n"                 \
    "\n"                                                                \
    "private:\n"                                                        \
    "    Q_OBJECT\n"                                                    \
    "%6"                                                                \
    "};\n"                                                              \
    " \n"                                                               \
    "#endif // %1OBJECT_H"

#define MONGOOBJECT_HEADER_UPDATE_TEMPLATE                              \
    "%3\n"                                                              \
    "%4"                                                                \
    "\n"                                                                \
    "    enum PropertyIndex {\n"                                        \
    "%5"                                                                \
    "    };\n"                                                          \
    "\n"                                                                \
    "    virtual QString collectionName() const { return \"%2\"; }\n"   \
    "    virtual QString objectId() const { return _id; }\n"            \
    "    virtual QString &objectId() { return _id; }\n"                 \
    "\n"                                                                \
    "private:\n"                                                        \
    "    Q_OBJECT\n"                                                    \
    "%6"                                                                \
    "};\n"                                                              \
    " \n"                                                               \
    "#endif // %1OBJECT_H"

#define MONGOOBJECT_PROPERTY_TEMPLATE                \
    "    Q_PROPERTY(%1 %2 READ get%2 WRITE set%2)\n" \
    "    T_DEFINE_PROPERTY(%1, %2)\n"

const QRegExp rxstart("\\{\\s*public\\s*:", Qt::CaseSensitive, QRegExp::RegExp2);


MongoObjGenerator::MongoObjGenerator(const QString &model)
    : modelName(model), fields()
{ }


QString MongoObjGenerator::mongoObjectFilePath(const QString &dstDir) const
{
    return QDir(dstDir + "/mongoobjects").filePath(modelName.toLower() + "object.h");
}


QString MongoObjGenerator::generate(const QString &dstDir)
{
    QString mobjpath = mongoObjectFilePath(dstDir);

    if (QFile(mobjpath).exists()) {
        updateMongoObject(mobjpath);
    } else {
        createMongoObject(mobjpath);
    }

    return QLatin1String("mongoobjects/") + QFileInfo(mobjpath).fileName();
}


static QStringList generateCode(const QList<QPair<QString, QVariant::Type> > &fieldList)<commit_msg>When scaffolding, creates a directory if directory does not exist.<commit_after>/* Copyright (c) 2013, <NAME>
 * All rights reserved.
 *
 * This software may be used and distributed according to the terms of
 * the New BSD License, which is incorporated herein by reference.
 */

#include "mongoobjgenerator.h"
#include "global.h"
#include "projectfilegenerator.h"
#include "filewriter.h"
#include <QPair>
#include <QRegExp>
#include <unistd.h>

#define MONGOOBJECT_HEADER_TEMPLATE                                     \
    "#ifndef %1OBJECT_H\n"                                              \
    "#define %1OBJECT_H\n"                                              \
    "\n"                                                                \
    "#include <TMongoObject>\n"                                         \
    "#include <QSharedData>\n"                                          \
    "\n\n"                                                              \
    "class T_MODEL_EXPORT %2Object : public TMongoObject, public QSharedData\n" \
    "{\n"                                                               \
    "public:\n"                                                         \
    "%3"                                                                \
    "\n"                                                                \
    "    enum PropertyIndex {\n"                                        \
    "%4"                                                                \
    "    };\n"                                                          \
    "\n"                                                                \
    "    virtual QString collectionName() const { return \"%5\"; }\n"   \
    "    virtual QString objectId() const { return _id; }\n"            \
    "    virtual QString &objectId() { return _id; }\n"                 \
    "\n"                                                                \
    "private:\n"                                                        \
    "    Q_OBJECT\n"                                                    \
    "%6"                                                                \
    "};\n"                                                              \
    " \n"                                                               \
    "#endif // %1OBJECT_H"

#define MONGOOBJECT_HEADER_UPDATE_TEMPLATE                              \
    "%3\n"                                                              \
    "%4"                                                                \
    "\n"                                                                \
    "    enum PropertyIndex {\n"                                        \
    "%5"                                                                \
    "    };\n"                                                          \
    "\n"                                                                \
    "    virtual QString collectionName() const { return \"%2\"; }\n"   \
    "    virtual QString objectId() const { return _id; }\n"            \
    "    virtual QString &objectId() { return _id; }\n"                 \
    "\n"                                                                \
    "private:\n"                                                        \
    "    Q_OBJECT\n"                                                    \
    "%6"                                                                \
    "};\n"                                                              \
    " \n"                                                               \
    "#endif // %1OBJECT_H"

#define MONGOOBJECT_PROPERTY_TEMPLATE                \
    "    Q_PROPERTY(%1 %2 READ get%2 WRITE set%2)\n" \
    "    T_DEFINE_PROPERTY(%1, %2)\n"

const QRegExp rxstart("\\{\\s*public\\s*:", Qt::CaseSensitive, QRegExp::RegExp2);


MongoObjGenerator::MongoObjGenerator(const QString &model)
    : modelName(model), fields()
{ }


QString MongoObjGenerator::mongoObjectFilePath(const QString &dstDir) const
{
    return QDir(dstDir + "/mongoobjects").filePath(modelName.toLower() + "object.h");
}


QString MongoObjGenerator::generate(const QString &dstDir)
{
    QString mobjpath = mongoObjectFilePath(dstDir);
    QFileInfo fi(mobjpath);

    if (fi.exists()) {
        updateMongoObject(mobjpath);
    } else {
        QDir dir = fi.dir();
        if (!dir.exists()) {
            dir.mkpath(".");
        }
        createMongoObject(mobjpath);
    }

    return QLatin1String("mongoobjects/") + QFileInfo(mobjpath).fileName();
}


static QStringList generateCode(const QList<QPair<QString, QVariant::Type> > &fieldList)
{'fraction_non_alphanumeric': 0.09465833059120325, 'fraction_numerical': 0.005703630580234726, 'mean_word_length': 0.6742563349247154, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 134, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5376842', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1880, 'n_words': 396}