1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "qbscleanstep.h"
27 
28 #include "qbsbuildconfiguration.h"
29 #include "qbsproject.h"
30 #include "qbsprojectmanagerconstants.h"
31 #include "qbssession.h"
32 
33 #include <projectexplorer/buildsteplist.h>
34 #include <projectexplorer/kit.h>
35 #include <projectexplorer/projectexplorerconstants.h>
36 #include <projectexplorer/buildmanager.h>
37 #include <projectexplorer/target.h>
38 #include <utils/qtcassert.h>
39 
40 #include <QJsonArray>
41 #include <QJsonObject>
42 
43 using namespace ProjectExplorer;
44 using namespace Utils;
45 
46 namespace QbsProjectManager {
47 namespace Internal {
48 
49 // --------------------------------------------------------------------
50 // QbsCleanStep:
51 // --------------------------------------------------------------------
52 
QbsCleanStep(BuildStepList * bsl,Utils::Id id)53 QbsCleanStep::QbsCleanStep(BuildStepList *bsl, Utils::Id id)
54     : BuildStep(bsl, id)
55 {
56     setDisplayName(tr("Qbs Clean"));
57 
58     m_dryRunAspect = addAspect<BoolAspect>();
59     m_dryRunAspect->setSettingsKey("Qbs.DryRun");
60     m_dryRunAspect->setLabel(tr("Dry run:"), BoolAspect::LabelPlacement::InExtraLabel);
61 
62     m_keepGoingAspect = addAspect<BoolAspect>();
63     m_keepGoingAspect->setSettingsKey("Qbs.DryKeepGoing");
64     m_keepGoingAspect->setLabel(tr("Keep going:"), BoolAspect::LabelPlacement::InExtraLabel);
65 
66     auto effectiveCommandAspect = addAspect<StringAspect>();
67     effectiveCommandAspect->setDisplayStyle(StringAspect::TextEditDisplay);
68     effectiveCommandAspect->setLabelText(tr("Equivalent command line:"));
69 
70     setSummaryUpdater([this, effectiveCommandAspect] {
71         QbsBuildStepData data;
72         data.command = "clean";
73         data.dryRun = m_dryRunAspect->value();
74         data.keepGoing = m_keepGoingAspect->value();
75         QString command = static_cast<QbsBuildConfiguration *>(buildConfiguration())
76                  ->equivalentCommandLine(data);
77         effectiveCommandAspect->setValue(command);
78         return tr("<b>Qbs:</b> %1").arg("clean");
79     });
80 }
81 
~QbsCleanStep()82 QbsCleanStep::~QbsCleanStep()
83 {
84     doCancel();
85     if (m_session)
86         m_session->disconnect(this);
87 }
88 
dropSession()89 void QbsCleanStep::dropSession()
90 {
91     if (m_session) {
92         doCancel();
93         m_session->disconnect(this);
94         m_session = nullptr;
95     }
96 }
97 
init()98 bool QbsCleanStep::init()
99 {
100     if (buildSystem()->isParsing() || m_session)
101         return false;
102     const auto bc = static_cast<QbsBuildConfiguration *>(buildConfiguration());
103     if (!bc)
104         return false;
105     m_products = bc->products();
106     return true;
107 }
108 
doRun()109 void QbsCleanStep::doRun()
110 {
111     m_session = static_cast<QbsBuildSystem*>(buildSystem())->session();
112     if (!m_session) {
113         emit addOutput(tr("No qbs session exists for this target."), OutputFormat::ErrorMessage);
114         emit finished(false);
115         return;
116     }
117 
118     QJsonObject request;
119     request.insert("type", "clean-project");
120     if (!m_products.isEmpty())
121         request.insert("products", QJsonArray::fromStringList(m_products));
122     request.insert("dry-run", m_dryRunAspect->value());
123     request.insert("keep-going", m_keepGoingAspect->value());
124     m_session->sendRequest(request);
125     m_maxProgress = 0;
126     connect(m_session, &QbsSession::projectCleaned, this, &QbsCleanStep::cleaningDone);
127     connect(m_session, &QbsSession::taskStarted, this, &QbsCleanStep::handleTaskStarted);
128     connect(m_session, &QbsSession::taskProgress, this, &QbsCleanStep::handleProgress);
129     connect(m_session, &QbsSession::errorOccurred, this, [this] {
130         cleaningDone(ErrorInfo(tr("Cleaning canceled: Qbs session failed.")));
131     });
132 }
133 
doCancel()134 void QbsCleanStep::doCancel()
135 {
136     if (m_session)
137         m_session->cancelCurrentJob();
138 }
139 
cleaningDone(const ErrorInfo & error)140 void QbsCleanStep::cleaningDone(const ErrorInfo &error)
141 {
142     m_session->disconnect(this);
143     m_session = nullptr;
144 
145     for (const ErrorInfoItem &item : error.items)
146         createTaskAndOutput(Task::Error, item.description, item.filePath.toString(), item.line);
147     emit finished(!error.hasError());
148 }
149 
handleTaskStarted(const QString & desciption,int max)150 void QbsCleanStep::handleTaskStarted(const QString &desciption, int max)
151 {
152     Q_UNUSED(desciption)
153     m_maxProgress = max;
154 }
155 
handleProgress(int value)156 void QbsCleanStep::handleProgress(int value)
157 {
158     if (m_maxProgress > 0)
159         emit progress(value * 100 / m_maxProgress, m_description);
160 }
161 
createTaskAndOutput(ProjectExplorer::Task::TaskType type,const QString & message,const QString & file,int line)162 void QbsCleanStep::createTaskAndOutput(ProjectExplorer::Task::TaskType type, const QString &message, const QString &file, int line)
163 {
164     emit addOutput(message, OutputFormat::Stdout);
165     emit addTask(CompileTask(type, message, Utils::FilePath::fromString(file), line), 1);
166 }
167 
168 // --------------------------------------------------------------------
169 // QbsCleanStepFactory:
170 // --------------------------------------------------------------------
171 
QbsCleanStepFactory()172 QbsCleanStepFactory::QbsCleanStepFactory()
173 {
174     registerStep<QbsCleanStep>(Constants::QBS_CLEANSTEP_ID);
175     setSupportedStepList(ProjectExplorer::Constants::BUILDSTEPS_CLEAN);
176     setSupportedConfiguration(Constants::QBS_BC_ID);
177     setDisplayName(QbsCleanStep::tr("Qbs Clean"));
178 }
179 
180 } // namespace Internal
181 } // namespace QbsProjectManager
182