Utils: Add gzip support to unarchiver

Change-Id: I223d97169cd9e1850d221370d8dd16b770918e20
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-05-30 09:55:21 +02:00
parent 4bbbf29e7d
commit b0354eaf16
2 changed files with 49 additions and 1 deletions

View File

@@ -43,6 +43,7 @@ static const QList<Tool> &sTools()
{ {
static QList<Tool> tools; static QList<Tool> tools;
if (tools.isEmpty()) { if (tools.isEmpty()) {
// clang-format off
if (HostOsInfo::isWindowsHost()) { if (HostOsInfo::isWindowsHost()) {
tools << Tool{{"powershell", "-command Expand-Archive -Force '%{src}' '%{dest}'", CommandLine::Raw}, tools << Tool{{"powershell", "-command Expand-Archive -Force '%{src}' '%{dest}'", CommandLine::Raw},
{"application/zip"}, {"application/zip"},
@@ -74,6 +75,10 @@ static const QList<Tool> &sTools()
tools << Tool{{"cmake", {"-E", "tar", "xvjf", "%{src}"}}, tools << Tool{{"cmake", {"-E", "tar", "xvjf", "%{src}"}},
{"application/x-bzip-compressed-tar"}, {"application/x-bzip-compressed-tar"},
additionalCMakeDirs}; additionalCMakeDirs};
// Keep this at the end so its only used as last resort. Otherwise it might be used for
// .tar.gz files.
tools << Tool{{"gzip", {"-d", "%{src}", "-c"}}, {"application/gzip"}, {}};
// clang-format on
} }
return tools; return tools;
} }
@@ -147,6 +152,45 @@ void Unarchiver::start()
m_sourceAndCommand->m_sourceFile, m_destDir); m_sourceAndCommand->m_sourceFile, m_destDir);
m_destDir.ensureWritableDir(); m_destDir.ensureWritableDir();
if (command.executable().fileName() == "gzip") {
std::shared_ptr<QFile> outputFile = std::make_shared<QFile>(
(m_destDir / m_gzipFileDestName).toFSPathString());
if (!outputFile->open(QIODevice::WriteOnly)) {
emit outputReceived(Tr::tr("Failed to open output file."));
emit done(DoneResult::Error);
return;
}
m_process.reset(new Process);
QObject::connect(m_process.get(), &Process::readyReadStandardOutput, this, [this, outputFile] {
const QByteArray data = m_process->readAllRawStandardOutput();
if (outputFile->write(data) != data.size()) {
emit outputReceived(Tr::tr("Failed to write output file."));
emit done(DoneResult::Error);
}
});
QObject::connect(m_process.get(), &Process::readyReadStandardError, this, [this] {
emit outputReceived(m_process->readAllStandardError());
});
QObject::connect(m_process.get(), &Process::done, this, [outputFile, this] {
outputFile->close();
const bool success = m_process->result() == ProcessResult::FinishedWithSuccess;
if (!success) {
outputFile->remove();
emit outputReceived(Tr::tr("Command failed."));
}
emit done(toDoneResult(success));
});
emit outputReceived(
Tr::tr("Running %1\nin \"%2\".\n\n", "Running <cmd> in <workingdirectory>")
.arg(command.toUserOutput(), m_destDir.toUserOutput()));
m_process->setCommand(command);
m_process->setWorkingDirectory(m_destDir);
m_process->start();
return;
}
m_process.reset(new Process); m_process.reset(new Process);
m_process->setProcessChannelMode(QProcess::MergedChannels); m_process->setProcessChannelMode(QProcess::MergedChannels);
QObject::connect(m_process.get(), &Process::readyReadStandardOutput, this, [this] { QObject::connect(m_process.get(), &Process::readyReadStandardOutput, this, [this] {

View File

@@ -32,7 +32,10 @@ public:
void setSourceAndCommand(const SourceAndCommand &data) { m_sourceAndCommand = data; } void setSourceAndCommand(const SourceAndCommand &data) { m_sourceAndCommand = data; }
void setDestDir(const FilePath &destDir) { m_destDir = destDir; } void setDestDir(const FilePath &destDir) { m_destDir = destDir; }
void setGZipFileDestName(const QString &gzipFileDestName)
{
m_gzipFileDestName = gzipFileDestName;
}
void start(); void start();
signals: signals:
@@ -43,6 +46,7 @@ private:
std::optional<SourceAndCommand> m_sourceAndCommand; std::optional<SourceAndCommand> m_sourceAndCommand;
FilePath m_destDir; FilePath m_destDir;
std::unique_ptr<Process> m_process; std::unique_ptr<Process> m_process;
QString m_gzipFileDestName;
}; };
class QTCREATOR_UTILS_EXPORT UnarchiverTaskAdapter : public Tasking::TaskAdapter<Unarchiver> class QTCREATOR_UTILS_EXPORT UnarchiverTaskAdapter : public Tasking::TaskAdapter<Unarchiver>