| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "templateparser.h" |
| 6 | |
| 7 | #include "common/util/custompaths.h" |
| 8 | |
| 9 | #include <QFile> |
| 10 | #include <QJsonParseError> |
| 11 | #include <QJsonObject> |
| 12 | #include <QJsonArray> |
| 13 | #include <QDir> |
| 14 | |
| 15 | namespace templateMgr { |
| 16 | |
| 17 | TemplateParser::TemplateParser(QObject *parent) |
| 18 | : QObject(parent) |
| 19 | { |
| 20 | |
| 21 | } |
| 22 | |
| 23 | TemplateParser::~TemplateParser() |
| 24 | { |
| 25 | |
| 26 | } |
| 27 | |
| 28 | bool TemplateParser::readTemplateConfig(TemplateVector &templateVec) |
| 29 | { |
| 30 | QString configPath = CustomPaths::global(CustomPaths::Templates) |
| 31 | + QDir::separator() + QString("templates.json" ); |
| 32 | |
| 33 | QFile file(configPath); |
| 34 | if (!file.open(QIODevice::ReadOnly)) |
| 35 | return false; |
| 36 | |
| 37 | QByteArray data = file.readAll(); |
| 38 | file.close(); |
| 39 | |
| 40 | QJsonParseError parseError; |
| 41 | QJsonDocument doc = QJsonDocument::fromJson(data, &parseError); |
| 42 | if (QJsonParseError::NoError != parseError.error) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | if (!doc.isObject()) |
| 47 | return false; |
| 48 | |
| 49 | QJsonObject rootObject = doc.object(); |
| 50 | |
| 51 | foreach (auto key, rootObject.keys()) { |
| 52 | Template tpl; |
| 53 | tpl.category = key; |
| 54 | |
| 55 | QJsonArray categoryArray = rootObject.value(key).toArray(); |
| 56 | foreach (auto category, categoryArray) { |
| 57 | TemplateCategory tplCategory; |
| 58 | QJsonObject objcategory = category.toObject(); |
| 59 | tplCategory.type = objcategory.value("type" ).toString(); |
| 60 | QJsonArray tplDetailArray = objcategory.value("templates" ).toArray(); |
| 61 | |
| 62 | foreach (auto detail, tplDetailArray) { |
| 63 | TemplateDetail tplDetail; |
| 64 | QJsonObject detailObj = detail.toObject(); |
| 65 | tplDetail.name = detailObj.value("name" ).toString(); |
| 66 | tplDetail.path = detailObj.value("path" ).toString(); |
| 67 | tplDetail.leafNode = detailObj.value("leafNode" ).toBool(); |
| 68 | |
| 69 | tplCategory.templateVec.push_back(tplDetail); |
| 70 | } |
| 71 | |
| 72 | tpl.templateVec.push_back(tplCategory); |
| 73 | } |
| 74 | |
| 75 | templateVec.append(tpl); |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | bool TemplateParser::readWizardConfig(const QString &projectPath, WizardInfo &wizardInfo) |
| 82 | { |
| 83 | QString configPath = CustomPaths::global(CustomPaths::Templates) |
| 84 | + QDir::separator() + projectPath |
| 85 | + QDir::separator() + QString("wizard.json" ); |
| 86 | |
| 87 | QFile file(configPath); |
| 88 | if (!file.open(QIODevice::ReadOnly)) |
| 89 | return false; |
| 90 | |
| 91 | QByteArray data = file.readAll(); |
| 92 | file.close(); |
| 93 | |
| 94 | QJsonParseError parseError; |
| 95 | QJsonDocument doc = QJsonDocument::fromJson(data, &parseError); |
| 96 | if (QJsonParseError::NoError != parseError.error) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | if (!doc.isObject()) |
| 101 | return false; |
| 102 | |
| 103 | QJsonObject rootObject = doc.object(); |
| 104 | |
| 105 | wizardInfo.type = rootObject.value("type" ).toString(); |
| 106 | wizardInfo.kit = rootObject.value("kit" ).toString(); |
| 107 | wizardInfo.language = rootObject.value("language" ).toString(); |
| 108 | wizardInfo.trDisplayName = rootObject.value("trDisplayName" ).toString(); |
| 109 | wizardInfo.trDescription = rootObject.value("trDescription" ).toString(); |
| 110 | QJsonArray configures = rootObject.value("configures" ).toArray(); |
| 111 | QJsonObject generator = rootObject.value("generator" ).toObject(); |
| 112 | |
| 113 | foreach (auto configure, configures) { |
| 114 | EditItem editItem; |
| 115 | editItem.key = configure.toObject().value("key" ).toString(); |
| 116 | editItem.displayName = configure.toObject().value("displayName" ).toString(); |
| 117 | editItem.type = configure.toObject().value("type" ).toString(); |
| 118 | QJsonArray defaultValueArray = configure.toObject().value("defaultValues" ).toArray(); |
| 119 | foreach (auto defaultValue, defaultValueArray) { |
| 120 | editItem.defaultValues.push_back(defaultValue.toString()); |
| 121 | } |
| 122 | editItem.browse = configure.toObject().value("browse" ).toBool(); |
| 123 | |
| 124 | wizardInfo.configures.push_back(editItem); |
| 125 | } |
| 126 | |
| 127 | wizardInfo.generator.rootFolder = generator.value("rootFolder" ).toString(); |
| 128 | wizardInfo.generator.templateFile = generator.value("templateFile" ).toString(); |
| 129 | wizardInfo.generator.destPath = generator.value("destPath" ).toString(); |
| 130 | wizardInfo.generator.newfileName = generator.value("newfileName" ).toString(); |
| 131 | QJsonArray dataArray = generator.value("operations" ).toArray(); |
| 132 | foreach (auto data, dataArray) { |
| 133 | FileOperator op; |
| 134 | op.sourceFile = data.toObject().value("sourceFile" ).toString(); |
| 135 | QJsonArray replaceKeys = data.toObject().value("replaceKeys" ).toArray(); |
| 136 | foreach (auto replaceKey, replaceKeys) { |
| 137 | op.replaceKeys.push_back(replaceKey.toString()); |
| 138 | } |
| 139 | |
| 140 | wizardInfo.generator.operations.push_back(op); |
| 141 | } |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | } //namespace templateMgr |
| 147 | |