FluentUI/example/src/helper/InitializrHelper.cpp

102 lines
4.1 KiB
C++
Raw Normal View History

2024-04-03 11:09:35 +08:00
#include "InitializrHelper.h"
2024-03-09 15:35:48 +08:00
#include <QDir>
2024-03-11 00:02:11 +08:00
#include <QGuiApplication>
2024-03-09 15:35:48 +08:00
2024-04-11 14:51:43 +08:00
[[maybe_unused]] InitializrHelper::InitializrHelper(QObject *parent) : QObject(parent) {
2024-03-09 15:35:48 +08:00
}
2024-04-03 11:09:35 +08:00
InitializrHelper::~InitializrHelper() = default;
2024-03-09 15:35:48 +08:00
2024-04-11 14:51:43 +08:00
bool InitializrHelper::copyDir(const QDir &fromDir, const QDir &toDir, bool coverIfFileExists) {
const QDir &_formDir = fromDir;
2024-03-11 00:02:11 +08:00
QDir _toDir = toDir;
2024-04-11 14:51:43 +08:00
if (!_toDir.exists()) {
if (!_toDir.mkdir(toDir.absolutePath()))
2024-03-11 00:02:11 +08:00
return false;
}
QFileInfoList fileInfoList = _formDir.entryInfoList();
2024-04-11 14:51:43 +08:00
foreach(QFileInfo fileInfo, fileInfoList) {
if (fileInfo.fileName() == "." || fileInfo.fileName() == "..")
continue;
if (fileInfo.isDir()) {
if (!copyDir(fileInfo.filePath(), _toDir.filePath(fileInfo.fileName()), true))
return false;
} else {
if (coverIfFileExists && _toDir.exists(fileInfo.fileName())) {
_toDir.remove(fileInfo.fileName());
}
if (!QFile::copy(fileInfo.filePath(), _toDir.filePath(fileInfo.fileName()))) {
return false;
}
2024-03-11 00:02:11 +08:00
}
}
return true;
}
2024-04-11 14:51:43 +08:00
template<typename...Args>
void InitializrHelper::templateToFile(const QString &source, const QString &dest, Args &&...args) {
2024-03-11 00:02:11 +08:00
QFile file(source);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
QString content = in.readAll().arg(std::forward<Args>(args)...);
file.close();
2024-03-13 19:14:56 +08:00
QDir outputDir = QFileInfo(dest).absoluteDir();
2024-04-11 14:51:43 +08:00
if (!outputDir.exists()) {
2024-03-13 19:14:56 +08:00
outputDir.mkpath(outputDir.absolutePath());
}
2024-03-11 00:02:11 +08:00
QFile outputFile(dest);
if (outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&outputFile);
out << content;
outputFile.close();
} else {
qDebug() << "Failed to open output file.";
}
} else {
qDebug() << "Failed to open resource file.";
}
}
2024-04-11 14:51:43 +08:00
void InitializrHelper::copyFile(const QString &source, const QString &dest) {
QFile::copy(source, dest);
2024-03-11 00:02:11 +08:00
QFile::setPermissions(dest, QFile::WriteOwner | QFile::WriteUser | QFile::WriteGroup | QFile::WriteOther);
}
2024-03-09 15:35:48 +08:00
2024-04-11 14:51:43 +08:00
[[maybe_unused]] void InitializrHelper::generate(const QString &name, const QString &path) {
if (name.isEmpty()) {
2024-03-09 15:35:48 +08:00
error(tr("The name cannot be empty"));
return;
}
2024-04-11 14:51:43 +08:00
if (path.isEmpty()) {
2024-03-09 15:35:48 +08:00
error(tr("The creation path cannot be empty"));
return;
}
2024-03-11 00:02:11 +08:00
QDir projectRootDir(path);
2024-04-11 14:51:43 +08:00
if (!projectRootDir.exists()) {
2024-03-09 15:35:48 +08:00
error(tr("The path does not exist"));
return;
}
2024-03-11 00:02:11 +08:00
QString projectPath = projectRootDir.filePath(name);
QDir projectDir(projectPath);
2024-04-11 14:51:43 +08:00
if (projectDir.exists()) {
2024-03-11 00:02:11 +08:00
error(tr("%1 folder already exists").arg(name));
return;
}
projectDir.mkpath(projectPath);
QDir fluentDir(projectDir.filePath("FluentUI"));
2024-04-11 14:51:43 +08:00
copyDir(QDir(QGuiApplication::applicationDirPath() + "/source"), fluentDir);
templateToFile(":/example/res/template/CMakeLists.txt.in", projectDir.filePath("CMakeLists.txt"), name);
templateToFile(":/example/res/template/src/CMakeLists.txt.in", projectDir.filePath("src/CMakeLists.txt"), name);
templateToFile(":/example/res/template/src/main.cpp.in", projectDir.filePath("src/main.cpp"), name);
templateToFile(":/example/res/template/src/main.qml.in", projectDir.filePath("src/main.qml"), name);
templateToFile(":/example/res/template/src/en_US.ts.in", projectDir.filePath("src/" + name + "_en_US.ts"), name);
templateToFile(":/example/res/template/src/zh_CN.ts.in", projectDir.filePath("src/" + name + "_zh_CN.ts"), name);
copyFile(":/example/res/template/src/App.qml.in", projectDir.filePath("src/App.qml"));
copyFile(":/example/res/template/src/qml.qrc.in", projectDir.filePath("src/qml.qrc"));
copyFile(":/example/res/template/src/logo.ico.in", projectDir.filePath("src/logo.ico"));
copyFile(":/example/res/template/src/README.md.in", projectDir.filePath("src/README.md"));
2024-04-23 00:31:20 +08:00
return this->success(projectPath+"/CMakeLists.txt");
2024-03-09 15:35:48 +08:00
}