qbscleanstep.cpp 11.6 KB
Newer Older
Tobias Hunger's avatar
Tobias Hunger committed
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
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "qbscleanstep.h"

#include "qbsbuildconfiguration.h"
#include "qbsproject.h"
#include "qbsprojectmanagerconstants.h"

#include "ui_qbscleanstepconfigwidget.h"

#include <projectexplorer/buildsteplist.h>
#include <projectexplorer/kit.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/target.h>
#include <utils/qtcassert.h>

static const char QBS_CLEAN_ALL[] = "Qbs.CleanAll";
static const char QBS_DRY_RUN[] = "Qbs.DryRun";
static const char QBS_KEEP_GOING[] = "Qbs.DryKeepGoing";

// --------------------------------------------------------------------
// Constants:
// --------------------------------------------------------------------

namespace QbsProjectManager {
namespace Internal {

// --------------------------------------------------------------------
// QbsCleanStep:
// --------------------------------------------------------------------

QbsCleanStep::QbsCleanStep(ProjectExplorer::BuildStepList *bsl) :
    ProjectExplorer::BuildStep(bsl, Core::Id(Constants::QBS_CLEANSTEP_ID)),
61
    m_job(0), m_showCompilerOutput(true), m_parser(0)
Tobias Hunger's avatar
Tobias Hunger committed
62
{
Tobias Hunger's avatar
Tobias Hunger committed
63
    setDisplayName(tr("Qbs Clean"));
Tobias Hunger's avatar
Tobias Hunger committed
64 65 66 67
}

QbsCleanStep::QbsCleanStep(ProjectExplorer::BuildStepList *bsl, const QbsCleanStep *other) :
    ProjectExplorer::BuildStep(bsl, Core::Id(Constants::QBS_CLEANSTEP_ID)),
68
    m_qbsCleanOptions(other->m_qbsCleanOptions), m_job(0),
Tobias Hunger's avatar
Tobias Hunger committed
69 70 71 72 73 74
    m_showCompilerOutput(other->m_showCompilerOutput), m_parser(0)
{ }

QbsCleanStep::~QbsCleanStep()
{
    cancel();
75 76 77 78
    if (m_job) {
        m_job->deleteLater();
        m_job = 0;
    }
Tobias Hunger's avatar
Tobias Hunger committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
}

bool QbsCleanStep::init()
{
    if (static_cast<QbsProject *>(project())->isParsing() || m_job)
        return false;

    QbsBuildConfiguration *bc = static_cast<QbsBuildConfiguration *>(buildConfiguration());
    if (!bc)
        bc = static_cast<QbsBuildConfiguration *>(target()->activeBuildConfiguration());

    if (!bc)
        return false;

    return true;
}

void QbsCleanStep::run(QFutureInterface<bool> &fi)
{
    m_fi = &fi;

    QbsProject *pro = static_cast<QbsProject *>(project());
101
    qbs::CleanOptions options(m_qbsCleanOptions);
Tobias Hunger's avatar
Tobias Hunger committed
102

103
    m_job = pro->clean(options);
Tobias Hunger's avatar
Tobias Hunger committed
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

    if (!m_job) {
        m_fi->reportResult(false);
        return;
    }

    m_progressBase = 0;

    connect(m_job, SIGNAL(finished(bool,qbs::AbstractJob*)), this, SLOT(cleaningDone(bool)));
    connect(m_job, SIGNAL(taskStarted(QString,int,qbs::AbstractJob*)),
            this, SLOT(handleTaskStarted(QString,int)));
    connect(m_job, SIGNAL(taskProgress(int,qbs::AbstractJob*)),
            this, SLOT(handleProgress(int)));
}

ProjectExplorer::BuildStepConfigWidget *QbsCleanStep::createConfigWidget()
{
    return new QbsCleanStepConfigWidget(this);
}

bool QbsCleanStep::runInGuiThread() const
{
    return true;
}

void QbsCleanStep::cancel()
{
    if (m_job)
        m_job->cancel();
}

bool QbsCleanStep::dryRun() const
{
Christian Kandeler's avatar
Christian Kandeler committed
137
    return m_qbsCleanOptions.dryRun();
Tobias Hunger's avatar
Tobias Hunger committed
138 139 140 141
}

bool QbsCleanStep::keepGoing() const
{
Christian Kandeler's avatar
Christian Kandeler committed
142
    return m_qbsCleanOptions.keepGoing();
Tobias Hunger's avatar
Tobias Hunger committed
143 144 145 146
}

int QbsCleanStep::maxJobs() const
{
147
    return 1;
Tobias Hunger's avatar
Tobias Hunger committed
148 149 150 151
}

bool QbsCleanStep::cleanAll() const
{
Christian Kandeler's avatar
Christian Kandeler committed
152
    return m_qbsCleanOptions.cleanType() == qbs::CleanOptions::CleanupAll;
Tobias Hunger's avatar
Tobias Hunger committed
153 154 155 156 157 158 159
}

bool QbsCleanStep::fromMap(const QVariantMap &map)
{
    if (!ProjectExplorer::BuildStep::fromMap(map))
        return false;

Christian Kandeler's avatar
Christian Kandeler committed
160 161 162 163
    m_qbsCleanOptions.setDryRun(map.value(QLatin1String(QBS_DRY_RUN)).toBool());
    m_qbsCleanOptions.setKeepGoing(map.value(QLatin1String(QBS_KEEP_GOING)).toBool());
    m_qbsCleanOptions.setCleanType(map.value(QLatin1String(QBS_CLEAN_ALL)).toBool()
            ? qbs::CleanOptions::CleanupAll : qbs::CleanOptions::CleanupTemporaries);
Tobias Hunger's avatar
Tobias Hunger committed
164 165 166 167 168 169 170

    return true;
}

QVariantMap QbsCleanStep::toMap() const
{
    QVariantMap map = ProjectExplorer::BuildStep::toMap();
Christian Kandeler's avatar
Christian Kandeler committed
171 172
    map.insert(QLatin1String(QBS_DRY_RUN), m_qbsCleanOptions.dryRun());
    map.insert(QLatin1String(QBS_KEEP_GOING), m_qbsCleanOptions.keepGoing());
173
    map.insert(QLatin1String(QBS_CLEAN_ALL),
Christian Kandeler's avatar
Christian Kandeler committed
174
               m_qbsCleanOptions.cleanType() == qbs::CleanOptions::CleanupAll);
Tobias Hunger's avatar
Tobias Hunger committed
175 176 177 178 179 180 181

    return map;
}

void QbsCleanStep::cleaningDone(bool success)
{
    // Report errors:
182 183 184
    foreach (const qbs::ErrorItem &item, m_job->error().items()) {
        createTaskAndOutput(ProjectExplorer::Task::Error, item.description(),
                            item.codeLocation().fileName(), item.codeLocation().line());
Tobias Hunger's avatar
Tobias Hunger committed
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
    }

    QTC_ASSERT(m_fi, return);
    m_fi->reportResult(success);
    m_fi = 0; // do not delete, it is not ours
    m_job->deleteLater();
    m_job = 0;

    emit finished();
}

void QbsCleanStep::handleTaskStarted(const QString &desciption, int max)
{
    Q_UNUSED(desciption);
    QTC_ASSERT(m_fi, return);
    m_progressBase = m_fi->progressValue();
    m_fi->setProgressRange(0, m_progressBase + max);
}

void QbsCleanStep::handleProgress(int value)
{
    QTC_ASSERT(m_fi, return);
    m_fi->setProgressValue(m_progressBase + value);
}

void QbsCleanStep::createTaskAndOutput(ProjectExplorer::Task::TaskType type, const QString &message, const QString &file, int line)
{
    emit addTask(ProjectExplorer::Task(type, message,
                                       Utils::FileName::fromString(file), line,
                                       ProjectExplorer::Constants::TASK_CATEGORY_COMPILE));
    emit addOutput(message, NormalOutput);
}

void QbsCleanStep::setDryRun(bool dr)
{
Christian Kandeler's avatar
Christian Kandeler committed
220
    if (m_qbsCleanOptions.dryRun() == dr)
Tobias Hunger's avatar
Tobias Hunger committed
221
        return;
Christian Kandeler's avatar
Christian Kandeler committed
222
    m_qbsCleanOptions.setDryRun(dr);
Tobias Hunger's avatar
Tobias Hunger committed
223 224 225 226 227
    emit changed();
}

void QbsCleanStep::setKeepGoing(bool kg)
{
Christian Kandeler's avatar
Christian Kandeler committed
228
    if (m_qbsCleanOptions.keepGoing() == kg)
Tobias Hunger's avatar
Tobias Hunger committed
229
        return;
Christian Kandeler's avatar
Christian Kandeler committed
230
    m_qbsCleanOptions.setKeepGoing(kg);
Tobias Hunger's avatar
Tobias Hunger committed
231 232 233 234 235
    emit changed();
}

void QbsCleanStep::setMaxJobs(int jobcount)
{
236
    Q_UNUSED(jobcount); // TODO: Remove all job count-related stuff.
Tobias Hunger's avatar
Tobias Hunger committed
237 238 239 240 241
    emit changed();
}

void QbsCleanStep::setCleanAll(bool ca)
{
242 243
    qbs::CleanOptions::CleanType newType = ca
            ? qbs::CleanOptions::CleanupAll : qbs::CleanOptions::CleanupTemporaries;
Christian Kandeler's avatar
Christian Kandeler committed
244
    if (m_qbsCleanOptions.cleanType() == newType)
Tobias Hunger's avatar
Tobias Hunger committed
245
        return;
Christian Kandeler's avatar
Christian Kandeler committed
246
    m_qbsCleanOptions.setCleanType(newType);
Tobias Hunger's avatar
Tobias Hunger committed
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
    emit changed();
}

// --------------------------------------------------------------------
// QbsCleanStepConfigWidget:
// --------------------------------------------------------------------

QbsCleanStepConfigWidget::QbsCleanStepConfigWidget(QbsCleanStep *step) :
    m_step(step)
{
    connect(m_step, SIGNAL(displayNameChanged()), this, SLOT(updateState()));
    connect(m_step, SIGNAL(changed()), this, SLOT(updateState()));

    setContentsMargins(0, 0, 0, 0);

    m_ui = new Ui::QbsCleanStepConfigWidget;
    m_ui->setupUi(this);

    connect(m_ui->cleanAllCheckBox, SIGNAL(toggled(bool)),
            this, SLOT(changeCleanAll(bool)));
    connect(m_ui->dryRunCheckBox, SIGNAL(toggled(bool)), this, SLOT(changeDryRun(bool)));
    connect(m_ui->keepGoingCheckBox, SIGNAL(toggled(bool)), this, SLOT(changeKeepGoing(bool)));

    updateState();
}

QString QbsCleanStepConfigWidget::summaryText() const
{
    return m_summary;
}

QString QbsCleanStepConfigWidget::displayName() const
{
    return m_step->displayName();
}

void QbsCleanStepConfigWidget::updateState()
{
    m_ui->cleanAllCheckBox->setChecked(m_step->cleanAll());
    m_ui->dryRunCheckBox->setChecked(m_step->dryRun());
    m_ui->keepGoingCheckBox->setChecked(m_step->keepGoing());

    QString command = QLatin1String("qbs clean ");
    if (m_step->dryRun())
Tobias Hunger's avatar
Tobias Hunger committed
291
        command += QLatin1String("--dry-run ");
Tobias Hunger's avatar
Tobias Hunger committed
292
    if (m_step->keepGoing())
Tobias Hunger's avatar
Tobias Hunger committed
293
        command += QLatin1String("--keep-going ");
Tobias Hunger's avatar
Tobias Hunger committed
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
    if (m_step->cleanAll())
        command += QLatin1String(" --all-artifacts");

    QString summary = tr("<b>Qbs:</b> %1").arg(command);
    if (m_summary !=  summary) {
        m_summary = summary;
        emit updateSummary();
    }
}

void QbsCleanStepConfigWidget::changeCleanAll(bool ca)
{
    m_step->setCleanAll(ca);
}

void QbsCleanStepConfigWidget::changeDryRun(bool dr)
{
    m_step->setDryRun(dr);
}

void QbsCleanStepConfigWidget::changeKeepGoing(bool kg)
{
    m_step->setKeepGoing(kg);
}

void QbsCleanStepConfigWidget::changeJobCount(int count)
{
    m_step->setMaxJobs(count);
}

// --------------------------------------------------------------------
// QbsCleanStepFactory:
// --------------------------------------------------------------------

QbsCleanStepFactory::QbsCleanStepFactory(QObject *parent) :
    ProjectExplorer::IBuildStepFactory(parent)
{ }

QList<Core::Id> QbsCleanStepFactory::availableCreationIds(ProjectExplorer::BuildStepList *parent) const
{
    if (parent->id() == ProjectExplorer::Constants::BUILDSTEPS_CLEAN
            && qobject_cast<QbsBuildConfiguration *>(parent->parent()))
        return QList<Core::Id>() << Core::Id(Constants::QBS_CLEANSTEP_ID);
    return QList<Core::Id>();
}

QString QbsCleanStepFactory::displayNameForId(const Core::Id id) const
{
    if (id == Core::Id(Constants::QBS_CLEANSTEP_ID))
Tobias Hunger's avatar
Tobias Hunger committed
343
        return tr("Qbs Clean");
Tobias Hunger's avatar
Tobias Hunger committed
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
    return QString();
}

bool QbsCleanStepFactory::canCreate(ProjectExplorer::BuildStepList *parent, const Core::Id id) const
{
    if (parent->id() != Core::Id(ProjectExplorer::Constants::BUILDSTEPS_CLEAN)
            || !qobject_cast<QbsBuildConfiguration *>(parent->parent()))
        return false;
    return id == Core::Id(Constants::QBS_CLEANSTEP_ID);
}

ProjectExplorer::BuildStep *QbsCleanStepFactory::create(ProjectExplorer::BuildStepList *parent, const Core::Id id)
{
    if (!canCreate(parent, id))
        return 0;
    return new QbsCleanStep(parent);
}

bool QbsCleanStepFactory::canRestore(ProjectExplorer::BuildStepList *parent, const QVariantMap &map) const
{
    return canCreate(parent, ProjectExplorer::idFromMap(map));
}

ProjectExplorer::BuildStep *QbsCleanStepFactory::restore(ProjectExplorer::BuildStepList *parent, const QVariantMap &map)
{
    if (!canRestore(parent, map))
        return 0;
    QbsCleanStep *bs = new QbsCleanStep(parent);
    if (!bs->fromMap(map)) {
        delete bs;
        return 0;
    }
    return bs;
}

bool QbsCleanStepFactory::canClone(ProjectExplorer::BuildStepList *parent, ProjectExplorer::BuildStep *product) const
{
    return canCreate(parent, product->id());
}

ProjectExplorer::BuildStep *QbsCleanStepFactory::clone(ProjectExplorer::BuildStepList *parent, ProjectExplorer::BuildStep *product)
{
    if (!canClone(parent, product))
        return 0;
    return new QbsCleanStep(parent, static_cast<QbsCleanStep *>(product));
}

} // namespace Internal
} // namespace QbsProjectManager