Commit 89f100ad authored by Tobias Hunger's avatar Tobias Hunger
Browse files

JsonWizard: Add a generator that just scans an existing directory



This will be used in the VCS checkout wizards.

Change-Id: I68bbdefa6b8287dc50a4ec8a9ca6136831571d3b
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
parent 40a76e2b
......@@ -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
......@@ -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<Internal::JsonWizardFileGenerator> gen(new Internal::JsonWizardFileGenerator());
if (!gen->setup(data, errorMessage))
return false;
QScopedPointer<JsonWizardFileGenerator> gen(new JsonWizardFileGenerator);
return gen->setup(data, errorMessage);
}
return true;
// --------------------------------------------------------------------
// 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<JsonWizardScannerGenerator> gen(new JsonWizardScannerGenerator);
return gen->setup(data, errorMessage);
}
} // namespace Internal
} // namespace ProjectExplorer
......@@ -94,6 +94,8 @@ private:
QList<Core::Id> 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
/****************************************************************************
**
** 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 <coreplugin/editormanager/editormanager.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <utils/macroexpander.h>
#include <utils/mimetypes/mimedatabase.h>
#include <QCoreApplication>
#include <QDir>
#include <QVariant>
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<IProjectManager *> projectManagers
= ExtensionSystem::PluginManager::getObjects<IProjectManager>();
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 &regexp, 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<QFileInfo> 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
/****************************************************************************
**
** 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 <QDir>
#include <QRegularExpression>
#include <QVariant>
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<QRegularExpression> m_subDirectoryExpressions;
};
} // namespace Internal
} // namespace ProjectExplorer
#endif // JSONWIZARDSCANNERGENERATOR_H
......@@ -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);
......
......@@ -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"
]
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment