aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmlprojectmanager/buildsystem/projectitem/converters.cpp
blob: 2ae52eede3c5aa6eb7627a1c33a6d5841aeb4574 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "converters.h"
#include "utils/algorithm.h"
#include "../../qmlprojectconstants.h"
#include "../../qmlprojectexporter/filetypes.h"

#include <QJsonDocument>

namespace QmlProjectManager::Converters {

const static QStringList qmlFilesFilter{QStringLiteral("*.qml")};
const static QStringList javaScriptFilesFilter{QStringLiteral("*.js"), QStringLiteral("*.ts")};

const QStringList imageFilesFilter() {
    return imageFiles([](const QString& suffix) { return "*." + suffix; });
}

QString jsonValueToString(const QJsonValue &val, int indentationLevel, bool indented);

QString jsonToQmlProject(const QJsonObject &rootObject)
{
    QString qmlProjectString;
    QTextStream ts{&qmlProjectString};

    QJsonObject runConfig = rootObject["runConfig"].toObject();
    QJsonObject languageConfig = rootObject["language"].toObject();
    QJsonObject shaderConfig = rootObject["shaderTool"].toObject();
    QJsonObject versionConfig = rootObject["versions"].toObject();
    QJsonObject environmentConfig = rootObject["environment"].toObject();
    QJsonObject deploymentConfig = rootObject["deployment"].toObject();
    const QJsonArray filesConfig = rootObject["fileGroups"].toArray();
    QJsonObject otherProperties = rootObject["otherProperties"].toObject();

    QJsonObject mcuObject = rootObject["mcu"].toObject();
    QJsonObject mcuConfig = mcuObject["config"].toObject();
    QJsonObject mcuModule = mcuObject["module"].toObject();

    int indentationLevel = 0;

    auto appendBreak = [&ts]() { ts << Qt::endl; };

    auto appendComment = [&ts, &indentationLevel](const QString &comment) {
        ts << QString(" ").repeated(indentationLevel * 4) << "// " << comment << Qt::endl;
    };

    auto appendItem =
        [&ts, &indentationLevel](const QString &key, const QString &value, const bool isEnclosed) {
            ts << QString(" ").repeated(indentationLevel * 4) << key << ": "
               << (isEnclosed ? "\"" : "") << value << (isEnclosed ? "\"" : "") << Qt::endl;
        };

    auto appendString = [&appendItem](const QString &key, const QString &val) {
        if (val.isEmpty())
            return;
        appendItem(key, val, true);
    };

    auto appendBool = [&appendItem](const QString &key, const bool &val) {
        appendItem(key, QString::fromStdString(val ? "true" : "false"), false);
    };

    auto appendBoolOpt = [&appendBool](const QString &key, const QJsonObject &source) {
        if (!source.keys().contains(key)) {
            return;
        }
        appendBool(key, source[key].toBool());
    };

    auto appendStringArray = [&appendItem](const QString &key, const QStringList &vals) {
        if (vals.isEmpty())
            return;
        QString finalString;
        for (const QString &value : vals)
            finalString.append("\"").append(value).append("\"").append(",");

        finalString.remove(finalString.size() - 1, 1);
        finalString.prepend("[ ").append(" ]");
        appendItem(key, finalString, false);
    };

    auto appendJsonArray = [&appendItem, &indentationLevel](const QString &key,
                                                            const QJsonArray &vals) {
        if (vals.isEmpty())
            return;
        appendItem(key, jsonValueToString(vals, indentationLevel, /*indented*/ true), false);
    };

    auto appendProperties = [&appendItem, &indentationLevel](const QJsonObject &props,
                                                             const QString &prefix) {
        for (const auto &key : props.keys()) {
            QJsonValue val = props[key];
            QString keyWithPrefix = key;
            if (!prefix.isEmpty()) {
                keyWithPrefix.prepend(prefix + ".");
            }
            appendItem(keyWithPrefix,
                       jsonValueToString(val, indentationLevel, /*indented*/ false),
                       false);
        }
    };

    auto startObject = [&ts, &indentationLevel](const QString &objectName) {
        ts << Qt::endl
           << QString(" ").repeated(indentationLevel * 4) << objectName << " {" << Qt::endl;
        indentationLevel++;
    };

    auto endObject = [&ts, &indentationLevel]() {
        indentationLevel--;
        ts << QString(" ").repeated(indentationLevel * 4) << "}" << Qt::endl;
    };

    auto appendFileGroup = [&startObject,
                            &endObject,
                            &appendString,
                            &appendProperties,
                            &appendJsonArray](const QJsonObject &fileGroup,
                                              const QString &nodeName) {
        startObject(nodeName);
        appendString("directory", fileGroup["directory"].toString());
        QStringList filters = fileGroup["filters"].toVariant().toStringList();
        QStringList filter = {};
        if (nodeName.toLower() == "qmlfiles") {
            filter = qmlFilesFilter;
        } else if (nodeName.toLower() == "imagefiles") {
            filter = imageFilesFilter();
        } else if (nodeName.toLower() == "javascriptfiles") {
            filter = javaScriptFilesFilter;
        }
        for (const QString &entry : std::as_const(filter)) {
            filters.removeOne(entry);
        }
        appendString("filter", filters.join(";"));
        appendJsonArray("files", fileGroup["files"].toArray());
        appendProperties(fileGroup["mcuProperties"].toObject(), "MCU");
        appendProperties(fileGroup["otherProperties"].toObject(), "");
        endObject();
    };

    // start creating the file content
    appendComment("prop: json-converted");
    appendComment("prop: auto-generated");

    ts << Qt::endl << "import QmlProject" << Qt::endl;
    {
        startObject("Project");

        // append non-object props
        appendString("mainFile", runConfig["mainFile"].toString());
        appendString("mainUiFile", runConfig["mainUiFile"].toString());
        appendString("targetDirectory", deploymentConfig["targetDirectory"].toString());
        appendBoolOpt("enableCMakeGeneration", deploymentConfig);
        appendBoolOpt("enablePythonGeneration", deploymentConfig);
        appendBoolOpt("standaloneApp", deploymentConfig);
        appendBool("widgetApp", runConfig["widgetApp"].toBool());
        appendStringArray("importPaths", rootObject["importPaths"].toVariant().toStringList());
        appendStringArray("mockImports", rootObject["mockImports"].toVariant().toStringList());
        appendBreak();
        appendString("qdsVersion", versionConfig["designStudio"].toString());
        appendString("quickVersion", versionConfig["qtQuick"].toString());
        appendBool("qt6Project", versionConfig["qt"].toString() == "6");
        appendBool("qtForMCUs",
                   mcuObject["enabled"].toBool() || !mcuConfig.isEmpty() || !mcuModule.isEmpty());
        if (!languageConfig.isEmpty()) {
            appendBreak();
            appendBool("multilanguageSupport", languageConfig["multiLanguageSupport"].toBool());
            appendString("primaryLanguage", languageConfig["primaryLanguage"].toString());
            appendStringArray("supportedLanguages",
                              languageConfig["supportedLanguages"].toVariant().toStringList());
        }

        // Since different versions of Qt for MCUs may introduce new properties, we collect all
        // unknown properties in a separate object.
        // We need to avoid losing content regardless of which QDS/QUL version combo is used.
        if (!otherProperties.isEmpty()) {
            appendBreak();
            appendProperties(otherProperties, "");
        }

        // append Environment object
        if (!environmentConfig.isEmpty()) {
            startObject("Environment");
            for (const QString &key : environmentConfig.keys()) {
                appendItem(key, environmentConfig[key].toString(), true);
            }
            endObject();
        }

        // append ShaderTool object
        if (!shaderConfig["args"].toVariant().toStringList().isEmpty()) {
            startObject("ShaderTool");
            appendString("args",
                         shaderConfig["args"].toVariant().toStringList().join(" ").replace("\"",
                                                                                           "\\\""));
            appendStringArray("files", shaderConfig["files"].toVariant().toStringList());
            endObject();
        }

        // append the MCU.Config object
        if (!mcuConfig.isEmpty()) {
            // Append MCU.Config
            startObject("MCU.Config");
            appendProperties(mcuConfig, "");
            endObject();
        }

        // Append the MCU.Module object
        if (!mcuModule.isEmpty()) {
            // Append MCU.Module
            startObject("MCU.Module");
            appendProperties(mcuModule, "");
            endObject();
        }

        // append files objects
        for (const QJsonValue &fileGroup : filesConfig) {
            QString nodeType = QString("%1Files").arg(fileGroup["type"].toString());
            if (fileGroup["type"].toString().isEmpty()
                && fileGroup["filters"].toArray().contains("*.qml")) {
                // TODO: IS this important? It turns Files node with *.qml in the filters into QmlFiles nodes
                nodeType = "QmlFiles";
            }
            appendFileGroup(fileGroup.toObject(), nodeType);
        }

        endObject(); // Closing 'Project'
    }
    return qmlProjectString;
}

QStringList qmlprojectsFromFilesNodes(const QJsonArray &fileGroups,
                                      const Utils::FilePath &projectRootPath)
{
    QStringList qmlProjectFiles;
    for (const QJsonValue &fileGroup : fileGroups) {
        if (fileGroup["type"].toString() != "Module") {
            continue;
        }
        // In Qul, paths are relative to the project root directory, not the "directory" entry
        qmlProjectFiles.append(fileGroup["files"].toVariant().toStringList());

        // If the "directory" property is set, all qmlproject files in the directory are also added
        // as relative paths from the project root directory, in addition to explicitly added files
        const QString directoryProp = fileGroup["directory"].toString("");
        if (directoryProp.isEmpty()) {
            continue;
        }
        const Utils::FilePath dir = projectRootPath / directoryProp;
        qmlProjectFiles.append(Utils::transform<QStringList>(
            dir.dirEntries(Utils::FileFilter({"*.qmlproject"}, QDir::Files)),
            [&projectRootPath](Utils::FilePath file) {
                return file.absoluteFilePath().relativePathFromDir(projectRootPath);
            }));
    }

    return qmlProjectFiles;
}

QString moduleUriFromQmlProject(const QString &qmlProjectFilePath)
{
    QmlJS::SimpleReader simpleReader;
    const auto rootNode = simpleReader.readFile(qmlProjectFilePath);
    // Since the file wasn't explicitly added, skip qmlproject files with errors
    if (!rootNode || !simpleReader.errors().isEmpty()) {
        return QString();
    }

    for (const auto &child : rootNode->children()) {
        if (child->name() == "MCU.Module") {
            const auto prop = child->property("uri").isValid() ? child->property("uri")
                                                               : child->property("MCU.uri");
            if (prop.isValid()) {
                return prop.value.toString();
            }
            break;
        }
    }

    return QString();
}

QJsonObject nodeToJsonObject(const QmlJS::SimpleReaderNode::Ptr &node)
{
    QJsonObject tObj;
    for (const QString &childPropName : node->propertyNames())
        tObj.insert(childPropName, node->property(childPropName).value.toJsonValue());

    for (const auto &childNode : node->children())
        tObj.insert(childNode->name(), nodeToJsonObject(childNode));

    return tObj;
};

// Returns a list of qmlproject files in currentSearchPath which are valid modules,
// with URIs matching the relative path from importPathBase.
QStringList getModuleQmlProjectFiles(const Utils::FilePath &importPath,
                                     const Utils::FilePath &projectRootPath)
{
    QStringList qmlProjectFiles;

    QDirIterator it(importPath.toFSPathString(),
                    QDir::NoDotAndDotDot | QDir::Files,
                    QDirIterator::Subdirectories);
    while (it.hasNext()) {
        const QString file = it.next();
        if (!file.endsWith(".qmlproject")) {
            continue;
        }

        // Add if matching
        QString uri = moduleUriFromQmlProject(file);
        if (uri.isEmpty()) {
            // If the qmlproject file is not a valid module, skip it
            continue;
        }

        const auto filePath = Utils::FilePath::fromUserInput(file);
        const bool isBaseImportPath = filePath.parentDir() == importPath;

        // Check the URI against the original import path before adding
        // If we look directly in the search path, the URI doesn't matter
        const QString relativePath = filePath.parentDir().relativePathFromDir(importPath);
        if (isBaseImportPath || uri.replace(".", "/") == relativePath) {
            // If the URI matches the path or the file is directly in the import path, add it
            qmlProjectFiles.emplace_back(filePath.relativePathFromDir(projectRootPath));
        }
    }
    return qmlProjectFiles;
}

QStringList qmlprojectsFromImportPaths(const QStringList &importPaths,
                                       const Utils::FilePath &projectRootPath)
{
    return Utils::transform<QStringList>(importPaths, [&projectRootPath](const QString &importPath) {
        const auto importDir = projectRootPath / importPath;
        return getModuleQmlProjectFiles(importDir, projectRootPath);
    });
}

QJsonObject qmlProjectTojson(const Utils::FilePath &projectFile)
{
    QmlJS::SimpleReader simpleQmlJSReader;

    QmlJS::SimpleReaderNode::Ptr rootNode;

    if (!projectFile.isEmpty()) {
        rootNode = simpleQmlJSReader.readFile(projectFile.toFSPathString());
    } else {
        rootNode = simpleQmlJSReader.readFromSource("import QmlProject 1.1\n"

                                                    "Project {\n"
                                                    "QmlFiles {\n"
                                                    "directory: \".\"\n"
                                                    "}\n"
                                                    "qt6Project: true\n"
                                                    "}\n");
    }

    if (!simpleQmlJSReader.errors().isEmpty() || !rootNode->isValid()) {
        qCritical() << "Unable to parse:" << projectFile;
        qCritical() << simpleQmlJSReader.errors();
        return {};
    }

    if (rootNode->name() != QLatin1String("Project")) {
        qCritical() << "Cannot find root 'Project' item in the project file: " << projectFile;
        return {};
    }

    auto toCamelCase = [](const QString &s) { return QString(s).replace(0, 1, s[0].toLower()); };

    QJsonObject rootObject; // root object
    QJsonArray fileGroupsObject;
    QJsonObject languageObject;
    QJsonObject versionObject;
    QJsonObject runConfigObject;
    QJsonObject deploymentObject;
    QJsonObject mcuObject;
    QJsonObject mcuConfigObject;
    QJsonObject mcuModuleObject;
    QJsonObject shaderToolObject;
    QJsonObject otherProperties;

    bool qtForMCUs = false;

    QStringList importPaths;
    Utils::FilePath projectRootPath = projectFile.parentDir();

    // convert the non-object props
    for (const QString &propName : rootNode->propertyNames()) {
        QJsonObject *currentObj = &rootObject;
        QString objKey = QString(propName).remove("QDS.", Qt::CaseInsensitive);
        QJsonValue value = rootNode->property(propName).value.toJsonValue();

        if (propName.contains("language", Qt::CaseInsensitive)) {
            currentObj = &languageObject;
            if (propName.contains("multilanguagesupport", Qt::CaseInsensitive))
                // fixing the camelcase
                objKey = "multiLanguageSupport";
        } else if (propName.contains("version", Qt::CaseInsensitive)) {
            currentObj = &versionObject;
            if (propName.contains("qdsversion", Qt::CaseInsensitive))
                objKey = "designStudio";
            else if (propName.contains("quickversion", Qt::CaseInsensitive))
                objKey = "qtQuick";
        } else if (propName.contains("widgetapp", Qt::CaseInsensitive)
                   || propName.contains("fileselector", Qt::CaseInsensitive)
                   || propName.contains("mainfile", Qt::CaseInsensitive)
                   || propName.contains("mainuifile", Qt::CaseInsensitive)
                   || propName.contains("forcefreetype", Qt::CaseInsensitive)) {
            currentObj = &runConfigObject;
        } else if (propName.contains("targetdirectory", Qt::CaseInsensitive)
                   || propName.contains("enableCMakeGeneration", Qt::CaseInsensitive)
                   || propName.contains("enablePythonGeneration", Qt::CaseInsensitive)
                   || propName.contains("standaloneApp", Qt::CaseInsensitive)) {
            currentObj = &deploymentObject;
        } else if (propName.contains("qtformcus", Qt::CaseInsensitive)) {
            qtForMCUs = value.toBool();
            continue;
        } else if (propName.contains("qt6project", Qt::CaseInsensitive)) {
            currentObj = &versionObject;
            objKey = "qt";
            value = rootNode->property(propName).value.toBool() ? "6" : "5";
        } else if (propName.contains("importpaths", Qt::CaseInsensitive)) {
            objKey = "importPaths";
            importPaths = value.toVariant().toStringList();
        } else if (propName.contains("mockImports", Qt::CaseInsensitive)) {
            objKey = "mockImports";
        } else {
            currentObj = &otherProperties;
            objKey = propName; // With prefix
        }

        currentObj->insert(objKey, value);
    }

    // add missing non-object props if any
    if (!runConfigObject.contains("fileSelectors")) {
        runConfigObject.insert("fileSelectors", QJsonArray{});
    }

    if (!versionObject.contains("qt")) {
        versionObject.insert("qt", "5");
    }

    rootObject.insert("otherProperties", otherProperties);

    // convert the object props
    for (const QmlJS::SimpleReaderNode::Ptr &childNode : rootNode->children()) {
        if (childNode->name().contains("files", Qt::CaseInsensitive)) {
            QString childNodeName = childNode->name().remove("qds.", Qt::CaseInsensitive);
            qsizetype filesPos = childNodeName.indexOf("files", 0, Qt::CaseInsensitive);
            const QString childNodeType = childNodeName.first(filesPos);
            childNodeName = childNodeName.toLower();

            QJsonArray childNodeFiles = childNode->property("files").value.toJsonArray();
            QString childNodeDirectory = childNode->property("directory").value.toString();
            QStringList filters
                = childNode->property("filter").value.toString().split(";", Qt::SkipEmptyParts);
            QJsonArray childNodeFilters = QJsonArray::fromStringList(filters);

            // files have priority over filters
            // if explicit files are given, then filters will be ignored
            // and all files are prefixed such as "directory/<filename>".
            // if directory is empty, then the files are prefixed with the project directory
            if (childNodeFiles.empty()) {
                auto inserter = [&childNodeFilters](auto &filterSource) {
                    if (!childNodeFilters.empty())
                        return;

                    std::for_each(std::cbegin(filterSource),
                                  std::cend(filterSource),
                                  [&childNodeFilters](const auto &value) {
                                      if (!childNodeFilters.contains(value)) {
                                          childNodeFilters << value;
                                      }
                                  });
                };

                // Those 4 file groups are the special ones
                // that have a default set of filters.
                // The default filters are written to the
                // qmlproject file after conversion
                if (childNodeName == "qmlfiles") {
                    inserter(qmlFilesFilter);
                } else if (childNodeName == "javascriptfiles") {
                    inserter(javaScriptFilesFilter);
                } else if (childNodeName == "imagefiles") {
                    inserter(imageFilesFilter());
                } else if (childNodeName == "fontfiles") {
                    inserter(QmlProjectManager::Constants::QDS_FONT_FILES_FILTERS);
                }
            }

            // create the file group object
            QJsonObject targetObject;
            targetObject.insert("directory", childNodeDirectory);
            targetObject.insert("filters", childNodeFilters);
            targetObject.insert("files", childNodeFiles);
            targetObject.insert("type", childNodeType);

            QJsonObject mcuPropertiesObject;
            QJsonObject otherPropertiesObject;
            for (const auto &propName : childNode->propertyNames()) {
                if (propName == "directory" || propName == "filter" || propName == "files") {
                    continue;
                }

                auto val = QJsonValue::fromVariant(childNode->property(propName).value);

                if (propName.startsWith("MCU.", Qt::CaseInsensitive)) {
                    mcuPropertiesObject.insert(propName.mid(4), val);
                } else {
                    otherPropertiesObject.insert(propName, val);
                }
            }

            targetObject.insert("mcuProperties", mcuPropertiesObject);
            targetObject.insert("otherProperties", otherPropertiesObject);

            fileGroupsObject.append(targetObject);
        } else if (childNode->name().contains("shadertool", Qt::CaseInsensitive)) {
            QStringList quotedArgs
                = childNode->property("args").value.toString().split('\"', Qt::SkipEmptyParts);
            QStringList args;
            for (int i = 0; i < quotedArgs.size(); ++i) {
                // Each odd arg in this list is a single quoted argument, which we should
                // not be split further
                if (i % 2 == 0)
                    args.append(quotedArgs[i].trimmed().split(' '));
                else
                    args.append(quotedArgs[i].prepend("\"").append("\""));
            }

            shaderToolObject.insert("args", QJsonArray::fromStringList(args));
            shaderToolObject.insert("files", childNode->property("files").value.toJsonValue());
        } else if (childNode->name().contains("config", Qt::CaseInsensitive)) {
            mcuConfigObject = nodeToJsonObject(childNode);
            if (const auto fileSelector = childNode->property("fileSelector"); fileSelector.isValid()) {
                auto currentSelectors = runConfigObject.value("fileSelectors").toArray();
                const auto mcuSelectors = fileSelector.value.toJsonArray();
                for (const auto &elem : mcuSelectors) {
                    if (!currentSelectors.contains(elem)) {
                        currentSelectors.append(elem);
                    }
                }
                runConfigObject.insert("fileSelectors", currentSelectors);
            }
        } else if (childNode->name().contains("module", Qt::CaseInsensitive)) {
            mcuModuleObject = nodeToJsonObject(childNode);
        } else {
            rootObject.insert(toCamelCase(childNode->name().remove("qds.", Qt::CaseInsensitive)),
                              nodeToJsonObject(childNode));
        }
    }

    QStringList qmlProjectDependencies;
    qmlProjectDependencies.append(qmlprojectsFromImportPaths(importPaths, projectRootPath));
    qmlProjectDependencies.append(qmlprojectsFromFilesNodes(fileGroupsObject, projectRootPath));
    qmlProjectDependencies.removeDuplicates();
    qmlProjectDependencies.sort();
    rootObject.insert("qmlprojectDependencies", QJsonArray::fromStringList(qmlProjectDependencies));

    mcuObject.insert("config", mcuConfigObject);
    mcuObject.insert("module", mcuModuleObject);
    qtForMCUs |= !(mcuModuleObject.isEmpty() && mcuConfigObject.isEmpty());
    mcuObject.insert("enabled", qtForMCUs);

    rootObject.insert("fileGroups", fileGroupsObject);
    rootObject.insert("language", languageObject);
    rootObject.insert("versions", versionObject);
    rootObject.insert("runConfig", runConfigObject);
    rootObject.insert("deployment", deploymentObject);
    rootObject.insert("mcu", mcuObject);
    if (!shaderToolObject.isEmpty())
        rootObject.insert("shaderTool", shaderToolObject);
    rootObject.insert("fileVersion", 1);
    return rootObject;
}

QString jsonValueToString(const QJsonValue &val, int indentationLevel, bool indented)
{
    if (val.isArray()) {
        auto jsonFormat = indented ? QJsonDocument::JsonFormat::Indented
                                   : QJsonDocument::JsonFormat::Compact;
        QString str = QString::fromUtf8((QJsonDocument(val.toArray()).toJson(jsonFormat)));
        if (indented) {
            // Strip trailing newline
            str.chop(1);
        }
        return str.replace("\n", QString(" ").repeated(indentationLevel * 4).prepend("\n"));
    } else if (val.isBool()) {
        return val.toBool() ? QString("true") : QString("false");
    } else if (val.isDouble()) {
        return QString("%1").arg(val.toDouble());
    } else if (val.isObject()) {
        QString nodeContent = "{\n";
        QJsonObject obj = val.toObject();
        for (QString key : obj.keys()) {
            QJsonValue val = obj[key];
            nodeContent += key.append(": ").prepend(QString(" ").repeated((indentationLevel + 1) * 4));
            nodeContent += jsonValueToString(val, indentationLevel + 1, indented) + "\n";
        }
        return nodeContent + QString(" ").repeated(indentationLevel * 4).append("}");
    } else {
        return val.toString().prepend("\"").append("\"");
    }
}

} // namespace QmlProjectManager::Converters