Utils: Move SynchronousProcess::locateBinary to QtcProcess

Change-Id: I1df5008f8f33d25f208ab8be13b4a6e72c901be2
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2021-05-05 15:04:30 +02:00
parent a61e81272e
commit 2c70ad63d1
5 changed files with 92 additions and 92 deletions

View File

@@ -1698,4 +1698,89 @@ QString QtcProcess::Arguments::toString() const
return QtcProcess::joinArgs(m_unixArgs, OsTypeLinux); return QtcProcess::joinArgs(m_unixArgs, OsTypeLinux);
} }
// Path utilities
// Locate a binary in a directory, applying all kinds of
// extensions the operating system supports.
static QString checkBinary(const QDir &dir, const QString &binary)
{
// naive UNIX approach
const QFileInfo info(dir.filePath(binary));
if (info.isFile() && info.isExecutable())
return info.absoluteFilePath();
// Does the OS have some weird extension concept or does the
// binary have a 3 letter extension?
if (HostOsInfo::isAnyUnixHost() && !HostOsInfo::isMacHost())
return QString();
const int dotIndex = binary.lastIndexOf(QLatin1Char('.'));
if (dotIndex != -1 && dotIndex == binary.size() - 4)
return QString();
switch (HostOsInfo::hostOs()) {
case OsTypeLinux:
case OsTypeOtherUnix:
case OsTypeOther:
break;
case OsTypeWindows: {
static const char *windowsExtensions[] = {".cmd", ".bat", ".exe", ".com"};
// Check the Windows extensions using the order
const int windowsExtensionCount = sizeof(windowsExtensions)/sizeof(const char*);
for (int e = 0; e < windowsExtensionCount; e ++) {
const QFileInfo windowsBinary(dir.filePath(binary + QLatin1String(windowsExtensions[e])));
if (windowsBinary.isFile() && windowsBinary.isExecutable())
return windowsBinary.absoluteFilePath();
}
}
break;
case OsTypeMac: {
// Check for Mac app folders
const QFileInfo appFolder(dir.filePath(binary + QLatin1String(".app")));
if (appFolder.isDir()) {
QString macBinaryPath = appFolder.absoluteFilePath();
macBinaryPath += QLatin1String("/Contents/MacOS/");
macBinaryPath += binary;
const QFileInfo macBinary(macBinaryPath);
if (macBinary.isFile() && macBinary.isExecutable())
return macBinary.absoluteFilePath();
}
}
break;
}
return QString();
}
QString QtcProcess::locateBinary(const QString &path, const QString &binary)
{
// Absolute file?
const QFileInfo absInfo(binary);
if (absInfo.isAbsolute())
return checkBinary(absInfo.dir(), absInfo.fileName());
// Windows finds binaries in the current directory
if (HostOsInfo::isWindowsHost()) {
const QString currentDirBinary = checkBinary(QDir::current(), binary);
if (!currentDirBinary.isEmpty())
return currentDirBinary;
}
const QStringList paths = path.split(HostOsInfo::pathListSeparator());
if (paths.empty())
return QString();
const QStringList::const_iterator cend = paths.constEnd();
for (QStringList::const_iterator it = paths.constBegin(); it != cend; ++it) {
const QDir dir(*it);
const QString rc = checkBinary(dir, binary);
if (!rc.isEmpty())
return rc;
}
return QString();
}
QString QtcProcess::locateBinary(const QString &binary)
{
const QByteArray path = qgetenv("PATH");
return locateBinary(QString::fromLocal8Bit(path), binary);
}
} // namespace Utils } // namespace Utils

View File

@@ -157,6 +157,11 @@ public:
static QString normalizeNewlines(const QString &text); static QString normalizeNewlines(const QString &text);
// Helpers to find binaries. Do not use it for other path variables
// and file types.
static QString locateBinary(const QString &binary);
static QString locateBinary(const QString &path, const QString &binary);
private: private:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void setupChildProcess() override; void setupChildProcess() override;

View File

@@ -638,91 +638,6 @@ void SynchronousProcess::processStdErr(bool emitSignals)
d->m_stdErr.append(d->m_process.readAllStandardError(), emitSignals); d->m_stdErr.append(d->m_process.readAllStandardError(), emitSignals);
} }
// Path utilities
// Locate a binary in a directory, applying all kinds of
// extensions the operating system supports.
static QString checkBinary(const QDir &dir, const QString &binary)
{
// naive UNIX approach
const QFileInfo info(dir.filePath(binary));
if (info.isFile() && info.isExecutable())
return info.absoluteFilePath();
// Does the OS have some weird extension concept or does the
// binary have a 3 letter extension?
if (HostOsInfo::isAnyUnixHost() && !HostOsInfo::isMacHost())
return QString();
const int dotIndex = binary.lastIndexOf(QLatin1Char('.'));
if (dotIndex != -1 && dotIndex == binary.size() - 4)
return QString();
switch (HostOsInfo::hostOs()) {
case OsTypeLinux:
case OsTypeOtherUnix:
case OsTypeOther:
break;
case OsTypeWindows: {
static const char *windowsExtensions[] = {".cmd", ".bat", ".exe", ".com"};
// Check the Windows extensions using the order
const int windowsExtensionCount = sizeof(windowsExtensions)/sizeof(const char*);
for (int e = 0; e < windowsExtensionCount; e ++) {
const QFileInfo windowsBinary(dir.filePath(binary + QLatin1String(windowsExtensions[e])));
if (windowsBinary.isFile() && windowsBinary.isExecutable())
return windowsBinary.absoluteFilePath();
}
}
break;
case OsTypeMac: {
// Check for Mac app folders
const QFileInfo appFolder(dir.filePath(binary + QLatin1String(".app")));
if (appFolder.isDir()) {
QString macBinaryPath = appFolder.absoluteFilePath();
macBinaryPath += QLatin1String("/Contents/MacOS/");
macBinaryPath += binary;
const QFileInfo macBinary(macBinaryPath);
if (macBinary.isFile() && macBinary.isExecutable())
return macBinary.absoluteFilePath();
}
}
break;
}
return QString();
}
QString SynchronousProcess::locateBinary(const QString &path, const QString &binary)
{
// Absolute file?
const QFileInfo absInfo(binary);
if (absInfo.isAbsolute())
return checkBinary(absInfo.dir(), absInfo.fileName());
// Windows finds binaries in the current directory
if (HostOsInfo::isWindowsHost()) {
const QString currentDirBinary = checkBinary(QDir::current(), binary);
if (!currentDirBinary.isEmpty())
return currentDirBinary;
}
const QStringList paths = path.split(HostOsInfo::pathListSeparator());
if (paths.empty())
return QString();
const QStringList::const_iterator cend = paths.constEnd();
for (QStringList::const_iterator it = paths.constBegin(); it != cend; ++it) {
const QDir dir(*it);
const QString rc = checkBinary(dir, binary);
if (!rc.isEmpty())
return rc;
}
return QString();
}
QString SynchronousProcess::locateBinary(const QString &binary)
{
const QByteArray path = qgetenv("PATH");
return locateBinary(QString::fromLocal8Bit(path), binary);
}
} // namespace Utils } // namespace Utils
#include "synchronousprocess.moc" #include "synchronousprocess.moc"

View File

@@ -129,11 +129,6 @@ public:
// Starts the command blocking the UI fully // Starts the command blocking the UI fully
SynchronousProcessResponse runBlocking(const CommandLine &cmd); SynchronousProcessResponse runBlocking(const CommandLine &cmd);
// Helpers to find binaries. Do not use it for other path variables
// and file types.
static QString locateBinary(const QString &binary);
static QString locateBinary(const QString &path, const QString &binary);
signals: signals:
void stdOutBuffered(const QString &lines, bool firstTime); void stdOutBuffered(const QString &lines, bool firstTime);
void stdErrBuffered(const QString &lines, bool firstTime); void stdErrBuffered(const QString &lines, bool firstTime);

View File

@@ -27,7 +27,7 @@
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/hostosinfo.h> #include <utils/hostosinfo.h>
#include <utils/synchronousprocess.h> #include <utils/qtcprocess.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h> #include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectexplorerconstants.h> #include <projectexplorer/projectexplorerconstants.h>
@@ -187,7 +187,7 @@ bool ExternalQtEditor::getEditorLaunchData(const QString &fileName,
data->binary = findFirstCommand(qtVersionsToCheck, m_commandForQtVersion); data->binary = findFirstCommand(qtVersionsToCheck, m_commandForQtVersion);
// fallback // fallback
if (data->binary.isEmpty()) if (data->binary.isEmpty())
data->binary = Utils::SynchronousProcess::locateBinary(m_commandForQtVersion(nullptr)); data->binary = Utils::QtcProcess::locateBinary(m_commandForQtVersion(nullptr));
if (data->binary.isEmpty()) { if (data->binary.isEmpty()) {
*errorMessage = msgAppNotFound(id().toString()); *errorMessage = msgAppNotFound(id().toString());
return false; return false;