QtDeployer/source/CPP/outputmanager.cpp

478 lines
12 KiB
C++
Raw Normal View History

2018-03-12 11:43:03 +03:30
#include "outputmanager.h"
2018-06-18 02:43:52 +03:00
#include "utils.h"
#include <QDate>
2018-03-12 11:43:03 +03:30
2018-06-24 19:30:56 +03:00
OutputManager::OutputManager(QObject *parent) : BaseClass(parent) {/*finished*/
connect(&installer_process, &QProcess::readyReadStandardOutput, this, &OutputManager::buildLog);
// connect(&installer_process, &QProcess::finished, this, &OutputManager::buildFunished);
connect(&installer_process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(buildFunished(int, QProcess::ExitStatus)));
}
2018-03-12 11:43:03 +03:30
void OutputManager::setPathsToCopy(const QStringList &pathsToCopy)
{
if (m_pathsToCopy == pathsToCopy) return;
m_pathsToCopy = pathsToCopy;
emit pathsToCopyChanged(m_pathsToCopy);
}
void OutputManager::setCopySuccess(const QList<bool> &copySuccess)
{
if (m_copySuccess == copySuccess) return;
m_copySuccess = copySuccess;
emit copySuccessChanged(m_copySuccess);
}
2018-06-24 19:30:56 +03:00
const QString& OutputManager::log() const{
return tempLog;
}
void OutputManager::buildLog(){
tempLog = installer_process.readAll();
emit logChanged(tempLog);
}
void OutputManager::buildFunished(int error, QProcess::ExitStatus){
if(!error){
emit finished();
}
}
2018-03-12 11:43:03 +03:30
bool OutputManager::copyDir(const QString &source, const QString &destin)
{
QDir().mkpath(destin);
QProcess P;
P.start(QString("cp -a %1/. %2/").arg(source, destin));
P.waitForStarted(1000);
P.waitForFinished();
return P.exitStatus() == QProcess::NormalExit;
}
void OutputManager::copyCpp(const QStringList &libs)
2018-06-18 02:43:52 +03:00
{
2018-06-24 19:30:56 +03:00
2018-06-18 02:43:52 +03:00
QString temp = "/lib";
if(isInstallFW()){
temp = "/packages/lib/data/lib";
}
auto libdir = m_outputdir + temp;
2018-03-12 11:43:03 +03:30
for (const QString &S : libs)
{
auto path = libdir + "/" + QFileInfo(S).fileName();
m_pathsToCopy << path;
m_copySuccess << copyFile(S, path);
}
}
void OutputManager::copyAll(const QStringList &qtlibs, const QStringList &libs,
const QStringList &dirs, const QStringList &plugins,
bool erase)
{
m_pathsToCopy.clear();
m_copySuccess.clear();
2018-06-24 19:30:56 +03:00
tempLog = tr("Start preparing project");
emit logChanged(tempLog);
2018-03-12 11:43:03 +03:30
if (erase)
{
2018-06-24 19:30:56 +03:00
tempLog = tr("Start erase output dir");
emit logChanged(tempLog);
2018-03-12 11:43:03 +03:30
QDir(m_outputdir).removeRecursively();
QDir dir(m_outputdir);
dir.cdUp();
dir.mkdir(QDir(m_outputdir).dirName());
}
2018-06-18 02:43:52 +03:00
checkInstallFrameWork();
2018-03-12 11:43:03 +03:30
createDirectories();
2018-06-24 19:30:56 +03:00
if (libs.count() != 0){
tempLog = tr("Copy c++ libs");
emit logChanged(tempLog);
copyCpp(libs);
}
tempLog = tr("Copy c++/qt libs");
emit logChanged(tempLog);
2018-03-12 11:43:03 +03:30
copyCpp(qtlibs);
2018-06-24 19:30:56 +03:00
tempLog = tr("Copy qml libs");
emit logChanged(tempLog);
2018-03-12 11:43:03 +03:30
copyQml(dirs);
2018-06-24 19:30:56 +03:00
tempLog = tr("Copy Plugins");
emit logChanged(tempLog);
2018-03-12 11:43:03 +03:30
copyPlugins(plugins);
2018-06-24 19:30:56 +03:00
tempLog = tr("Copy Execute files");
emit logChanged(tempLog);
2018-03-12 11:43:03 +03:30
copyExec();
2018-06-24 19:30:56 +03:00
2018-03-12 11:43:03 +03:30
createRunFile();
2018-06-24 19:30:56 +03:00
if(isInstallFW())
createInstaller();
2018-03-12 11:43:03 +03:30
emit pathsToCopyChanged(m_pathsToCopy);
emit copySuccessChanged(m_copySuccess);
}
void OutputManager::copyQml(const QStringList &dirs)
{
2018-06-18 02:43:52 +03:00
QString temp = "/qml";
if(isInstallFW()){
temp = "/packages/qml/data/qml";
}
auto qmldir = m_outputdir + temp;
2018-03-12 11:43:03 +03:30
for (const QString &S : dirs)
{
QString path = qmldir + "/" + S;
m_pathsToCopy << path + "/";
m_copySuccess << copyDir(m_qtdir + "/qml/" + S, path);
}
}
2018-06-18 02:43:52 +03:00
bool OutputManager::isInstallFW() const {
return !m_binarycreator.isEmpty();
}
void OutputManager::checkInstallFrameWork(){
2018-06-24 19:30:56 +03:00
tempLog = tr("check InstallFrameWork");
emit logChanged(tempLog);
2018-06-18 02:43:52 +03:00
QDir dir(m_qtdir);
if(!dir.cd("../../Tools/QtInstallerFramework")){
m_binarycreator = QString();
return;
}
QStringList list = dir.entryList();
int index_of_max = 0;
double maxVersion = 0;
for(int i = 0; i < list.size(); i++){
if(list.at(i).toDouble() > maxVersion){
index_of_max = i;
maxVersion = list.at(i).toDouble();
}
}
if(!maxVersion || !index_of_max){
m_binarycreator = QString();
return;
}
if(!dir.cd(list[index_of_max])){
m_binarycreator = QString();
return;
}
m_binarycreator = dir.absolutePath() + "/bin/binarycreator";
if(!QFile::exists(m_binarycreator)){
m_binarycreator = QString();
2018-06-24 19:30:56 +03:00
tempLog = tr("InstallFrameWork not findet");
emit logChanged(tempLog);
2018-06-18 02:43:52 +03:00
return;
}
2018-06-24 19:30:56 +03:00
tempLog = tr("InstallFrameWork findet in %0").arg(m_binarycreator);
emit logChanged(tempLog);
2018-06-18 02:43:52 +03:00
}
2018-03-12 11:43:03 +03:30
void OutputManager::copyPlugins(const QStringList &plugins)
{
2018-06-18 02:43:52 +03:00
QString temp = "/plugins/";
if(isInstallFW()){
temp = "/packages/plugins/data/plugins/";
}
2018-03-12 11:43:03 +03:30
QString qtpath = m_qtdir + "/plugins/";
2018-06-18 02:43:52 +03:00
QString path = m_outputdir + temp;
2018-03-12 11:43:03 +03:30
for (const QString &plugin : plugins)
{
m_pathsToCopy << path + plugin + "/";
m_copySuccess << copyDir(qtpath + plugin, path + plugin);
}
}
void OutputManager::copyExec()
{
2018-06-18 02:43:52 +03:00
QString temp = "/bin/";
if(isInstallFW()){
temp = "/packages/base/data/bin/";
}
auto path = m_outputdir + temp + QFileInfo(m_executablepath).fileName();
2018-03-12 11:43:03 +03:30
m_pathsToCopy << path;
m_copySuccess << copyFile(m_executablepath, path);
}
2018-06-24 19:30:56 +03:00
bool OutputManager::createIcon(){
QFile f(appIcon);
QByteArray icon;
if(!f.open(QIODevice::ReadOnly)){
return false;
}
icon = f.readAll();
f.close();
QString name = QFileInfo(appIcon).fileName();
f.setFileName(m_outputdir + "/packages/base/data/" + name);
if(!f.open(QIODevice::WriteOnly)){
return false;
}
f.write(icon.data(), icon.size());
f.close();
2018-06-26 10:07:46 +03:00
f.setFileName(m_outputdir + "/config/" + name);
if(!f.open(QIODevice::WriteOnly)){
return false;
}
f.write(icon.data(), icon.size());
f.close();
2018-06-24 19:30:56 +03:00
appIcon = name;
return true;
}
void OutputManager::createEntryScript(){
QString temp = "";
if(isInstallFW()){
temp = "/packages/base/data";
}
QFile f(":/install/InstallTemplate/CreateDesktopEntry.sh");
f.open(QIODevice::ReadOnly);
QString content = f.readAll();
QString fname = m_outputdir + temp + QDir::separator() +
"CreateDesktopEntry.sh";
QFile F(fname);
m_pathsToCopy << fname;
m_copySuccess << F.open(QIODevice::WriteOnly);
F.write(content.toUtf8());
F.flush();
F.close();
F.setPermissions(QFileDevice::ExeUser | QFileDevice::WriteUser |
QFileDevice::ReadUser);
}
2018-03-12 11:43:03 +03:30
void OutputManager::createRunFile()
{
2018-06-18 02:43:52 +03:00
QString temp = "";
if(isInstallFW()){
temp = "/packages/base/data";
}
2018-06-30 16:18:16 +03:00
QString content =
"#!/bin/sh\n"
"BASEDIR=$(dirname $0)\n"
"export LD_LIBRARY_PATH=\"$BASEDIR/lib\"\n"
"export QML_IMPORT_PATH=\"$BASEDIR/qml\"\n"
"export QML2_IMPORT_PATH=\"$BASEDIR/qml\"\n"
"export QT_PLUGIN_PATH=\"$BASEDIR/plugins\"\n"
"export QT_QPA_PLATFORM_PLUGIN_PATH=\"$BASEDIR/plugins/platforms\"\n"
"$BASEDIR/bin/%1";
2018-03-12 11:43:03 +03:30
content = content.arg(QFileInfo(m_executablepath).completeBaseName());
2018-06-18 02:43:52 +03:00
QString fname = m_outputdir + temp + QDir::separator() +
2018-03-12 11:43:03 +03:30
QFileInfo(m_executablepath).completeBaseName() + ".sh";
QFile F(fname);
m_pathsToCopy << fname;
m_copySuccess << F.open(QIODevice::WriteOnly);
F.write(content.toUtf8());
F.flush();
F.close();
F.setPermissions(QFileDevice::ExeUser | QFileDevice::WriteUser |
QFileDevice::ReadUser);
2018-06-24 19:30:56 +03:00
2018-03-12 11:43:03 +03:30
}
2018-06-18 02:43:52 +03:00
bool OutputManager::createModule(const QString &from, const QString &to, const QStringList &params){
QFile tempFile;
tempFile.setFileName(from);
if(!tempFile.open(QIODevice::ReadOnly)){
return false;
}
QString text = tempFile.readAll();
tempFile.close();
for(QString i : params){
text = text.arg(i);
}
tempFile.setFileName(to);
if(!tempFile.open(QIODevice::ReadWrite) &&
QDir().mkpath(QFileInfo(to).absolutePath()) &&
!tempFile.open(QIODevice::ReadWrite)){
return false;
}
tempFile.write(text.toLatin1());
tempFile.close();
return true;
}
void OutputManager::createInstaller(){
2018-06-24 19:30:56 +03:00
tempLog = tr("create installer");
emit logChanged(tempLog);
2018-06-26 10:07:46 +03:00
if(!createIcon()){
tempLog = tr("create icon fail!");
emit logChanged(tempLog);
return;
}
2018-06-18 02:43:52 +03:00
createModule(":/install/InstallTemplate/config.xml",
m_outputdir + "/config/config.xml",
QStringList() << projectName <<
Utils::getVersion() <<
Utils::getPublicher() <<
"QtDeployer" <<
2018-06-26 10:07:46 +03:00
QDate::currentDate().toString("yyyy-MM-dd") <<
appIcon);
createModule(":/install/InstallTemplate/controlScript.js",
m_outputdir + "/config/controlScript.js",
QStringList() << appIcon);
2018-06-18 02:43:52 +03:00
createModule(":/install/InstallTemplate/package.xml",
m_outputdir + "/packages/base/meta/package.xml",
QStringList() << "base" <<
"base data of " + projectName <<
Utils::getVersion() <<
2018-06-24 19:30:56 +03:00
QDate::currentDate().toString("yyyy-MM-dd") <<
"true" <<
2018-06-30 16:18:16 +03:00
"<Script>componentScript.js</Script>");
createModule(":/install/InstallTemplate/componentScript.js",
m_outputdir + "/packages/base/meta/componentScript.js",
QStringList() << appIcon);
2018-06-24 19:30:56 +03:00
2018-06-18 02:43:52 +03:00
createModule(":/install/InstallTemplate/package.xml",
m_outputdir + "/packages/qml/meta/package.xml",
QStringList() << "qml" <<
"qml data of " + projectName <<
Utils::getVersion() <<
2018-06-24 19:30:56 +03:00
QDate::currentDate().toString("yyyy-MM-dd") <<
"true" <<
"");
2018-06-18 02:43:52 +03:00
createModule(":/install/InstallTemplate/package.xml",
m_outputdir + "/packages/lib/meta/package.xml",
QStringList() << "lib" <<
"lib data of " + projectName <<
Utils::getVersion() <<
2018-06-24 19:30:56 +03:00
QDate::currentDate().toString("yyyy-MM-dd") <<
"true" <<
"");
2018-06-18 02:43:52 +03:00
createModule(":/install/InstallTemplate/package.xml",
m_outputdir + "/packages/plugins/meta/package.xml",
QStringList() << "plugins" <<
"plugins data of " + projectName <<
Utils::getVersion() <<
2018-06-24 19:30:56 +03:00
QDate::currentDate().toString("yyyy-MM-dd") <<
"true" <<
"");
2018-06-18 02:43:52 +03:00
2018-06-24 19:30:56 +03:00
installer_process.setProgram(m_binarycreator);
2018-06-18 02:43:52 +03:00
/*
-c $$PWD/config/config.xml -p $$PWD/packages ${QMAKE_FILE_OUT}*/
2018-06-24 19:30:56 +03:00
installer_process.setArguments(QStringList() << "-c" <<
2018-06-18 02:43:52 +03:00
m_outputdir + "/config/config.xml" <<
"-p" <<
m_outputdir + "/packages" <<
2018-06-24 19:30:56 +03:00
m_outputdir + "/installer_" + projectName <<
"-v");
2018-06-18 02:43:52 +03:00
2018-06-24 19:30:56 +03:00
installer_process.start();
2018-06-18 02:43:52 +03:00
}
2018-03-12 11:43:03 +03:30
void OutputManager::createDirectories()
{
2018-06-24 19:30:56 +03:00
tempLog = tr("Create Directories");
emit logChanged(tempLog);
2018-06-18 02:43:52 +03:00
if(isInstallFW()){
m_pathsToCopy << m_outputdir + "/config";
m_copySuccess << QDir(m_outputdir).mkpath("config");
m_pathsToCopy << m_outputdir + "/packages/base/data/bin";
m_copySuccess << QDir(m_outputdir).mkpath("packages/base/data/bin");
m_pathsToCopy << m_outputdir + "/packages/qml/data/qml";
m_copySuccess << QDir(m_outputdir).mkpath("packages/qml/data/qml");
m_pathsToCopy << m_outputdir + "/packages/lib/data/lib/";
m_copySuccess << QDir(m_outputdir).mkpath("packages/lib/data/lib");
m_pathsToCopy << m_outputdir + "/packages/plugins/data/plugins";
m_copySuccess << QDir(m_outputdir).mkpath("packages/plugins/data/plugins");
} else {
m_pathsToCopy << m_outputdir + "/bin/";
m_copySuccess << QDir(m_outputdir).mkdir("bin");
m_pathsToCopy << m_outputdir + "/qml/";
m_copySuccess << QDir(m_outputdir).mkdir("qml");
2018-03-12 11:43:03 +03:30
2018-06-18 02:43:52 +03:00
m_pathsToCopy << m_outputdir + "/lib/";
m_copySuccess << QDir(m_outputdir).mkdir("lib");
2018-03-12 11:43:03 +03:30
2018-06-18 02:43:52 +03:00
m_pathsToCopy << m_outputdir + "/plugins/";
m_copySuccess << QDir(m_outputdir).mkdir("plugins");
}
2018-03-12 11:43:03 +03:30
}
bool OutputManager::copyFile(const QString &source, const QString &destin)
{
return QFile::copy(source, destin);
}
QStringList OutputManager::pathsToCopy() const { return m_pathsToCopy; }
QList<bool> OutputManager::copySuccess() const { return m_copySuccess; }