aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/winrt/winrtpackagedeploymentstep.cpp
blob: 1a45fb92a082ef1a2cdb141e44dc4c6b5bcad8b8 (plain)
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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "winrtpackagedeploymentstep.h"

#include "winrtconstants.h"

#include <projectexplorer/abstractprocessstep.h>
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/buildtargetinfo.h>
#include <projectexplorer/deployablefile.h>
#include <projectexplorer/deploymentdata.h>
#include <projectexplorer/processparameters.h>
#include <projectexplorer/projectconfigurationaspects.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/project.h>
#include <projectexplorer/runconfiguration.h>
#include <projectexplorer/target.h>

#include <qtsupport/qtkitinformation.h>

#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>
#include <utils/fancylineedit.h>

#include <QLabel>
#include <QLayout>
#include <QRegularExpression>
#include <QToolButton>

using namespace ProjectExplorer;
using namespace Utils;

namespace WinRt {
namespace Internal {

const char ARGUMENTS_KEY[] = "WinRt.BuildStep.Deploy.Arguments";
const char DEFAULTARGUMENTS_KEY[] = "WinRt.BuildStep.Deploy.DefaultArguments";

class WinRtArgumentsAspect final : public ProjectConfigurationAspect
{
    Q_DECLARE_TR_FUNCTIONS(WinRt::Internal::WinRtArgumentsAspect)

public:
    WinRtArgumentsAspect() = default;

    void addToLayout(LayoutBuilder &builder) final;

    void fromMap(const QVariantMap &map) final;
    void toMap(QVariantMap &map) const final;

    void setValue(const QString &value);
    QString value() const { return m_value; }

    void setDefaultValue(const QString &value) { m_defaultValue = value; }
    QString defaultValue() const { return m_defaultValue; }

    void restoreDefaultValue();

private:
    FancyLineEdit *m_lineEdit = nullptr;
    QString m_value;
    QString m_defaultValue;
};

class WinRtPackageDeploymentStep final : public AbstractProcessStep
{
    Q_DECLARE_TR_FUNCTIONS(WinRt::Internal::WinRtPackageDeploymentStep)

public:
    WinRtPackageDeploymentStep(BuildStepList *bsl, Utils::Id id);

    QString defaultWinDeployQtArguments() const;

    void raiseError(const QString &errorMessage);
    void raiseWarning(const QString &warningMessage);

private:
    bool init() override;
    void doRun() override;
    bool processSucceeded(int exitCode, QProcess::ExitStatus status) override;
    void stdOutput(const QString &line) override;

    bool parseIconsAndExecutableFromManifest(QString manifestFileName, QStringList *items, QString *executable);

    WinRtArgumentsAspect *m_argsAspect = nullptr;
    QString m_targetFilePath;
    QString m_targetDirPath;
    QString m_executablePathInManifest;
    QString m_mappingFileContent;
    QString m_manifestFileName;
    bool m_createMappingFile = false;
};

void WinRtArgumentsAspect::addToLayout(LayoutBuilder &builder)
{
    QTC_CHECK(!m_lineEdit);
    auto label = new QLabel(tr("Arguments:"));
    label->setTextInteractionFlags(Qt::TextSelectableByMouse);
    builder.addItem(label);

    auto *layout = new QHBoxLayout();
    m_lineEdit = new Utils::FancyLineEdit();
    if (!m_value.isEmpty())
        m_lineEdit->setText(m_value);
    else if (!m_defaultValue.isEmpty())
        m_lineEdit->setText(m_defaultValue);
    connect(m_lineEdit, &Utils::FancyLineEdit::textEdited,
            this, &WinRtArgumentsAspect::setValue);
    layout->addWidget(m_lineEdit);

    auto restoreDefaultButton = new QToolButton();
    restoreDefaultButton->setText(tr("Restore Default Arguments"));
    connect(restoreDefaultButton, &QToolButton::clicked,
            this, &WinRtArgumentsAspect::restoreDefaultValue);
    layout->addWidget(restoreDefaultButton);
    builder.addItem(layout);
}

void WinRtArgumentsAspect::fromMap(const QVariantMap &map)
{
    m_defaultValue = map.value(DEFAULTARGUMENTS_KEY).toString();
    m_value = map.value(ARGUMENTS_KEY).toString();
}

void WinRtArgumentsAspect::toMap(QVariantMap &map) const
{
    map.insert(DEFAULTARGUMENTS_KEY, m_defaultValue);
    map.insert(ARGUMENTS_KEY, m_value);
}

void WinRtArgumentsAspect::setValue(const QString &value)
{
    if (value == m_value)
        return;

    m_value = value;
    if (m_lineEdit)
        m_lineEdit->setText(value);
    emit changed();
}

void WinRtArgumentsAspect::restoreDefaultValue()
{
    if (m_defaultValue == m_value)
        return;

    setValue(m_defaultValue);
}

WinRtPackageDeploymentStep::WinRtPackageDeploymentStep(BuildStepList *bsl, Utils::Id id)
    : AbstractProcessStep(bsl, id)
{
    setDisplayName(tr("Run windeployqt"));

    m_argsAspect = addAspect<WinRtArgumentsAspect>();
    m_argsAspect->setDefaultValue(defaultWinDeployQtArguments());
    m_argsAspect->setValue(defaultWinDeployQtArguments());
}

bool WinRtPackageDeploymentStep::init()
{
    RunConfiguration *rc = target()->activeRunConfiguration();
    QTC_ASSERT(rc, return false);

    const BuildTargetInfo bti = rc->buildTargetInfo();
    Utils::FilePath appTargetFilePath = bti.targetFilePath;

    m_targetFilePath = appTargetFilePath.toString();
    if (m_targetFilePath.isEmpty()) {
        raiseError(tr("No executable to deploy found in %1.").arg(bti.projectFilePath.toString()));
        return false;
    }

    // ### Ideally, the file paths in applicationTargets() should already have the .exe suffix.
    // Whenever this will eventually work, we can drop appending the .exe suffix here.
    if (!m_targetFilePath.endsWith(QLatin1String(".exe"), Qt::CaseInsensitive))
        m_targetFilePath.append(QLatin1String(".exe"));

    m_targetDirPath = appTargetFilePath.parentDir().toString();
    if (!m_targetDirPath.endsWith(QLatin1Char('/')))
        m_targetDirPath += QLatin1Char('/');

    const QtSupport::BaseQtVersion *qt = QtSupport::QtKitAspect::qtVersion(target()->kit());
    if (!qt)
        return false;

    const FilePath windeployqtPath = qt->hostBinPath().resolvePath("windeployqt.exe");

    CommandLine windeployqt{windeployqtPath};
    windeployqt.addArg(QDir::toNativeSeparators(m_targetFilePath));
    windeployqt.addArgs(m_argsAspect->value(), CommandLine::Raw);

    if (qt->type() == Constants::WINRT_WINPHONEQT) {
        m_createMappingFile = true;
        windeployqt.addArgs({"-list", "mapping"});
    }

    ProcessParameters *params = processParameters();
    if (!windeployqtPath.exists()) {
        raiseError(tr("Cannot find windeployqt.exe in \"%1\".")
                       .arg(QDir::toNativeSeparators(qt->hostBinPath().toString())));
        return false;
    }
    params->setCommandLine(windeployqt);
    params->setEnvironment(buildEnvironment());

    return AbstractProcessStep::init();
}

void WinRtPackageDeploymentStep::doRun()
{
    const QtSupport::BaseQtVersion *qt = QtSupport::QtKitAspect::qtVersion(target()->kit());
    if (!qt)
        return;

    m_manifestFileName = QStringLiteral("AppxManifest");

    if (m_createMappingFile) {
        m_mappingFileContent = QLatin1String("[Files]\n");

        QDir assetDirectory(m_targetDirPath + QLatin1String("assets"));
        if (assetDirectory.exists()) {
            QStringList iconsToDeploy;
            const QString fullManifestPath = m_targetDirPath + m_manifestFileName
                    + QLatin1String(".xml");
            if (!parseIconsAndExecutableFromManifest(fullManifestPath, &iconsToDeploy,
                                                     &m_executablePathInManifest)) {
                raiseError(tr("Cannot parse manifest file %1.").arg(fullManifestPath));
                return;
            }
            foreach (const QString &icon, iconsToDeploy) {
                m_mappingFileContent += QLatin1Char('"')
                        + QDir::toNativeSeparators(m_targetDirPath + icon) + QLatin1String("\" \"")
                        + QDir::toNativeSeparators(icon) + QLatin1String("\"\n");
            }
        }
    }

    AbstractProcessStep::doRun();
}

bool WinRtPackageDeploymentStep::processSucceeded(int exitCode, QProcess::ExitStatus status)
{
    if (m_createMappingFile) {
        QString targetInstallationPath;
        // The list holds the local file paths and the "remote" file paths
        QList<QPair<QString, QString> > installableFilesList;
        foreach (DeployableFile file, target()->deploymentData().allFiles()) {
            QString remoteFilePath = file.remoteFilePath();
            while (remoteFilePath.startsWith(QLatin1Char('/')))
                remoteFilePath.remove(0, 1);
            QString localFilePath = file.localFilePath().toString();
            if (localFilePath == m_targetFilePath) {
                if (!m_targetFilePath.endsWith(QLatin1String(".exe"))) {
                    remoteFilePath += QLatin1String(".exe");
                    localFilePath += QLatin1String(".exe");
                }
                targetInstallationPath = remoteFilePath;
            }
            installableFilesList.append(QPair<QString, QString>(localFilePath, remoteFilePath));
        }

        // if there are no INSTALLS set we just deploy the files from windeployqt,
        // the icons referenced in the manifest file and the actual build target
        QString baseDir;
        if (targetInstallationPath.isEmpty()) {
            if (!m_targetFilePath.endsWith(QLatin1String(".exe")))
                m_targetFilePath.append(QLatin1String(".exe"));
            m_mappingFileContent
                    += QLatin1Char('"') + QDir::toNativeSeparators(m_targetFilePath)
                    + QLatin1String("\" \"")
                    + QDir::toNativeSeparators(m_executablePathInManifest) + QLatin1String("\"\n");
            baseDir = m_targetDirPath;
        } else {
            baseDir = targetInstallationPath.left(targetInstallationPath.lastIndexOf(QLatin1Char('/')) + 1);
        }

        typedef QPair<QString, QString> QStringPair;
        foreach (const QStringPair &pair, installableFilesList) {
            // For the mapping file we need the remote paths relative to the application's executable
            QString relativeRemotePath;
            if (QDir(pair.second).isRelative())
                relativeRemotePath = pair.second;
            else
                relativeRemotePath = QDir(baseDir).relativeFilePath(pair.second);

            if (QDir(relativeRemotePath).isAbsolute() || relativeRemotePath.startsWith(QLatin1String(".."))) {
                raiseWarning(tr("File %1 is outside of the executable's directory. These files cannot be installed.").arg(relativeRemotePath));
                continue;
            }

            m_mappingFileContent += QLatin1Char('"') + QDir::toNativeSeparators(pair.first)
                    + QLatin1String("\" \"") + QDir::toNativeSeparators(relativeRemotePath)
                    + QLatin1String("\"\n");
        }

        const QString mappingFilePath = m_targetDirPath + m_manifestFileName
                + QLatin1String(".map");
        QFile mappingFile(mappingFilePath);
        if (!mappingFile.open(QFile::WriteOnly | QFile::Text)) {
            raiseError(tr("Cannot open mapping file %1 for writing.").arg(mappingFilePath));
            return false;
        }
        mappingFile.write(m_mappingFileContent.toUtf8());
    }

    return AbstractProcessStep::processSucceeded(exitCode, status);
}

void WinRtPackageDeploymentStep::stdOutput(const QString &line)
{
    if (m_createMappingFile)
        m_mappingFileContent += line;
    AbstractProcessStep::stdOutput(line);
}

QString WinRtPackageDeploymentStep::defaultWinDeployQtArguments() const
{
    QString args;
    QtcProcess::addArg(&args, QStringLiteral("--qmldir"));
    QtcProcess::addArg(&args, project()->projectDirectory().toUserOutput());
    return args;
}

void WinRtPackageDeploymentStep::raiseError(const QString &errorMessage)
{
    emit addOutput(errorMessage, BuildStep::OutputFormat::ErrorMessage);
    emit addTask(DeploymentTask(Task::Error, errorMessage), 1);
}

void WinRtPackageDeploymentStep::raiseWarning(const QString &warningMessage)
{
    emit addOutput(warningMessage, BuildStep::OutputFormat::NormalMessage);
    emit addTask(DeploymentTask(Task::Warning, warningMessage), 1);
}

bool WinRtPackageDeploymentStep::parseIconsAndExecutableFromManifest(QString manifestFileName, QStringList *icons, QString *executable)
{
    if (!icons->isEmpty())
        icons->clear();
    QFile manifestFile(manifestFileName);
    if (!manifestFile.open(QFile::ReadOnly))
        return false;
    const QString contents = QString::fromUtf8(manifestFile.readAll());

    QRegularExpression iconPattern(QStringLiteral("[\\\\/a-zA-Z0-9_\\-\\!]*\\.(png|jpg|jpeg)"));
    QRegularExpressionMatchIterator iterator = iconPattern.globalMatch(contents);
    while (iterator.hasNext()) {
        QRegularExpressionMatch match = iterator.next();
        const QString icon = match.captured(0);
        icons->append(icon);
    }

    const QLatin1String executablePrefix(manifestFileName.contains(QLatin1String("AppxManifest")) ? "Executable=" : "ImagePath=");
    QRegularExpression executablePattern(executablePrefix + QStringLiteral("\"([a-zA-Z0-9_-]*\\.exe)\""));
    QRegularExpressionMatch match = executablePattern.match(contents);
    if (!match.hasMatch())
        return false;
    *executable = match.captured(1);

    return true;
}

// WinRtDeployStepFactory

WinRtDeployStepFactory::WinRtDeployStepFactory()
{
    registerStep<WinRtPackageDeploymentStep>(Constants::WINRT_BUILD_STEP_DEPLOY);
    setDisplayName(QCoreApplication::translate("WinRt::Internal::WinRtDeployStepFactory", "Run windeployqt"));
    setFlags(BuildStepInfo::Unclonable);
    setSupportedStepList(ProjectExplorer::Constants::BUILDSTEPS_DEPLOY);
    setSupportedDeviceTypes({Constants::WINRT_DEVICE_TYPE_LOCAL,
                             Constants::WINRT_DEVICE_TYPE_EMULATOR,
                             Constants::WINRT_DEVICE_TYPE_PHONE});
    setRepeatable(false);
}

} // namespace Internal
} // namespace WinRt