From d175c629aa34f49f7a229e77aa8d647e9a9d8d67 Mon Sep 17 00:00:00 2001 From: hjk Date: Fri, 13 Dec 2024 07:30:33 +0100 Subject: [PATCH] Coco: Merge cocoinstallation.* into globalsettings.* This is mechanical so far, the plan is to merge the class together with the GlobalSettings namespace into a CocoSettings class with the usual access pattern. Later the m_cocoPathAspect from GlobalSettingsWidget can be moved there and /probably/ be merged with CocoInstallationPrivate ::cocoPath. Change-Id: If156a5ea78335e30372414cb1583d8e68f4f90ba Reviewed-by: Jarek Kobus --- src/plugins/coco/CMakeLists.txt | 2 - src/plugins/coco/coco.qbs | 2 - src/plugins/coco/cocobuildstep.cpp | 2 +- src/plugins/coco/cocoinstallation.cpp | 177 ------------------------- src/plugins/coco/cocoinstallation.h | 39 ------ src/plugins/coco/cocoprojectwidget.h | 2 +- src/plugins/coco/cocoqmakesettings.cpp | 2 +- src/plugins/coco/globalsettings.cpp | 163 ++++++++++++++++++++++- src/plugins/coco/globalsettings.h | 28 +++- 9 files changed, 191 insertions(+), 226 deletions(-) diff --git a/src/plugins/coco/CMakeLists.txt b/src/plugins/coco/CMakeLists.txt index 7a70c23b50e..0fde8939412 100644 --- a/src/plugins/coco/CMakeLists.txt +++ b/src/plugins/coco/CMakeLists.txt @@ -17,8 +17,6 @@ add_qtc_plugin(Coco cococmakesettings.h cococommon.cpp cococommon.h - cocoinstallation.cpp - cocoinstallation.h cocolanguageclient.cpp cocolanguageclient.h cocoplugin.cpp diff --git a/src/plugins/coco/coco.qbs b/src/plugins/coco/coco.qbs index 0395e286435..19630ad09cf 100644 --- a/src/plugins/coco/coco.qbs +++ b/src/plugins/coco/coco.qbs @@ -23,8 +23,6 @@ QtcPlugin { "cococmakesettings.h", "cococommon.cpp", "cococommon.h", - "cocoinstallation.cpp", - "cocoinstallation.h", "cocolanguageclient.cpp", "cocolanguageclient.h", "cocoplugin.cpp", diff --git a/src/plugins/coco/cocobuildstep.cpp b/src/plugins/coco/cocobuildstep.cpp index e809785f493..dea41c52dee 100644 --- a/src/plugins/coco/cocobuildstep.cpp +++ b/src/plugins/coco/cocobuildstep.cpp @@ -3,9 +3,9 @@ #include "cocobuildstep.h" -#include "cocoinstallation.h" #include "cocopluginconstants.h" #include "cocotr.h" +#include "globalsettings.h" #include #include diff --git a/src/plugins/coco/cocoinstallation.cpp b/src/plugins/coco/cocoinstallation.cpp index 7a3c4441249..e69de29bb2d 100644 --- a/src/plugins/coco/cocoinstallation.cpp +++ b/src/plugins/coco/cocoinstallation.cpp @@ -1,177 +0,0 @@ -// Copyright (C) 2024 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 - -#include "cocoinstallation.h" - -#include "cococommon.h" -#include "cocotr.h" -#include "globalsettings.h" - -#include -#include - -#include -#include -#include - -namespace Coco::Internal { - -struct CocoInstallationPrivate -{ - Utils::FilePath cocoPath; - bool isValid = false; - QString errorMessage = Tr::tr("Error: Coco installation directory not set. (This can't happen.)"); -}; - -CocoInstallationPrivate *CocoInstallation::d = nullptr; - -CocoInstallation::CocoInstallation() -{ - if (!d) - d = new CocoInstallationPrivate; -} - -Utils::FilePath CocoInstallation::directory() const -{ - return d->cocoPath; -} - -Utils::FilePath CocoInstallation::coverageBrowserPath() const -{ - QString browserPath; - - if (Utils::HostOsInfo::isAnyUnixHost() || Utils::HostOsInfo::isMacHost()) - browserPath = "bin/coveragebrowser"; - else - browserPath = "coveragebrowser.exe"; - - return d->cocoPath.resolvePath(browserPath); -} - -void CocoInstallation::setDirectory(const Utils::FilePath &dir) -{ - if (isCocoDirectory(dir)) { - d->cocoPath = dir; - d->isValid = true; - d->errorMessage = ""; - verifyCocoDirectory(); - } - else { - d->cocoPath = Utils::FilePath(); - d->isValid = false; - d->errorMessage - = Tr::tr("Error: Coco installation directory not found at \"%1\".").arg(dir.nativePath()); - } -} - -Utils::FilePath CocoInstallation::coverageScannerPath(const Utils::FilePath &cocoDir) const -{ - QString scannerPath; - - if (Utils::HostOsInfo::isAnyUnixHost() || Utils::HostOsInfo::isMacHost()) - scannerPath = "bin/coveragescanner"; - else - scannerPath = "coveragescanner.exe"; - - return cocoDir.resolvePath(scannerPath); -} - -bool CocoInstallation::isCocoDirectory(const Utils::FilePath &cocoDir) const -{ - return coverageScannerPath(cocoDir).exists(); -} - -void CocoInstallation::logError(const QString &msg) -{ - logFlashing(msg); - d->isValid = false; - d->errorMessage = msg; -} - -bool CocoInstallation::verifyCocoDirectory() -{ - QString coveragescanner = coverageScannerPath(d->cocoPath).nativePath(); - - QProcess proc; - proc.setProgram(coveragescanner); - proc.setArguments({"--cs-help"}); - proc.start(); - - if (!proc.waitForStarted()) { - logError(Tr::tr("Error: Coveragescanner at \"%1\" did not start.").arg(coveragescanner)); - return false; - } - - if (!proc.waitForFinished()) { - logError(Tr::tr("Error: Coveragescanner at \"%1\" did not finish.").arg(coveragescanner)); - return false; - } - - QString result = QString::fromLatin1(proc.readAll()); - static const QRegularExpression linebreak("\n|\r\n|\r"); - QStringList lines = result.split(linebreak, Qt::SkipEmptyParts); - - const qsizetype n = lines.size(); - if (n >= 2 && lines[n - 2].startsWith("Version:") && lines[n - 1].startsWith("Date:")) { - logSilently(Tr::tr("Valid CoverageScanner found at \"%1\":").arg(coveragescanner)); - logSilently(" " + lines[n - 2]); - logSilently(" " + lines[n - 1]); - return true; - } else { - logError( - Tr::tr("Error: Coveragescanner at \"%1\" did not run correctly.").arg(coveragescanner)); - for (const QString &l : lines) { - logSilently(l); - } - return false; - } -} - -bool CocoInstallation::isValid() const -{ - return d->isValid; -} - -QString CocoInstallation::errorMessage() const -{ - return d->errorMessage; -} - -void CocoInstallation::tryPath(const QString &path) -{ - if (d->isValid) - return; - - const auto fpath = Utils::FilePath::fromString(path); - const QString nativePath = fpath.nativePath(); - if (isCocoDirectory(fpath)) { - logSilently(Tr::tr("Found Coco directory \"%1\".").arg(nativePath)); - setDirectory(fpath); - GlobalSettings::save(); - } else - logSilently(Tr::tr("Checked Coco directory \"%1\".").arg(nativePath)); -} - -QString CocoInstallation::envVar(const QString &var) const -{ - return QProcessEnvironment::systemEnvironment().value(var); -} - -void CocoInstallation::findDefaultDirectory() -{ - if (Utils::HostOsInfo::isMacHost()) - tryPath("/Applications/SquishCoco"); - else if (Utils::HostOsInfo::isAnyUnixHost()) { - tryPath((Utils::FileUtils::homePath() / "SquishCoco").nativePath()); - tryPath("/opt/SquishCoco"); - } else { - tryPath(envVar("SQUISHCOCO")); - QStringList homeDirs = QStandardPaths::standardLocations(QStandardPaths::HomeLocation); - if (!homeDirs.isEmpty()) - tryPath(homeDirs[0] + "/squishcoco"); - tryPath(envVar("ProgramFiles") + "\\squishcoco"); - tryPath(envVar("ProgramFiles(x86)") + "\\squishcoco"); - } -} - -} // namespace Coco::Internal diff --git a/src/plugins/coco/cocoinstallation.h b/src/plugins/coco/cocoinstallation.h index 73d83bf704d..e69de29bb2d 100644 --- a/src/plugins/coco/cocoinstallation.h +++ b/src/plugins/coco/cocoinstallation.h @@ -1,39 +0,0 @@ -// Copyright (C) 2024 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 - -#pragma once - -#include - -class QString; - -namespace Coco::Internal { - -struct CocoInstallationPrivate; - -// Borg pattern: There are many instances of this class, but all are the same. -class CocoInstallation -{ -public: - CocoInstallation(); - - Utils::FilePath directory() const; - Utils::FilePath coverageBrowserPath() const; - void setDirectory(const Utils::FilePath &dir); - void findDefaultDirectory(); - - bool isValid() const; - QString errorMessage() const; - -private: - Utils::FilePath coverageScannerPath(const Utils::FilePath &cocoDir) const; - void logError(const QString &msg); - bool isCocoDirectory(const Utils::FilePath &cocoDir) const; - bool verifyCocoDirectory(); - void tryPath(const QString &path); - QString envVar(const QString &var) const; - - static CocoInstallationPrivate *d; -}; - -} // namespace Coco::Internal diff --git a/src/plugins/coco/cocoprojectwidget.h b/src/plugins/coco/cocoprojectwidget.h index 8c39b0ad363..ccc04fddc5d 100644 --- a/src/plugins/coco/cocoprojectwidget.h +++ b/src/plugins/coco/cocoprojectwidget.h @@ -4,7 +4,7 @@ #pragma once #include "buildsettings.h" -#include "cocoinstallation.h" +#include "globalsettings.h" #include #include diff --git a/src/plugins/coco/cocoqmakesettings.cpp b/src/plugins/coco/cocoqmakesettings.cpp index ea8a3fef4bd..61115a8c01d 100644 --- a/src/plugins/coco/cocoqmakesettings.cpp +++ b/src/plugins/coco/cocoqmakesettings.cpp @@ -5,10 +5,10 @@ #include "buildsettings.h" #include "cococommon.h" -#include "cocoinstallation.h" #include "cocopluginconstants.h" #include "cocoprojectwidget.h" #include "cocotr.h" +#include "globalsettings.h" #include "qmakefeaturefile.h" #include diff --git a/src/plugins/coco/globalsettings.cpp b/src/plugins/coco/globalsettings.cpp index 82d71e8d766..7a83e3309c0 100644 --- a/src/plugins/coco/globalsettings.cpp +++ b/src/plugins/coco/globalsettings.cpp @@ -3,7 +3,7 @@ #include "globalsettings.h" -#include "cocoinstallation.h" +#include "cococommon.h" #include "cocopluginconstants.h" #include "cocotr.h" @@ -11,10 +11,14 @@ #include #include +#include +#include #include #include +#include #include +#include namespace Coco::Internal { namespace GlobalSettings { @@ -55,6 +59,163 @@ void save() } // namespace GlobalSettings +struct CocoInstallationPrivate +{ + Utils::FilePath cocoPath; + bool isValid = false; + QString errorMessage = Tr::tr("Error: Coco installation directory not set. (This can't happen.)"); +}; + +CocoInstallationPrivate *CocoInstallation::d = nullptr; + +CocoInstallation::CocoInstallation() +{ + if (!d) + d = new CocoInstallationPrivate; +} + +Utils::FilePath CocoInstallation::directory() const +{ + return d->cocoPath; +} + +Utils::FilePath CocoInstallation::coverageBrowserPath() const +{ + QString browserPath; + + if (Utils::HostOsInfo::isAnyUnixHost() || Utils::HostOsInfo::isMacHost()) + browserPath = "bin/coveragebrowser"; + else + browserPath = "coveragebrowser.exe"; + + return d->cocoPath.resolvePath(browserPath); +} + +void CocoInstallation::setDirectory(const Utils::FilePath &dir) +{ + if (isCocoDirectory(dir)) { + d->cocoPath = dir; + d->isValid = true; + d->errorMessage = ""; + verifyCocoDirectory(); + } + else { + d->cocoPath = Utils::FilePath(); + d->isValid = false; + d->errorMessage + = Tr::tr("Error: Coco installation directory not found at \"%1\".").arg(dir.nativePath()); + } +} + +Utils::FilePath CocoInstallation::coverageScannerPath(const Utils::FilePath &cocoDir) const +{ + QString scannerPath; + + if (Utils::HostOsInfo::isAnyUnixHost() || Utils::HostOsInfo::isMacHost()) + scannerPath = "bin/coveragescanner"; + else + scannerPath = "coveragescanner.exe"; + + return cocoDir.resolvePath(scannerPath); +} + +bool CocoInstallation::isCocoDirectory(const Utils::FilePath &cocoDir) const +{ + return coverageScannerPath(cocoDir).exists(); +} + +void CocoInstallation::logError(const QString &msg) +{ + logFlashing(msg); + d->isValid = false; + d->errorMessage = msg; +} + +bool CocoInstallation::verifyCocoDirectory() +{ + QString coveragescanner = coverageScannerPath(d->cocoPath).nativePath(); + + QProcess proc; + proc.setProgram(coveragescanner); + proc.setArguments({"--cs-help"}); + proc.start(); + + if (!proc.waitForStarted()) { + logError(Tr::tr("Error: Coveragescanner at \"%1\" did not start.").arg(coveragescanner)); + return false; + } + + if (!proc.waitForFinished()) { + logError(Tr::tr("Error: Coveragescanner at \"%1\" did not finish.").arg(coveragescanner)); + return false; + } + + QString result = QString::fromLatin1(proc.readAll()); + static const QRegularExpression linebreak("\n|\r\n|\r"); + QStringList lines = result.split(linebreak, Qt::SkipEmptyParts); + + const qsizetype n = lines.size(); + if (n >= 2 && lines[n - 2].startsWith("Version:") && lines[n - 1].startsWith("Date:")) { + logSilently(Tr::tr("Valid CoverageScanner found at \"%1\":").arg(coveragescanner)); + logSilently(" " + lines[n - 2]); + logSilently(" " + lines[n - 1]); + return true; + } else { + logError( + Tr::tr("Error: Coveragescanner at \"%1\" did not run correctly.").arg(coveragescanner)); + for (const QString &l : lines) { + logSilently(l); + } + return false; + } +} + +bool CocoInstallation::isValid() const +{ + return d->isValid; +} + +QString CocoInstallation::errorMessage() const +{ + return d->errorMessage; +} + +void CocoInstallation::tryPath(const QString &path) +{ + if (d->isValid) + return; + + const auto fpath = Utils::FilePath::fromString(path); + const QString nativePath = fpath.nativePath(); + if (isCocoDirectory(fpath)) { + logSilently(Tr::tr("Found Coco directory \"%1\".").arg(nativePath)); + setDirectory(fpath); + GlobalSettings::save(); + } else + logSilently(Tr::tr("Checked Coco directory \"%1\".").arg(nativePath)); +} + +QString CocoInstallation::envVar(const QString &var) const +{ + return QProcessEnvironment::systemEnvironment().value(var); +} + +void CocoInstallation::findDefaultDirectory() +{ + if (Utils::HostOsInfo::isMacHost()) + tryPath("/Applications/SquishCoco"); + else if (Utils::HostOsInfo::isAnyUnixHost()) { + tryPath((Utils::FileUtils::homePath() / "SquishCoco").nativePath()); + tryPath("/opt/SquishCoco"); + } else { + tryPath(envVar("SQUISHCOCO")); + QStringList homeDirs = QStandardPaths::standardLocations(QStandardPaths::HomeLocation); + if (!homeDirs.isEmpty()) + tryPath(homeDirs[0] + "/squishcoco"); + tryPath(envVar("ProgramFiles") + "\\squishcoco"); + tryPath(envVar("ProgramFiles(x86)") + "\\squishcoco"); + } +} GlobalSettingsWidget::GlobalSettingsWidget(QFrame *parent) : QFrame(parent) { diff --git a/src/plugins/coco/globalsettings.h b/src/plugins/coco/globalsettings.h index 5bd4a1107f3..ce682e857e6 100644 --- a/src/plugins/coco/globalsettings.h +++ b/src/plugins/coco/globalsettings.h @@ -3,8 +3,6 @@ #pragma once -#include "cocoinstallation.h" - #include #include @@ -17,6 +15,32 @@ void save(); } // GlobalSettings +struct CocoInstallationPrivate; + +// Borg pattern: There are many instances of this class, but all are the same. +class CocoInstallation +{ +public: + CocoInstallation(); + + Utils::FilePath directory() const; + Utils::FilePath coverageBrowserPath() const; + void setDirectory(const Utils::FilePath &dir); + void findDefaultDirectory(); + + bool isValid() const; + QString errorMessage() const; + +private: + Utils::FilePath coverageScannerPath(const Utils::FilePath &cocoDir) const; + void logError(const QString &msg); + bool isCocoDirectory(const Utils::FilePath &cocoDir) const; + bool verifyCocoDirectory(); + void tryPath(const QString &path); + QString envVar(const QString &var) const; + + static CocoInstallationPrivate *d; +}; class GlobalSettingsWidget : public QFrame {