forked from qt-creator/qt-creator
Terminal: Close Terminal.app window automatically
Change-Id: I6756c55b644e0db55b6d0b7ca892a890805e7285 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include "externalterminalprocessimpl.h"
|
||||
|
||||
#include "algorithm.h"
|
||||
#include "process.h"
|
||||
#include "terminalcommand.h"
|
||||
#include "utilstr.h"
|
||||
@@ -19,43 +21,73 @@ ProcessStubCreator::ProcessStubCreator(TerminalInterface *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)
|
||||
{
|
||||
const TerminalCommand terminal = TerminalCommand::terminalEmulator();
|
||||
|
||||
if (HostOsInfo::isMacHost() && terminal.command == "Terminal.app") {
|
||||
QTemporaryFile f;
|
||||
f.setAutoRemove(false);
|
||||
f.open();
|
||||
f.setPermissions(QFile::ExeUser | QFile::ReadUser | QFile::WriteUser);
|
||||
f.write("#!/bin/sh\n");
|
||||
f.write(QString("cd %1\n").arg(setupData.m_workingDirectory.nativePath()).toUtf8());
|
||||
f.write("clear\n");
|
||||
f.write(QString("exec '%1' %2\n")
|
||||
.arg(setupData.m_commandLine.executable().nativePath())
|
||||
.arg(setupData.m_commandLine.arguments())
|
||||
.toUtf8());
|
||||
f.close();
|
||||
|
||||
const QString path = f.fileName();
|
||||
const QString exe
|
||||
= QString("tell app \"Terminal\" to do script \"'%1'; rm -f '%1'; exit\"").arg(path);
|
||||
|
||||
Process process;
|
||||
|
||||
process.setCommand({"osascript", {"-e", "tell app \"Terminal\" to activate", "-e", exe}});
|
||||
process.runBlocking();
|
||||
|
||||
if (process.exitCode() != 0) {
|
||||
return make_unexpected(
|
||||
Tr::tr("Failed to start terminal process: \"%1\"").arg(process.errorString()));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool detached = setupData.m_terminalMode == TerminalMode::Detached;
|
||||
|
||||
if (HostOsInfo::isMacHost()) {
|
||||
static const QMap<QString, QString> terminalMap = {
|
||||
{"Terminal.app", TerminalAppScript},
|
||||
};
|
||||
|
||||
if (terminalMap.contains(terminal.command.toString())) {
|
||||
const QString env
|
||||
= Utils::transform(setupData.m_environment.toStringList(), [](const QString &env) {
|
||||
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.arguments());
|
||||
|
||||
Process *process = new Process(detached ? nullptr : this);
|
||||
if (detached)
|
||||
QObject::connect(process, &Process::done, process, &Process::deleteLater);
|
||||
|
||||
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();
|
||||
|
||||
FilePath::fromUserInput(shFile->fileName())
|
||||
.setPermissions(QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther | QFile::ReadUser
|
||||
| QFile::ReadGroup | QFile::ReadOther | QFile::WriteUser
|
||||
| QFile::WriteGroup | QFile::WriteOther);
|
||||
|
||||
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(
|
||||
Tr::tr("Failed to start terminal process: \"%1\"").arg(process->errorString()));
|
||||
}
|
||||
|
||||
QObject::connect(process, &Process::done, m_interface, &TerminalInterface::onStubExited);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Process *process = new Process(detached ? nullptr : this);
|
||||
if (detached)
|
||||
QObject::connect(process, &Process::done, process, &Process::deleteLater);
|
||||
@@ -77,6 +109,8 @@ expected_str<qint64> ProcessStubCreator::startStubProcess(const ProcessSetupData
|
||||
process->setCommand(cmdLine);
|
||||
}
|
||||
|
||||
process->setEnvironment(setupData.m_environment);
|
||||
|
||||
process->start();
|
||||
process->waitForStarted();
|
||||
if (process->error() != QProcess::UnknownError) {
|
||||
|
||||
Reference in New Issue
Block a user