// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qmljsplugindumper.h"

#include "qmljsinterpreter.h"
#include "qmljsmodelmanagerinterface.h"
#include "qmljstr.h"
#include "qmljsutils.h"

#include <utils/algorithm.h>
#include <utils/async.h>
#include <utils/filesystemwatcher.h>
#include <utils/hostosinfo.h>
#include <utils/qtcprocess.h>

#include <QDir>
#include <QRegularExpression>

using namespace LanguageUtils;
using namespace QtTaskTree;
using namespace Utils;

namespace QmlJS {

PluginDumper::PluginDumper(ModelManagerInterface *modelManager)
    : QObject(modelManager)
    , m_modelManager(modelManager)
{
    qRegisterMetaType<QmlJS::ModelManagerInterface::ProjectInfo>("QmlJS::ModelManagerInterface::ProjectInfo");
}

void PluginDumper::loadBuiltinTypes(const QmlJS::ModelManagerInterface::ProjectInfo &info)
{
    // move to the owning thread
    metaObject()->invokeMethod(this, [this, info] { onLoadBuiltinTypes(info); });
}

void PluginDumper::loadPluginTypes(const FilePath &libraryPath,
                                   const FilePath &importPath,
                                   const QString &importUri,
                                   const QString &importVersion)
{
    // move to the owning thread
    metaObject()->invokeMethod(this, [this, libraryPath, importPath, importUri, importVersion] {
        onLoadPluginTypes(libraryPath, importPath, importUri, importVersion);
    });
}

void PluginDumper::scheduleRedumpPlugins()
{
    // move to the owning thread
    metaObject()->invokeMethod(this, &PluginDumper::dumpAllPlugins, Qt::QueuedConnection);
}

void PluginDumper::onLoadBuiltinTypes(const QmlJS::ModelManagerInterface::ProjectInfo &info, bool force)
{
    if (info.qmlDumpPath.isEmpty() || info.qtQmlPath.isEmpty())
        return;

    if (m_dumperRunner.isKeyRunning(info.qmlDumpPath))
        return;

    LibraryInfo builtinInfo;
    if (!force) {
        const Snapshot snapshot = m_modelManager->snapshot();
        builtinInfo = snapshot.libraryInfo(info.qtQmlPath);
        if (builtinInfo.isValid())
            return;
    }
    builtinInfo = LibraryInfo(LibraryInfo::Found);
    m_modelManager->updateLibraryInfo(info.qtQmlPath, builtinInfo);

    // prefer QTDIR/qml/builtins.qmltypes if available
    const FilePath builtinQmltypesPath = info.qtQmlPath / "builtins.qmltypes";
    if (builtinQmltypesPath.exists()) {
        loadQmltypesFile({builtinQmltypesPath}, info.qtQmlPath, builtinInfo);
        return;
    }

    runQmlDump(info, QStringList(QLatin1String("--builtins")), info.qtQmlPath);
}

void PluginDumper::onLoadPluginTypes(const FilePath &libraryPath,
                                     const FilePath &importPath,
                                     const QString &importUri,
                                     const QString &importVersion)
{
    const FilePath canonicalLibraryPath = libraryPath.cleanPath();
    if (m_dumperRunner.isKeyRunning(canonicalLibraryPath))
        return;
    const Snapshot snapshot = m_modelManager->snapshot();
    const LibraryInfo libraryInfo = snapshot.libraryInfo(canonicalLibraryPath);
    if (libraryInfo.pluginTypeInfoStatus() != LibraryInfo::NoTypeInfo)
        return;

    // avoid inserting the same plugin twice
    int index;
    for (index = 0; index < m_plugins.size(); ++index) {
        if (m_plugins.at(index).qmldirPath == canonicalLibraryPath)
            break;
    }
    if (index == m_plugins.size())
        m_plugins.append(Plugin());

    Plugin &plugin = m_plugins[index];
    plugin.qmldirPath = canonicalLibraryPath;
    plugin.importPath = importPath;
    plugin.importUri = importUri;
    plugin.importVersion = importVersion;

    // add default qmltypes file if it exists
    const FilePaths libEntries = canonicalLibraryPath.dirEntries(FileFilter{{"*.qmltypes"}, DirFilterFlag::Files});
    for (const FilePath &libEntry : libEntries) {
        if (!plugin.typeInfoPaths.contains(libEntry))
            plugin.typeInfoPaths += libEntry;
    }

    // add typeinfo files listed in qmldir
    for (const QString &typeInfo : libraryInfo.typeInfos()) {
        const FilePath pathNow = canonicalLibraryPath.resolvePath(typeInfo);
        if (!plugin.typeInfoPaths.contains(pathNow) && pathNow.exists())
            plugin.typeInfoPaths += pathNow;
    }

    FilePaths pathsToWatch;
    // watch plugin libraries
    const QList<QmlDirParser::Plugin> plugins = snapshot.libraryInfo(canonicalLibraryPath).plugins();
    for (const QmlDirParser::Plugin &plugin : plugins) {
        const FilePath pluginLibrary = resolvePlugin(canonicalLibraryPath,
                                                     libraryPath.withNewPath(plugin.path),
                                                     plugin.name);
        if (!pluginLibrary.isEmpty()) {
            pathsToWatch << pluginLibrary;
            m_libraryToPluginIndex.insert(pluginLibrary, index);
        }
    }

    // watch library qmltypes file
    if (!plugin.typeInfoPaths.isEmpty()) {
        for (const FilePath &path : std::as_const(plugin.typeInfoPaths)) {
            if (!path.exists())
                continue;
            pathsToWatch << path;
            m_libraryToPluginIndex.insert(path, index);
        }
    }

    watchFilePaths(pathsToWatch);

    dump(plugin);
}

void PluginDumper::dumpAllPlugins()
{
    for (const Plugin &plugin : std::as_const(m_plugins)) {
        dump(plugin);
    }
}

static QString noTypeinfoError(const FilePath &libraryPath)
{
    return Tr::tr("QML module does not contain information about components contained in plugins.\n\n"
                  "Module path: %1\n"
                  "See \"Using QML Modules with Plugins\" in the documentation.").arg(
                libraryPath.toUserOutput());
}

static QString qmldumpErrorMessage(const FilePath &libraryPath, const QString &error)
{
    return noTypeinfoError(libraryPath) + "\n\n" +
            Tr::tr("Automatic type dump of QML module failed.\nErrors:\n%1").
            arg(error) + QLatin1Char('\n');
}

static QString qmldumpFailedMessage(const FilePath &libraryPath, const QString &error)
{
    QString firstLines = QStringList(error.split('\n').mid(0, 10)).join('\n');
    return noTypeinfoError(libraryPath) + "\n\n" +
            Tr::tr("Automatic type dump of QML module failed.\n"
                   "First 10 lines or errors:\n"
                   "\n"
                   "%1"
                   "\n"
                   "Check General Messages for details."
                   ).arg(firstLines);
}

static void printParseWarnings(const FilePath &libraryPath, const QString &warning)
{
    ModelManagerInterface::writeWarning(
                Tr::tr("Warnings while parsing QML type information of %1:\n"
                                 "%2").arg(libraryPath.toUserOutput(), warning));
}

static QString qmlPluginDumpErrorMessage(Process *process)
{
    QString errorMessage;
    const QString binary = process->commandLine().executable().toUserOutput();
    switch (process->error()) {
    case ProcessError::FailedToStart:
        errorMessage = Tr::tr("\"%1\" failed to start: %2").arg(binary, process->errorString());
        break;
    case ProcessError::Crashed:
        errorMessage = Tr::tr("\"%1\" crashed.").arg(binary);
        break;
    case ProcessError::Timedout:
        errorMessage = Tr::tr("\"%1\" timed out.").arg(binary);
        break;
    case ProcessError::ReadError:
    case ProcessError::WriteError:
        errorMessage = Tr::tr("I/O error running \"%1\".").arg(binary);
        break;
    case ProcessError::UnknownError:
        if (process->exitCode())
            errorMessage = Tr::tr("\"%1\" returned exit code %2.").arg(binary).arg(process->exitCode());
        break;
    }
    errorMessage += '\n' + Tr::tr("Arguments: %1").arg(process->commandLine().arguments());
    if (process->error() != ProcessError::FailedToStart) {
        const QString stdErr = QString::fromLocal8Bit(process->readAllRawStandardError());
        if (!stdErr.isEmpty()) {
            errorMessage += QLatin1Char('\n');
            errorMessage += stdErr;
        }
    }
    return errorMessage;
}

void PluginDumper::pluginChanged(const FilePath &pluginLibrary)
{
    // FilePathWatcher emits also when the file is deleted.
    if (!pluginLibrary.exists())
        unwatchFilePath(pluginLibrary);

    const int pluginIndex = m_libraryToPluginIndex.value(pluginLibrary, -1);
    if (pluginIndex == -1)
        return;

    const Plugin &plugin = m_plugins.at(pluginIndex);
    dump(plugin);
}

QFuture<PluginDumper::QmlTypeDescription> PluginDumper::loadQmlTypeDescription(const FilePaths &paths) const
{
    auto future = Utils::asyncRun(m_modelManager->threadPool(),
                                  [paths](QPromise<PluginDumper::QmlTypeDescription> &promise) {
        PluginDumper::QmlTypeDescription result;

        for (const FilePath &p: paths) {
            const Result<QByteArray> contents = p.fileContents();
            if (!contents) {
                result.errors += contents.error();
                continue;
            }
            QString error;
            QString warning;
            CppQmlTypesLoader::BuiltinObjects objs;
            QList<ModuleApiInfo> apis;
            QStringList deps;
            CppQmlTypesLoader::parseQmlTypeDescriptions(*contents, &objs, &apis, &deps,
                                                        &error, &warning, p.toFSPathString());
            if (!error.isEmpty()) {
                result.errors += Tr::tr("Failed to parse \"%1\".\nError: %2").arg(p.toUserOutput(), error);
            } else {
                result.objects += objs.values();
                result.moduleApis += apis;
                if (!deps.isEmpty())
                    result.dependencies += deps;
            }
            if (!warning.isEmpty())
                result.warnings += warning;
        }
        promise.addResult(result);
    });
    m_modelManager->addFuture(future);

    return future;
}

/*!
 * \brief Build the path of an existing qmltypes file from a module name.
 * \param name
 * \return the module's qmltypes file path or an empty string if not found
 *
 * Look for \a name qmltypes file in model manager's import paths.
 *
 * \sa QmlJs::modulePath
 * \sa LinkPrivate::importNonFile
 */
FilePath PluginDumper::buildQmltypesPath(const QString &name) const
{
    QString qualifiedName;
    QString version;

    static const QRegularExpression import("^(?<name>[\\w|\\.]+)\\s+(?<major>\\d+)\\.(?<minor>\\d+)$");
    QRegularExpressionMatch m = import.match(name);
    if (m.hasMatch()) {
        qualifiedName = m.captured("name");
        version = m.captured("major") + QLatin1Char('.') + m.captured("minor");
    }

    const FilePaths paths = modulePaths(qualifiedName,
                                        version,
                                        m_modelManager->importPathsNames());

    if (paths.isEmpty())
        return {};

    for (const FilePath &path : paths) {
        auto qmltypes = path.dirEntries(FileFilter(QStringList{"*.qmltypes"}, DirFilterFlag::Files));
        if (!qmltypes.isEmpty())
            return qmltypes.first();
    }
    return {};
}

/*!
 * \brief Recursively load dependencies.
 *
 * \param dependencies Something like {"QtQuick 2.0"}
 *
 * Recursively load type descriptions of dependencies.
 */
QFuture<PluginDumper::DependencyInfo> PluginDumper::loadDependencies(const QStringList &dependencies,
                                                                     QSharedPointer<QSet<QString>> visited) const
{
    auto iface = QSharedPointer<QFutureInterface<PluginDumper::DependencyInfo>>(new QFutureInterface<PluginDumper::DependencyInfo>);

    if (visited.isNull())
        visited = QSharedPointer<QSet<QString>>(new QSet<QString>());

    FilePaths dependenciesPaths;
    FilePath path;
    for (const QString &name : dependencies) {
        path = buildQmltypesPath(name);
        if (!path.isEmpty())
            dependenciesPaths << path;
        visited->insert(name);
    }

    Utils::onFinished(loadQmlTypeDescription(dependenciesPaths), const_cast<PluginDumper*>(this),
            [this, iface, visited](const QFuture<PluginDumper::QmlTypeDescription> &typesFuture) {
        if (typesFuture.resultCount() == 0 || typesFuture.isCanceled()) {
            iface->reportCanceled();
            return;
        }
        PluginDumper::QmlTypeDescription typesResult = typesFuture.result();
        QStringList newDependencies = typesResult.dependencies;

        newDependencies = Utils::toList(Utils::toSet(newDependencies) - *visited.data());
        if (!newDependencies.isEmpty()) {
            Utils::onFinished(loadDependencies(newDependencies, visited),
                              const_cast<PluginDumper*>(this), [typesResult, iface] (const QFuture<PluginDumper::DependencyInfo> &future) {
                PluginDumper::DependencyInfo result = future.result();

                result.errors += typesResult.errors;
                result.objects += typesResult.objects;
                result.warnings+= typesResult.warnings;

                iface->reportFinished(&result);
            });

        } else {
            PluginDumper::DependencyInfo result;
            result.errors += typesResult.errors;
            result.objects += typesResult.objects;
            result.warnings+= typesResult.warnings;
            iface->reportFinished(&result);
        }
    });

    return iface->future();
}

// Fills \a highestVersion with the largest export version for \a package
// and sets \a hasExportName to true if a type called \a exportName is found.
static void getHighestExportVersion(
        const QList<LanguageUtils::FakeMetaObject::ConstPtr> &objects,
        const QString &package,
        const QString &exportName,
        bool *hasExportName,
        ComponentVersion *highestVersion)
{
    *highestVersion = ComponentVersion();
    *hasExportName = false;
    for (const auto &object : objects) {
        for (const auto &e : object->exports()) {
            if (e.package == package) {
                if (e.version > *highestVersion)
                    *highestVersion = e.version;
                if (e.type == exportName)
                    *hasExportName = true;
            }
        }
    }

}

/*** Workaround for implicit dependencies in >= 5.15.0.
 *
 * When "QtQuick" is imported, "QtQml" is implicitly loaded as well.
 * When "QtQml" is imported, "QtQml.Models" and "QtQml.WorkerScript" are implicitly loaded.
 * Add these imports as if they were "import" commands in the qmldir file.
 *
 * Qt 6 is planned to have these included in the qmldir file.
 */
static void applyQt515MissingImportWorkaround(const QString &path, LibraryInfo &info)
{
    if (!info.imports().isEmpty())
        return;

    const bool isQtQuick = path.endsWith(QStringLiteral("/QtQuick"))
            || path.endsWith(QStringLiteral("/QtQuick.2"));
    const bool isQtQml = path.endsWith(QStringLiteral("/QtQml"))
            || path.endsWith(QStringLiteral("/QtQml.2"));
    if (!isQtQuick && !isQtQml)
        return;

    ComponentVersion highestVersion;
    const auto package = isQtQuick ? QStringLiteral("QtQuick") : QStringLiteral("QtQml");
    const auto missingTypeName = isQtQuick ? QStringLiteral("QtObject") : QStringLiteral("ListElement");
    bool hasMissingType = false;
    getHighestExportVersion(
                info.metaObjects(),
                package,
                missingTypeName,
                &hasMissingType,
                &highestVersion);

    // If the highest export version is < 2.15, we expect Qt <5.15
    if (highestVersion.majorVersion() != 2 || highestVersion.minorVersion() < 15)
        return;
    // As an extra sanity check: if the type from the dependent module already exists,
    // don't proceeed either.
    if (hasMissingType)
        return;

    if (isQtQuick) {
        info.setImports(QList<QmlDirParser::Import>(
                            {QmlDirParser::Import("QtQml", ComponentVersion(), {})}));
    } else if (isQtQml) {
        info.setImports(QList<QmlDirParser::Import>(
                            { QmlDirParser::Import("QtQml.Models", ComponentVersion(), {}),
                              QmlDirParser::Import("QtQml.WorkerScript", ComponentVersion(), {}) }));
    }
}

void PluginDumper::prepareLibraryInfo(
    LibraryInfo &libInfo,
    const FilePath &libraryPath,
    const QStringList &deps,
    const QStringList &errors,
    const QStringList &warnings,
    const QList<ModuleApiInfo> &moduleApis,
    const QList<FakeMetaObject::ConstPtr> &objects)
{
    QStringList errs = errors;

    libInfo.setMetaObjects(objects);
    libInfo.setModuleApis(moduleApis);
    libInfo.setDependencies(deps);

    if (errs.isEmpty()) {
        libInfo.setPluginTypeInfoStatus(LibraryInfo::TypeInfoFileDone);
    } else {
        printParseWarnings(libraryPath, errors.join(QLatin1Char('\n')));
        errs.prepend(Tr::tr("Errors while reading typeinfo files:"));
        libInfo.setPluginTypeInfoStatus(LibraryInfo::TypeInfoFileError, errs.join(QLatin1Char('\n')));
    }

    if (!warnings.isEmpty())
        printParseWarnings(libraryPath, warnings.join(QLatin1String("\n")));

    applyQt515MissingImportWorkaround(libraryPath.toUrlishString(), libInfo);

    libInfo.updateFingerprint();
}

void PluginDumper::loadQmltypesFile(const FilePaths &qmltypesFilePaths,
                                    const FilePath &libraryPath,
                                    QmlJS::LibraryInfo libraryInfo)
{
    Utils::onFinished(loadQmlTypeDescription(qmltypesFilePaths), this,
            [this, qmltypesFilePaths, libraryPath, libraryInfo]
            (const QFuture<PluginDumper::QmlTypeDescription> &typesFuture) {
        if (typesFuture.isCanceled() || typesFuture.resultCount() == 0)
            return;

        PluginDumper::QmlTypeDescription typesResult = typesFuture.result();
        if (!typesResult.dependencies.isEmpty())
        {
            Utils::onFinished(loadDependencies(typesResult.dependencies, {}), this,
                              [typesResult, libraryInfo, libraryPath, this] (const QFuture<PluginDumper::DependencyInfo> &loadFuture)
            {
                if (loadFuture.isCanceled() || loadFuture.resultCount() == 0)
                    return;

                PluginDumper::DependencyInfo loadResult = loadFuture.result();

                m_modelManager->addFuture(
                    Utils::asyncRun([this, libraryPath, libraryInfo, typesResult, loadResult]() {
                        QStringList errors = typesResult.errors;
                        QStringList warnings = typesResult.errors;
                        QList<FakeMetaObject::ConstPtr> objects = typesResult.objects;

                        errors += loadResult.errors;
                        warnings += loadResult.warnings;
                        objects += loadResult.objects;
                        QmlJS::LibraryInfo libInfo = libraryInfo;
                        prepareLibraryInfo(
                            libInfo,
                            libraryPath,
                            typesResult.dependencies,
                            errors,
                            warnings,
                            typesResult.moduleApis,
                            objects);
                        m_modelManager->updateLibraryInfo(libraryPath, libInfo);
                    }));
            });
        } else {
            m_modelManager->addFuture(
                Utils::asyncRun([this, libraryPath, libraryInfo, typesResult]() {
                    QmlJS::LibraryInfo libInfo = libraryInfo;
                    prepareLibraryInfo(
                        libInfo,
                        libraryPath,
                        typesResult.dependencies,
                        typesResult.errors,
                        typesResult.warnings,
                        typesResult.moduleApis,
                        typesResult.objects);
                    m_modelManager->updateLibraryInfo(libraryPath, libInfo);
                }));
        }
    });
}

void PluginDumper::watchFilePaths(const FilePaths &paths)
{
    const FilePaths pathsToWatch = Utils::filtered(paths, [this](const FilePath &p) {
        return !m_pluginWatcher.contains(p) && p.exists();
    });

    std::vector<Result<std::unique_ptr<FilePathWatcher>>> results = pathsToWatch.watch();
    for (size_t i = 0; i < results.size(); ++i) {
        Result<std::unique_ptr<FilePathWatcher>> &res = results.at(i);
        const FilePath path = pathsToWatch.at(i);
        if (res) {
            connect(res->get(), &FilePathWatcher::pathChanged, this, &PluginDumper::pluginChanged);
            m_pluginWatcher.insert(path, std::move(*res));
        } else {
            qWarning() << res.error();
        }
    }
}

void PluginDumper::unwatchFilePath(const FilePath &path)
{
    auto foundPath = m_pluginWatcher.find(path);
    if (foundPath == m_pluginWatcher.end())
        return;

    disconnect(foundPath->get(), &FilePathWatcher::pathChanged, this, &PluginDumper::pluginChanged);
    m_pluginWatcher.erase(foundPath);
}

struct CppQmlTypesInfo
{
    QString error;
    QString warning;
    CppQmlTypesLoader::BuiltinObjects objectsList;
    QList<ModuleApiInfo> moduleApis;
    QStringList dependencies;
};

static void parse(QPromise<CppQmlTypesInfo> &promise, const QByteArray &output,
                  const FilePath &importPath)
{
    CppQmlTypesInfo infos;
    CppQmlTypesLoader::parseQmlTypeDescriptions(output, &infos.objectsList, &infos.moduleApis,
                                                &infos.dependencies, &infos.error, &infos.warning,
                                                "<dump of " + importPath.toUserOutput() + '>');
    promise.addResult(infos);
}

void PluginDumper::runQmlDump(const ModelManagerInterface::ProjectInfo &info,
    const QStringList &arguments, const FilePath &importPath)
{
    struct StorageData
    {
        LibraryInfo libraryInfo;
        QByteArray output;
        bool privatePlugin = true;
    };

    const Storage<StorageData> storage;

    const auto onProcessSetup = [info, arguments, importPath](Process &process) {
        process.setEnvironment(info.qmlDumpEnvironment);
        process.setWorkingDirectory(importPath);
        process.setCommand({info.qmlDumpPath, arguments});
    };
    const auto onProcessDone = [this, importPath, storage](const Process &process, DoneWith result) {
        const Snapshot snapshot = m_modelManager->snapshot();
        LibraryInfo libraryInfo = snapshot.libraryInfo(importPath);
        if (result == DoneWith::Success) {
            libraryInfo.setPluginTypeInfoStatus(LibraryInfo::DumpDone);
            libraryInfo.updateFingerprint();
            m_modelManager->updateLibraryInfo(importPath, libraryInfo);
            return DoneResult::Success;
        }

        Process *processPtr = const_cast<Process *>(&process);
        const QString errorMessages = qmlPluginDumpErrorMessage(processPtr);
        const bool privatePlugin = importPath.endsWith(QLatin1String("private"));
        if (!privatePlugin)
            ModelManagerInterface::writeWarning(qmldumpErrorMessage(importPath, errorMessages));

        libraryInfo.setPluginTypeInfoStatus(LibraryInfo::DumpError,
                                            qmldumpFailedMessage(importPath, errorMessages));
        if (process.error() != ProcessError::UnknownError) {
            libraryInfo.updateFingerprint();
            m_modelManager->updateLibraryInfo(importPath, libraryInfo);
            return DoneResult::Success;
        }

        *storage = {libraryInfo, processPtr->readAllRawStandardOutput(), privatePlugin};

        return DoneResult::Error;
    };

    const auto onParseSetup = [this, importPath, storage](Async<CppQmlTypesInfo> &task) {
        task.setThreadPool(m_modelManager->threadPool());
        task.setConcurrentCallData(parse, storage->output, importPath);
    };
    const auto onParseDone = [this, importPath, storage](const Async<CppQmlTypesInfo> &task) {
        if (!task.isResultAvailable())
            return;

        CppQmlTypesInfo infos = task.result();

        LibraryInfo libInfo = storage->libraryInfo;

        if (!infos.error.isEmpty()) {
            libInfo.setPluginTypeInfoStatus(LibraryInfo::DumpError,
                                            qmldumpErrorMessage(importPath, infos.error));
            if (!storage->privatePlugin)
                printParseWarnings(importPath, libInfo.pluginTypeInfoError());
        } else {
            libInfo.setMetaObjects(infos.objectsList.values());
            libInfo.setModuleApis(infos.moduleApis);
            libInfo.setPluginTypeInfoStatus(LibraryInfo::DumpDone);
        }

        if (!infos.warning.isEmpty())
            printParseWarnings(importPath, infos.warning);

        libInfo.updateFingerprint();

        m_modelManager->updateLibraryInfo(importPath, libInfo);
    };

    const Group recipe {
        stopOnSuccess,
        storage,
        ProcessTask(onProcessSetup, onProcessDone, CallDoneFlag::OnSuccess | CallDoneFlag::OnError),
        AsyncTask<CppQmlTypesInfo>(onParseSetup, onParseDone)
    };

    m_dumperRunner.start(importPath, recipe);
}

void PluginDumper::dump(const Plugin &plugin)
{
    ModelManagerInterface::ProjectInfo info = m_modelManager->defaultProjectInfo();
    const Snapshot snapshot = m_modelManager->snapshot();
    LibraryInfo libraryInfo = snapshot.libraryInfo(plugin.qmldirPath);

    // if there are type infos, don't dump!
    if (!plugin.typeInfoPaths.isEmpty()) {
        if (!libraryInfo.isValid())
            return;

        loadQmltypesFile(plugin.typeInfoPaths, plugin.qmldirPath, libraryInfo);
        return;
    }

    if (plugin.importUri.isEmpty())
        return; // initial scan without uri, ignore

    if (!info.tryQmlDump || info.qmlDumpPath.isEmpty()) {
        if (!libraryInfo.isValid())
            return;

        QString errorMessage;
        if (!info.tryQmlDump) {
            errorMessage = noTypeinfoError(plugin.qmldirPath);
        } else {
            errorMessage = qmldumpErrorMessage(plugin.qmldirPath,
                    Tr::tr("Could not locate the helper application for dumping type information from C++ plugins.\n"
                       "Please build the qmldump application on the Qt version options page."));
        }

        libraryInfo.setPluginTypeInfoStatus(LibraryInfo::DumpError, errorMessage);
        libraryInfo.updateFingerprint();
        m_modelManager->updateLibraryInfo(plugin.qmldirPath, libraryInfo);
        return;
    }

    QStringList args;
    if (info.qmlDumpHasRelocatableFlag)
        args << QLatin1String("-nonrelocatable");
    args << plugin.importUri;
    args << plugin.importVersion;
    args << (plugin.importPath.isEmpty() ? FilePath::fromString(".") : plugin.importPath)
                .toUrlishString();
    runQmlDump(info, args, plugin.qmldirPath);
}

/*!
  Returns the result of the merge of \a baseName with \a path, \a suffixes, and \a prefix.
  The \a prefix must contain the dot.

  \a qmldirPath is the location of the qmldir file.

  Adapted from QDeclarativeImportDatabase::resolvePlugin.
*/
FilePath PluginDumper::resolvePlugin(const FilePath &qmldirPath, const FilePath &qmldirPluginPath,
                                     const QString &baseName, const QStringList &suffixes,
                                     const QString &prefix)
{
    FilePaths searchPaths = {"."};

    bool qmldirPluginPathIsRelative = qmldirPluginPath.isRelativePath();
    if (!qmldirPluginPathIsRelative)
        searchPaths.prepend(qmldirPluginPath);

    for (const FilePath &pluginPath : std::as_const(searchPaths)) {

        FilePath resolvedPath;

        if (pluginPath.path() == QLatin1String(".")) {
            if (qmldirPluginPathIsRelative)
                resolvedPath = qmldirPath.resolvePath(qmldirPluginPath);
            else
                resolvedPath = qmldirPath.absoluteFilePath();
        } else {
            resolvedPath = pluginPath;
        }

        for (const QString &suffix : suffixes) {
            FilePath candidate = resolvedPath.pathAppended(prefix + baseName + suffix);
            if (candidate.exists())
                return candidate.absoluteFilePath();
        }
    }

    return {};
}

/*!
  Returns the result of the merge of \a baseName with \a dir and the platform suffix.

  Adapted from QQmlPluginImporter::resolvePlugin()

  Version numbers on Unix are ignored.
*/
FilePath PluginDumper::resolvePlugin(const FilePath &qmldirPath, const FilePath &qmldirPluginPath,
                                     const QString &baseName)
{
    QStringList validSuffixList;
    QString prefix;
    if (qmldirPath.osType() == OsTypeWindows) {
        // try a qmake-style debug build first
        validSuffixList = QStringList({"d.dll",  ".dll"});
    } else if (qmldirPath.osType() == Utils::OsTypeMac) {
        // try a qmake-style debug build first
        validSuffixList = QStringList({"_debug.dylib", ".dylib", ".so", ".bundle", "lib"});
    } else {
        // Examples of valid library names:
        //  libfoo.so
        prefix = "lib";
        validSuffixList << QLatin1String(".so");
    }
    return resolvePlugin(qmldirPath, qmldirPluginPath, baseName, validSuffixList, prefix);
}

} // QmlJS
