CQtDeployer/Deploy/metafilemanager.cpp

252 lines
7.5 KiB
C++
Raw Normal View History

2019-09-23 16:46:57 +03:00
/*
* Copyright (C) 2018-2021 QuasarApp.
2019-09-23 16:46:57 +03:00
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
2019-09-15 17:38:47 +03:00
#include "deploycore.h"
#include "metafilemanager.h"
#include <quasarapp.h>
#include <QDir>
#include <configparser.h>
#include "filemanager.h"
2019-12-09 15:13:00 +03:00
#include <assert.h>
2019-09-15 17:38:47 +03:00
bool MetaFileManager::createRunScriptWindows(const QString &target) {
2019-12-11 18:06:54 +03:00
auto cnf = DeployCore::_config;
auto targetinfo = cnf->targets().value(target);
if (!targetinfo.isValid()) {
2019-12-11 18:06:54 +03:00
return false;
}
2019-12-14 17:38:43 +03:00
auto distro = cnf->getDistro(target);
2019-12-11 18:06:54 +03:00
2021-01-08 22:02:50 +03:00
QFileInfo targetInfo(target);
QString content;
auto runScript = targetinfo.getRunScript();
QFile script(runScript);
if (!script.open(QIODevice::ReadOnly)) {
return false;
2021-01-08 22:02:50 +03:00
}
content = script.readAll();
script.close();
replace(toReplace(target, distro), content);
2019-09-15 17:38:47 +03:00
2021-01-08 22:02:50 +03:00
QString fname = DeployCore::_config->getTargetDir(target) + QDir::separator() + targetInfo.baseName()+ ".bat";
2019-09-15 17:38:47 +03:00
QFile F(fname);
if (!F.open(QIODevice::WriteOnly)) {
return false;
}
F.write(content.toUtf8());
F.flush();
F.close();
_fileManager->addToDeployed(fname);
return F.setPermissions(QFileDevice::ExeOther | QFileDevice::WriteOther |
QFileDevice::ReadOther | QFileDevice::ExeUser |
QFileDevice::WriteUser | QFileDevice::ReadUser |
QFileDevice::ExeOwner | QFileDevice::WriteOwner |
QFileDevice::ReadOwner);
}
bool MetaFileManager::createRunScriptLinux(const QString &target) {
2019-12-11 18:06:54 +03:00
auto cnf = DeployCore::_config;
auto targetinfo = cnf->targets().value(target);
2019-12-11 18:06:54 +03:00
2020-01-02 19:27:40 +03:00
if (!cnf->targets().contains(target)) {
2019-12-11 18:06:54 +03:00
return false;
}
2019-12-14 17:38:43 +03:00
auto distro = cnf->getDistro(target);
2019-12-11 18:06:54 +03:00
2021-01-08 22:02:50 +03:00
QFileInfo targetInfo(target);
QString content;
auto runScript = targetinfo.getRunScript();
QFile script(runScript);
if (!script.open(QIODevice::ReadOnly)) {
return false;
2021-01-08 22:02:50 +03:00
}
content = script.readAll();
script.close();
replace(toReplace(target, distro), content);
2019-12-27 12:51:03 +03:00
2021-01-08 22:02:50 +03:00
QString fname = DeployCore::_config->getTargetDir(target) + QDir::separator() + targetInfo.baseName()+ ".sh";
2019-09-15 17:38:47 +03:00
QFile F(fname);
if (!F.open(QIODevice::WriteOnly)) {
return false;
}
F.write(content.toUtf8());
F.flush();
F.close();
_fileManager->addToDeployed(fname);
return F.setPermissions(QFileDevice::ExeOther | QFileDevice::WriteOther |
QFileDevice::ReadOther | QFileDevice::ExeUser |
QFileDevice::WriteUser | QFileDevice::ReadUser |
QFileDevice::ExeOwner | QFileDevice::WriteOwner |
QFileDevice::ReadOwner);
}
2019-12-27 12:51:03 +03:00
QString MetaFileManager::generateCustoScriptBlok(bool bat) const {
QString res = "";
QString commentMarker = "# ";
if (bat) {
commentMarker = ":: ";
}
2021-04-07 19:36:08 +03:00
auto cstSh = QuasarAppUtils::Params::getArg("customScript", "");
2019-12-27 12:51:03 +03:00
if (cstSh.size()) {
res = "\n" +
commentMarker + "Begin Custom Script (generated by customScript flag)\n"
2019-12-27 12:51:03 +03:00
"%0\n" +
commentMarker + "End Custom Script\n"
2019-12-27 12:51:03 +03:00
"\n";
res = res.arg(cstSh);
}
return res;
}
2019-09-15 17:38:47 +03:00
MetaFileManager::MetaFileManager(FileManager *manager):
_fileManager(manager)
{
assert(_fileManager);
}
bool MetaFileManager::createRunScript(const QString &target) {
QFileInfo info(target);
auto sufix = info.completeSuffix();
if (sufix.contains("exe", Qt::CaseSensitive)) {
return createRunScriptWindows(target);
}
2019-11-16 17:05:37 +03:00
if (sufix.isEmpty()) {
return createRunScriptLinux(target);
}
return true;
2019-09-15 17:38:47 +03:00
}
2019-12-11 18:06:54 +03:00
bool MetaFileManager::createQConf(const QString &target) {
auto cnf = DeployCore::_config;
2020-01-02 19:27:40 +03:00
if (!cnf->targets().contains(target)) {
2019-12-11 18:06:54 +03:00
return false;
}
2019-12-14 17:38:43 +03:00
auto distro = cnf->getDistro(target);
2019-09-15 17:38:47 +03:00
QString content =
"[Paths]\n"
"Prefix= ." + distro.getRootDir(distro.getBinOutDir()) + "\n"
"Libraries= ." + distro.getLibOutDir() + "\n"
"Plugins= ." + distro.getPluginsOutDir() + "\n"
"Imports= ." + distro.getQmlOutDir() + "\n"
"Translations= ." + distro.getTrOutDir() + "\n"
"Qml2Imports= ." + distro.getQmlOutDir() + "\n";
2019-09-15 17:38:47 +03:00
content.replace("//", "/");
content = QDir::fromNativeSeparators(content);
2019-12-12 22:50:04 +03:00
QString fname = DeployCore::_config->getTargetDir(target) + distro.getBinOutDir() + "qt.conf";
2019-09-15 17:38:47 +03:00
QFile F(fname);
if (!F.open(QIODevice::WriteOnly)) {
return false;
}
F.write(content.toUtf8());
F.flush();
F.close();
_fileManager->addToDeployed(fname);
return F.setPermissions(QFileDevice::ExeOther | QFileDevice::WriteOther |
QFileDevice::ReadOther | QFileDevice::ExeUser |
QFileDevice::WriteUser | QFileDevice::ReadUser |
QFileDevice::ExeOwner | QFileDevice::WriteOwner |
QFileDevice::ReadOwner);
}
QHash<QString, QString> MetaFileManager::toReplace(const QString& target,
const DistroModule& distro) const {
QFileInfo targetInfo(target);
QHash<QString, QString> result = {
{"CQT_BIN_PATH", distro.getBinOutDir()},
{"CQT_LIB_PATH", distro.getLibOutDir()},
{"CQT_QML_PATH", distro.getQmlOutDir()},
{"CQT_PLUGIN_PATH", distro.getPluginsOutDir()},
{"CQT_SYSTEM_LIB_PATH", distro.getLibOutDir() + DeployCore::systemLibsFolderName()},
{"CQT_BASE_NAME", targetInfo.baseName()}
};
bool fGui = DeployCore::isGui(_mudulesMap.value(target));
if (targetInfo.completeSuffix().compare(".exe", Qt::CaseInsensitive) == 0) {
result.insert("CQT_CUSTOM_SCRIPT_BLOCK", generateCustoScriptBlok(true));
// Run application as invoke of the console for consle applications
// And run gui applciation in the detached mode.
QString runCmd;
if (fGui) {
runCmd = "start \"" + targetInfo.baseName() + "\" /B " +
"\"%BASE_DIR%" + distro.getBinOutDir() + targetInfo.fileName() + "\" %*";
} else {
runCmd = "call \"%BASE_DIR%" + distro.getBinOutDir() + targetInfo.fileName() + "\" %*";
}
result.insert("CQT_RUN_COMMAND", runCmd);
} else {
result.insert("CQT_CUSTOM_SCRIPT_BLOCK", generateCustoScriptBlok(false));
QString runCmd = "\"$BASE_DIR" + distro.getBinOutDir() + targetInfo.fileName() + "\" \"$@\" ";
result.insert("CQT_RUN_COMMAND", runCmd);
}
return result;
}
void MetaFileManager::replace(const QHash<QString, QString> &map, QString &content) {
for (auto it = map.begin(); it != map.end(); ++it) {
content = content.replace(it.key(), it.value());
}
}
2021-04-22 11:14:33 +03:00
void MetaFileManager::createRunMetaFiles(const QHash<QString, DeployCore::QtModule>& modulesMap) {
2019-09-15 17:38:47 +03:00
2021-04-22 11:14:33 +03:00
_mudulesMap = modulesMap;
2020-01-02 19:27:40 +03:00
for (auto i = DeployCore::_config->targets().cbegin(); i != DeployCore::_config->targets().cend(); ++i) {
2019-09-15 17:38:47 +03:00
if (i.value().fEnableRunScript() && !createRunScript(i.key())) {
QuasarAppUtils::Params::log("Failed to create a run script: " + i.key(),
QuasarAppUtils::Error);
2019-09-15 17:38:47 +03:00
}
2019-12-11 18:06:54 +03:00
if (!createQConf(i.key())) {
2021-05-02 20:10:01 +03:00
QuasarAppUtils::Params::log("Failed to create the qt.conf file", QuasarAppUtils::Warning);
2019-12-11 18:06:54 +03:00
}
2019-09-15 17:38:47 +03:00
}
}