aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/tools/qmlprojectmanager/main.cpp
blob: 688a8e6f540fc92af033e8fedee85d1c7501549c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <QJsonDocument>
#include <qmlprojectmanager/buildsystem/projectitem/converters.h>

class DataSet
{
public:
    DataSet(const QString &rootDir)
        : m_rootDir(rootDir)
    {}
    void setDataSource(const QString &dataSetName)
    {
        m_dataSetDirectory.setPath(m_rootDir.path() + "/" + dataSetName);

        m_qmlProjectFile = Utils::FilePath::fromString(
            QString(m_dataSetDirectory.absolutePath()).append("/testfile.qmlproject"));
        m_jsonToQmlProjectFile = Utils::FilePath::fromString(
            QString(m_dataSetDirectory.absolutePath()).append("/testfile.jsontoqml"));
        m_qmlProjectToJsonFile = Utils::FilePath::fromString(
            QString(m_dataSetDirectory.absolutePath()).append("/testfile.qmltojson"));
    }

    QString qmlProjectContent() const
    {
        return (m_qmlProjectFile.fileContents()
                    ? QString::fromLatin1(m_qmlProjectFile.fileContents().value())
                    : QString{});
    }
    QString jsonToQmlProjectContent() const
    {
        return m_jsonToQmlProjectFile.fileContents()
                   ? QString::fromLatin1(m_jsonToQmlProjectFile.fileContents().value())
                   : QString{};
    }
    QString qmlProjectToJsonContent() const
    {
        return m_qmlProjectToJsonFile.fileContents()
                   ? QString::fromLatin1(m_qmlProjectToJsonFile.fileContents().value())
                   : QString{};
    }

    QString dataSetPath() const { return m_dataSetDirectory.absolutePath(); }
    QString dataSetName() const { return m_dataSetDirectory.dirName(); }
    Utils::FilePath qmlProjectFile() const { return m_qmlProjectFile; }
    Utils::FilePath jsonToQmlProjectFile() const { return m_jsonToQmlProjectFile; }
    Utils::FilePath qmlProjectToJsonFile() const { return m_qmlProjectToJsonFile; }

private:
    QDir m_rootDir;
    QDir m_dataSetDirectory;
    Utils::FilePath m_qmlProjectFile;
    Utils::FilePath m_jsonToQmlProjectFile;
    Utils::FilePath m_qmlProjectToJsonFile;
};

int main(int argc, char **argv)
{
    const QString helpText{"./dataSetGenerator [path]\n"
                           "[path]:     Path to the data set folders. The default is current dir.\n"
                           "            Folder names should be in the form of test-set-x.\n"};

    QDir dataSetPath{QDir::currentPath()};
    if (argc >= 2) {
        dataSetPath.setPath(argv[1]);
    }

    if (!dataSetPath.exists()) {
        qDebug() << "Data path does not exist:" << dataSetPath.path() << Qt::endl;
        qDebug().noquote() << helpText;
        return -1;
    }

    QStringList dataSetList{dataSetPath.entryList({"test-set-*"})};
    if (!dataSetList.size()) {
        qDebug() << "No test sets are available under" << dataSetPath.path() << Qt::endl;
        qDebug().noquote() << helpText;
        return -1;
    }

    DataSet dataSet(dataSetPath.path());
    for (const auto &dataSetName : dataSetList) {
        dataSet.setDataSource(dataSetName);

        qDebug() << "Regenerating data set:" << dataSet.dataSetName();
        QJsonObject qml2json = QmlProjectManager::Converters::qmlProjectTojson(
            dataSet.qmlProjectFile());
        QString json2qml = QmlProjectManager::Converters::jsonToQmlProject(qml2json);

        dataSet.qmlProjectToJsonFile().writeFileContents(QJsonDocument(qml2json).toJson());
        dataSet.jsonToQmlProjectFile().writeFileContents(json2qml.toUtf8());
    }
    return 0;
}