Skip to content
Snippets Groups Projects
Commit 7ab30d8a authored by Friedemann Kleint's avatar Friedemann Kleint
Browse files

C++-Settings/License template: Add %FILENAME%, %CLASS% placeholders.

... for doxygen. Change logic for known empty keywords to be replaced
by an empty string.

Task-number: QTCREATORBUG-1854
parent 934a9590
No related branches found
No related tags found
No related merge requests found
Showing
with 78 additions and 45 deletions
......@@ -233,14 +233,19 @@ bool CppClassWizard::generateHeaderAndSource(const CppClassWizardParameters &par
if (namespaceList.empty()) // Paranoia!
return false;
const QString license = CppTools::AbstractEditorSupport::licenseTemplate();
const QString headerLicense =
CppTools::AbstractEditorSupport::licenseTemplate(params.headerFile,
params.className);
const QString sourceLicense =
CppTools::AbstractEditorSupport::licenseTemplate(params.sourceFile,
params.className);
const QString unqualifiedClassName = namespaceList.takeLast();
const QString guard = Utils::headerGuard(params.headerFile);
// == Header file ==
QTextStream headerStr(header);
headerStr << license << "#ifndef " << guard
headerStr << headerLicense << "#ifndef " << guard
<< "\n#define " << guard << '\n';
const QRegExp qtClassExpr(QLatin1String("^Q[A-Z3].+"));
......@@ -328,7 +333,7 @@ bool CppClassWizard::generateHeaderAndSource(const CppClassWizardParameters &par
// == Source file ==
QTextStream sourceStr(source);
sourceStr << license;
sourceStr << sourceLicense;
Utils::writeIncludeFileDirective(params.headerFile, false, sourceStr);
if (params.classType == Utils::NewClassWidget::SharedDataClass)
Utils::writeIncludeFileDirective(QLatin1String("QSharedData"), true, sourceStr);
......
......@@ -68,10 +68,9 @@ Core::GeneratedFiles CppFileWizard::generateFilesFromPath(const QString &path,
QString CppFileWizard::fileContents(FileType type, const QString &fileName) const
{
const QString baseName = QFileInfo(fileName).completeBaseName();
QString contents;
QTextStream str(&contents);
str << CppTools::AbstractEditorSupport::licenseTemplate();
str << CppTools::AbstractEditorSupport::licenseTemplate(fileName);
switch (type) {
case Header: {
const QString guard = Utils::headerGuard(fileName);
......
......@@ -92,9 +92,9 @@ QString AbstractEditorSupport::functionAt(const CppModelManagerInterface *modelM
return QString();
}
QString AbstractEditorSupport::licenseTemplate()
QString AbstractEditorSupport::licenseTemplate(const QString &file, const QString &className)
{
return Internal::CppFileSettings::licenseTemplate();
return Internal::CppFileSettings::licenseTemplate(file, className);
}
}
......@@ -41,6 +41,7 @@
#include <QtCore/QSettings>
#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QCoreApplication>
#include <QtCore/QDate>
#include <QtCore/QLocale>
......@@ -106,10 +107,22 @@ bool CppFileSettings::equals(const CppFileSettings &rhs) const
}
// Replacements of special license template keywords.
static QString keyWordReplacement(const QString &keyWord)
static bool keyWordReplacement(const QString &keyWord,
const QString &file,
const QString &className,
QString *value)
{
if (keyWord == QLatin1String("%YEAR%")) {
return QString::number(QDate::currentDate().year());
*value = QString::number(QDate::currentDate().year());
return true;
}
if (keyWord == QLatin1String("%CLASS%")) {
*value = className;
return true;
}
if (keyWord == QLatin1String("%FILENAME%")) {
*value = QFileInfo(file).fileName();
return true;
}
if (keyWord == QLatin1String("%DATE%")) {
static QString format;
......@@ -121,26 +134,29 @@ static QString keyWordReplacement(const QString &keyWord)
if (format.count(ypsilon) == 2)
format.insert(format.indexOf(ypsilon), QString(2, ypsilon));
}
return QDate::currentDate().toString(format);
*value = QDate::currentDate().toString(format);
return true;
}
if (keyWord == QLatin1String("%USER%")) {
#ifdef Q_OS_WIN
return QString::fromLocal8Bit(qgetenv("USERNAME"));
*value = QString::fromLocal8Bit(qgetenv("USERNAME"));
#else
return QString::fromLocal8Bit(qgetenv("USER"));
*value = QString::fromLocal8Bit(qgetenv("USER"));
#endif
return true;
}
// Environment variables (for example '%$EMAIL%').
if (keyWord.startsWith(QLatin1String("%$"))) {
const QString varName = keyWord.mid(2, keyWord.size() - 3);
return QString::fromLocal8Bit(qgetenv(varName.toLocal8Bit()));
*value = QString::fromLocal8Bit(qgetenv(varName.toLocal8Bit()));
return true;
}
return QString();
return false;
}
// Parse a license template, scan for %KEYWORD% and replace if known.
// Replace '%%' by '%'.
static void parseLicenseTemplatePlaceholders(QString *t)
static void parseLicenseTemplatePlaceholders(QString *t, const QString &file, const QString &className)
{
int pos = 0;
const QChar placeHolder = QLatin1Char('%');
......@@ -156,19 +172,20 @@ static void parseLicenseTemplatePlaceholders(QString *t)
pos = placeHolderPos + 1;
} else {
const QString keyWord = t->mid(placeHolderPos, endPlaceHolderPos + 1 - placeHolderPos);
const QString replacement = keyWordReplacement(keyWord);
if (replacement.isEmpty()) {
pos = endPlaceHolderPos + 1;
} else {
QString replacement;
if (keyWordReplacement(keyWord, file, className, &replacement)) {
t->replace(placeHolderPos, keyWord.size(), replacement);
pos = placeHolderPos + replacement.size();
} else {
// Leave invalid keywords as is.
pos = endPlaceHolderPos + 1;
}
}
} while (pos < t->size());
}
// Convenience that returns the formatted license template.
QString CppFileSettings::licenseTemplate()
QString CppFileSettings::licenseTemplate(const QString &fileName, const QString &className)
{
const QSettings *s = Core::ICore::instance()->settings();
......@@ -184,7 +201,7 @@ QString CppFileSettings::licenseTemplate()
return QString();
}
QString license = QString::fromUtf8(file.readAll());
parseLicenseTemplatePlaceholders(&license);
parseLicenseTemplatePlaceholders(&license, fileName, className);
// Ensure exactly one additional new line separating stuff
const QChar newLine = QLatin1Char('\n');
if (!license.endsWith(newLine))
......
......@@ -60,7 +60,7 @@ struct CppFileSettings
// Convenience to return a license template completely formatted.
// Currently made public in
static QString licenseTemplate();
static QString licenseTemplate(const QString &file = QString(), const QString &className = QString());
bool equals(const CppFileSettings &rhs) const;
};
......
......@@ -170,7 +170,7 @@ public:
const QString &fileName,
int line, int column);
static QString licenseTemplate();
static QString licenseTemplate(const QString &file = QString(), const QString &className = QString());
private:
CppModelManagerInterface *m_modelmanager;
......
......@@ -341,7 +341,10 @@ bool FormClassWizardParametersPrivate::generateCpp(const FormClassWizardGenerati
const QString unqualifiedClassName = namespaceList.takeLast();
const QString license = CppTools::AbstractEditorSupport::licenseTemplate();
const QString headerLicense =
CppTools::AbstractEditorSupport::licenseTemplate(headerFile, className);
const QString sourceLicense =
CppTools::AbstractEditorSupport::licenseTemplate(sourceFile, className);
// Include guards
const QString guard = Utils::headerGuard(headerFile);
......@@ -351,7 +354,7 @@ bool FormClassWizardParametersPrivate::generateCpp(const FormClassWizardGenerati
// 1) Header file
QTextStream headerStr(header);
headerStr << license << "#ifndef " << guard
headerStr << headerLicense << "#ifndef " << guard
<< "\n#define " << guard << '\n' << '\n';
// Include 'ui_'
......@@ -409,7 +412,7 @@ bool FormClassWizardParametersPrivate::generateCpp(const FormClassWizardGenerati
// 2) Source file
QTextStream sourceStr(source);
sourceStr << license;
sourceStr << sourceLicense;
Utils::writeIncludeFileDirective(headerFile, false, sourceStr);
if (embedding == FormClassWizardGenerationParameters::PointerAggregatedUiClass)
Utils::writeIncludeFileDirective(uiInclude, false, sourceStr);
......
......@@ -33,7 +33,6 @@
#include "pluginoptions.h"
#include "filenamingparameters.h"
#include <cpptools/cppmodelmanagerinterface.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <QtGui/QIcon>
......@@ -71,7 +70,6 @@ Core::GeneratedFiles CustomWidgetWizard::generateFiles(const QWizard *w,
GenerationParameters p;
p.fileName = cw->projectName();
p.path = cw->path();
p.license = CppTools::AbstractEditorSupport::licenseTemplate();
p.templatePath = QtWizard::templateDir();
p.templatePath += QLatin1String("/customwidgetwizard");
return PluginGenerator::generatePlugin(p, *(cw->pluginOptions()), errorMessage);
......
......@@ -32,6 +32,8 @@
#include <coreplugin/basefilewizard.h>
#include <cpptools/cppmodelmanagerinterface.h>
#include <QtCore/QFileInfo>
#include <QtCore/QDir>
#include <QtCore/QSet>
......@@ -103,7 +105,8 @@ QList<Core::GeneratedFile> PluginGenerator::generatePlugin(const GenerationPara
if (pluginHeaderContents.isEmpty())
return QList<Core::GeneratedFile>();
Core::GeneratedFile pluginHeader(baseDir + wo.pluginHeaderFile);
pluginHeader.setContents(p.license + pluginHeaderContents);
pluginHeader.setContents(CppTools::AbstractEditorSupport::licenseTemplate(wo.pluginHeaderFile, wo.pluginClassName)
+ pluginHeaderContents);
rc.push_back(pluginHeader);
sm.remove(QLatin1String("SINGLE_INCLUDE_GUARD"));
......@@ -134,7 +137,8 @@ QList<Core::GeneratedFile> PluginGenerator::generatePlugin(const GenerationPara
if (pluginSourceContents.isEmpty())
return QList<Core::GeneratedFile>();
Core::GeneratedFile pluginSource(baseDir + wo.pluginSourceFile);
pluginSource.setContents(p.license + pluginSourceContents);
pluginSource.setContents(CppTools::AbstractEditorSupport::licenseTemplate(wo.pluginSourceFile, wo.pluginClassName)
+ pluginSourceContents);
if (i == 0 && widgetCount == 1) // Open first widget unless collection
pluginSource.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
rc.push_back(pluginSource);
......@@ -179,7 +183,8 @@ QList<Core::GeneratedFile> PluginGenerator::generatePlugin(const GenerationPara
if (widgetHeaderContents.isEmpty())
return QList<Core::GeneratedFile>();
Core::GeneratedFile widgetHeader(baseDir + wo.widgetHeaderFile);
widgetHeader.setContents(p.license + widgetHeaderContents);
widgetHeader.setContents(CppTools::AbstractEditorSupport::licenseTemplate(wo.widgetHeaderFile, wo.widgetClassName)
+ widgetHeaderContents);
rc.push_back(widgetHeader);
sm.remove(QLatin1String("WIDGET_INCLUDE_GUARD"));
......@@ -188,7 +193,8 @@ QList<Core::GeneratedFile> PluginGenerator::generatePlugin(const GenerationPara
if (widgetSourceContents.isEmpty())
return QList<Core::GeneratedFile>();
Core::GeneratedFile widgetSource(baseDir + wo.widgetSourceFile);
widgetSource.setContents(p.license + widgetSourceContents);
widgetSource.setContents(CppTools::AbstractEditorSupport::licenseTemplate(wo.widgetSourceFile, wo.widgetClassName)
+ widgetSourceContents);
rc.push_back(widgetSource);
}
}
......@@ -221,7 +227,8 @@ QList<Core::GeneratedFile> PluginGenerator::generatePlugin(const GenerationPara
if (collectionHeaderContents.isEmpty())
return QList<Core::GeneratedFile>();
Core::GeneratedFile collectionHeader(baseDir + options.collectionHeaderFile);
collectionHeader.setContents(p.license + collectionHeaderContents);
collectionHeader.setContents(CppTools::AbstractEditorSupport::licenseTemplate(options.collectionHeaderFile, options.collectionClassName)
+ collectionHeaderContents);
rc.push_back(collectionHeader);
sm.remove(QLatin1String("COLLECTION_INCLUDE_GUARD"));
......@@ -241,7 +248,8 @@ QList<Core::GeneratedFile> PluginGenerator::generatePlugin(const GenerationPara
if (collectionSourceFileContents.isEmpty())
return QList<Core::GeneratedFile>();
Core::GeneratedFile collectionSource(baseDir + options.collectionSourceFile);
collectionSource.setContents(p.license + collectionSourceFileContents);
collectionSource.setContents(CppTools::AbstractEditorSupport::licenseTemplate(options.collectionSourceFile, options.collectionClassName)
+ collectionSourceFileContents);
collectionSource.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
rc.push_back(collectionSource);
......
......@@ -51,7 +51,6 @@ struct PluginOptions;
struct GenerationParameters {
QString path;
QString fileName;
QString license;
QString templatePath;
};
......
......@@ -81,13 +81,12 @@ Core::GeneratedFiles
const ConsoleAppWizardDialog *wizard = qobject_cast< const ConsoleAppWizardDialog *>(w);
const QtProjectParameters params = wizard->parameters();
const QString projectPath = params.projectPath();
const QString license = CppTools::AbstractEditorSupport::licenseTemplate();
// Create files: Source
const QString sourceFileName = Core::BaseFileWizard::buildFileName(projectPath, QLatin1String(mainSourceFileC), sourceSuffix());
Core::GeneratedFile source(sourceFileName);
source.setContents(license + QLatin1String(mainCppC));
source.setContents(CppTools::AbstractEditorSupport::licenseTemplate(sourceFileName) + QLatin1String(mainCppC));
source.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
// Create files: Profile
const QString profileName = Core::BaseFileWizard::buildFileName(projectPath, params.fileName, profileSuffix());
......
......@@ -165,7 +165,8 @@ Core::GeneratedFiles GuiAppWizard::generateFiles(const QWizard *w,
Core::GeneratedFile mainSource(mainSourceFileName);
if (!parametrizeTemplate(templatePath, QLatin1String("main.cpp"), params, &contents, errorMessage))
return Core::GeneratedFiles();
mainSource.setContents(license + contents);
mainSource.setContents(CppTools::AbstractEditorSupport::licenseTemplate(mainSourceFileName)
+ contents);
// Create files: form source with or without form
const QString formSourceFileName = buildFileName(projectPath, params.sourceFileName, sourceSuffix());
const QString formHeaderName = buildFileName(projectPath, params.headerFileName, headerSuffix());
......@@ -187,13 +188,15 @@ Core::GeneratedFiles GuiAppWizard::generateFiles(const QWizard *w,
const QString formSourceTemplate = QLatin1String("mywidget.cpp");
if (!parametrizeTemplate(templatePath, formSourceTemplate, params, &contents, errorMessage))
return Core::GeneratedFiles();
formSource.setContents(license + contents);
formSource.setContents(CppTools::AbstractEditorSupport::licenseTemplate(formSourceFileName)
+ contents);
formSource.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
// Create files: form header
const QString formHeaderTemplate = QLatin1String("mywidget.h");
if (!parametrizeTemplate(templatePath, formHeaderTemplate, params, &contents, errorMessage))
return Core::GeneratedFiles();
formHeader.setContents(license + contents);
formHeader.setContents(CppTools::AbstractEditorSupport::licenseTemplate(formHeaderName)
+ contents);
}
// Create files: profile
const QString profileName = buildFileName(projectPath, projectParams.fileName, profileSuffix());
......
......@@ -82,7 +82,6 @@ Core::GeneratedFiles LibraryWizard::generateFiles(const QWizard *w,
const QtProjectParameters projectParams = dialog->parameters();
const QString projectPath = projectParams.projectPath();
const LibraryParameters params = dialog->libraryParameters();
const QString license = CppTools::AbstractEditorSupport::licenseTemplate();
const QString sharedLibExportMacro = QtProjectParameters::exportMacro(projectParams.fileName);
......@@ -102,7 +101,8 @@ Core::GeneratedFiles LibraryWizard::generateFiles(const QWizard *w,
const QString globalHeaderName = buildFileName(projectPath, projectParams.fileName + QLatin1String(sharedHeaderPostfixC), headerSuffix());
Core::GeneratedFile globalHeader(globalHeaderName);
globalHeaderFileName = QFileInfo(globalHeader.path()).fileName();
globalHeader.setContents(license + LibraryParameters::generateSharedHeader(globalHeaderFileName, projectParams.fileName, sharedLibExportMacro));
globalHeader.setContents(CppTools::AbstractEditorSupport::licenseTemplate(globalHeaderFileName)
+ LibraryParameters::generateSharedHeader(globalHeaderFileName, projectParams.fileName, sharedLibExportMacro));
rc.push_back(globalHeader);
}
......@@ -112,8 +112,10 @@ Core::GeneratedFiles LibraryWizard::generateFiles(const QWizard *w,
globalHeaderFileName, sharedLibExportMacro,
/* indentation*/ 4, &headerContents, &sourceContents);
source.setContents(license + sourceContents);
header.setContents(license + headerContents);
source.setContents(CppTools::AbstractEditorSupport::licenseTemplate(sourceFileName, params.className)
+ sourceContents);
header.setContents(CppTools::AbstractEditorSupport::licenseTemplate(headerFileFullName, params.className)
+ headerContents);
rc.push_back(source);
rc.push_back(header);
// Create files: profile
......
......@@ -96,7 +96,7 @@ static QString generateTestCode(const TestWizardParameters &testParams,
const QString indent = QString(4, QLatin1Char(' '));
QTextStream str(&rc);
// Includes
str << CppTools::AbstractEditorSupport::licenseTemplate()
str << CppTools::AbstractEditorSupport::licenseTemplate(testParams.fileName, testParams.className)
<< "#include <QtCore/QString>\n#include <QtTest/QtTest>\n";
if (testParams.requiresQApplication)
str << "#include <QtCore/QCoreApplication>\n";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment