From e3f6eca25d6899ece962c6e4c97860d5fd8f230c Mon Sep 17 00:00:00 2001 From: hjk Date: Thu, 26 Jan 2023 12:53:56 +0100 Subject: [PATCH] Utils: Move some code from QtcProcess to its only user This is not really nice/up-to-date either, but I'd rather follow up on the user side. Change-Id: I426fea3251b8984aea19788a16e574dccc3d057d Reviewed-by: Jarek Kobus --- src/libs/utils/qtcprocess.cpp | 77 ---------------------- src/libs/utils/qtcprocess.h | 7 +- src/plugins/qtsupport/externaleditors.cpp | 79 ++++++++++++++++++++++- 3 files changed, 80 insertions(+), 83 deletions(-) diff --git a/src/libs/utils/qtcprocess.cpp b/src/libs/utils/qtcprocess.cpp index 25e40fad26a..547e03e3c2b 100644 --- a/src/libs/utils/qtcprocess.cpp +++ b/src/libs/utils/qtcprocess.cpp @@ -1348,83 +1348,6 @@ QString QtcProcess::errorString() const // 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 {}; - const int dotIndex = binary.lastIndexOf(QLatin1Char('.')); - if (dotIndex != -1 && dotIndex == binary.size() - 4) - return {}; - - 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 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 {}; - 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 {}; -} - Environment QtcProcess::systemEnvironmentForBinary(const FilePath &filePath) { if (filePath.needsDevice()) { diff --git a/src/libs/utils/qtcprocess.h b/src/libs/utils/qtcprocess.h index ae99bd666c2..0451c0cd046 100644 --- a/src/libs/utils/qtcprocess.h +++ b/src/libs/utils/qtcprocess.h @@ -127,11 +127,8 @@ public: // These (or some of them) may be potentially moved outside of the class. // For some we may aggregate in another public utils class (or subclass of QtcProcess)? - // TODO: How below 2 methods relate to QtcProcess? - // Action: move/merge them somewhere else, FilePath::searchInPath() ? - // Helpers to find binaries. Do not use it for other path variables - // and file types. - static QString locateBinary(const QString &path, const QString &binary); + // TODO: How below method relates to QtcProcess? + // Action: move/merge it somewhere else static QString normalizeNewlines(const QString &text); // TODO: Unused currently? Should it serve as a compartment for contrary of remoteEnvironment? diff --git a/src/plugins/qtsupport/externaleditors.cpp b/src/plugins/qtsupport/externaleditors.cpp index 001e017ea1d..afff6a0b77f 100644 --- a/src/plugins/qtsupport/externaleditors.cpp +++ b/src/plugins/qtsupport/externaleditors.cpp @@ -39,6 +39,83 @@ struct Tr { Q_DECLARE_TR_FUNCTIONS(::QmakeProjectManager) }; +// 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 {}; + const int dotIndex = binary.lastIndexOf(QLatin1Char('.')); + if (dotIndex != -1 && dotIndex == binary.size() - 4) + return {}; + + 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 {}; +} + +static QString 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 {}; + 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 {}; +} + static QString msgStartFailed(const QString &binary, QStringList arguments) { arguments.push_front(binary); @@ -122,7 +199,7 @@ static bool getEditorLaunchData(const CommandForQtVersion &commandForQtVersion, // fallback if (data->binary.isEmpty()) { const QString path = qtcEnvironmentVariable("PATH"); - data->binary = QtcProcess::locateBinary(path, commandForQtVersion(nullptr)); + data->binary = locateBinary(path, commandForQtVersion(nullptr)); } if (data->binary.isEmpty()) {