diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizard.pri b/src/plugins/projectexplorer/jsonwizard/jsonwizard.pri index b9d7fe5136a..72f914f4f49 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizard.pri +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizard.pri @@ -8,7 +8,8 @@ HEADERS += $$PWD/jsonfieldpage.h \ $$PWD/jsonwizardfilegenerator.h \ $$PWD/jsonwizardgeneratorfactory.h \ $$PWD/jsonwizardpagefactory.h \ - $$PWD/jsonwizardpagefactory_p.h + $$PWD/jsonwizardpagefactory_p.h \ + $$PWD/jsonwizardscannergenerator.h SOURCES += $$PWD/jsonfieldpage.cpp \ $$PWD/jsonfilepage.cpp \ @@ -20,4 +21,5 @@ SOURCES += $$PWD/jsonfieldpage.cpp \ $$PWD/jsonwizardfilegenerator.cpp \ $$PWD/jsonwizardgeneratorfactory.cpp \ $$PWD/jsonwizardpagefactory.cpp \ - $$PWD/jsonwizardpagefactory_p.cpp + $$PWD/jsonwizardpagefactory_p.cpp \ + $$PWD/jsonwizardscannergenerator.cpp diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.cpp index b3f487ae7ae..4fb25ea90a8 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.cpp @@ -32,6 +32,7 @@ #include "jsonwizard.h" #include "jsonwizardfilegenerator.h" +#include "jsonwizardscannergenerator.h" #include "../editorconfiguration.h" #include "../project.h" @@ -232,7 +233,8 @@ JsonWizardGenerator::OverwriteResult JsonWizardGenerator::promptForOverwrite(Jso return OverwriteOk; } -bool JsonWizardGenerator::formatFiles(const JsonWizard *wizard, JsonWizard::GeneratorFiles *files, QString *errorMessage) +bool JsonWizardGenerator::formatFiles(const JsonWizard *wizard, JsonWizard::GeneratorFiles *files, + QString *errorMessage) { for (auto i = files->begin(); i != files->end(); ++i) { if (!i->generator->formatFile(wizard, &(i->file), errorMessage)) @@ -241,7 +243,8 @@ bool JsonWizardGenerator::formatFiles(const JsonWizard *wizard, JsonWizard::Gene return true; } -bool JsonWizardGenerator::writeFiles(const JsonWizard *wizard, JsonWizard::GeneratorFiles *files, QString *errorMessage) +bool JsonWizardGenerator::writeFiles(const JsonWizard *wizard, JsonWizard::GeneratorFiles *files, + QString *errorMessage) { for (auto i = files->begin(); i != files->end(); ++i) { if (!i->generator->writeFile(wizard, &(i->file), errorMessage)) @@ -250,7 +253,8 @@ bool JsonWizardGenerator::writeFiles(const JsonWizard *wizard, JsonWizard::Gener return true; } -bool JsonWizardGenerator::postWrite(const JsonWizard *wizard, JsonWizard::GeneratorFiles *files, QString *errorMessage) +bool JsonWizardGenerator::postWrite(const JsonWizard *wizard, JsonWizard::GeneratorFiles *files, + QString *errorMessage) { for (auto i = files->begin(); i != files->end(); ++i) { if (!i->generator->postWrite(wizard, &(i->file), errorMessage)) @@ -259,7 +263,8 @@ bool JsonWizardGenerator::postWrite(const JsonWizard *wizard, JsonWizard::Genera return true; } -bool JsonWizardGenerator::polish(const JsonWizard *wizard, JsonWizard::GeneratorFiles *files, QString *errorMessage) +bool JsonWizardGenerator::polish(const JsonWizard *wizard, JsonWizard::GeneratorFiles *files, + QString *errorMessage) { for (auto i = files->begin(); i != files->end(); ++i) { if (!i->generator->polish(wizard, &(i->file), errorMessage)) @@ -268,7 +273,8 @@ bool JsonWizardGenerator::polish(const JsonWizard *wizard, JsonWizard::Generator return true; } -bool JsonWizardGenerator::allDone(const JsonWizard *wizard, JsonWizard::GeneratorFiles *files, QString *errorMessage) +bool JsonWizardGenerator::allDone(const JsonWizard *wizard, JsonWizard::GeneratorFiles *files, + QString *errorMessage) { for (auto i = files->begin(); i != files->end(); ++i) { if (!i->generator->allDone(wizard, &(i->file), errorMessage)) @@ -296,6 +302,7 @@ void JsonWizardGeneratorFactory::setTypeIdsSuffix(const QString &suffix) // FileGeneratorFactory: // -------------------------------------------------------------------- +namespace Internal { FileGeneratorFactory::FileGeneratorFactory() { @@ -306,14 +313,13 @@ JsonWizardGenerator *FileGeneratorFactory::create(Id typeId, const QVariant &dat const QString &path, const QString &platform, const QVariantMap &variables) { - Q_UNUSED(data); Q_UNUSED(path); Q_UNUSED(platform); Q_UNUSED(variables); QTC_ASSERT(canCreate(typeId), return 0); - auto *gen = new Internal::JsonWizardFileGenerator(); + auto gen = new JsonWizardFileGenerator; QString errorMessage; gen->setup(data, &errorMessage); @@ -328,16 +334,51 @@ JsonWizardGenerator *FileGeneratorFactory::create(Id typeId, const QVariant &dat bool FileGeneratorFactory::validateData(Id typeId, const QVariant &data, QString *errorMessage) { - Q_UNUSED(data); - Q_UNUSED(errorMessage); - QTC_ASSERT(canCreate(typeId), return false); - QScopedPointer gen(new Internal::JsonWizardFileGenerator()); - if (!gen->setup(data, errorMessage)) - return false; - - return true; + QScopedPointer gen(new JsonWizardFileGenerator); + return gen->setup(data, errorMessage); } +// -------------------------------------------------------------------- +// ScannerGeneratorFactory: +// -------------------------------------------------------------------- + +ScannerGeneratorFactory::ScannerGeneratorFactory() +{ + setTypeIdsSuffix(QLatin1String("Scanner")); +} + +JsonWizardGenerator *ScannerGeneratorFactory::create(Id typeId, const QVariant &data, + const QString &path, const QString &platform, + const QVariantMap &variables) +{ + Q_UNUSED(path); + Q_UNUSED(platform); + Q_UNUSED(variables); + + QTC_ASSERT(canCreate(typeId), return 0); + + auto gen = new JsonWizardScannerGenerator; + QString errorMessage; + gen->setup(data, &errorMessage); + + if (!errorMessage.isEmpty()) { + qWarning() << "ScannerGeneratorFactory setup error:" << errorMessage; + delete gen; + return 0; + } + + return gen; +} + +bool ScannerGeneratorFactory::validateData(Id typeId, const QVariant &data, QString *errorMessage) +{ + QTC_ASSERT(canCreate(typeId), return false); + + QScopedPointer gen(new JsonWizardScannerGenerator); + return gen->setup(data, errorMessage); +} + +} // namespace Internal } // namespace ProjectExplorer diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.h b/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.h index 26e5a02e3ff..e9b182dfc1c 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.h +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardgeneratorfactory.h @@ -94,6 +94,8 @@ private: QList m_typeIds; }; +namespace Internal { + class FileGeneratorFactory : public JsonWizardGeneratorFactory { Q_OBJECT @@ -107,6 +109,20 @@ public: bool validateData(Core::Id typeId, const QVariant &data, QString *errorMessage); }; +class ScannerGeneratorFactory : public JsonWizardGeneratorFactory +{ + Q_OBJECT + +public: + ScannerGeneratorFactory(); + + JsonWizardGenerator *create(Core::Id typeId, const QVariant &data, + const QString &path, const QString &platform, + const QVariantMap &variables); + bool validateData(Core::Id typeId, const QVariant &data, QString *errorMessage); +}; + +} // namespace Internal } // namespace ProjectExplorer #endif // JSONWIZARDGENERATORFACTORY_H diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardscannergenerator.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardscannergenerator.cpp new file mode 100644 index 00000000000..f39c2fb268c --- /dev/null +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardscannergenerator.cpp @@ -0,0 +1,167 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms and +** conditions see http://www.qt.io/terms-conditions. For further information +** use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#include "jsonwizardscannergenerator.h" + +#include "../customwizard/customwizardpreprocessor.h" +#include "../projectexplorer.h" +#include "../iprojectmanager.h" +#include "jsonwizard.h" +#include "jsonwizardfactory.h" + +#include + +#include + +#include +#include +#include +#include + +#include +#include +#include + +namespace ProjectExplorer { +namespace Internal { + +bool JsonWizardScannerGenerator::setup(const QVariant &data, QString *errorMessage) +{ + if (data.isNull()) + return true; + + if (data.type() != QVariant::Map) { + *errorMessage = QCoreApplication::translate("ProjectExplorer::Internal::JsonWizard", + "Key is not an object."); + return false; + } + + QVariantMap gen = data.toMap(); + + m_binaryPattern = gen.value(QLatin1String("binaryPattern")).toString(); + QStringList patterns = gen.value(QLatin1String("subdirectoryPatterns")).toStringList(); + foreach (const QString pattern, patterns) { + QRegularExpression regexp(pattern); + if (!regexp.isValid()) { + *errorMessage = QCoreApplication::translate("ProjectExplorer::Internal::JsonWizard", + "Pattern \"%1\" is no valid regular expression."); + return false; + } + m_subDirectoryExpressions << regexp; + } + + return true; +} + +Core::GeneratedFiles JsonWizardScannerGenerator::fileList(Utils::MacroExpander *expander, + const QString &wizardDir, + const QString &projectDir, + QString *errorMessage) +{ + Q_UNUSED(wizardDir); + errorMessage->clear(); + + QDir project(projectDir); + Core::GeneratedFiles result; + Utils::MimeDatabase mdb; + + QRegularExpression binaryPattern; + if (!m_binaryPattern.isEmpty()) { + binaryPattern = QRegularExpression(expander->expand(m_binaryPattern)); + if (!binaryPattern.isValid()) { + qWarning() << QCoreApplication::translate("ProjectExplorer::Internal::JsonWizard", + "ScannerGenerator: Binary pattern \"%1\" not valid.") + .arg(m_binaryPattern); + return result; + } + } + + result = scan(project.absolutePath(), project); + + QList projectManagers + = ExtensionSystem::PluginManager::getObjects(); + + for (auto it = result.begin(); it != result.end(); ++it) { + const QString relPath = project.relativeFilePath(it->path()); + it->setBinary(binaryPattern.match(relPath).hasMatch()); + + Utils::MimeType mt = mdb.mimeTypeForFile(relPath); + if (mt.isValid()) { + bool foundProjectManager = false; + foreach (IProjectManager *manager, projectManagers) { + if (mt.matchesName(manager->mimeType())) { + foundProjectManager = true; + break; + } + } + if (foundProjectManager) + it->setAttributes(it->attributes() | Core::GeneratedFile::OpenProjectAttribute); + } + } + + return result; +} + +bool JsonWizardScannerGenerator::matchesSubdirectoryPattern(const QString &path) +{ + foreach (const QRegularExpression ®exp, m_subDirectoryExpressions) { + if (regexp.match(path).hasMatch()) + return true; + } + return false; +} + +Core::GeneratedFiles JsonWizardScannerGenerator::scan(const QString &dir, const QDir &base) +{ + Core::GeneratedFiles result; + QDir directory(dir); + + if (!directory.exists()) + return result; + + QList entries = directory.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot, + QDir::DirsLast | QDir::Name); + foreach (const QFileInfo &fi, entries) { + const QString relativePath = base.relativeFilePath(fi.absoluteFilePath()); + if (fi.isDir() && matchesSubdirectoryPattern(relativePath)) { + result += scan(fi.absoluteFilePath(), base); + } else { + Core::GeneratedFile f(fi.absoluteFilePath()); + f.setAttributes(f.attributes() | Core::GeneratedFile::KeepExistingFileAttribute); + + result.append(f); + } + } + + return result; +} + +} // namespace Internal +} // namespace ProjectExplorer diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardscannergenerator.h b/src/plugins/projectexplorer/jsonwizard/jsonwizardscannergenerator.h new file mode 100644 index 00000000000..4fc62a287cd --- /dev/null +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardscannergenerator.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms and +** conditions see http://www.qt.io/terms-conditions. For further information +** use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#ifndef JSONWIZARDSCANNERGENERATOR_H +#define JSONWIZARDSCANNERGENERATOR_H + +#include "jsonwizardgeneratorfactory.h" + +#include +#include +#include + +namespace ProjectExplorer { +namespace Internal { + +// Documentation inside. +class JsonWizardScannerGenerator : public JsonWizardGenerator +{ +public: + bool setup(const QVariant &data, QString *errorMessage); + + Core::GeneratedFiles fileList(Utils::MacroExpander *expander, + const QString &wizardDir, const QString &projectDir, + QString *errorMessage); + +private: + Core::GeneratedFiles scan(const QString &dir, const QDir &base); + bool matchesSubdirectoryPattern(const QString &path); + + QString m_binaryPattern; + QList m_subDirectoryExpressions; +}; + +} // namespace Internal +} // namespace ProjectExplorer + +#endif // JSONWIZARDSCANNERGENERATOR_H diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index b58cf669c0a..1896790a461 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -538,6 +538,7 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er JsonWizardFactory::registerPageFactory(new SummaryPageFactory); JsonWizardFactory::registerGeneratorFactory(new FileGeneratorFactory); + JsonWizardFactory::registerGeneratorFactory(new ScannerGeneratorFactory); dd->m_proWindow = new ProjectWindow; addAutoReleasedObject(dd->m_proWindow); diff --git a/src/plugins/projectexplorer/projectexplorer.qbs b/src/plugins/projectexplorer/projectexplorer.qbs index ccc7169b180..3a229070dcb 100644 --- a/src/plugins/projectexplorer/projectexplorer.qbs +++ b/src/plugins/projectexplorer/projectexplorer.qbs @@ -179,7 +179,8 @@ QtcPlugin { "jsonwizardfilegenerator.cpp", "jsonwizardfilegenerator.h", "jsonwizardgeneratorfactory.cpp", "jsonwizardgeneratorfactory.h", "jsonwizardpagefactory.cpp", "jsonwizardpagefactory.h", - "jsonwizardpagefactory_p.cpp", "jsonwizardpagefactory_p.h" + "jsonwizardpagefactory_p.cpp", "jsonwizardpagefactory_p.h", + "jsonwizardscannergenerator.cpp", "jsonwizardscannergenerator.h" ] }