diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2012-04-10 18:11:30 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-05-04 13:15:01 +0200 |
commit | 6f3bda0dce945a5fc75d8ebad302820fe9979d9b (patch) | |
tree | 6581aad8a7fb21ccbebe09d23c30af0e3236e266 /tools/qmlbundle/main.cpp | |
parent | 44f9412bf789d73dd462292038686f5b07026132 (diff) |
Initial bundle support
Change-Id: I095249f64ecf4ef1e3fbfb164e3d50edffab61e8
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
Diffstat (limited to 'tools/qmlbundle/main.cpp')
-rw-r--r-- | tools/qmlbundle/main.cpp | 220 |
1 files changed, 220 insertions, 0 deletions
diff --git a/tools/qmlbundle/main.cpp b/tools/qmlbundle/main.cpp new file mode 100644 index 0000000000..16bf20e2cb --- /dev/null +++ b/tools/qmlbundle/main.cpp @@ -0,0 +1,220 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the tools applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <private/qqmlbundle_p.h> +#include <private/qqmlscript_p.h> +#include <QtCore/QtCore> +#include <iostream> + +static bool createBundle(const QString &fileName, const QStringList &fileNames) +{ + QQmlBundle bundle(fileName); + if (!bundle.open(QFile::WriteOnly)) + return false; + foreach (const QString &fileName, fileNames) + bundle.add(fileName); + return true; +} + +static bool removeFiles(const QString &fileName, const QStringList &fileNames) +{ + const QSet<QString> filesToRemove = QSet<QString>::fromList(fileNames); + + QQmlBundle bundle(fileName); + bundle.open(QFile::ReadWrite); + foreach (const QQmlBundle::FileEntry *entry, bundle.files()) { + if (filesToRemove.contains(entry->fileName())) + bundle.remove(entry); + } + return true; +} + +static void showHelp() +{ + std::cerr << "Usage: qmlbundle <command> [<args>]" << std::endl + << std::endl + << "The commands are:" << std::endl + << " create Create a new bundle" << std::endl + << " add Add files to the bundle" << std::endl + << " rm Remove files from the bundle" << std::endl + << " update Add files to the bundle or update them if they are already added" << std::endl + << " ls List the files in the bundle" << std::endl + << " cat Concatenates files and print on the standard output" << std::endl + << " optimize Insert optimization data for all recognised content" << std::endl + << std::endl + << "See 'qmlbundle help <command>' for more information on a specific command." << std::endl; +} + +static void usage(const QString &action, const QString &error = QString()) +{ + if (! error.isEmpty()) + std::cerr << qPrintable(error) << std::endl << std::endl; + + if (action == QLatin1String("create")) { + std::cerr << "usage: qmlbundle create <bundle name> [files]" << std::endl; + } else if (action == QLatin1String("add")) { + std::cerr << "usage: qmlbundle add <bundle name> [files]" << std::endl; + } else if (action == QLatin1String("rm")) { + std::cerr << "usage: qmlbundle rm <bundle name> [files]" << std::endl; + } else if (action == QLatin1String("update")) { + std::cerr << "usage: qmlbundle update <bundle name> [files]" << std::endl; + } else if (action == QLatin1String("ls")) { + std::cerr << "usage: qmlbundle ls <bundle name>" << std::endl; + } else if (action == QLatin1String("cat")) { + std::cerr << "usage: qmlbundle cat <bundle name> [files]" << std::endl; + } else { + showHelp(); + } +} + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + + QStringList args = app.arguments(); + /*const QString exeName =*/ args.takeFirst(); + + if (args.isEmpty()) { + showHelp(); + return 0; + } + + const QString action = args.takeFirst(); + + if (action == QLatin1String("help")) { + if (args.empty()) + showHelp(); + else + usage(args.takeFirst()); + } else if (action == QLatin1String("ls")) { + if (args.isEmpty()) { + usage(action, "You must specify a bundle"); + return EXIT_FAILURE; + } + QQmlBundle bundle(args.takeFirst()); + if (bundle.open(QFile::ReadOnly)) { + foreach (const QQmlBundle::FileEntry *fileEntry, bundle.files()) + std::cout << qPrintable(fileEntry->fileName()) << std::endl; + } + } else if (action == QLatin1String("create")) { + if (args.isEmpty()) { + usage(action, "You must specify a bundle"); + return EXIT_FAILURE; + } + const QString bundleFileName = args.takeFirst(); + createBundle(bundleFileName, args); + } else if (action == QLatin1String("add")) { + if (args.isEmpty()) { + usage(action, "You must specify a bundle"); + return EXIT_FAILURE; + } + const QString bundleFileName = args.takeFirst(); + QQmlBundle bundle(bundleFileName); + bundle.open(); + foreach (const QString &fileName, args) { + if (! bundle.add(fileName)) + std::cerr << "cannot add file " << qPrintable(fileName) << " to " << qPrintable(bundleFileName) << std::endl; + } + } else if (action == QLatin1String("rm")) { + if (args.isEmpty()) { + usage(action, "You must specify a bundle"); + return EXIT_FAILURE; + } + const QString bundleFileName = args.takeFirst(); + removeFiles(bundleFileName, args); + } else if (action == QLatin1String("update")) { + if (args.isEmpty()) { + usage(action, "You must specify a bundle"); + return EXIT_FAILURE; + } + const QString bundleFileName = args.takeFirst(); + removeFiles(bundleFileName, args); + QQmlBundle bundle(bundleFileName); + bundle.open(); + foreach (const QString &fileName, args) { + if (! bundle.add(fileName)) + std::cerr << "cannot add file " << qPrintable(fileName) << " to " << qPrintable(bundleFileName) << std::endl; + } + } else if (action == QLatin1String("cat")) { + if (args.isEmpty()) { + usage(action, "You must specify a bundle"); + return EXIT_FAILURE; + } + const QString bundleFileName = args.takeFirst(); + QQmlBundle bundle(bundleFileName); + if (bundle.open(QFile::ReadOnly)) { + const QSet<QString> filesToShow = QSet<QString>::fromList(args); + + foreach (const QQmlBundle::FileEntry *fileEntry, bundle.files()) { + if (filesToShow.contains(fileEntry->fileName())) + std::cout.write(fileEntry->contents(), fileEntry->fileSize()); + } + } + } else if (action == QLatin1String("optimize")) { + if (args.isEmpty()) { + usage(action, "You must specify a bundle"); + return EXIT_FAILURE; + } + const QString bundleFileName = args.takeFirst(); + QQmlBundle bundle(bundleFileName); + if (bundle.open(QFile::ReadWrite)) { + QList<const QQmlBundle::FileEntry *> files = bundle.files(); + for (int ii = 0; ii < files.count(); ++ii) { + const QQmlBundle::FileEntry *file = files.at(ii); + + if (!file->fileName().endsWith(".qml")) + continue; + + QQmlScript::Parser parser; + QString data = QString::fromUtf8(file->contents(), file->fileSize()); + parser.parse(data, QByteArray()); + QByteArray preparse = parser.preparseData(); + + if (!preparse.isEmpty()) + bundle.addMetaLink(file->fileName(), QLatin1String("qml:preparse"), preparse); + } + } + } else { + showHelp(); + } + + return 0; +} |