File indexing completed on 2024-11-10 04:40:08

0001 /*
0002  * SPDX-FileCopyrightText: 2008 Igor Trindade Oliveira <igor_trindade@yahoo.com.br>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #include "shellscript.h"
0008 #include "akonaditest_debug.h"
0009 #include "config.h" //krazy:exclude=includes
0010 
0011 #include <QCoreApplication>
0012 #include <QFile>
0013 #include <QHashIterator>
0014 
0015 ShellScript::ShellScript()
0016 {
0017 }
0018 
0019 void ShellScript::writeEnvironmentVariables()
0020 {
0021     for (const auto &envvar : std::as_const(mEnvVars)) {
0022 #ifdef Q_OS_WIN
0023         const auto tmpl = QStringLiteral(
0024             "$env:_old_%1=$env:%1\r\n"
0025             "$env:%1=\"%2\"\r\n");
0026 #else
0027         const auto tmpl = QStringLiteral(
0028             "_old_%1=$%1\n"
0029             "%1=\"%2\"\n"
0030             "export %1\n");
0031 #endif
0032         mScript += tmpl.arg(QString::fromLocal8Bit(envvar.first), QString::fromLocal8Bit(envvar.second).replace(QLatin1Char('"'), QStringLiteral("\\\"")));
0033     }
0034 
0035 #ifdef Q_OS_WIN
0036     mScript += QStringLiteral("\r\n\r\n");
0037 #else
0038     mScript += QStringLiteral("\n\n");
0039 #endif
0040 }
0041 
0042 void ShellScript::writeShutdownFunction()
0043 {
0044 #ifdef Q_OS_WIN
0045     const auto tmpl = QStringLiteral(
0046         "Function shutdownTestEnvironment()\r\n"
0047         "{\r\n"
0048         "  qdbus %1 %2 %3\r\n"
0049         "%4"
0050         "}\r\n\r\n");
0051     const auto restoreTmpl = QStringLiteral("  $env:%1=$env:_old_%1\r\n");
0052 #else
0053     const auto tmpl = QStringLiteral(
0054         "function shutdown-testenvironment()\n"
0055         "{\n"
0056         "  qdbus %1 %2 %3\n"
0057         "%4"
0058         "}\n\n");
0059     const auto restoreTmpl = QStringLiteral(
0060         "  %1=$_old_%1\n"
0061         "  export %1\n");
0062 #endif
0063     QString restore;
0064     for (const auto &envvar : std::as_const(mEnvVars)) {
0065         restore += restoreTmpl.arg(QString::fromLocal8Bit(envvar.first));
0066     }
0067 
0068     mScript += tmpl.arg(QStringLiteral("org.kde.Akonadi.Testrunner-%1").arg(qApp->applicationPid()),
0069                         QStringLiteral("/"),
0070                         QStringLiteral("org.kde.Akonadi.Testrunner.shutdown"),
0071                         restore);
0072 }
0073 
0074 void ShellScript::makeShellScript(const QString &fileName)
0075 {
0076     qCDebug(AKONADITEST_LOG) << "Writing environment shell script to" << fileName;
0077     QFile file(fileName); // can user define the file name/location?
0078 
0079     if (file.open(QIODevice::WriteOnly)) {
0080         writeEnvironmentVariables();
0081         writeShutdownFunction();
0082 
0083         file.write(mScript.toLatin1());
0084         file.close();
0085     } else {
0086         qCritical() << "Failed to write" << fileName;
0087     }
0088 }
0089 
0090 void ShellScript::setEnvironmentVariables(const QList<ShellScript::EnvVar> &envVars)
0091 {
0092     mEnvVars = envVars;
0093 }