| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. | 
|---|
| 2 | // | 
|---|
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later | 
|---|
| 4 |  | 
|---|
| 5 | #include "projectgenerate.h" | 
|---|
| 6 |  | 
|---|
| 7 | #include <QFile> | 
|---|
| 8 | #include <QDir> | 
|---|
| 9 | #include <QTextStream> | 
|---|
| 10 |  | 
|---|
| 11 | namespace templateMgr{ | 
|---|
| 12 |  | 
|---|
| 13 | class ProjectGeneratePrivate | 
|---|
| 14 | { | 
|---|
| 15 | friend class ProjectGenerate; | 
|---|
| 16 | }; | 
|---|
| 17 |  | 
|---|
| 18 | ProjectGenerate::ProjectGenerate(QObject *parent) | 
|---|
| 19 | : QObject(parent) | 
|---|
| 20 | , d(new ProjectGeneratePrivate()) | 
|---|
| 21 | { | 
|---|
| 22 |  | 
|---|
| 23 | } | 
|---|
| 24 |  | 
|---|
| 25 | ProjectGenerate::~ProjectGenerate() | 
|---|
| 26 | { | 
|---|
| 27 | if (d) | 
|---|
| 28 | delete d; | 
|---|
| 29 | } | 
|---|
| 30 |  | 
|---|
| 31 | bool ProjectGenerate::copyDir(QString &retMsg, const QString &srcPath, const QString &dstPath, bool cover) | 
|---|
| 32 | { | 
|---|
| 33 | QDir srcDir(srcPath); | 
|---|
| 34 | QDir dstDir(dstPath); | 
|---|
| 35 | if (!dstDir.exists()) { | 
|---|
| 36 | if (!dstDir.mkdir(dstDir.absolutePath())) { | 
|---|
| 37 | retMsg = tr( "Create ") + dstPath + tr( " failed."); | 
|---|
| 38 | return false; | 
|---|
| 39 | } | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | QFileInfoList fileInfoList = srcDir.entryInfoList(); | 
|---|
| 43 | foreach(auto fileInfo, fileInfoList) { | 
|---|
| 44 | if (fileInfo.fileName() == "."|| fileInfo.fileName() == "..") | 
|---|
| 45 | continue; | 
|---|
| 46 |  | 
|---|
| 47 | if (fileInfo.isDir()) { | 
|---|
| 48 | if (!copyDir(retMsg, fileInfo.filePath(), dstDir.filePath(fileInfo.fileName()), cover)) | 
|---|
| 49 | return false; | 
|---|
| 50 | } else { | 
|---|
| 51 | if (cover && dstDir.exists(fileInfo.fileName())) { | 
|---|
| 52 | dstDir.remove(fileInfo.fileName()); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | if (!QFile::copy(fileInfo.filePath(), dstDir.filePath(fileInfo.fileName()))) { | 
|---|
| 56 | retMsg = tr( "Copy file ") + fileInfo.fileName() + tr( " failed."); | 
|---|
| 57 | return false; | 
|---|
| 58 | } | 
|---|
| 59 | } | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | return true; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | bool ProjectGenerate::copyFile(QString &retMsg, const QString &srcPath, const QString &dstPath, bool cover) | 
|---|
| 66 | { | 
|---|
| 67 | if (!QFile::exists(srcPath)) { | 
|---|
| 68 | retMsg = tr( "Template file ") + srcPath + tr( " is not exist."); | 
|---|
| 69 | return false; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | if (QFile::exists(dstPath) && cover) { | 
|---|
| 73 | QFile::remove(dstPath); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | QFile::copy(srcPath, dstPath); | 
|---|
| 77 |  | 
|---|
| 78 | return true; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | bool ProjectGenerate::create(PojectGenResult &retResult, const PojectGenParam &genParam) | 
|---|
| 82 | { | 
|---|
| 83 | if (genParam.type == Project) { | 
|---|
| 84 | return genProject(retResult, genParam); | 
|---|
| 85 | } else if (genParam.type == File) { | 
|---|
| 86 | return genFile(retResult, genParam); | 
|---|
| 87 | } else { | 
|---|
| 88 | retResult.message = tr( "Template type is unknown."); | 
|---|
| 89 | return false; | 
|---|
| 90 | } | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | bool ProjectGenerate::genProject(PojectGenResult &retResult, const PojectGenParam &genParam) | 
|---|
| 94 | { | 
|---|
| 95 | if (genParam.templatePath.isEmpty()) { | 
|---|
| 96 | retResult.message = tr( "Template folder is empty!"); | 
|---|
| 97 | return false; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | QString dstPath = genParam.settingParamMap.value(genParam.generator.destPath); | 
|---|
| 101 | if (dstPath.isEmpty()) { | 
|---|
| 102 | retResult.message = tr( "Target path is empty!"); | 
|---|
| 103 | return false; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | QString projectPath = dstPath + QDir::separator() + | 
|---|
| 107 | genParam.settingParamMap.value(genParam.generator.rootFolder); | 
|---|
| 108 |  | 
|---|
| 109 | if (QDir(projectPath).exists()) { | 
|---|
| 110 | retResult.message = projectPath + tr( " has existed, please remove it firstly."); | 
|---|
| 111 | return false; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | bool ret = copyDir(retResult.message, genParam.templatePath, projectPath, false); | 
|---|
| 115 | if (!ret) { | 
|---|
| 116 | retResult.message = tr( "Create project failed!"); | 
|---|
| 117 | return false; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | QString wiazardFile = projectPath + QDir::separator() + "wizard.json"; | 
|---|
| 121 | QFile::remove(wiazardFile); | 
|---|
| 122 |  | 
|---|
| 123 | ret = transform(retResult.message, genParam, projectPath); | 
|---|
| 124 | if (!ret) { | 
|---|
| 125 | retResult.message = tr( "Transform project failed!") + retResult.message; | 
|---|
| 126 | return false; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | retResult.message = tr( "succeed"); | 
|---|
| 130 | retResult.kit = genParam.kit; | 
|---|
| 131 | retResult.language = genParam.language; | 
|---|
| 132 | retResult.projectPath = projectPath; | 
|---|
| 133 |  | 
|---|
| 134 | return true; | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | bool ProjectGenerate::transform(QString &retMsg, const PojectGenParam &genParam, const QString &projectPath) | 
|---|
| 138 | { | 
|---|
| 139 | auto iter = genParam.generator.operations.begin(); | 
|---|
| 140 | for (; iter != genParam.generator.operations.end(); ++iter) { | 
|---|
| 141 | QString filePath = projectPath + QDir::separator() + iter->sourceFile; | 
|---|
| 142 | if (!QFileInfo(filePath).isFile()) { | 
|---|
| 143 | retMsg = filePath + tr( " is not exsit."); | 
|---|
| 144 | return false; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | QFile fileRead(filePath); | 
|---|
| 148 | QString contentRead; | 
|---|
| 149 | if(!fileRead.open(QIODevice::ReadOnly | QIODevice::Text)){ | 
|---|
| 150 | retMsg = tr( "Open ") + filePath + tr( " failed."); | 
|---|
| 151 | return false; | 
|---|
| 152 | } | 
|---|
| 153 | contentRead = fileRead.readAll(); | 
|---|
| 154 | fileRead.close(); | 
|---|
| 155 |  | 
|---|
| 156 | QStringList contentList = contentRead.split( "\n"); | 
|---|
| 157 |  | 
|---|
| 158 | QFile fileWrite(filePath); | 
|---|
| 159 | if(fileWrite.open(QIODevice::WriteOnly | QIODevice::Text)) { | 
|---|
| 160 | QTextStream streamWrite(&fileWrite); | 
|---|
| 161 | foreach (auto line, contentList) { | 
|---|
| 162 | QString contentWrite = line; | 
|---|
| 163 | foreach (auto key, iter->replaceKeys) { | 
|---|
| 164 | if(contentWrite.contains(key)) { | 
|---|
| 165 | contentWrite.replace(key, genParam.settingParamMap.value(key)); | 
|---|
| 166 | } | 
|---|
| 167 | } | 
|---|
| 168 | streamWrite << contentWrite << "\n"; | 
|---|
| 169 | } | 
|---|
| 170 | } | 
|---|
| 171 | fileWrite.close(); | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | return true; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | bool ProjectGenerate::genFile(PojectGenResult &retResult, const PojectGenParam &genParam) | 
|---|
| 178 | { | 
|---|
| 179 | if (genParam.templatePath.isEmpty()) { | 
|---|
| 180 | retResult.message = tr( "Template file is empty!"); | 
|---|
| 181 | return false; | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | QString srcFilePath = genParam.templatePath + QDir::separator() + genParam.generator.templateFile; | 
|---|
| 185 |  | 
|---|
| 186 | QString dstPath = genParam.settingParamMap.value(genParam.generator.destPath); | 
|---|
| 187 | if (dstPath.isEmpty()) { | 
|---|
| 188 | retResult.message = tr( "Target path is empty!"); | 
|---|
| 189 | return false; | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | QString suffix = QFileInfo(srcFilePath).suffix(); | 
|---|
| 193 | QString newFilePath = dstPath + QDir::separator() | 
|---|
| 194 | + genParam.settingParamMap.value(genParam.generator.newfileName) | 
|---|
| 195 | + "." | 
|---|
| 196 | + suffix; | 
|---|
| 197 |  | 
|---|
| 198 | if (QFile::exists(newFilePath)) { | 
|---|
| 199 | retResult.message = newFilePath + tr( " has existed, please remove it firstly."); | 
|---|
| 200 | return false; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | bool ret = copyFile(retResult.message, srcFilePath, newFilePath, false); | 
|---|
| 204 | if (!ret) { | 
|---|
| 205 | retResult.message = tr( "Create File failed!"); | 
|---|
| 206 | return false; | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | retResult.message = tr( "succeed"); | 
|---|
| 210 | retResult.language = genParam.language; | 
|---|
| 211 | retResult.filePath = newFilePath; | 
|---|
| 212 |  | 
|---|
| 213 | return true; | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | } //namespace templateMgr | 
|---|
| 217 |  | 
|---|