forked from qt-creator/qt-creator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qbsprojectmanagerplugin.cpp
793 lines (669 loc) · 31.4 KB
/
qbsprojectmanagerplugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "qbsprojectmanagerplugin.h"
#include "qbsbuildconfiguration.h"
#include "qbsbuildstep.h"
#include "qbscleanstep.h"
#include "qbseditor.h"
#include "qbsinstallstep.h"
#include "qbsnodes.h"
#include "qbsprofilessettingspage.h"
#include "qbsproject.h"
#include "qbsprojectmanagerconstants.h"
#include "qbsprojectmanagertr.h"
#include "qbssession.h"
#include "qbssettings.h"
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/featureprovider.h>
#include <coreplugin/helpmanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/idocument.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/session.h>
#include <extensionsystem/iplugin.h>
#include <projectexplorer/buildmanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectexplorericons.h>
#include <projectexplorer/projectmanager.h>
#include <projectexplorer/projecttree.h>
#include <projectexplorer/runconfigurationaspects.h>
#include <projectexplorer/runcontrol.h>
#include <projectexplorer/target.h>
#include <utils/action.h>
#include <utils/fsengine/fileiconprovider.h>
#include <utils/mimeconstants.h>
#include <utils/qtcprocess.h>
#include <utils/qtcassert.h>
#include <utils/utilsicons.h>
#include <QMenu>
#if defined(Q_OS_WIN)
#include <Windows.h>
#endif
using namespace ProjectExplorer;
using namespace Utils;
namespace QbsProjectManager::Internal {
static Node *currentEditorNode()
{
Core::IDocument *doc = Core::EditorManager::currentDocument();
return doc ? ProjectTree::nodeForFile(doc->filePath()) : nullptr;
}
static QbsProject *currentEditorProject()
{
Core::IDocument *doc = Core::EditorManager::currentDocument();
return doc ? qobject_cast<QbsProject *>(ProjectManager::projectForFile(doc->filePath())) : nullptr;
}
static void windowsStartProcessDetached(FilePath appPath, QStringList args) {
// note: QProcess cannot launch devenv.exe correctly. Default escape codes don't work here
#if defined(Q_OS_WIN)
auto startupInfo = STARTUPINFOW{sizeof(STARTUPINFOW)};
auto processInfo = PROCESS_INFORMATION{};
auto cmdLine = QString{appPath.fileName() + ' ' + args.join(" ")};
CreateProcessW(reinterpret_cast<const wchar_t *>(appPath.nativePath().utf16()),
reinterpret_cast<wchar_t *>(const_cast<ushort *>(cmdLine.utf16())),
nullptr, // process attributes
nullptr, // thread attributes
false, // inherit handles
DETACHED_PROCESS,
nullptr, // environment
nullptr, // current directory
&startupInfo,
&processInfo);
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
#else
(void)appPath;
(void)args;
#endif
}
class QbsProjectManagerPluginPrivate
{
public:
QbsBuildConfigurationFactory buildConfigFactory;
QbsBuildStepFactory buildStepFactory;
QbsCleanStepFactory cleanStepFactory;
QbsInstallStepFactory installStepFactory;
QbsSettingsPage settingsPage;
QbsProfilesSettingsPage profilesSetttingsPage;
QbsEditorFactory editorFactory;
};
class QbsProjectManagerPlugin final : public ExtensionSystem::IPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "QbsProjectManager.json")
public:
static void runStepsForProducts(QbsProject *project,
const QStringList &products,
const QList<Id> &stepTypes);
private:
~QbsProjectManagerPlugin() final;
void initialize() final;
void targetWasAdded(ProjectExplorer::Target *target);
void projectChanged(QbsProject *project);
void generateVs2022Project();
void debugWithVs2022Project();
void buildFileContextMenu();
void buildFile();
void buildProductContextMenu();
void cleanProductContextMenu();
void rebuildProductContextMenu();
void runStepsForProductContextMenu(const QList<Id> &stepTypes);
void buildProduct();
void cleanProduct();
void rebuildProduct();
void runStepsForProduct(const QList<Id> &stepTypes);
void buildSubprojectContextMenu();
void cleanSubprojectContextMenu();
void rebuildSubprojectContextMenu();
void runStepsForSubprojectContextMenu(const QList<Id> &stepTypes);
void reparseSelectedProject();
void reparseCurrentProject();
void reparseProject(QbsProject *project);
void updateContextActions(ProjectExplorer::Node *node);
void updateReparseQbsAction();
void updateBuildActions();
void buildFiles(QbsProject *project, const QStringList &files,
const QStringList &activeFileTags);
void buildSingleFile(QbsProject *project, const QString &file);
QbsProjectManagerPluginPrivate *d = nullptr;
QAction *m_reparseQbs = nullptr;
QAction *m_reparseQbsCtx = nullptr;
QAction *m_buildFileCtx = nullptr;
QAction *m_buildProductCtx = nullptr;
QAction *m_cleanProductCtx = nullptr;
QAction *m_rebuildProductCtx = nullptr;
QAction *m_buildSubprojectCtx = nullptr;
QAction *m_cleanSubprojectCtx = nullptr;
QAction *m_rebuildSubprojectCtx = nullptr;
Action *m_buildFile = nullptr;
Action *m_buildProduct = nullptr;
QAction *m_menuAction = nullptr;
QAction *m_generateVs2022Ctx = nullptr;
QAction *m_debugWithVs2022Ctx = nullptr;
QAction *m_cleanProduct = nullptr;
QAction *m_rebuildProduct = nullptr;
};
QbsProjectManagerPlugin::~QbsProjectManagerPlugin()
{
delete d;
}
void QbsProjectManagerPlugin::initialize()
{
d = new QbsProjectManagerPluginPrivate;
const Core::Context projectContext(::QbsProjectManager::Constants::PROJECT_ID);
Utils::FileIconProvider::registerIconOverlayForSuffix(ProjectExplorer::Constants::FILEOVERLAY_QT, "qbs");
Core::HelpManager::registerDocumentation({Core::HelpManager::documentationPath() + "/qbs.qch"});
ProjectManager::registerProjectType<QbsProject>(Utils::Constants::QBS_MIMETYPE);
//menus
// Build Menu:
Core::ActionContainer *mbuild =
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
// PE Context menu for projects
Core::ActionContainer *mproject =
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_PROJECTCONTEXT);
Core::ActionContainer *msubproject =
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT);
Core::ActionContainer *mfile =
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_FILECONTEXT);
Core::ActionContainer *toolsContainer =
Core::ActionManager::actionContainer(Core::Constants::M_TOOLS);
Core::ActionContainer *qbsContainer = Core::ActionManager::createMenu("Qbs");
qbsContainer->menu()->setTitle(tr("&Qbs"));
toolsContainer->addMenu(qbsContainer);
m_menuAction = qbsContainer->menu()->menuAction();
//register actions
Core::Command *command;
m_generateVs2022Ctx = new QAction(Tr::tr("Generate VisualStudio2022 Project"), this);
command = Core::ActionManager::registerAction(m_generateVs2022Ctx,
"Qbs.GenerateVisualStudio2022",
projectContext);
qbsContainer->addAction(command);
connect(m_generateVs2022Ctx,
&QAction::triggered,
this,
&QbsProjectManagerPlugin::generateVs2022Project);
m_debugWithVs2022Ctx = new QAction(Tr::tr("Debug Target with VisualStudio2022"), this);
command = Core::ActionManager::registerAction(m_debugWithVs2022Ctx, "Qbs.DebugWithVisualStudio2022", projectContext);
qbsContainer->addAction(command);
connect(m_debugWithVs2022Ctx, &QAction::triggered, this, &QbsProjectManagerPlugin::debugWithVs2022Project);
m_reparseQbs = new QAction(Tr::tr("Reparse Qbs"), this);
command = Core::ActionManager::registerAction(m_reparseQbs, Constants::ACTION_REPARSE_QBS, projectContext);
command->setAttribute(Core::Command::CA_Hide);
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD);
connect(m_reparseQbs, &QAction::triggered,
this, &QbsProjectManagerPlugin::reparseCurrentProject);
connect(Core::EditorManager::instance(), &Core::EditorManager::openWithVisualStudio, this, [this](const Utils::FilePath &path) {
RunConfiguration* rc = ProjectManager::startupRunConfiguration();
if (!rc)
return;
if (const auto envAspect = rc->aspect<EnvironmentAspect>()) {
const auto devEnv = envAspect->environment().searchInPath(QLatin1String("devenv.exe"));
if (devEnv.isEmpty())
return;
windowsStartProcessDetached(devEnv, QStringList{} << "/edit" << QStringLiteral("\"%1\"").arg(path.nativePath()));
}
});
m_reparseQbsCtx = new QAction(Tr::tr("Reparse Qbs"), this);
command = Core::ActionManager::registerAction(m_reparseQbsCtx, Constants::ACTION_REPARSE_QBS_CONTEXT, projectContext);
command->setAttribute(Core::Command::CA_Hide);
mproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
connect(m_reparseQbsCtx, &QAction::triggered,
this, &QbsProjectManagerPlugin::reparseSelectedProject);
m_buildFileCtx = new QAction(Tr::tr("Build File"), this);
command = Core::ActionManager::registerAction(m_buildFileCtx, Constants::ACTION_BUILD_FILE_CONTEXT, projectContext);
command->setAttribute(Core::Command::CA_Hide);
mfile->addAction(command, ProjectExplorer::Constants::G_FILE_OTHER);
connect(m_buildFileCtx, &QAction::triggered,
this, &QbsProjectManagerPlugin::buildFileContextMenu);
m_buildFile = new Utils::Action(Tr::tr("Build File"), Tr::tr("Build File \"%1\""),
Utils::Action::AlwaysEnabled, this);
command = Core::ActionManager::registerAction(m_buildFile, Constants::ACTION_BUILD_FILE);
command->setAttribute(Core::Command::CA_Hide);
command->setAttribute(Core::Command::CA_UpdateText);
command->setDescription(m_buildFile->text());
command->setDefaultKeySequence(QKeySequence(Tr::tr("Ctrl+Alt+B")));
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_FILE);
connect(m_buildFile, &QAction::triggered, this, &QbsProjectManagerPlugin::buildFile);
m_buildProductCtx = new QAction(Tr::tr("Build"), this);
command = Core::ActionManager::registerAction(m_buildProductCtx, Constants::ACTION_BUILD_PRODUCT_CONTEXT, projectContext);
command->setAttribute(Core::Command::CA_Hide);
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
connect(m_buildProductCtx, &QAction::triggered,
this, &QbsProjectManagerPlugin::buildProductContextMenu);
m_buildProduct = new Utils::Action(Tr::tr("Build Product"), Tr::tr("Build Product \"%1\""),
Utils::Action::AlwaysEnabled, this);
command = Core::ActionManager::registerAction(m_buildProduct, Constants::ACTION_BUILD_PRODUCT);
command->setAttribute(Core::Command::CA_Hide);
command->setAttribute(Core::Command::CA_UpdateText);
command->setDescription(m_buildFile->text());
command->setDefaultKeySequence(QKeySequence(Tr::tr("Ctrl+Alt+Shift+B")));
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_PRODUCT);
connect(m_buildProduct, &QAction::triggered, this, &QbsProjectManagerPlugin::buildProduct);
m_cleanProductCtx = new QAction(Tr::tr("Clean"), this);
command = Core::ActionManager::registerAction(
m_cleanProductCtx, Constants::ACTION_CLEAN_PRODUCT_CONTEXT, projectContext);
command->setAttribute(Core::Command::CA_Hide);
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
connect(m_cleanProductCtx, &QAction::triggered,
this, &QbsProjectManagerPlugin::cleanProductContextMenu);
m_cleanProduct = new QAction(Utils::Icons::CLEAN.icon(), Tr::tr("Clean"), this);
m_cleanProduct->setWhatsThis(Tr::tr("Clean Product"));
command = Core::ActionManager::registerAction(m_cleanProduct, Constants::ACTION_CLEAN_PRODUCT);
command->setAttribute(Core::Command::CA_Hide);
command->setAttribute(Core::Command::CA_UpdateText);
command->setDescription(m_cleanProduct->whatsThis());
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_PRODUCT);
connect(m_cleanProduct, &QAction::triggered, this, &QbsProjectManagerPlugin::cleanProduct);
m_rebuildProductCtx = new QAction(Tr::tr("Rebuild"), this);
command = Core::ActionManager::registerAction(
m_rebuildProductCtx, Constants::ACTION_REBUILD_PRODUCT_CONTEXT, projectContext);
command->setAttribute(Core::Command::CA_Hide);
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
connect(m_rebuildProductCtx, &QAction::triggered,
this, &QbsProjectManagerPlugin::rebuildProductContextMenu);
m_rebuildProduct = new QAction(ProjectExplorer::Icons::REBUILD.icon(), Tr::tr("Rebuild"), this);
m_rebuildProduct->setWhatsThis(Tr::tr("Rebuild Product"));
command = Core::ActionManager::registerAction(m_rebuildProduct,
Constants::ACTION_REBUILD_PRODUCT);
command->setAttribute(Core::Command::CA_Hide);
command->setAttribute(Core::Command::CA_UpdateText);
command->setDescription(m_rebuildProduct->whatsThis());
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_PRODUCT);
connect(m_rebuildProduct, &QAction::triggered, this, &QbsProjectManagerPlugin::rebuildProduct);
m_buildSubprojectCtx = new QAction(Tr::tr("Build"), this);
command = Core::ActionManager::registerAction(m_buildSubprojectCtx, Constants::ACTION_BUILD_SUBPROJECT_CONTEXT, projectContext);
command->setAttribute(Core::Command::CA_Hide);
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
connect(m_buildSubprojectCtx, &QAction::triggered,
this, &QbsProjectManagerPlugin::buildSubprojectContextMenu);
m_cleanSubprojectCtx = new QAction(Tr::tr("Clean"), this);
command = Core::ActionManager::registerAction(
m_cleanSubprojectCtx, Constants::ACTION_CLEAN_SUBPROJECT_CONTEXT, projectContext);
command->setAttribute(Core::Command::CA_Hide);
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
connect(m_cleanSubprojectCtx, &QAction::triggered,
this, &QbsProjectManagerPlugin::cleanSubprojectContextMenu);
m_rebuildSubprojectCtx = new QAction(Tr::tr("Rebuild"), this);
command = Core::ActionManager::registerAction(
m_rebuildSubprojectCtx, Constants::ACTION_REBUILD_SUBPROJECT_CONTEXT,
projectContext);
command->setAttribute(Core::Command::CA_Hide);
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
connect(m_rebuildSubprojectCtx, &QAction::triggered,
this, &QbsProjectManagerPlugin::rebuildSubprojectContextMenu);
// Connect
connect(ProjectTree::instance(), &ProjectTree::currentNodeChanged,
this, &QbsProjectManagerPlugin::updateContextActions);
connect(BuildManager::instance(), &BuildManager::buildStateChanged,
this, std::bind(&QbsProjectManagerPlugin::projectChanged, this, nullptr));
connect(Core::EditorManager::instance(), &Core::EditorManager::currentEditorChanged,
this, &QbsProjectManagerPlugin::updateBuildActions);
connect(ProjectManager::instance(), &ProjectManager::targetAdded,
this, &QbsProjectManagerPlugin::targetWasAdded);
connect(ProjectManager::instance(), &ProjectManager::targetRemoved,
this, &QbsProjectManagerPlugin::updateBuildActions);
connect(ProjectManager::instance(), &ProjectManager::startupProjectChanged,
this, &QbsProjectManagerPlugin::updateReparseQbsAction);
connect(ProjectManager::instance(), &ProjectManager::projectAdded,
this, [this](Project *project) {
auto qbsProject = qobject_cast<QbsProject *>(project);
connect(project, &Project::anyParsingStarted,
this, std::bind(&QbsProjectManagerPlugin::projectChanged, this, qbsProject));
connect(project, &Project::anyParsingFinished,
this, std::bind(&QbsProjectManagerPlugin::projectChanged, this, qbsProject));
});
// Run initial setup routines
updateContextActions(ProjectTree::currentNode());
updateReparseQbsAction();
updateBuildActions();
}
void QbsProjectManagerPlugin::targetWasAdded(Target *target)
{
if (!qobject_cast<QbsProject *>(target->project()))
return;
connect(target, &Target::parsingStarted,
this, std::bind(&QbsProjectManagerPlugin::projectChanged, this, nullptr));
connect(target, &Target::parsingFinished,
this, std::bind(&QbsProjectManagerPlugin::projectChanged, this, nullptr));
}
void QbsProjectManagerPlugin::updateContextActions(Node *node)
{
auto project = qobject_cast<Internal::QbsProject *>(ProjectTree::currentProject());
bool isEnabled = !BuildManager::isBuilding(project)
&& project && project->activeTarget()
&& !project->activeTarget()->buildSystem()->isParsing()
&& node && node->isEnabled();
const bool isFile = project && node && node->asFileNode();
const bool isProduct = project && node && dynamic_cast<const QbsProductNode *>(node);
const auto subproject = dynamic_cast<const QbsProjectNode *>(node);
bool isSubproject = project && subproject && subproject != project->rootProjectNode();
m_reparseQbsCtx->setEnabled(isEnabled);
m_buildFileCtx->setEnabled(isEnabled && isFile);
m_buildProductCtx->setVisible(isEnabled && isProduct);
m_cleanProductCtx->setVisible(isEnabled && isProduct);
m_rebuildProductCtx->setVisible(isEnabled && isProduct);
m_buildSubprojectCtx->setVisible(isEnabled && isSubproject);
m_cleanSubprojectCtx->setVisible(isEnabled && isSubproject);
m_rebuildSubprojectCtx->setVisible(isEnabled && isSubproject);
}
void QbsProjectManagerPlugin::updateReparseQbsAction()
{
auto project = qobject_cast<QbsProject *>(ProjectManager::startupProject());
m_reparseQbs->setEnabled(project
&& !BuildManager::isBuilding(project)
&& project && project->activeTarget()
&& !project->activeTarget()->buildSystem()->isParsing());
m_generateVs2022Ctx->setEnabled(m_reparseQbs->isEnabled());
}
void QbsProjectManagerPlugin::updateBuildActions()
{
bool enabled = false;
bool fileVisible = false;
bool productVisible = false;
QString fileName;
QString productName;
if (Node *editorNode = currentEditorNode()) {
fileName = editorNode->filePath().fileName();
ProjectNode *parentProjectNode = editorNode->parentProjectNode();
const QbsProductNode *productNode = nullptr;
for (const ProjectNode *potentialProductNode = parentProjectNode;
potentialProductNode && !productNode;
potentialProductNode = potentialProductNode->parentProjectNode()) {
productNode = dynamic_cast<const QbsProductNode *>(potentialProductNode);
}
if (productNode) {
productVisible = true;
productName = productNode->displayName();
}
if (QbsProject *editorProject = currentEditorProject()) {
enabled = !BuildManager::isBuilding(editorProject)
&& editorProject->activeTarget()
&& !editorProject->activeTarget()->buildSystem()->isParsing();
fileVisible = productNode
|| dynamic_cast<QbsProjectNode *>(parentProjectNode)
|| dynamic_cast<QbsGroupNode *>(parentProjectNode);
}
}
m_buildFile->setEnabled(enabled);
m_buildFile->setVisible(fileVisible);
m_buildFile->setParameter(fileName);
m_buildProduct->setEnabled(enabled);
m_buildProduct->setVisible(productVisible);
m_buildProduct->setParameter(productName);
m_cleanProduct->setEnabled(enabled);
m_cleanProduct->setVisible(productVisible);
m_rebuildProduct->setEnabled(enabled);
m_rebuildProduct->setVisible(productVisible);
}
void QbsProjectManagerPlugin::projectChanged(QbsProject *project)
{
auto qbsProject = qobject_cast<QbsProject *>(project);
if (!qbsProject || qbsProject == ProjectManager::startupProject())
updateReparseQbsAction();
if (!qbsProject || qbsProject == ProjectTree::currentProject())
updateContextActions(ProjectTree::currentNode());
if (!qbsProject || qbsProject == currentEditorProject())
updateBuildActions();
}
void QbsProjectManagerPlugin::generateVs2022Project()
{
QbsProject *project = qobject_cast<QbsProject *>(ProjectManager::startupProject());
if (!project)
return;
Target *target = project->activeTarget();
if (!target)
return;
QbsBuildSystem *bs = static_cast<QbsBuildSystem *>(target->buildSystem());
if (!bs)
return;
auto *bc = static_cast<QbsBuildConfiguration *>(target->activeBuildConfiguration());
if (!bc)
return;
auto commandLine = Utils::CommandLine{QbsSettings::qbsExecutableFilePath()};
commandLine.addArg("generate");
commandLine.addArgs({"-g", "visualstudio2022"});
commandLine.addArgs(
{"-d", QDir::toNativeSeparators((bc->buildDirectory() / "vs2022").toString())});
commandLine.addArgs({"-f", QDir::toNativeSeparators(project->projectFilePath().toString())});
if (QbsSettings::useCreatorSettingsDirForQbs()) {
commandLine.addArgs(
{"--settings-dir", QDir::toNativeSeparators(QbsSettings::qbsSettingsBaseDir())});
}
commandLine.addArg("config:" + QbsBuildConfiguration::buildTypeName(bc->buildType()));
const QString profileName = QbsProfileManager::profileNameForKit(target->kit());
commandLine.addArg("profile:" + profileName);
Core::MessageManager::writeSilently(
QString("Starting \"%1\"\n").arg(commandLine.toUserOutput()));
auto cmdProc = Utils::Process{};
using namespace std::chrono_literals;
auto const timeout
= 10 * 60s; // note: Qbs has to build a graph what might be slow for large projects
cmdProc.setEnvironment(Utils::Environment::systemEnvironment());
cmdProc.setWorkingDirectory(project->rootProjectDirectory());
cmdProc.setCommand(commandLine);
cmdProc.runBlocking(timeout, Utils::EventLoopMode::On);
auto output = cmdProc.allOutput();
if (!output.isEmpty()) {
Core::MessageManager::writeFlashing(output);
}
Core::MessageManager::writeSilently(cmdProc.exitMessage());
}
void QbsProjectManagerPlugin::debugWithVs2022Project()
{
RunConfiguration* rc = ProjectManager::startupRunConfiguration();
if (!rc)
return;
auto commandLine = rc->commandLine();
auto executable = commandLine.executable();
auto solution = executable.withSuffix(".sln");
{
auto buffer = QByteArray{};
{
auto textStream = QTextStream{&buffer, QIODevice::WriteOnly};
textStream
<< "Microsoft Visual Studio Solution File, Format Version 12.00\n"
<< "# Visual Studio 17\n"
<< "Project(\""<< QUuid::createUuid().toString() << "\") = \""
<< rc->buildTargetInfo().displayName << "\", \""
<< executable.fileName() <<"\", \""
<< QUuid::createUuid().toString() << "\"\n"
<< "\tProjectSection(DebuggerProjectSystem) = preProject\n"
<< "\t\tExecutable = " << executable.nativePath() << "\n"
<< "\t\tArguments = " << commandLine.arguments() << "\n";
if (const auto wdAspect = rc->aspect<WorkingDirectoryAspect>()) {
textStream << "\t\tStartingDirectory = " << wdAspect->workingDirectory().nativePath() << "\n";
}
if (const auto envAspect = rc->aspect<EnvironmentAspect>()) {
textStream << "\t\tEnvironment = " << envAspect->environment().toStringList().join('\t') << "\t\n";
}
textStream
<< "\tEndProjectSection\n"
<< "EndProject\n";
}
solution.writeFileContents(buffer);
}
if (const auto envAspect = rc->aspect<EnvironmentAspect>()) {
const auto devEnv = envAspect->environment().searchInPath(QLatin1String("devenv.exe"));
if (!devEnv.isEmpty()) {
windowsStartProcessDetached(devEnv, QStringList{} << QStringLiteral("\"%1\"").arg(solution.nativePath()));
}
}
}
void QbsProjectManagerPlugin::buildFileContextMenu()
{
const Node *node = ProjectTree::currentNode();
QTC_ASSERT(node, return);
auto project = qobject_cast<QbsProject *>(ProjectTree::currentProject());
QTC_ASSERT(project, return);
buildSingleFile(project, node->filePath().toString());
}
void QbsProjectManagerPlugin::buildFile()
{
Node *node = currentEditorNode();
QbsProject *project = currentEditorProject();
if (!project || !node)
return;
buildSingleFile(project, node->filePath().toString());
}
void QbsProjectManagerPlugin::buildProductContextMenu()
{
runStepsForProductContextMenu({Utils::Id(ProjectExplorer::Constants::BUILDSTEPS_BUILD)});
}
void QbsProjectManagerPlugin::cleanProductContextMenu()
{
runStepsForProductContextMenu({Utils::Id(ProjectExplorer::Constants::BUILDSTEPS_CLEAN)});
}
void QbsProjectManagerPlugin::rebuildProductContextMenu()
{
runStepsForProductContextMenu({Utils::Id(ProjectExplorer::Constants::BUILDSTEPS_CLEAN),
Utils::Id(ProjectExplorer::Constants::BUILDSTEPS_BUILD)});
}
void QbsProjectManagerPlugin::runStepsForProductContextMenu(const QList<Utils::Id> &stepTypes)
{
const Node *node = ProjectTree::currentNode();
QTC_ASSERT(node, return);
auto project = qobject_cast<QbsProject *>(ProjectTree::currentProject());
QTC_ASSERT(project, return);
const auto *const productNode = dynamic_cast<const QbsProductNode *>(node);
QTC_ASSERT(productNode, return);
runStepsForProducts(project, {productNode->productData().value("full-display-name").toString()},
{stepTypes});
}
void QbsProjectManagerPlugin::buildProduct()
{
runStepsForProduct({Utils::Id(ProjectExplorer::Constants::BUILDSTEPS_BUILD)});
}
void QbsProjectManagerPlugin::cleanProduct()
{
runStepsForProduct({Utils::Id(ProjectExplorer::Constants::BUILDSTEPS_CLEAN)});
}
void QbsProjectManagerPlugin::rebuildProduct()
{
runStepsForProduct({
Utils::Id(ProjectExplorer::Constants::BUILDSTEPS_CLEAN),
Utils::Id(ProjectExplorer::Constants::BUILDSTEPS_BUILD),
});
}
void QbsProjectManagerPlugin::runStepsForProduct(const QList<Utils::Id> &stepTypes)
{
Node *node = currentEditorNode();
if (!node)
return;
auto productNode = dynamic_cast<QbsProductNode *>(node->parentProjectNode());
if (!productNode)
return;
QbsProject *project = currentEditorProject();
if (!project)
return;
runStepsForProducts(project, {productNode->productData().value("full-display-name").toString()},
{stepTypes});
}
void QbsProjectManagerPlugin::buildSubprojectContextMenu()
{
runStepsForSubprojectContextMenu({Utils::Id(ProjectExplorer::Constants::BUILDSTEPS_BUILD)});
}
void QbsProjectManagerPlugin::cleanSubprojectContextMenu()
{
runStepsForSubprojectContextMenu({Utils::Id(ProjectExplorer::Constants::BUILDSTEPS_CLEAN)});
}
void QbsProjectManagerPlugin::rebuildSubprojectContextMenu()
{
runStepsForSubprojectContextMenu({Utils::Id(ProjectExplorer::Constants::BUILDSTEPS_CLEAN),
Utils::Id(ProjectExplorer::Constants::BUILDSTEPS_BUILD)});
}
void QbsProjectManagerPlugin::runStepsForSubprojectContextMenu(const QList<Utils::Id> &stepTypes)
{
const Node *node = ProjectTree::currentNode();
QTC_ASSERT(node, return);
auto project = qobject_cast<QbsProject *>(ProjectTree::currentProject());
QTC_ASSERT(project, return);
const auto subProject = dynamic_cast<const QbsProjectNode *>(node);
QTC_ASSERT(subProject, return);
QStringList toBuild;
forAllProducts(subProject->projectData(), [&toBuild](const QJsonObject &data) {
toBuild << data.value("full-display-name").toString();
});
runStepsForProducts(project, toBuild, {stepTypes});
}
void QbsProjectManagerPlugin::buildFiles(QbsProject *project, const QStringList &files,
const QStringList &activeFileTags)
{
QTC_ASSERT(project, return);
QTC_ASSERT(!files.isEmpty(), return);
Target *t = project->activeTarget();
if (!t)
return;
auto bc = qobject_cast<QbsBuildConfiguration *>(t->activeBuildConfiguration());
if (!bc)
return;
if (!ProjectExplorerPlugin::saveModifiedFiles())
return;
bc->setChangedFiles(files);
bc->setActiveFileTags(activeFileTags);
bc->setProducts(QStringList());
BuildManager::buildList(bc->buildSteps());
bc->setChangedFiles(QStringList());
bc->setActiveFileTags(QStringList());
}
void QbsProjectManagerPlugin::buildSingleFile(QbsProject *project, const QString &file)
{
buildFiles(project, QStringList(file), QStringList({"obj", "hpp"}));
}
void QbsProjectManagerPlugin::runStepsForProducts(QbsProject *project,
const QStringList &products,
const QList<Utils::Id> &stepTypes)
{
QTC_ASSERT(project, return);
QTC_ASSERT(!products.isEmpty(), return);
Target *t = project->activeTarget();
if (!t)
return;
auto bc = qobject_cast<QbsBuildConfiguration *>(t->activeBuildConfiguration());
if (!bc)
return;
if (stepTypes.contains(ProjectExplorer::Constants::BUILDSTEPS_BUILD)
&& !ProjectExplorerPlugin::saveModifiedFiles()) {
return;
}
bc->setChangedFiles(QStringList());
bc->setProducts(products);
QList<ProjectExplorer::BuildStepList *> stepLists;
for (const Utils::Id &stepType : stepTypes) {
if (stepType == ProjectExplorer::Constants::BUILDSTEPS_BUILD)
stepLists << bc->buildSteps();
else if (stepType == ProjectExplorer::Constants::BUILDSTEPS_CLEAN)
stepLists << bc->cleanSteps();
}
BuildManager::buildLists(stepLists);
bc->setProducts(QStringList());
}
void QbsProjectManagerPlugin::reparseSelectedProject()
{
reparseProject(qobject_cast<QbsProject *>(ProjectTree::currentProject()));
}
void QbsProjectManagerPlugin::reparseCurrentProject()
{
reparseProject(qobject_cast<QbsProject *>(ProjectManager::startupProject()));
}
void QbsProjectManagerPlugin::reparseProject(QbsProject *project)
{
if (!project)
return;
Target *t = project->activeTarget();
if (!t)
return;
if (auto bs = qobject_cast<QbsBuildSystem *>(t->buildSystem()))
bs->scheduleParsing();
}
void buildNamedProduct(QbsProject *project, const QString &product)
{
QbsProjectManagerPlugin::runStepsForProducts(
project, QStringList(product), {Utils::Id(ProjectExplorer::Constants::BUILDSTEPS_BUILD)});
}
} // QbsProjectManager::Internal
#include "qbsprojectmanagerplugin.moc"