CMakePM: Add action to reload CMake Presets

The CMake presets will be reloaded. The preset kits will get the CMake
configuration cleared (no more CMakeCache.txt)

All the kits will be removed from the project, so that the Kit
configuration wizard will be displayed at the end.

If a normal Qt Kit was configured, the user will get the chance to
import the existing configuration (the initial configuration will be
lost though).

Change-Id: Ieda83098d7716913d7870b67ab522705da4ed93b
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Cristian Adam
2023-05-09 18:22:47 +02:00
parent ce7ee677d8
commit 9ff0cf7306
9 changed files with 141 additions and 7 deletions

View File

@@ -9,6 +9,7 @@
#include "cmakeprojectconstants.h"
#include "cmakeprojectmanagertr.h"
#include "cmakeprojectnodes.h"
#include "cmakespecificsettings.h"
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
@@ -16,6 +17,7 @@
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icore.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/modemanager.h>
#include <cppeditor/cpptoolsreuse.h>
@@ -26,6 +28,8 @@
#include <projectexplorer/projecttree.h>
#include <projectexplorer/target.h>
#include <utils/checkablemessagebox.h>
#include <utils/utilsicons.h>
#include <utils/parameteraction.h>
#include <QAction>
@@ -42,6 +46,8 @@ CMakeManager::CMakeManager()
, m_clearCMakeCacheAction(new QAction(QIcon(), Tr::tr("Clear CMake Configuration"), this))
, m_runCMakeActionContextMenu(new QAction(QIcon(), Tr::tr("Run CMake"), this))
, m_rescanProjectAction(new QAction(QIcon(), Tr::tr("Rescan Project"), this))
, m_reloadCMakePresetsAction(
new QAction(Utils::Icons::RELOAD_TOOLBAR.icon(), Tr::tr("Reload CMake Presets"), this))
{
Core::ActionContainer *mbuild =
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
@@ -101,6 +107,15 @@ CMakeManager::CMakeManager()
rescanProject(ProjectTree::currentBuildSystem());
});
command = Core::ActionManager::registerAction(m_reloadCMakePresetsAction,
Constants::RELOAD_CMAKE_PRESETS,
globalContext);
command->setAttribute(Core::Command::CA_Hide);
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD);
connect(m_reloadCMakePresetsAction, &QAction::triggered, this, [this] {
reloadCMakePresets();
});
m_buildFileAction = new Utils::ParameterAction(Tr::tr("Build File"),
Tr::tr("Build File \"%1\""),
Utils::ParameterAction::AlwaysEnabled,
@@ -135,6 +150,16 @@ void CMakeManager::updateCmakeActions(Node *node)
m_runCMakeActionContextMenu->setEnabled(visible);
m_clearCMakeCacheAction->setVisible(visible);
m_rescanProjectAction->setVisible(visible);
const bool reloadPresetsVisible = [project] {
if (!project)
return false;
const FilePath presetsPath = project->projectFilePath().parentDir().pathAppended(
"CMakePresets.json");
return presetsPath.exists();
}();
m_reloadCMakePresetsAction->setVisible(reloadPresetsVisible);
enableBuildFileMenus(node);
}
@@ -204,6 +229,70 @@ void CMakeManager::enableBuildFileMenus(Node *node)
}
}
void CMakeManager::reloadCMakePresets()
{
auto settings = CMakeSpecificSettings::instance();
bool doNotAsk = !settings->askBeforePresetsReload.value();
if (!doNotAsk) {
CheckableMessageBox question(Core::ICore::dialogParent());
question.setIcon(QMessageBox::Question);
question.setWindowTitle(Tr::tr("Reload CMake Presets"));
question.setText(Tr::tr("Re-generates the CMake presets kits. The manual CMake project modifications will be lost."));
question.setCheckBoxText(Tr::tr("Do not ask again"));
question.setCheckBoxVisible(true);
question.setChecked(doNotAsk);
question.setStandardButtons(QDialogButtonBox::Cancel);
question.addButton(Tr::tr("Reload"), QDialogButtonBox::YesRole);
question.setDefaultButton(QDialogButtonBox::Yes);
question.exec();
doNotAsk = question.isChecked();
settings->askBeforePresetsReload.setValue(!doNotAsk);
settings->writeSettings(Core::ICore::settings());
if (question.clickedStandardButton() == QDialogButtonBox::Cancel)
return;
}
CMakeProject *project = static_cast<CMakeProject *>(ProjectTree::currentProject());
if (!project)
return;
const QSet<QString> oldPresets = Utils::transform<QSet>(project->presetsData().configurePresets,
[](const auto &preset) {
return preset.name;
});
project->readPresets();
QList<Kit*> oldKits;
for (const auto &target : project->targets()) {
const CMakeConfigItem presetItem = CMakeConfigurationKitAspect::cmakePresetConfigItem(
target->kit());
if (BuildManager::isBuilding(target))
BuildManager::cancel();
// Only clear the CMake configuration for preset kits. Any manual kit configuration
// will get the chance to get imported afterwards in the Kit selection wizard
CMakeBuildSystem *bs = static_cast<CMakeBuildSystem *>(target->buildSystem());
if (!presetItem.isNull() && bs)
bs->clearCMakeCache();
if (!presetItem.isNull() && oldPresets.contains(QString::fromUtf8(presetItem.value)))
oldKits << target->kit();
project->removeTarget(target);
}
project->setOldPresetKits(oldKits);
Core::ModeManager::activateMode(ProjectExplorer::Constants::MODE_SESSION);
Core::ModeManager::setFocusToCurrentMode();
}
void CMakeManager::buildFile(Node *node)
{
if (!node) {