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 "abstractremotelinuxdeploystep.h"
27 
28 #include "abstractremotelinuxdeployservice.h"
29 #include "remotelinuxdeployconfiguration.h"
30 
31 #include <projectexplorer/projectexplorerconstants.h>
32 #include <projectexplorer/kitinformation.h>
33 
34 using namespace ProjectExplorer;
35 
36 namespace RemoteLinux {
37 namespace Internal {
38 
39 class AbstractRemoteLinuxDeployStepPrivate
40 {
41 public:
42     bool hasError;
43     std::function<CheckResult()> internalInit;
44     std::function<void()> runPreparer;
45     AbstractRemoteLinuxDeployService *deployService = nullptr;
46 };
47 
48 } // namespace Internal
49 
AbstractRemoteLinuxDeployStep(BuildStepList * bsl,Utils::Id id)50 AbstractRemoteLinuxDeployStep::AbstractRemoteLinuxDeployStep(BuildStepList *bsl, Utils::Id id)
51     : BuildStep(bsl, id), d(new Internal::AbstractRemoteLinuxDeployStepPrivate)
52 {
53 }
54 
setInternalInitializer(const std::function<CheckResult ()> & init)55 void AbstractRemoteLinuxDeployStep::setInternalInitializer(const std::function<CheckResult ()> &init)
56 {
57     d->internalInit = init;
58 }
59 
setRunPreparer(const std::function<void ()> & prep)60 void AbstractRemoteLinuxDeployStep::setRunPreparer(const std::function<void ()> &prep)
61 {
62     d->runPreparer = prep;
63 }
64 
setDeployService(AbstractRemoteLinuxDeployService * service)65 void AbstractRemoteLinuxDeployStep::setDeployService(AbstractRemoteLinuxDeployService *service)
66 {
67     d->deployService = service;
68 }
69 
~AbstractRemoteLinuxDeployStep()70 AbstractRemoteLinuxDeployStep::~AbstractRemoteLinuxDeployStep()
71 {
72     delete d->deployService;
73     delete d;
74 }
75 
fromMap(const QVariantMap & map)76 bool AbstractRemoteLinuxDeployStep::fromMap(const QVariantMap &map)
77 {
78     if (!BuildStep::fromMap(map))
79         return false;
80     d->deployService->importDeployTimes(map);
81     return true;
82 }
83 
toMap() const84 QVariantMap AbstractRemoteLinuxDeployStep::toMap() const
85 {
86 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
87     QVariantMap map = BuildStep::toMap();
88     map.insert(d->deployService->exportDeployTimes());
89     return map;
90 #else
91     return BuildStep::toMap().unite(d->deployService->exportDeployTimes());
92 #endif
93 }
94 
init()95 bool AbstractRemoteLinuxDeployStep::init()
96 {
97     d->deployService->setTarget(target());
98 
99     QTC_ASSERT(d->internalInit, return false);
100     const CheckResult canDeploy = d->internalInit();
101     if (!canDeploy) {
102         emit addOutput(tr("Cannot deploy: %1").arg(canDeploy.errorMessage()),
103                        OutputFormat::ErrorMessage);
104     }
105     return canDeploy;
106 }
107 
doRun()108 void AbstractRemoteLinuxDeployStep::doRun()
109 {
110     if (d->runPreparer)
111         d->runPreparer();
112 
113     connect(d->deployService, &AbstractRemoteLinuxDeployService::errorMessage,
114             this, &AbstractRemoteLinuxDeployStep::handleErrorMessage);
115     connect(d->deployService, &AbstractRemoteLinuxDeployService::progressMessage,
116             this, &AbstractRemoteLinuxDeployStep::handleProgressMessage);
117     connect(d->deployService, &AbstractRemoteLinuxDeployService::warningMessage,
118             this, &AbstractRemoteLinuxDeployStep::handleWarningMessage);
119     connect(d->deployService, &AbstractRemoteLinuxDeployService::stdOutData,
120             this, &AbstractRemoteLinuxDeployStep::handleStdOutData);
121     connect(d->deployService, &AbstractRemoteLinuxDeployService::stdErrData,
122             this, &AbstractRemoteLinuxDeployStep::handleStdErrData);
123     connect(d->deployService, &AbstractRemoteLinuxDeployService::finished,
124             this, &AbstractRemoteLinuxDeployStep::handleFinished);
125 
126     d->hasError = false;
127     d->deployService->start();
128 }
129 
doCancel()130 void AbstractRemoteLinuxDeployStep::doCancel()
131 {
132     if (d->hasError)
133         return;
134 
135     emit addOutput(tr("User requests deployment to stop; cleaning up."),
136                    OutputFormat::NormalMessage);
137     d->hasError = true;
138     d->deployService->stop();
139 }
140 
handleProgressMessage(const QString & message)141 void AbstractRemoteLinuxDeployStep::handleProgressMessage(const QString &message)
142 {
143     emit addOutput(message, OutputFormat::NormalMessage);
144 }
145 
handleErrorMessage(const QString & message)146 void AbstractRemoteLinuxDeployStep::handleErrorMessage(const QString &message)
147 {
148     emit addOutput(message, OutputFormat::ErrorMessage);
149     emit addTask(DeploymentTask(Task::Error, message), 1); // TODO correct?
150     d->hasError = true;
151 }
152 
handleWarningMessage(const QString & message)153 void AbstractRemoteLinuxDeployStep::handleWarningMessage(const QString &message)
154 {
155     emit addOutput(message, OutputFormat::ErrorMessage);
156     emit addTask(DeploymentTask(Task::Warning, message), 1); // TODO correct?
157 }
158 
handleFinished()159 void AbstractRemoteLinuxDeployStep::handleFinished()
160 {
161     if (d->hasError)
162         emit addOutput(tr("Deploy step failed."), OutputFormat::ErrorMessage);
163     else
164         emit addOutput(tr("Deploy step finished."), OutputFormat::NormalMessage);
165     disconnect(d->deployService, nullptr, this, nullptr);
166     emit finished(!d->hasError);
167 }
168 
handleStdOutData(const QString & data)169 void AbstractRemoteLinuxDeployStep::handleStdOutData(const QString &data)
170 {
171     emit addOutput(data, OutputFormat::Stdout, DontAppendNewline);
172 }
173 
handleStdErrData(const QString & data)174 void AbstractRemoteLinuxDeployStep::handleStdErrData(const QString &data)
175 {
176     emit addOutput(data, OutputFormat::Stderr, DontAppendNewline);
177 }
178 
179 } // namespace RemoteLinux
180