2015-05-04 14:33:19 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-05-04 14:33:19 +02:00
|
|
|
**
|
|
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2015-05-04 14:33:19 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2015-05-04 14:33:19 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "jsonwizardscannergenerator.h"
|
|
|
|
|
|
|
|
|
|
#include "../projectexplorer.h"
|
2017-03-03 18:16:34 +01:00
|
|
|
#include "../projectmanager.h"
|
2015-05-04 14:33:19 +02:00
|
|
|
#include "jsonwizard.h"
|
|
|
|
|
#include "jsonwizardfactory.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
|
2015-05-18 15:11:47 +02:00
|
|
|
#include <utils/algorithm.h>
|
2015-05-04 14:33:19 +02:00
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
#include <utils/macroexpander.h>
|
2022-02-23 17:11:20 +01:00
|
|
|
#include <utils/mimeutils.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
2015-05-04 14:33:19 +02:00
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QVariant>
|
|
|
|
|
|
2019-04-10 18:15:47 +02:00
|
|
|
#include <limits>
|
|
|
|
|
|
2015-05-04 14:33:19 +02:00
|
|
|
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();
|
2022-05-03 15:40:40 +02:00
|
|
|
const QStringList patterns = gen.value(QLatin1String("subdirectoryPatterns")).toStringList();
|
|
|
|
|
for (const QString &pattern : patterns) {
|
2015-05-04 14:33:19 +02:00
|
|
|
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)
|
|
|
|
|
{
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(wizardDir)
|
2015-05-04 14:33:19 +02:00
|
|
|
errorMessage->clear();
|
|
|
|
|
|
|
|
|
|
QDir project(projectDir);
|
|
|
|
|
Core::GeneratedFiles result;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2020-07-21 15:47:35 +02:00
|
|
|
static const auto getDepth = [](const QString &filePath) { return int(filePath.count('/')); };
|
2019-04-10 18:15:47 +02:00
|
|
|
int minDepth = std::numeric_limits<int>::max();
|
2015-05-04 14:33:19 +02:00
|
|
|
for (auto it = result.begin(); it != result.end(); ++it) {
|
|
|
|
|
const QString relPath = project.relativeFilePath(it->path());
|
|
|
|
|
it->setBinary(binaryPattern.match(relPath).hasMatch());
|
2017-03-03 18:16:34 +01:00
|
|
|
bool found = ProjectManager::canOpenProjectForMimeType(Utils::mimeTypeForFile(relPath));
|
2019-04-10 18:15:47 +02:00
|
|
|
if (found) {
|
2017-03-02 08:47:06 +01:00
|
|
|
it->setAttributes(it->attributes() | Core::GeneratedFile::OpenProjectAttribute);
|
2019-04-10 18:15:47 +02:00
|
|
|
minDepth = std::min(minDepth, getDepth(it->path()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Project files that appear on a lower level in the file system hierarchy than
|
|
|
|
|
// other project files are not candidates for opening.
|
|
|
|
|
for (Core::GeneratedFile &f : result) {
|
|
|
|
|
if (f.attributes().testFlag(Core::GeneratedFile::OpenProjectAttribute)
|
|
|
|
|
&& getDepth(f.path()) > minDepth) {
|
|
|
|
|
f.setAttributes(f.attributes().setFlag(Core::GeneratedFile::OpenProjectAttribute,
|
|
|
|
|
false));
|
|
|
|
|
}
|
2015-05-04 14:33:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool JsonWizardScannerGenerator::matchesSubdirectoryPattern(const QString &path)
|
|
|
|
|
{
|
2022-05-03 15:40:40 +02:00
|
|
|
for (const QRegularExpression ®exp : qAsConst(m_subDirectoryExpressions)) {
|
2015-05-04 14:33:19 +02:00
|
|
|
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;
|
|
|
|
|
|
2022-05-03 15:40:40 +02:00
|
|
|
const QFileInfoList entries = directory.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot,
|
|
|
|
|
QDir::DirsLast | QDir::Name);
|
|
|
|
|
for (const QFileInfo &fi : entries) {
|
2015-05-04 14:33:19 +02:00
|
|
|
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
|