diff options
| author | Christian Kandeler <christian.kandeler@digia.com> | 2013-11-19 15:27:07 +0100 |
|---|---|---|
| committer | Christian Kandeler <christian.kandeler@digia.com> | 2013-11-21 15:20:34 +0100 |
| commit | 146a9377c2cfb1ba26f6243509a4db40342be927 (patch) | |
| tree | 85e6afc20b252f3971c95d3ca5cac5cdd6190ed8 | |
| parent | 823b16071b85b7e2f8f340f64fbf4ddabeb7db41 (diff) | |
| download | qbs-146a9377c2cfb1ba26f6243509a4db40342be927.tar.gz | |
API: Allow adding files to a group with a directory prefix.
This case is not problematic.
Change-Id: I388df6180b231364a8e185eb85aee5adff417965
Reviewed-by: Joerg Bornemann <joerg.bornemann@digia.com>
| -rw-r--r-- | src/lib/api/project.cpp | 19 | ||||
| -rw-r--r-- | src/lib/api/projectfileupdater.cpp | 7 | ||||
| -rw-r--r-- | tests/auto/api/testdata/project-editing/project.qbs | 10 | ||||
| -rw-r--r-- | tests/auto/api/testdata/project-editing/subdir/file.txt | 0 | ||||
| -rw-r--r-- | tests/auto/api/tst_api.cpp | 25 |
5 files changed, 45 insertions, 16 deletions
diff --git a/src/lib/api/project.cpp b/src/lib/api/project.cpp index 56dc09b64..affe2c9f4 100644 --- a/src/lib/api/project.cpp +++ b/src/lib/api/project.cpp @@ -350,18 +350,21 @@ void ProjectPrivate::addFiles(const ProductData &product, const GroupData &group if (filePaths.isEmpty()) return; // "No-op". Could also be defined to be an error, I guess. - if (!resolvedGroup->prefix.isEmpty()) - throw ErrorInfo(Tr::tr("Cannot add files to a group with a prefix.")); + if (!resolvedGroup->prefix.isEmpty() && !resolvedGroup->prefix.endsWith(QLatin1Char('/'))) + throw ErrorInfo(Tr::tr("Cannot add files to a group with a non-directory prefix.")); QStringList absoluteFilePaths; - QString projectFileDir = QFileInfo(product.location().fileName()).dir().absolutePath(); + QStringList relativeFilePaths; + QString baseDirPath = QFileInfo(product.location().fileName()).dir().absolutePath() + + QLatin1Char('/') + resolvedGroup->prefix; + QDir baseDir(baseDirPath); foreach (const QString &filePath, filePaths) { - const QString absPath = QDir::cleanPath(FileInfo::resolvePath(projectFileDir, - filePath)); + const QString absPath = QDir::cleanPath(FileInfo::resolvePath(baseDirPath, filePath)); if (absoluteFilePaths.contains(absPath)) throw ErrorInfo(Tr::tr("File '%1' appears more than once.").arg(absPath)); if (!FileInfo(absPath).exists()) throw ErrorInfo(Tr::tr("File '%1' does not exist.").arg(absPath)); absoluteFilePaths << absPath; + relativeFilePaths << baseDir.relativeFilePath(absPath); } // We do not check for entries in other groups, because such doublettes might be legitimate @@ -376,7 +379,7 @@ void ProjectPrivate::addFiles(const ProductData &product, const GroupData &group } ProjectFileFilesAdder adder(currentProduct, group.isValid() ? currentGroup : GroupData(), - absoluteFilePaths); + relativeFilePaths); adder.apply(); m_projectData.d.detach(); @@ -831,8 +834,8 @@ ErrorInfo Project::addGroup(const ProductData &product, const QString &groupName * \brief Adds the given files to the given product. * If \c group is a default-constructed object, the files will be added to the product's * "files" property, otherwise to the one of \c group. - * The file paths can be absolute or relative to the location of \c product. The project file - * will always contain relative paths. + * The file paths can be absolute or relative to the location of \c product (including a possible + * prefix in the group). The project file will always contain relative paths. * After calling this function, it is recommended to re-fetch the project data, as other * items can be affected. * \sa qbs::Project::projectData() diff --git a/src/lib/api/projectfileupdater.cpp b/src/lib/api/projectfileupdater.cpp index 4849ef6c4..3c35af161 100644 --- a/src/lib/api/projectfileupdater.cpp +++ b/src/lib/api/projectfileupdater.cpp @@ -41,9 +41,7 @@ #include <parser/qmljsparser_p.h> #include <tools/qbsassert.h> -#include <QDir> #include <QFile> -#include <QFileInfo> using namespace QbsQmlJS; using namespace AST; @@ -220,11 +218,10 @@ void ProjectFileFilesAdder::doApply(QString &fileContent, UiProgram *ast) const int bindingIndentation = itemIndentation + 4; const int arrayElemIndentation = bindingIndentation + 4; QString newFilesString; - const QDir projectFileDir = QFileInfo(projectFile()).dir(); - foreach (const QString &absFilePath, m_files) { + foreach (const QString &relFilePath, m_files) { newFilesString += QString(arrayElemIndentation, QLatin1Char(' ')); newFilesString += QLatin1Char('"'); - newFilesString += projectFileDir.relativeFilePath(absFilePath); + newFilesString += relFilePath; newFilesString += QLatin1Char('"'); newFilesString += QLatin1String(",\n"); } diff --git a/tests/auto/api/testdata/project-editing/project.qbs b/tests/auto/api/testdata/project-editing/project.qbs index 1d7704fe6..9190f846c 100644 --- a/tests/auto/api/testdata/project-editing/project.qbs +++ b/tests/auto/api/testdata/project-editing/project.qbs @@ -17,5 +17,15 @@ CppApplication { return file; } } + Group { + name: "Existing Group 4" + prefix: "subdir/" + files: [] + } + Group { + name: "Existing Group 5" + prefix: "blubb" + files: [] + } files: "main.cpp" } diff --git a/tests/auto/api/testdata/project-editing/subdir/file.txt b/tests/auto/api/testdata/project-editing/subdir/file.txt new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/auto/api/testdata/project-editing/subdir/file.txt diff --git a/tests/auto/api/tst_api.cpp b/tests/auto/api/tst_api.cpp index c33f88010..536603d7d 100644 --- a/tests/auto/api/tst_api.cpp +++ b/tests/auto/api/tst_api.cpp @@ -130,7 +130,7 @@ void TestApi::changeContent() qbs::ProjectData projectData = project.projectData(); QCOMPARE(projectData.allProducts().count(), 1); qbs::ProductData product = projectData.allProducts().first(); - QCOMPARE(product.groups().count(), 4); + QCOMPARE(product.groups().count(), 6); // Error handling: Invalid product. qbs::ErrorInfo errorInfo = project.addGroup(qbs::ProductData(), "blubb"); @@ -163,7 +163,7 @@ void TestApi::changeContent() projectData = project.projectData(); QVERIFY(projectData.products().count() == 1); product = projectData.products().first(); - QCOMPARE(product.groups().count(), 6); + QCOMPARE(product.groups().count(), 8); qbs::GroupData group = findGroup(product, "New Group 1"); QVERIFY(group.isValid()); errorInfo = project.addFiles(product, group, QStringList() << "file.h" << "file.cpp"); @@ -173,7 +173,7 @@ void TestApi::changeContent() projectData = project.projectData(); QVERIFY(projectData.products().count() == 1); product = projectData.products().first(); - QCOMPARE(product.groups().count(), 6); + QCOMPARE(product.groups().count(), 8); group = findGroup(product, "New Group 1"); QVERIFY(group.isValid()); errorInfo = project.addFiles(product, group, QStringList() << "file.cpp"); @@ -215,6 +215,25 @@ void TestApi::changeContent() QVERIFY(errorInfo.hasError()); QVERIFY2(errorInfo.toString().contains("complex"), qPrintable(errorInfo.toString())); + // Add file to group with directory prefix. + projectData = project.projectData(); + QVERIFY(projectData.products().count() == 1); + product = projectData.products().first(); + group = findGroup(product, "Existing Group 4"); + QVERIFY(group.isValid()); + errorInfo = project.addFiles(product, group, QStringList() << "file.txt"); + QVERIFY2(!errorInfo.hasError(), qPrintable(errorInfo.toString())); + + // Error handling: Add file to group with non-directory prefix. + projectData = project.projectData(); + QVERIFY(projectData.products().count() == 1); + product = projectData.products().first(); + group = findGroup(product, "Existing Group 5"); + QVERIFY(group.isValid()); + errorInfo = project.addFiles(product, group, QStringList() << "newfile1.txt"); + QVERIFY(errorInfo.hasError()); + QVERIFY2(errorInfo.toString().contains("prefix"), qPrintable(errorInfo.toString())); + // Check whether building will take the newly added cpp file into account. // This must not be moved below the re-resolving test!!! qbs::BuildOptions buildOptions; |
