From a673f4f2591772eaa959fb9a24d66874e376a368 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Fri, 22 Dec 2017 17:53:39 +0100 Subject: Properly take the --dry-run option into account in the "run" command Do not try to execute the virtual binary. Change-Id: I19b0a9e7600b33cb42c4ab60f689dd4c3329930b Reviewed-by: Jake Petroules --- src/lib/corelib/api/runenvironment.cpp | 117 +++++++++++++++++++-------------- src/lib/corelib/api/runenvironment.h | 6 +- 2 files changed, 71 insertions(+), 52 deletions(-) (limited to 'src/lib/corelib/api') diff --git a/src/lib/corelib/api/runenvironment.cpp b/src/lib/corelib/api/runenvironment.cpp index 36d953572..9ca798e17 100644 --- a/src/lib/corelib/api/runenvironment.cpp +++ b/src/lib/corelib/api/runenvironment.cpp @@ -120,11 +120,11 @@ int RunEnvironment::runShell(ErrorInfo *error) } } -int RunEnvironment::runTarget(const QString &targetBin, const QStringList &arguments, +int RunEnvironment::runTarget(const QString &targetBin, const QStringList &arguments, bool dryRun, ErrorInfo *error) { try { - return doRunTarget(targetBin, arguments); + return doRunTarget(targetBin, arguments, dryRun); } catch (const ErrorInfo &e) { if (error) *error = e; @@ -263,7 +263,16 @@ static QString findMainIntent(const QString &aapt, const QString &apkFilePath) return QString(); } -int RunEnvironment::doRunTarget(const QString &targetBin, const QStringList &arguments) +void RunEnvironment::printStartInfo(const QProcess &proc, bool dryRun) +{ + QString message = dryRun ? Tr::tr("Would start target.") : Tr::tr("Starting target."); + message.append(QLatin1Char(' ')).append(Tr::tr("Full command line: %1") + .arg(shellQuote(QStringList(QDir::toNativeSeparators(proc.program())) + << proc.arguments()))); + d->logger.qbsInfo() << message; +} + +int RunEnvironment::doRunTarget(const QString &targetBin, const QStringList &arguments, bool dryRun) { const QStringList targetOS = d->resolvedProduct->moduleProperties->qbsPropertyValue( QLatin1String("targetOS")).toStringList(); @@ -282,34 +291,36 @@ int RunEnvironment::doRunTarget(const QString &targetBin, const QStringList &arg QStringLiteral("Android.sdk"), QStringLiteral("sdkDir")).toString(); targetExecutable = sdkDir + QStringLiteral("/platform-tools/adb"); - QProcess process; - process.setProcessChannelMode(QProcess::ForwardedChannels); - process.start(targetExecutable, QStringList() - << StringConstants::androidInstallCommand() - << QStringLiteral("-r") // replace existing application - << QStringLiteral("-t") // allow test packages - << QStringLiteral("-d") // allow version code downgrade - << targetBin); - if (!process.waitForFinished()) { - if (process.error() == QProcess::FailedToStart) { - throw ErrorInfo(Tr::tr("The process '%1' could not be started: %2") - .arg(targetExecutable) - .arg(process.errorString())); - } else { - d->logger.qbsWarning() - << "QProcess error: " << process.errorString(); + if (!dryRun) { + QProcess process; + process.setProcessChannelMode(QProcess::ForwardedChannels); + process.start(targetExecutable, QStringList() + << StringConstants::androidInstallCommand() + << QStringLiteral("-r") // replace existing application + << QStringLiteral("-t") // allow test packages + << QStringLiteral("-d") // allow version code downgrade + << targetBin); + if (!process.waitForFinished()) { + if (process.error() == QProcess::FailedToStart) { + throw ErrorInfo(Tr::tr("The process '%1' could not be started: %2") + .arg(targetExecutable) + .arg(process.errorString())); + } else { + d->logger.qbsWarning() + << "QProcess error: " << process.errorString(); + } + + return EXIT_FAILURE; } - return EXIT_FAILURE; + targetArguments << QStringList() + << QStringLiteral("shell") + << QStringLiteral("am") + << QStringLiteral("start") + << QStringLiteral("-W") // wait for launch to complete + << QStringLiteral("-n") + << intent; } - - targetArguments << QStringList() - << QStringLiteral("shell") - << QStringLiteral("am") - << QStringLiteral("start") - << QStringLiteral("-W") // wait for launch to complete - << QStringLiteral("-n") - << intent; } else if (targetOS.contains(QLatin1String("ios")) || targetOS.contains(QLatin1String("tvos"))) { const QString bundlePath = targetBin + StringConstants::slashDotDot(); @@ -322,28 +333,30 @@ int RunEnvironment::doRunTarget(const QString &targetBin, const QStringList &arg const auto bundleId = d->resolvedProduct->moduleProperties->moduleProperty( QStringLiteral("bundle"), QStringLiteral("identifier")).toString(); - QProcess process; - process.setProcessChannelMode(QProcess::ForwardedChannels); - process.start(targetExecutable, QStringList() - << StringConstants::simctlInstallCommand() - << simulatorId - << QDir::cleanPath(bundlePath)); - if (!process.waitForFinished()) { - if (process.error() == QProcess::FailedToStart) { - throw ErrorInfo(Tr::tr("The process '%1' could not be started: %2") - .arg(targetExecutable) - .arg(process.errorString())); + if (!dryRun) { + QProcess process; + process.setProcessChannelMode(QProcess::ForwardedChannels); + process.start(targetExecutable, QStringList() + << StringConstants::simctlInstallCommand() + << simulatorId + << QDir::cleanPath(bundlePath)); + if (!process.waitForFinished()) { + if (process.error() == QProcess::FailedToStart) { + throw ErrorInfo(Tr::tr("The process '%1' could not be started: %2") + .arg(targetExecutable) + .arg(process.errorString())); + } + + return EXIT_FAILURE; } - return EXIT_FAILURE; + targetArguments << QStringList() + << QStringLiteral("launch") + << QStringLiteral("--console") + << simulatorId + << bundleId + << arguments; } - - targetArguments << QStringList() - << QStringLiteral("launch") - << QStringLiteral("--console") - << simulatorId - << bundleId - << arguments; } else { if (QFileInfo(targetExecutable = findExecutable(QStringList() << QStringLiteral("iostool"))).isExecutable()) { @@ -403,7 +416,7 @@ int RunEnvironment::doRunTarget(const QString &targetBin, const QStringList &arg // Only check if the target is executable if we're not running it through another // known application such as msiexec or wine, as we can't check in this case anyways QFileInfo fi(targetExecutable); - if (targetBin == targetExecutable && (!fi.isFile() || !fi.isExecutable())) { + if (!dryRun && targetBin == targetExecutable && (!fi.isFile() || !fi.isExecutable())) { d->logger.qbsLog(LoggerError) << Tr::tr("File '%1' is not an executable.") .arg(QDir::toNativeSeparators(targetExecutable)); return EXIT_FAILURE; @@ -414,11 +427,15 @@ int RunEnvironment::doRunTarget(const QString &targetBin, const QStringList &arg EnvironmentScriptRunner(d->resolvedProduct.get(), &d->evalContext, env) .setupForRun(d->setupRunEnvConfig); - d->logger.qbsInfo() << Tr::tr("Starting target '%1'.").arg(QDir::toNativeSeparators(targetBin)); QProcess process; process.setProcessEnvironment(d->resolvedProduct->runEnvironment); process.setProcessChannelMode(QProcess::ForwardedChannels); - process.start(targetExecutable, targetArguments); + process.setProgram(targetExecutable); + process.setArguments(targetArguments); + printStartInfo(process, dryRun); + if (dryRun) + return EXIT_SUCCESS; + process.start(); if (!process.waitForFinished(-1)) { if (process.error() == QProcess::FailedToStart) { QString errorPrefixString; diff --git a/src/lib/corelib/api/runenvironment.h b/src/lib/corelib/api/runenvironment.h index 5c380d48c..658ec810b 100644 --- a/src/lib/corelib/api/runenvironment.h +++ b/src/lib/corelib/api/runenvironment.h @@ -46,6 +46,7 @@ #include QT_BEGIN_NAMESPACE +class QProcess; class QProcessEnvironment; class QString; class QStringList; @@ -69,7 +70,7 @@ public: ~RunEnvironment(); int runShell(ErrorInfo *error = nullptr); - int runTarget(const QString &targetBin, const QStringList &arguments, + int runTarget(const QString &targetBin, const QStringList &arguments, bool dryRun, ErrorInfo *error = nullptr); const QProcessEnvironment runEnvironment(ErrorInfo *error = nullptr) const; @@ -85,7 +86,8 @@ private: const Internal::Logger &logger); int doRunShell(); - int doRunTarget(const QString &targetBin, const QStringList &arguments); + int doRunTarget(const QString &targetBin, const QStringList &arguments, bool dryRun); + void printStartInfo(const QProcess &proc, bool dryRun); const QProcessEnvironment getRunEnvironment() const; const QProcessEnvironment getBuildEnvironment() const; -- cgit v1.2.3