Terminal: Close Terminal.app window automatically

Change-Id: I6756c55b644e0db55b6d0b7ca892a890805e7285
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Marcus Tillmanns
2023-06-08 13:36:37 +02:00
parent a4c406d4a1
commit 8e16822d73

View File

@@ -2,6 +2,8 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "externalterminalprocessimpl.h" #include "externalterminalprocessimpl.h"
#include "algorithm.h"
#include "process.h" #include "process.h"
#include "terminalcommand.h" #include "terminalcommand.h"
#include "utilstr.h" #include "utilstr.h"
@@ -19,42 +21,72 @@ ProcessStubCreator::ProcessStubCreator(TerminalInterface *interface)
: m_interface(interface) : m_interface(interface)
{} {}
static const QLatin1String TerminalAppScript{R"(
tell application "Terminal"
activate
set newTab to do script "echo Preparing terminal..."
set win to (the id of window 1 where its tab 1 = newTab) as text
do script "%1 && exit" in newTab
repeat until ((count of processes of newTab) = 0)
delay 0.1
end repeat
close window id win
end tell
)"};
expected_str<qint64> ProcessStubCreator::startStubProcess(const ProcessSetupData &setupData) expected_str<qint64> ProcessStubCreator::startStubProcess(const ProcessSetupData &setupData)
{ {
const TerminalCommand terminal = TerminalCommand::terminalEmulator(); const TerminalCommand terminal = TerminalCommand::terminalEmulator();
bool detached = setupData.m_terminalMode == TerminalMode::Detached;
if (HostOsInfo::isMacHost() && terminal.command == "Terminal.app") { if (HostOsInfo::isMacHost()) {
QTemporaryFile f; static const QMap<QString, QString> terminalMap = {
f.setAutoRemove(false); {"Terminal.app", TerminalAppScript},
f.open(); };
f.setPermissions(QFile::ExeUser | QFile::ReadUser | QFile::WriteUser);
f.write("#!/bin/sh\n"); if (terminalMap.contains(terminal.command.toString())) {
f.write(QString("cd %1\n").arg(setupData.m_workingDirectory.nativePath()).toUtf8()); const QString env
f.write("clear\n"); = Utils::transform(setupData.m_environment.toStringList(), [](const QString &env) {
f.write(QString("exec '%1' %2\n") return CommandLine{"export", {env}}.toUserOutput();
}).join('\n');
const QString shScript = QString("cd '%1'\n%2\nclear\n'%3' %4\n")
.arg(setupData.m_workingDirectory.nativePath())
.arg(env)
.arg(setupData.m_commandLine.executable().nativePath()) .arg(setupData.m_commandLine.executable().nativePath())
.arg(setupData.m_commandLine.arguments()) .arg(setupData.m_commandLine.arguments());
.toUtf8());
f.close();
const QString path = f.fileName(); Process *process = new Process(detached ? nullptr : this);
const QString exe if (detached)
= QString("tell app \"Terminal\" to do script \"'%1'; rm -f '%1'; exit\"").arg(path); QObject::connect(process, &Process::done, process, &Process::deleteLater);
Process process; QTemporaryFile *shFile = new QTemporaryFile(process);
QTC_ASSERT(shFile->open(),
return make_unexpected(Tr::tr("Failed to open temporary script file")));
shFile->write(shScript.toUtf8());
shFile->close();
process.setCommand({"osascript", {"-e", "tell app \"Terminal\" to activate", "-e", exe}}); FilePath::fromUserInput(shFile->fileName())
process.runBlocking(); .setPermissions(QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther | QFile::ReadUser
| QFile::ReadGroup | QFile::ReadOther | QFile::WriteUser
| QFile::WriteGroup | QFile::WriteOther);
if (process.exitCode() != 0) { const QString script
= terminalMap.value(terminal.command.toString()).arg(shFile->fileName());
process->setCommand({"osascript", {"-"}});
process->setWriteData(script.toUtf8());
process->start();
if (!process->waitForStarted()) {
return make_unexpected( return make_unexpected(
Tr::tr("Failed to start terminal process: \"%1\"").arg(process.errorString())); Tr::tr("Failed to start terminal process: \"%1\"").arg(process->errorString()));
} }
QObject::connect(process, &Process::done, m_interface, &TerminalInterface::onStubExited);
return 0; return 0;
} }
}
bool detached = setupData.m_terminalMode == TerminalMode::Detached;
Process *process = new Process(detached ? nullptr : this); Process *process = new Process(detached ? nullptr : this);
if (detached) if (detached)
@@ -77,6 +109,8 @@ expected_str<qint64> ProcessStubCreator::startStubProcess(const ProcessSetupData
process->setCommand(cmdLine); process->setCommand(cmdLine);
} }
process->setEnvironment(setupData.m_environment);
process->start(); process->start();
process->waitForStarted(); process->waitForStarted();
if (process->error() != QProcess::UnknownError) { if (process->error() != QProcess::UnknownError) {