androiddeployqt: Refactor apkSignerRunner

Avoid to publish all local variables in the lambda and
only provide the necessary information as parameter.

Change-Id: Iea68280f79f90ac3d5f7b9a0a92e073865a1d291
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
This commit is contained in:
Lars Schmertmann 2021-05-06 16:47:12 +02:00
parent 34d33becd4
commit 46db337975

View File

@ -2845,39 +2845,39 @@ bool signPackage(const Options &options)
} }
} }
QString apkSignerCommandLine = QLatin1String("%1 sign --ks %2") QString apkSignCommand = QLatin1String("%1 sign --ks %2")
.arg(shellQuote(apksignerTool), shellQuote(options.keyStore)); .arg(shellQuote(apksignerTool), shellQuote(options.keyStore));
if (!options.keyStorePassword.isEmpty()) if (!options.keyStorePassword.isEmpty())
apkSignerCommandLine += QLatin1String(" --ks-pass pass:%1").arg(shellQuote(options.keyStorePassword)); apkSignCommand += QLatin1String(" --ks-pass pass:%1").arg(shellQuote(options.keyStorePassword));
if (!options.keyStoreAlias.isEmpty()) if (!options.keyStoreAlias.isEmpty())
apkSignerCommandLine += QLatin1String(" --ks-key-alias %1").arg(shellQuote(options.keyStoreAlias)); apkSignCommand += QLatin1String(" --ks-key-alias %1").arg(shellQuote(options.keyStoreAlias));
if (!options.keyPass.isEmpty()) if (!options.keyPass.isEmpty())
apkSignerCommandLine += QLatin1String(" --key-pass pass:%1").arg(shellQuote(options.keyPass)); apkSignCommand += QLatin1String(" --key-pass pass:%1").arg(shellQuote(options.keyPass));
if (options.verbose) if (options.verbose)
apkSignerCommandLine += QLatin1String(" --verbose"); apkSignCommand += QLatin1String(" --verbose");
apkSignerCommandLine += QLatin1String(" %1") apkSignCommand += QLatin1String(" %1")
.arg(packagePath(options, SignedAPK)); .arg(packagePath(options, SignedAPK));
auto apkSignerRunner = [&] { auto apkSignerRunner = [](const QString &command, bool verbose) {
FILE *apkSignerCommand = openProcess(apkSignerCommandLine); FILE *apkSigner = openProcess(command);
if (apkSignerCommand == 0) { if (apkSigner == 0) {
fprintf(stderr, "Couldn't run apksigner.\n"); fprintf(stderr, "Couldn't run apksigner.\n");
return false; return false;
} }
char buffer[512]; char buffer[512];
while (fgets(buffer, sizeof(buffer), apkSignerCommand) != 0) while (fgets(buffer, sizeof(buffer), apkSigner) != 0)
fprintf(stdout, "%s", buffer); fprintf(stdout, "%s", buffer);
int errorCode = pclose(apkSignerCommand); int errorCode = pclose(apkSigner);
if (errorCode != 0) { if (errorCode != 0) {
fprintf(stderr, "apksigner command failed.\n"); fprintf(stderr, "apksigner command failed.\n");
if (!options.verbose) if (!verbose)
fprintf(stderr, " -- Run with --verbose for more information.\n"); fprintf(stderr, " -- Run with --verbose for more information.\n");
return false; return false;
} }
@ -2885,14 +2885,14 @@ bool signPackage(const Options &options)
}; };
// Sign the package // Sign the package
if (!apkSignerRunner()) if (!apkSignerRunner(apkSignCommand, options.verbose))
return false; return false;
apkSignerCommandLine = QLatin1String("%1 verify --verbose %2") const QString apkVerifyCommand = QLatin1String("%1 verify --verbose %2")
.arg(shellQuote(apksignerTool), packagePath(options, SignedAPK)); .arg(shellQuote(apksignerTool), packagePath(options, SignedAPK));
// Verify the package and remove the unsigned apk // Verify the package and remove the unsigned apk
return apkSignerRunner() && QFile::remove(packagePath(options, UnsignedAPK)); return apkSignerRunner(apkVerifyCommand, true) && QFile::remove(packagePath(options, UnsignedAPK));
} }
enum ErrorCode enum ErrorCode