2023-02-23 12:47:39 +01:00
|
|
|
// Copyright (C) 2022 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
|
|
|
|
|
|
|
#include "terminalhooks.h"
|
|
|
|
|
|
|
|
#include "filepath.h"
|
2023-03-07 17:55:38 +01:00
|
|
|
#include "qtcprocess.h"
|
|
|
|
#include "terminalcommand.h"
|
2023-03-14 09:09:55 +01:00
|
|
|
#include "terminalinterface.h"
|
2023-03-07 17:55:38 +01:00
|
|
|
|
|
|
|
#include <QTemporaryFile>
|
2023-02-23 12:47:39 +01:00
|
|
|
|
|
|
|
namespace Utils::Terminal {
|
|
|
|
|
2023-03-01 09:57:43 +01:00
|
|
|
FilePath defaultShellForDevice(const FilePath &deviceRoot)
|
|
|
|
{
|
2023-03-14 09:09:55 +01:00
|
|
|
if (deviceRoot.osType() == OsTypeWindows)
|
|
|
|
return deviceRoot.withNewPath("cmd.exe").searchInPath();
|
2023-03-01 09:57:43 +01:00
|
|
|
|
|
|
|
const Environment env = deviceRoot.deviceEnvironment();
|
|
|
|
FilePath shell = FilePath::fromUserInput(env.value_or("SHELL", "/bin/sh"));
|
|
|
|
|
|
|
|
if (!shell.isAbsolutePath())
|
|
|
|
shell = env.searchInPath(shell.nativePath());
|
|
|
|
|
|
|
|
if (shell.isEmpty())
|
|
|
|
return shell;
|
|
|
|
|
|
|
|
return shell.onDevice(deviceRoot);
|
|
|
|
}
|
|
|
|
|
2023-03-07 17:55:38 +01:00
|
|
|
class ExternalTerminalProcessImpl final : public TerminalInterface
|
|
|
|
{
|
|
|
|
class ProcessStubCreator : public StubCreator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ProcessStubCreator(ExternalTerminalProcessImpl *interface)
|
|
|
|
: m_interface(interface)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void startStubProcess(const CommandLine &cmd, const ProcessSetupData &) override
|
|
|
|
{
|
2023-03-14 09:09:55 +01:00
|
|
|
const TerminalCommand terminal = TerminalCommand::terminalEmulator();
|
|
|
|
|
2023-03-07 17:55:38 +01:00
|
|
|
if (HostOsInfo::isWindowsHost()) {
|
|
|
|
m_terminalProcess.setCommand(cmd);
|
|
|
|
QObject::connect(&m_terminalProcess, &QtcProcess::done, this, [this] {
|
|
|
|
m_interface->onStubExited();
|
|
|
|
});
|
2023-03-14 09:09:55 +01:00
|
|
|
m_terminalProcess.setCreateConsoleOnWindows(true);
|
|
|
|
m_terminalProcess.setProcessMode(ProcessMode::Writer);
|
2023-03-07 17:55:38 +01:00
|
|
|
m_terminalProcess.start();
|
2023-03-14 09:09:55 +01:00
|
|
|
} else if (HostOsInfo::isMacHost() && terminal.command == "Terminal.app") {
|
2023-03-07 17:55:38 +01:00
|
|
|
QTemporaryFile f;
|
|
|
|
f.setAutoRemove(false);
|
|
|
|
f.open();
|
|
|
|
f.setPermissions(QFile::ExeUser | QFile::ReadUser | QFile::WriteUser);
|
|
|
|
f.write("#!/bin/sh\n");
|
2023-03-14 09:09:55 +01:00
|
|
|
f.write("clear\n");
|
2023-03-07 17:55:38 +01:00
|
|
|
f.write(QString("exec '%1' %2\n")
|
|
|
|
.arg(cmd.executable().nativePath())
|
|
|
|
.arg(cmd.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);
|
|
|
|
|
2023-03-14 09:09:55 +01:00
|
|
|
m_terminalProcess.setCommand(
|
|
|
|
{"osascript", {"-e", "tell app \"Terminal\" to activate", "-e", exe}});
|
2023-03-07 17:55:38 +01:00
|
|
|
m_terminalProcess.runBlocking();
|
|
|
|
} else {
|
|
|
|
CommandLine cmdLine = {terminal.command, {terminal.executeArgs}};
|
|
|
|
cmdLine.addCommandLineAsArgs(cmd, CommandLine::Raw);
|
|
|
|
|
|
|
|
m_terminalProcess.setCommand(cmdLine);
|
|
|
|
m_terminalProcess.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExternalTerminalProcessImpl *m_interface;
|
|
|
|
QtcProcess m_terminalProcess;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
ExternalTerminalProcessImpl() { setStubCreator(new ProcessStubCreator(this)); }
|
|
|
|
};
|
|
|
|
|
2023-02-23 12:47:39 +01:00
|
|
|
struct HooksPrivate
|
|
|
|
{
|
|
|
|
HooksPrivate()
|
|
|
|
: m_openTerminalHook([](const OpenTerminalParameters ¶meters) {
|
|
|
|
DeviceFileHooks::instance().openTerminal(parameters.workingDirectory.value_or(
|
|
|
|
FilePath{}),
|
|
|
|
parameters.environment.value_or(Environment{}));
|
|
|
|
})
|
2023-03-07 17:55:38 +01:00
|
|
|
, m_createTerminalProcessInterfaceHook([] { return new ExternalTerminalProcessImpl(); })
|
|
|
|
, m_getTerminalCommandsForDevicesHook([] { return QList<NameAndCommandLine>{}; })
|
2023-02-23 12:47:39 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
Hooks::OpenTerminalHook m_openTerminalHook;
|
|
|
|
Hooks::CreateTerminalProcessInterfaceHook m_createTerminalProcessInterfaceHook;
|
2023-02-25 10:47:21 +01:00
|
|
|
Hooks::GetTerminalCommandsForDevicesHook m_getTerminalCommandsForDevicesHook;
|
2023-02-23 12:47:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Hooks &Hooks::instance()
|
|
|
|
{
|
|
|
|
static Hooks manager;
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hooks::Hooks()
|
|
|
|
: d(new HooksPrivate())
|
|
|
|
{}
|
|
|
|
|
|
|
|
Hooks::~Hooks() = default;
|
|
|
|
|
|
|
|
Hooks::OpenTerminalHook &Hooks::openTerminalHook()
|
|
|
|
{
|
|
|
|
return d->m_openTerminalHook;
|
|
|
|
}
|
2023-02-25 10:47:21 +01:00
|
|
|
|
2023-02-23 12:47:39 +01:00
|
|
|
Hooks::CreateTerminalProcessInterfaceHook &Hooks::createTerminalProcessInterfaceHook()
|
|
|
|
{
|
|
|
|
return d->m_createTerminalProcessInterfaceHook;
|
|
|
|
}
|
|
|
|
|
2023-02-25 10:47:21 +01:00
|
|
|
Hooks::GetTerminalCommandsForDevicesHook &Hooks::getTerminalCommandsForDevicesHook()
|
|
|
|
{
|
|
|
|
return d->m_getTerminalCommandsForDevicesHook;
|
|
|
|
}
|
|
|
|
|
2023-02-23 12:47:39 +01:00
|
|
|
} // namespace Utils::Terminal
|