forked from qt-creator/qt-creator
JsonWizard: Enable directories as file source in file generator
Change-Id: I303f31311a619a86c5669ec7b15c341ab77a2c48 Reviewed-by: Tim Jenssen <tim.jenssen@theqtcompany.com> Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
committed by
Tobias Hunger
parent
79f82d0d0a
commit
db16798e0d
@@ -40,9 +40,11 @@
|
|||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
#include <utils/macroexpander.h>
|
#include <utils/macroexpander.h>
|
||||||
#include <utils/templateengine.h>
|
#include <utils/templateengine.h>
|
||||||
|
#include <utils/algorithm.h>
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
#include <QDirIterator>
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
|
|
||||||
namespace ProjectExplorer {
|
namespace ProjectExplorer {
|
||||||
@@ -108,6 +110,69 @@ bool JsonWizardFileGenerator::setup(const QVariant &data, QString *errorMessage)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Core::GeneratedFile JsonWizardFileGenerator::generateFile(const File &file,
|
||||||
|
Utils::MacroExpander *expander, QString *errorMessage)
|
||||||
|
{
|
||||||
|
// Read contents of source file
|
||||||
|
const QFile::OpenMode openMode = file.isBinary.toBool() ?
|
||||||
|
QIODevice::ReadOnly : (QIODevice::ReadOnly|QIODevice::Text);
|
||||||
|
|
||||||
|
Utils::FileReader reader;
|
||||||
|
if (!reader.fetch(file.source, openMode, errorMessage))
|
||||||
|
return Core::GeneratedFile();
|
||||||
|
|
||||||
|
// Generate file information:
|
||||||
|
Core::GeneratedFile gf;
|
||||||
|
gf.setPath(file.target);
|
||||||
|
|
||||||
|
if (!file.keepExisting) {
|
||||||
|
if (file.isBinary.toBool()) {
|
||||||
|
gf.setBinary(true);
|
||||||
|
gf.setBinaryContents(reader.data());
|
||||||
|
} else {
|
||||||
|
// TODO: Document that input files are UTF8 encoded!
|
||||||
|
gf.setBinary(false);
|
||||||
|
Utils::MacroExpander nested;
|
||||||
|
const File *fPtr = &file;
|
||||||
|
Utils::MacroExpander *thisExpander = &nested;
|
||||||
|
nested.registerExtraResolver([fPtr, thisExpander](QString n, QString *ret) -> bool {
|
||||||
|
foreach (const File::OptionDefinition &od, fPtr->options) {
|
||||||
|
if (!JsonWizard::boolFromVariant(od.condition, thisExpander))
|
||||||
|
continue;
|
||||||
|
if (n == od.key) {
|
||||||
|
*ret = od.value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
nested.registerExtraResolver([expander](QString n, QString *ret) { return expander->resolveMacro(n, ret); });
|
||||||
|
|
||||||
|
gf.setContents(Utils::TemplateEngine::processText(&nested, QString::fromUtf8(reader.data()),
|
||||||
|
errorMessage));
|
||||||
|
if (!errorMessage->isEmpty()) {
|
||||||
|
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", "When processing \"%1\":<br>%2")
|
||||||
|
.arg(file.source, *errorMessage);
|
||||||
|
return Core::GeneratedFile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Core::GeneratedFile::Attributes attributes = 0;
|
||||||
|
if (JsonWizard::boolFromVariant(file.openInEditor, expander))
|
||||||
|
attributes |= Core::GeneratedFile::OpenEditorAttribute;
|
||||||
|
if (JsonWizard::boolFromVariant(file.openAsProject, expander))
|
||||||
|
attributes |= Core::GeneratedFile::OpenProjectAttribute;
|
||||||
|
if (JsonWizard::boolFromVariant(file.overwrite, expander))
|
||||||
|
attributes |= Core::GeneratedFile::ForceOverwrite;
|
||||||
|
|
||||||
|
if (file.keepExisting)
|
||||||
|
attributes |= Core::GeneratedFile::KeepExistingFileAttribute;
|
||||||
|
|
||||||
|
gf.setAttributes(attributes);
|
||||||
|
return gf;
|
||||||
|
}
|
||||||
|
|
||||||
Core::GeneratedFiles JsonWizardFileGenerator::fileList(Utils::MacroExpander *expander,
|
Core::GeneratedFiles JsonWizardFileGenerator::fileList(Utils::MacroExpander *expander,
|
||||||
const QString &wizardDir, const QString &projectDir,
|
const QString &wizardDir, const QString &projectDir,
|
||||||
QString *errorMessage)
|
QString *errorMessage)
|
||||||
@@ -117,79 +182,64 @@ Core::GeneratedFiles JsonWizardFileGenerator::fileList(Utils::MacroExpander *exp
|
|||||||
QDir wizard(wizardDir);
|
QDir wizard(wizardDir);
|
||||||
QDir project(projectDir);
|
QDir project(projectDir);
|
||||||
|
|
||||||
Core::GeneratedFiles result;
|
const QList<File> enabledFiles
|
||||||
|
= Utils::filtered(m_fileList, [&expander](const File &f) {
|
||||||
|
return JsonWizard::boolFromVariant(f.condition, expander);
|
||||||
|
});
|
||||||
|
|
||||||
foreach (const File &f, m_fileList) {
|
const QList<File> concreteFiles
|
||||||
if (!JsonWizard::boolFromVariant(f.condition, expander))
|
= Utils::transform(enabledFiles,
|
||||||
continue;
|
[&expander, &wizard, &project](const File &f) -> File {
|
||||||
|
// Return a new file with concrete values based on input file:
|
||||||
|
File file = f;
|
||||||
|
|
||||||
const bool keepExisting = f.source.isEmpty();
|
file.keepExisting = file.source.isEmpty();
|
||||||
const QString targetPath = project.absoluteFilePath(expander->expand(f.target));
|
file.target = project.absoluteFilePath(expander->expand(file.target));
|
||||||
const QString sourcePath
|
file.source = file.keepExisting ? file.target : wizard.absoluteFilePath(
|
||||||
= keepExisting ? targetPath : wizard.absoluteFilePath(expander->expand(f.source));
|
expander->expand(file.source));
|
||||||
const bool isBinary = JsonWizard::boolFromVariant(f.isBinary, expander);
|
file.isBinary = JsonWizard::boolFromVariant(file.isBinary, expander);
|
||||||
|
|
||||||
// Read contents of source file
|
return file;
|
||||||
const QFile::OpenMode openMode
|
});
|
||||||
= JsonWizard::boolFromVariant(isBinary, expander)
|
|
||||||
? QIODevice::ReadOnly : (QIODevice::ReadOnly|QIODevice::Text);
|
|
||||||
|
|
||||||
Utils::FileReader reader;
|
QList<File> fileList;
|
||||||
if (!reader.fetch(sourcePath, openMode, errorMessage))
|
QList<File> dirList;
|
||||||
return Core::GeneratedFiles();
|
std::tie(fileList, dirList)
|
||||||
|
= Utils::partition(concreteFiles, [](const File &f) { return !QFileInfo(f.source).isDir(); });
|
||||||
|
|
||||||
// Generate file information:
|
const QSet<QString> knownFiles
|
||||||
Core::GeneratedFile gf;
|
= QSet<QString>::fromList(Utils::transform(fileList, [](const File &f) { return f.target; }));
|
||||||
gf.setPath(targetPath);
|
|
||||||
|
|
||||||
if (!keepExisting) {
|
foreach (const File &dir, dirList) {
|
||||||
if (isBinary) {
|
QDir sourceDir(dir.source);
|
||||||
gf.setBinary(true);
|
QDirIterator it(dir.source, QDir::NoDotAndDotDot | QDir::Files| QDir::Hidden,
|
||||||
gf.setBinaryContents(reader.data());
|
QDirIterator::Subdirectories);
|
||||||
} else {
|
|
||||||
// TODO: Document that input files are UTF8 encoded!
|
|
||||||
gf.setBinary(false);
|
|
||||||
Utils::MacroExpander nested;
|
|
||||||
const File *fPtr = &f;
|
|
||||||
Utils::MacroExpander *thisExpander = &nested;
|
|
||||||
nested.registerExtraResolver([fPtr, thisExpander](QString n, QString *ret) -> bool {
|
|
||||||
foreach (const File::OptionDefinition &od, fPtr->options) {
|
|
||||||
if (!JsonWizard::boolFromVariant(od.condition, thisExpander))
|
|
||||||
continue;
|
|
||||||
if (n == od.key) {
|
|
||||||
*ret = od.value;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
nested.registerExtraResolver([expander](QString n, QString *ret) { return expander->resolveMacro(n, ret); });
|
|
||||||
|
|
||||||
gf.setContents(Utils::TemplateEngine::processText(&nested, QString::fromUtf8(reader.data()),
|
while (it.hasNext()) {
|
||||||
errorMessage));
|
const QString relativeFilePath = sourceDir.relativeFilePath(it.next());
|
||||||
if (!errorMessage->isEmpty()) {
|
const QString targetPath = dir.target + QLatin1Char('/') + relativeFilePath;
|
||||||
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", "When processing \"%1\":<br>%2")
|
|
||||||
.arg(sourcePath, *errorMessage);
|
if (knownFiles.contains(targetPath))
|
||||||
return Core::GeneratedFiles();
|
continue;
|
||||||
}
|
|
||||||
}
|
// initialize each new file with properties (isBinary etc)
|
||||||
|
// from the current directory json entry
|
||||||
|
File newFile = dir;
|
||||||
|
newFile.source = dir.source + QLatin1Char('/') + relativeFilePath;
|
||||||
|
newFile.target = targetPath;
|
||||||
|
fileList.append(newFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
Core::GeneratedFile::Attributes attributes = 0;
|
|
||||||
if (JsonWizard::boolFromVariant(f.openInEditor, expander))
|
|
||||||
attributes |= Core::GeneratedFile::OpenEditorAttribute;
|
|
||||||
if (JsonWizard::boolFromVariant(f.openAsProject, expander))
|
|
||||||
attributes |= Core::GeneratedFile::OpenProjectAttribute;
|
|
||||||
if (JsonWizard::boolFromVariant(f.overwrite, expander))
|
|
||||||
attributes |= Core::GeneratedFile::ForceOverwrite;
|
|
||||||
|
|
||||||
if (keepExisting)
|
|
||||||
attributes |= Core::GeneratedFile::KeepExistingFileAttribute;
|
|
||||||
|
|
||||||
gf.setAttributes(attributes);
|
|
||||||
result.append(gf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Core::GeneratedFiles result
|
||||||
|
= Utils::transform(fileList,
|
||||||
|
[this, &expander, &errorMessage](const File &f) {
|
||||||
|
return generateFile(f, expander, errorMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (Utils::contains(result, [](const Core::GeneratedFile &gf) { return gf.path().isEmpty(); }))
|
||||||
|
return Core::GeneratedFiles();
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,18 +53,14 @@ public:
|
|||||||
private:
|
private:
|
||||||
class File {
|
class File {
|
||||||
public:
|
public:
|
||||||
File() :
|
bool keepExisting = false;
|
||||||
condition(true), isBinary(false), overwrite(false),
|
|
||||||
openInEditor(false), openAsProject(false)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
QString source;
|
QString source;
|
||||||
QString target;
|
QString target;
|
||||||
QVariant condition;
|
QVariant condition = true;
|
||||||
QVariant isBinary;
|
QVariant isBinary = false;
|
||||||
QVariant overwrite;
|
QVariant overwrite = false;
|
||||||
QVariant openInEditor;
|
QVariant openInEditor = false;
|
||||||
QVariant openAsProject;
|
QVariant openAsProject = false;
|
||||||
|
|
||||||
class OptionDefinition {
|
class OptionDefinition {
|
||||||
public:
|
public:
|
||||||
@@ -75,6 +71,9 @@ private:
|
|||||||
QList<OptionDefinition> options;
|
QList<OptionDefinition> options;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Core::GeneratedFile generateFile(const File &file, Utils::MacroExpander *expander,
|
||||||
|
QString *errorMessage);
|
||||||
|
|
||||||
QList<File> m_fileList;
|
QList<File> m_fileList;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user