| 174 | } |
| 175 | |
| 176 | bool Runner::execCommand(const RunCommandInfo &info) |
| 177 | { |
| 178 | bool ret = false; |
| 179 | QString retMsg = tr("Error: execute command error! The reason is unknown.\n"); |
| 180 | QProcess process; |
| 181 | process.setWorkingDirectory(info.workingDir); |
| 182 | process.setEnvironment(info.envs); |
| 183 | |
| 184 | QString startMsg = tr("Start execute command: \"%1\" \"%2\" in workspace \"%3\".\n") |
| 185 | .arg(info.program, info.arguments.join(" "), info.workingDir); |
| 186 | |
| 187 | connect(&process, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), |
| 188 | [&](int exitcode, QProcess::ExitStatus exitStatus) { |
| 189 | if (0 == exitcode && exitStatus == QProcess::ExitStatus::NormalExit) { |
| 190 | ret = true; |
| 191 | retMsg = tr("The process \"%1\" exited normally.\n").arg(process.program()); |
| 192 | } else if (exitStatus == QProcess::NormalExit) { |
| 193 | ret = false; |
| 194 | retMsg = tr("The process \"%1\" exited with code %2.\n") |
| 195 | .arg(process.program(), QString::number(exitcode)); |
| 196 | } else { |
| 197 | ret = false; |
| 198 | retMsg = tr("The process \"%1\" crashed.\n") |
| 199 | .arg(process.program()); |
| 200 | } |
| 201 | }); |
| 202 | |
| 203 | connect(&process, &QProcess::readyReadStandardOutput, [&]() { |
| 204 | process.setReadChannel(QProcess::StandardOutput); |
| 205 | while (process.canReadLine()) { |
| 206 | QString line = QString::fromUtf8(process.readLine()); |
| 207 | outputMsg(process.pid(), line, OutputPane::OutputFormat::StdOut); |
| 208 | } |
| 209 | }); |
| 210 | |
| 211 | connect(&process, &QProcess::readyReadStandardError, [&]() { |
| 212 | process.setReadChannel(QProcess::StandardError); |
| 213 | while (process.canReadLine()) { |
| 214 | QString line = QString::fromUtf8(process.readLine()); |
| 215 | outputMsg(process.pid(), line, OutputPane::OutputFormat::StdErr); |
| 216 | } |
| 217 | }); |
| 218 | |
| 219 | process.start(info.program, info.arguments); |
| 220 | quint64 pid = process.pid(); |
| 221 | QMetaObject::invokeMethod(AppOutputPane::instance(), "createApplicationPane", |
| 222 | Q_ARG(const QString &, QString::number(pid)), Q_ARG(QString, info.program)); |
| 223 | outputMsg(pid, startMsg, OutputPane::OutputFormat::NormalMessage); |
| 224 | process.waitForFinished(-1); |
| 225 | |
| 226 | AppOutputPane::instance()->setProcessFinished(QString::number(pid)); |
| 227 | |
| 228 | outputMsg(pid, retMsg, ret ? OutputPane::OutputFormat::NormalMessage : OutputPane::OutputFormat::StdErr); |
| 229 | |
| 230 | QString endMsg = tr("Execute command finished.\n"); |
| 231 | outputMsg(pid, endMsg, OutputPane::OutputFormat::NormalMessage); |
| 232 | |
| 233 | return ret; |
nothing calls this directly
no test coverage detected