1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the tools applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <private/qqmlbundle_p.h>
43 #include <private/qqmlscript_p.h>
44 #include <QtCore/QtCore>
47 static bool createBundle(const QString &fileName, const QStringList &fileNames)
49 QQmlBundle bundle(fileName);
50 if (!bundle.open(QFile::WriteOnly))
52 foreach (const QString &fileName, fileNames)
57 static bool removeFiles(const QString &fileName, const QStringList &fileNames)
59 const QSet<QString> filesToRemove = QSet<QString>::fromList(fileNames);
61 QQmlBundle bundle(fileName);
62 bundle.open(QFile::ReadWrite);
63 foreach (const QQmlBundle::FileEntry *entry, bundle.files()) {
64 if (filesToRemove.contains(entry->fileName()))
70 static void showHelp()
72 std::cerr << "Usage: qmlbundle <command> [<args>]" << std::endl
74 << "The commands are:" << std::endl
75 << " create Create a new bundle" << std::endl
76 << " add Add files to the bundle" << std::endl
77 << " rm Remove files from the bundle" << std::endl
78 << " update Add files to the bundle or update them if they are already added" << std::endl
79 << " ls List the files in the bundle" << std::endl
80 << " cat Concatenates files and print on the standard output" << std::endl
81 << " optimize Insert optimization data for all recognised content" << std::endl
83 << "See 'qmlbundle help <command>' for more information on a specific command." << std::endl;
86 static void usage(const QString &action, const QString &error = QString())
88 if (! error.isEmpty())
89 std::cerr << qPrintable(error) << std::endl << std::endl;
91 if (action == QLatin1String("create")) {
92 std::cerr << "usage: qmlbundle create <bundle name> [files]" << std::endl;
93 } else if (action == QLatin1String("add")) {
94 std::cerr << "usage: qmlbundle add <bundle name> [files]" << std::endl;
95 } else if (action == QLatin1String("rm")) {
96 std::cerr << "usage: qmlbundle rm <bundle name> [files]" << std::endl;
97 } else if (action == QLatin1String("update")) {
98 std::cerr << "usage: qmlbundle update <bundle name> [files]" << std::endl;
99 } else if (action == QLatin1String("ls")) {
100 std::cerr << "usage: qmlbundle ls <bundle name>" << std::endl;
101 } else if (action == QLatin1String("cat")) {
102 std::cerr << "usage: qmlbundle cat <bundle name> [files]" << std::endl;
108 int main(int argc, char *argv[])
110 QCoreApplication app(argc, argv);
112 QStringList args = app.arguments();
113 /*const QString exeName =*/ args.takeFirst();
115 if (args.isEmpty()) {
120 const QString action = args.takeFirst();
122 if (action == QLatin1String("help")) {
126 usage(args.takeFirst());
127 } else if (action == QLatin1String("ls")) {
128 if (args.isEmpty()) {
129 usage(action, "You must specify a bundle");
132 QQmlBundle bundle(args.takeFirst());
133 if (bundle.open(QFile::ReadOnly)) {
134 foreach (const QQmlBundle::FileEntry *fileEntry, bundle.files())
135 std::cout << qPrintable(fileEntry->fileName()) << std::endl;
137 } else if (action == QLatin1String("create")) {
138 if (args.isEmpty()) {
139 usage(action, "You must specify a bundle");
142 const QString bundleFileName = args.takeFirst();
143 createBundle(bundleFileName, args);
144 } else if (action == QLatin1String("add")) {
145 if (args.isEmpty()) {
146 usage(action, "You must specify a bundle");
149 const QString bundleFileName = args.takeFirst();
150 QQmlBundle bundle(bundleFileName);
152 foreach (const QString &fileName, args) {
153 if (! bundle.add(fileName))
154 std::cerr << "cannot add file " << qPrintable(fileName) << " to " << qPrintable(bundleFileName) << std::endl;
156 } else if (action == QLatin1String("rm")) {
157 if (args.isEmpty()) {
158 usage(action, "You must specify a bundle");
161 const QString bundleFileName = args.takeFirst();
162 removeFiles(bundleFileName, args);
163 } else if (action == QLatin1String("update")) {
164 if (args.isEmpty()) {
165 usage(action, "You must specify a bundle");
168 const QString bundleFileName = args.takeFirst();
169 removeFiles(bundleFileName, args);
170 QQmlBundle bundle(bundleFileName);
172 foreach (const QString &fileName, args) {
173 if (! bundle.add(fileName))
174 std::cerr << "cannot add file " << qPrintable(fileName) << " to " << qPrintable(bundleFileName) << std::endl;
176 } else if (action == QLatin1String("cat")) {
177 if (args.isEmpty()) {
178 usage(action, "You must specify a bundle");
181 const QString bundleFileName = args.takeFirst();
182 QQmlBundle bundle(bundleFileName);
183 if (bundle.open(QFile::ReadOnly)) {
184 const QSet<QString> filesToShow = QSet<QString>::fromList(args);
186 foreach (const QQmlBundle::FileEntry *fileEntry, bundle.files()) {
187 if (filesToShow.contains(fileEntry->fileName()))
188 std::cout.write(fileEntry->contents(), fileEntry->fileSize());
191 } else if (action == QLatin1String("optimize")) {
192 if (args.isEmpty()) {
193 usage(action, "You must specify a bundle");
196 const QString bundleFileName = args.takeFirst();
197 QQmlBundle bundle(bundleFileName);
198 if (bundle.open(QFile::ReadWrite)) {
199 QList<const QQmlBundle::FileEntry *> files = bundle.files();
200 for (int ii = 0; ii < files.count(); ++ii) {
201 const QQmlBundle::FileEntry *file = files.at(ii);
203 if (!file->fileName().endsWith(".qml"))
206 QQmlScript::Parser parser;
207 QString data = QString::fromUtf8(file->contents(), file->fileSize());
208 parser.parse(data, QByteArray());
209 QByteArray preparse = parser.preparseData();
211 if (!preparse.isEmpty())
212 bundle.addMetaLink(file->fileName(), QLatin1String("qml:preparse"), preparse);