forked from qt-creator/qt-creator
Utils: FilePathify Archive
Change-Id: Ia6b972b50f8b1cc7c5829ac863a5881849ea7678 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -37,23 +37,26 @@
|
||||
#include <QSettings>
|
||||
#include <QTimer>
|
||||
|
||||
namespace Utils {
|
||||
|
||||
namespace {
|
||||
|
||||
struct Tool
|
||||
{
|
||||
QString executable;
|
||||
QStringList arguments;
|
||||
CommandLine command;
|
||||
QStringList supportedMimeTypes;
|
||||
QStringList additionalSearchDirs;
|
||||
bool nativeWindowsArguments = false;
|
||||
FilePaths additionalSearchDirs;
|
||||
};
|
||||
|
||||
static QStringList additionalInstallDirs(const QString ®istryKey, const QString &valueName)
|
||||
} // anon
|
||||
|
||||
static FilePaths additionalInstallDirs(const QString ®istryKey, const QString &valueName)
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
const QSettings settings64(registryKey, QSettings::Registry64Format);
|
||||
const QSettings settings32(registryKey, QSettings::Registry32Format);
|
||||
return {settings64.value(valueName).toString(), settings32.value(valueName).toString()};
|
||||
return {FilePath::fromVariant(settings64.value(valueName)),
|
||||
FilePath::fromVariant(settings32.value(valueName))};
|
||||
#else
|
||||
Q_UNUSED(registryKey)
|
||||
Q_UNUSED(valueName)
|
||||
@@ -65,48 +68,42 @@ static const QVector<Tool> &sTools()
|
||||
{
|
||||
static QVector<Tool> tools;
|
||||
if (tools.isEmpty()) {
|
||||
if (Utils::HostOsInfo::isWindowsHost()) {
|
||||
tools << Tool{{"powershell"},
|
||||
{"-command Expand-Archive -Force '%{src}' '%{dest}'"},
|
||||
if (HostOsInfo::isWindowsHost()) {
|
||||
tools << Tool{{"powershell", "-command Expand-Archive -Force '%{src}' '%{dest}'", CommandLine::Raw},
|
||||
{"application/zip"},
|
||||
{},
|
||||
true};
|
||||
{}};
|
||||
}
|
||||
tools << Tool{{"unzip"}, {"-o", "%{src}", "-d", "%{dest}"}, {"application/zip"}, {}};
|
||||
tools << Tool{{"7z"},
|
||||
{"x", "-o%{dest}", "-y", "-bb", "%{src}"},
|
||||
tools << Tool{{"unzip", {"-o", "%{src}", "-d", "%{dest}"}}, {"application/zip"}, {}};
|
||||
tools << Tool{{"7z", {"x", "-o%{dest}", "-y", "-bb", "%{src}"}},
|
||||
{"application/zip", "application/x-7z-compressed"},
|
||||
additionalInstallDirs("HKEY_CURRENT_USER\\Software\\7-Zip", "Path")};
|
||||
tools << Tool{{"tar"},
|
||||
{"xvf", "%{src}"},
|
||||
tools << Tool{{"tar", {"xvf", "%{src}"}},
|
||||
{"application/zip", "application/x-tar", "application/x-7z-compressed"},
|
||||
{}};
|
||||
tools << Tool{{"tar"}, {"xvzf", "%{src}"}, {"application/x-compressed-tar"}, {}};
|
||||
tools << Tool{{"tar"}, {"xvJf", "%{src}"}, {"application/x-xz-compressed-tar"}, {}};
|
||||
tools << Tool{{"tar"}, {"xvjf", "%{src}"}, {"application/x-bzip-compressed-tar"}, {}};
|
||||
const QStringList additionalCMakeDirs = additionalInstallDirs(
|
||||
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Kitware\\CMake", "InstallDir");
|
||||
tools << Tool{{"cmake"},
|
||||
{"-E", "tar", "xvf", "%{src}"},
|
||||
tools << Tool{{"tar", {"xvzf", "%{src}"}}, {"application/x-compressed-tar"}, {}};
|
||||
tools << Tool{{"tar", {"xvJf", "%{src}"}}, {"application/x-xz-compressed-tar"}, {}};
|
||||
tools << Tool{{"tar", {"xvjf", "%{src}"}}, {"application/x-bzip-compressed-tar"}, {}};
|
||||
|
||||
const FilePaths additionalCMakeDirs =
|
||||
additionalInstallDirs("HKEY_LOCAL_MACHINE\\SOFTWARE\\Kitware\\CMake",
|
||||
"InstallDir");
|
||||
tools << Tool{{"cmake", {"-E", "tar", "xvf", "%{src}"}},
|
||||
{"application/zip", "application/x-tar", "application/x-7z-compressed"},
|
||||
additionalCMakeDirs};
|
||||
tools << Tool{{"cmake"},
|
||||
{"-E", "tar", "xvzf", "%{src}"},
|
||||
tools << Tool{{"cmake", {"-E", "tar", "xvzf", "%{src}"}},
|
||||
{"application/x-compressed-tar"},
|
||||
additionalCMakeDirs};
|
||||
tools << Tool{{"cmake"},
|
||||
{"-E", "tar", "xvJf", "%{src}"},
|
||||
tools << Tool{{"cmake", {"-E", "tar", "xvJf", "%{src}"}},
|
||||
{"application/x-xz-compressed-tar"},
|
||||
additionalCMakeDirs};
|
||||
tools << Tool{{"cmake"},
|
||||
{"-E", "tar", "xvjf", "%{src}"},
|
||||
tools << Tool{{"cmake", {"-E", "tar", "xvjf", "%{src}"}},
|
||||
{"application/x-bzip-compressed-tar"},
|
||||
additionalCMakeDirs};
|
||||
}
|
||||
return tools;
|
||||
}
|
||||
|
||||
static QVector<Tool> toolsForMimeType(const Utils::MimeType &mimeType)
|
||||
static QVector<Tool> toolsForMimeType(const MimeType &mimeType)
|
||||
{
|
||||
return Utils::filtered(sTools(), [mimeType](const Tool &tool) {
|
||||
return Utils::anyOf(tool.supportedMimeTypes,
|
||||
@@ -114,24 +111,21 @@ static QVector<Tool> toolsForMimeType(const Utils::MimeType &mimeType)
|
||||
});
|
||||
}
|
||||
|
||||
static QVector<Tool> toolsForFilePath(const Utils::FilePath &fp)
|
||||
static QVector<Tool> toolsForFilePath(const FilePath &fp)
|
||||
{
|
||||
return toolsForMimeType(Utils::mimeTypeForFile(fp));
|
||||
}
|
||||
|
||||
static Utils::optional<Tool> resolveTool(const Tool &tool)
|
||||
{
|
||||
const QString executable
|
||||
= Utils::Environment::systemEnvironment()
|
||||
.searchInPath(Utils::HostOsInfo::withExecutableSuffix(tool.executable),
|
||||
Utils::transform(tool.additionalSearchDirs, &Utils::FilePath::fromString))
|
||||
.toString();
|
||||
const FilePaths dirs = Environment::systemEnvironment().path() + tool.additionalSearchDirs;
|
||||
const FilePath executable = tool.command.executable().withExecutableSuffix().searchInDirectories(dirs);
|
||||
Tool resolvedTool = tool;
|
||||
resolvedTool.executable = executable;
|
||||
resolvedTool.command.setExecutable(executable);
|
||||
return executable.isEmpty() ? Utils::nullopt : Utils::make_optional(resolvedTool);
|
||||
}
|
||||
|
||||
Utils::optional<Tool> unzipTool(const Utils::FilePath &src, const Utils::FilePath &dest)
|
||||
static Utils::optional<Tool> unzipTool(const FilePath &src, const FilePath &dest)
|
||||
{
|
||||
const QVector<Tool> tools = toolsForFilePath(src);
|
||||
for (const Tool &tool : tools) {
|
||||
@@ -140,21 +134,13 @@ Utils::optional<Tool> unzipTool(const Utils::FilePath &src, const Utils::FilePat
|
||||
Tool result = *resolvedTool;
|
||||
const QString srcStr = src.toString();
|
||||
const QString destStr = dest.toString();
|
||||
result.arguments
|
||||
= Utils::transform(result.arguments, [srcStr, destStr](const QString &a) {
|
||||
QString val = a;
|
||||
return val.replace("%{src}", srcStr).replace("%{dest}", destStr);
|
||||
});
|
||||
return result;
|
||||
const QString args = result.command.arguments().replace("%{src}", srcStr).replace("%{dest}", destStr);
|
||||
result.command.setArguments(args);
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace Utils {
|
||||
|
||||
bool Archive::supportsFile(const FilePath &filePath, QString *reason)
|
||||
{
|
||||
const QVector<Tool> tools = toolsForFilePath(filePath);
|
||||
@@ -163,10 +149,14 @@ bool Archive::supportsFile(const FilePath &filePath, QString *reason)
|
||||
*reason = tr("File format not supported.");
|
||||
return false;
|
||||
}
|
||||
if (!anyOf(tools, [](const Tool &t) { return resolveTool(t); })) {
|
||||
if (reason)
|
||||
if (!anyOf(tools, [tools](const Tool &t) { return resolveTool(t); })) {
|
||||
if (reason) {
|
||||
const QStringList execs = transform<QStringList>(tools, [](const Tool &tool) {
|
||||
return tool.command.executable().toString();
|
||||
});
|
||||
*reason = tr("Could not find any unarchiving executable in PATH (%1).")
|
||||
.arg(transform<QStringList>(tools, &Tool::executable).join(", "));
|
||||
.arg(execs.join(", "));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -251,14 +241,10 @@ Archive *Archive::unarchive(const FilePath &src, const FilePath &dest)
|
||||
QTimer::singleShot(0, archive, [archive, tool, workingDirectory] {
|
||||
archive->outputReceived(
|
||||
tr("Running %1\nin \"%2\".\n\n", "Running <cmd> in <workingdirectory>")
|
||||
.arg(CommandLine(FilePath::fromString(tool->executable), tool->arguments).toUserOutput(),
|
||||
workingDirectory));
|
||||
.arg(tool->command.toUserOutput(), workingDirectory));
|
||||
});
|
||||
|
||||
CommandLine cmd = tool->nativeWindowsArguments
|
||||
? CommandLine{FilePath::fromString(tool->executable), tool->arguments[0], CommandLine::Raw}
|
||||
: CommandLine{FilePath::fromString(tool->executable), tool->arguments};
|
||||
archive->m_process->setCommand(cmd);
|
||||
archive->m_process->setCommand(tool->command);
|
||||
archive->m_process->setWorkingDirectory(workingDirectory);
|
||||
archive->m_process->start();
|
||||
return archive;
|
||||
|
Reference in New Issue
Block a user