Add --resize-to-root option, sets ResizeViewToRootObject on the qmlscene view.
[profile/ivi/qtdeclarative.git] / tools / qmlbundle / main.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <private/qqmlbundle_p.h>
43 #include <private/qqmlscript_p.h>
44 #include <QtCore/QtCore>
45 #include <iostream>
46
47 static bool createBundle(const QString &fileName, const QStringList &fileNames)
48 {
49     QQmlBundle bundle(fileName);
50     if (!bundle.open(QFile::WriteOnly))
51         return false;
52     foreach (const QString &fileName, fileNames)
53         bundle.add(fileName);
54     return true;
55 }
56
57 static bool removeFiles(const QString &fileName, const QStringList &fileNames)
58 {
59     const QSet<QString> filesToRemove = QSet<QString>::fromList(fileNames);
60
61     QQmlBundle bundle(fileName);
62     bundle.open(QFile::ReadWrite);
63     foreach (const QQmlBundle::FileEntry *entry, bundle.files()) {
64         if (filesToRemove.contains(entry->fileName()))
65             bundle.remove(entry);
66     }
67     return true;
68 }
69
70 static void showHelp()
71 {
72     std::cerr << "Usage: qmlbundle <command> [<args>]" << std::endl
73               << 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
82               << std::endl
83               << "See 'qmlbundle help <command>' for more information on a specific command." << std::endl;
84 }
85
86 static void usage(const QString &action, const QString &error = QString())
87 {
88     if (! error.isEmpty())
89         std::cerr << qPrintable(error) << std::endl << std::endl;
90
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;
103     } else {
104         showHelp();
105     }
106 }
107
108 int main(int argc, char *argv[])
109 {
110     QCoreApplication app(argc, argv);
111
112     QStringList args = app.arguments();
113     /*const QString exeName =*/ args.takeFirst();
114
115     if (args.isEmpty()) {
116         showHelp();
117         return 0;
118     }
119
120     const QString action = args.takeFirst();
121
122     if (action == QLatin1String("help")) {
123         if (args.empty())
124             showHelp();
125         else
126             usage(args.takeFirst());
127     } else if (action == QLatin1String("ls")) {
128         if (args.isEmpty()) {
129             usage(action, "You must specify a bundle");
130             return EXIT_FAILURE;
131         }
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;
136         }
137     } else if (action == QLatin1String("create")) {
138         if (args.isEmpty()) {
139             usage(action, "You must specify a bundle");
140             return EXIT_FAILURE;
141         }
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");
147             return EXIT_FAILURE;
148         }
149         const QString bundleFileName = args.takeFirst();
150         QQmlBundle bundle(bundleFileName);
151         bundle.open();
152         foreach (const QString &fileName, args) {
153             if (! bundle.add(fileName))
154                 std::cerr << "cannot add file " << qPrintable(fileName) << " to " << qPrintable(bundleFileName) << std::endl;
155         }
156     } else if (action == QLatin1String("rm")) {
157         if (args.isEmpty()) {
158             usage(action, "You must specify a bundle");
159             return EXIT_FAILURE;
160         }
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");
166             return EXIT_FAILURE;
167         }
168         const QString bundleFileName = args.takeFirst();
169         removeFiles(bundleFileName, args);
170         QQmlBundle bundle(bundleFileName);
171         bundle.open();
172         foreach (const QString &fileName, args) {
173             if (! bundle.add(fileName))
174                 std::cerr << "cannot add file " << qPrintable(fileName) << " to " << qPrintable(bundleFileName) << std::endl;
175         }
176     } else if (action == QLatin1String("cat")) {
177         if (args.isEmpty()) {
178             usage(action, "You must specify a bundle");
179             return EXIT_FAILURE;
180         }
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);
185
186             foreach (const QQmlBundle::FileEntry *fileEntry, bundle.files()) {
187                 if (filesToShow.contains(fileEntry->fileName()))
188                     std::cout.write(fileEntry->contents(), fileEntry->fileSize());
189             }
190         }
191     } else if (action == QLatin1String("optimize")) {
192         if (args.isEmpty()) {
193             usage(action, "You must specify a bundle");
194             return EXIT_FAILURE;
195         }
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);
202
203                 if (!file->fileName().endsWith(".qml"))
204                     continue;
205
206                 QQmlScript::Parser parser;
207                 QString data = QString::fromUtf8(file->contents(), file->fileSize());
208                 parser.parse(data, QByteArray());
209                 QByteArray preparse = parser.preparseData();
210
211                 if (!preparse.isEmpty())
212                     bundle.addMetaLink(file->fileName(), QLatin1String("qml:preparse"), preparse);
213             }
214         }
215     } else {
216         showHelp();
217     }
218
219     return 0;
220 }