2
* Copyright 2013 Canonical Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 2.1.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Author: Juhapekka Piiroinen <juhapekka.piiroinen@canonical.com>
19
#include "ubuntuprojectapp.h"
21
#include "ubuntushared.h"
22
#include "ubuntuprojectapplicationwizard.h"
23
#include "ubuntuconstants.h"
24
#include "ubuntuproject.h"
26
#include <app/app_version.h>
27
#include <projectexplorer/customwizard/customwizard.h>
28
#include <projectexplorer/projectexplorerconstants.h>
29
#include <projectexplorer/projectexplorer.h>
30
#include <projectexplorer/project.h>
31
#include <qtsupport/qtsupportconstants.h>
32
#include <coreplugin/icore.h>
33
#include <utils/filesearch.h>
34
#include <qmlprojectmanager/qmlprojectmanager.h>
35
#include <qt4projectmanager/qt4projectmanager.h>
41
#include <QTextStream>
42
#include <QDeclarativeEngine>
45
using namespace Ubuntu;
46
using namespace Ubuntu::Internal;
48
UbuntuProjectApp::UbuntuProjectApp(QObject *parent) :
53
Core::GeneratedFiles UbuntuProjectApp::generateFiles(const QWizard *w, QString *errorMessage) {
54
Q_UNUSED(errorMessage);
57
Core::GeneratedFiles files;
59
QJsonValue tmp_type = m_obj.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_TYPE));
60
QString projectType = QLatin1String(Constants::UBUNTU_QMLPROJECT_TYPE);
61
if (tmp_type.isUndefined() == false) {
62
projectType = tmp_type.toString();
65
setProjectType(projectType);
67
QJsonValue tmp_hasTests = m_obj.value(QLatin1String(Constants::UBUNTU_HAS_TESTS));
68
bool hasTests = false;
69
if (tmp_hasTests.isUndefined() == false) {
70
hasTests = tmp_hasTests.toBool();
73
const UbuntuProjectApplicationWizardDialog *wizard = qobject_cast<const UbuntuProjectApplicationWizardDialog *>(w);
74
const QString projectName = wizard->projectName();
75
const QString projectPath = wizard->path() + QLatin1Char('/') + projectName;
79
QJsonValue tmp_folder = m_obj.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_FOLDER));
80
if (tmp_folder.isUndefined() == false) {
81
folder = tmp_folder.toString();
84
if (m_projectType == QLatin1String(Constants::UBUNTU_QMLPROJECT_TYPE)
85
|| m_projectType == QLatin1String(Constants::UBUNTU_AUTOPILOTPROJECT_TYPE)
86
|| m_projectType == QLatin1String(Constants::UBUNTU_HTMLPROJECT_TYPE)
87
|| m_projectType == QLatin1String(Constants::UBUNTU_UBUNTUPROJECT_TYPE)
88
|| m_projectType == QLatin1String(Constants::UBUNTU_CORDOVAUBUNTU_TYPE)) {
90
const QString creatorFileName = Core::BaseFileWizard::buildFileName(projectPath,
97
if (m_projectType == QLatin1String(Constants::UBUNTU_CORDOVAUBUNTU_TYPE)) {
98
mainFileName = Core::BaseFileWizard::buildFileName(projectPath,
99
QLatin1String("index"),
100
QLatin1String("html"));
102
mainFileName = Core::BaseFileWizard::buildFileName(projectPath,
104
QLatin1String(Constants::UBUNTU_QML_TYPE));
106
QJsonValue tmp_mainFile = m_obj.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_MAINFILE));
108
if (tmp_mainFile.isUndefined() == false && tmp_folder.isUndefined() == false) {
110
QString fileName = tmp_mainFile.toString();
112
if (readFile(QString(QLatin1String("%0/%1")).arg(UbuntuProjectApplicationWizard::templatesPath(folder)).arg(fileName),&contents, &errorMsg) == false) {
113
contents = errorMsg.toAscii();
114
qDebug() << __PRETTY_FUNCTION__ << contents;
118
contents = processReservedWords(contents,projectPath,projectName);
119
mainFileName = processReservedWordsInFileName(mainFileName,projectName);
121
Core::GeneratedFile generatedMainFile(mainFileName);
122
generatedMainFile.setContents(QString::fromLatin1(contents));
123
generatedMainFile.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
125
// create the project file
126
QByteArray projectContents;
128
QTextStream out(&projectContents);
130
QDeclarativeEngine engine; // QQmlEngine engine;
132
out << "/* File generated by Qt Creator (with Ubuntu Plugin), version " << Core::Constants::IDE_VERSION_LONG << " */" << endl
134
<< "import QmlProject 1.1" << endl
136
<< "Project {" << endl
137
<< " mainFile: \"" << QDir(projectPath).relativeFilePath(mainFileName) << '"' << endl
139
<< " /* Include .qml, .js, and image files from current directory and subdirectories */" << endl
140
<< " QmlFiles {" << endl
141
<< " directory: \".\"" << endl
143
<< " JavaScriptFiles {" << endl
144
<< " directory: \".\"" << endl
146
<< " ImageFiles {" << endl
147
<< " directory: \".\"" << endl
149
<< " Files {" << endl
150
<< " filter: \"*.desktop\"" << endl
152
<< " Files {" << endl
153
<< " filter: \"Makefile\"" << endl
156
out << " Files {" << endl
157
<< " directory: \"html\"" << endl
158
<< " filter: \"*\"" << endl
161
out << " Files {" << endl
162
<< " directory: \"img/\"" << endl
163
<< " filter: \"*\"" << endl
166
out << " Files {" << endl
167
<< " directory: \"css/\"" << endl
168
<< " filter: \"*\"" << endl
171
out << " Files {" << endl
172
<< " directory: \"tests/\"" << endl
173
<< " filter: \"*\"" << endl
176
out << " Files {" << endl
177
<< " directory: \"debian\"" << endl
178
<< " filter: \"*\"" << endl
180
<< " /* List of plugin directories passed to QML runtime */" << endl
181
<< " importPaths: [ \".\" ,\""
183
// FIX ME: use QQmlEngine instead of QDeclarativeEngine. Then remove the replace.
184
<< engine.importPathList().join(QLatin1String("\",\"")).replace(QLatin1String("imports"),QLatin1String("qml"))
191
setProjectFileName(creatorFileName);
192
Core::GeneratedFile generatedCreatorFile(creatorFileName);
193
generatedCreatorFile.setContents(QLatin1String(projectContents));
194
generatedCreatorFile.setAttributes(Core::GeneratedFile::OpenProjectAttribute);
197
files.append(generatedMainFile);
198
files.append(generatedCreatorFile);
201
QJsonValue tmp_mainFile = m_obj.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_MAINFILE));
202
if (tmp_mainFile.isUndefined() == false) {
203
QString fileName_target = Core::BaseFileWizard::buildFileName(projectPath, tmp_mainFile.toString(),QString());
204
QString fileName_source = tmp_mainFile.toString();
205
if (readFile(QString(QLatin1String("%0/%1")).arg(UbuntuProjectApplicationWizard::templatesPath(folder)).arg(fileName_source),&contents, &errorMsg) == false) {
206
contents = errorMsg.toAscii();
207
qDebug() << __PRETTY_FUNCTION__ << contents;
210
contents = processReservedWords(contents,projectPath,projectName);
212
fileName_target = processReservedWordsInFileName(fileName_target,projectName);
215
Core::GeneratedFile generatedMainFile(fileName_target);
216
generatedMainFile.setContents(QLatin1String(contents));
217
generatedMainFile.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
218
files.append(generatedMainFile);
221
QJsonValue tmp_projectFile = m_obj.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_PROJECTFILE));
222
if (tmp_projectFile.isUndefined() == false) {
223
QString fileName_target = Core::BaseFileWizard::buildFileName(projectPath, QString(QLatin1String("%0.%1")).arg(projectName).arg(QLatin1String(Constants::UBUNTU_QTPROJECT_TYPE)),QString());
224
QString fileName_source = tmp_projectFile.toString();
225
if (readFile(QString(QLatin1String("%0/%1")).arg(UbuntuProjectApplicationWizard::templatesPath(folder)).arg(fileName_source),&contents, &errorMsg) == false) {
226
contents = errorMsg.toAscii();
227
qDebug() << __PRETTY_FUNCTION__ << contents;
230
contents = processReservedWords(contents,projectPath,projectName);
232
fileName_target = processReservedWordsInFileName(fileName_target,projectName);
234
setProjectFileName(fileName_target);
236
Core::GeneratedFile generatedCreatorFile(fileName_target);
237
generatedCreatorFile.setContents(QLatin1String(contents));
238
generatedCreatorFile.setAttributes(Core::GeneratedFile::OpenProjectAttribute);
239
files.append(generatedCreatorFile);
243
// create and add the other files
244
QJsonValue tmp = m_obj.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_FILES));
245
if (tmp.isUndefined() == false && tmp.isArray() ) {
246
QJsonArray jsonFiles = tmp.toArray();
247
for (int idx = 0; idx < jsonFiles.size(); idx++) {
248
QJsonValue tmp_value = jsonFiles.at(idx);
249
if (tmp_value.isObject()) {
250
QJsonValue tmp_fileName = tmp_value.toObject().value(QLatin1String(Constants::UBUNTU_PROJECTJSON_FILENAME));
251
if (tmp_fileName.isUndefined() == false) {
252
QString fileName = tmp_fileName.toString();
255
if (readFile(QString(QLatin1String("%0/%1")).arg(UbuntuProjectApplicationWizard::templatesPath(folder)).arg(fileName),&contents, &errorMsg) == false) {
256
contents = errorMsg.toAscii();
257
qDebug() << __PRETTY_FUNCTION__ << contents;
259
contents = processReservedWords(contents,projectPath,projectName);
261
fileName = processReservedWordsInFileName(fileName,projectName);
263
Core::GeneratedFile generatedFile(Core::BaseFileWizard::buildFileName(projectPath,fileName,QString()));
264
generatedFile.setBinaryContents(contents);
265
files.append(generatedFile);
274
QByteArray UbuntuProjectApp::processReservedWordsInFileName(QByteArray data, QString projectName) {
275
data = data.replace(Constants::UBUNTU_FILENAME_DISPLAYNAME_UPPER,projectName.toUpper().toLatin1());
276
data = data.replace(Constants::UBUNTU_FILENAME_DISPLAYNAME_LOWER,projectName.toLower().toLatin1());
277
data = data.replace(Constants::UBUNTU_FILENAME_DISPLAYNAME_CAPITAL,Utils::matchCaseReplacement(QLatin1String("Capitalize"),projectName).toLatin1());
278
data = data.replace(Constants::UBUNTU_FILENAME_DISPLAYNAME,projectName.toLatin1());
282
QByteArray UbuntuProjectApp::processReservedWords(QByteArray data, QString projectPath, QString projectName) {
283
QString folderName = QFileInfo(projectPath).baseName();
284
data = data.replace(Constants::UBUNTU_ACTION_FOLDERNAME,folderName.toLatin1());
285
data = data.replace(Constants::UBUNTU_ACTION_DISPLAYNAME_LOWER,projectName.toLower().toLatin1());
286
data = data.replace(Constants::UBUNTU_ACTION_DISPLAYNAME_UPPER,projectName.toUpper().toLatin1());
287
data = data.replace(Constants::UBUNTU_ACTION_DISPLAYNAME_CAPITAL,Utils::matchCaseReplacement(QLatin1String("Capitalize"),projectName).toLatin1());
288
data = data.replace(Constants::UBUNTU_ACTION_DISPLAYNAME,projectName.toLatin1());
289
data = data.replace(Constants::UBUNTU_ACTION_SHAREDIRECTORY,Constants::UBUNTU_SHAREPATH.toLatin1());
293
Core::BaseFileWizardParameters UbuntuProjectApp::parameters(QJsonObject params) {
294
QString displayName, id, description, category, displayCategory;
295
category = QLatin1String(Ubuntu::Constants::UBUNTU_PROJECT_WIZARD_CATEGORY);
296
displayCategory = QLatin1String(Ubuntu::Constants::UBUNTU_PROJECT_WIZARD_CATEGORY_DISPLAY);
298
QJsonValue tmp = params.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_DISPLAYNAME));
299
if (tmp.isUndefined() == false) {
300
displayName = tmp.toString();
303
tmp = params.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_ID));
304
if (tmp.isUndefined() == false) {
308
tmp = params.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_DESCRIPTION));
309
if (tmp.isUndefined() == false) {
310
description = tmp.toString();
313
tmp = params.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_CATEGORY));
314
if (tmp.isUndefined() == false) {
315
category = tmp.toString();
318
tmp = params.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_CATEGORY_DISPLAY));
319
if (tmp.isUndefined() == false) {
320
displayCategory = tmp.toString();
323
Core::BaseFileWizardParameters parameters;
324
parameters.setIcon(QIcon(QLatin1String(QtSupport::Constants::QML_WIZARD_ICON)));
325
parameters.setDisplayName(displayName);
326
parameters.setId(id);
327
parameters.setKind(Core::IWizard::ProjectWizard);
328
parameters.setFlags(Core::IWizard::PlatformIndependent);
329
parameters.setDescription(description);
330
parameters.setCategory(category);
331
parameters.setDisplayCategory(displayCategory);
335
Core::Feature UbuntuProjectApp::requiredFeature() {
336
QJsonValue tmp_feature = m_obj.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_REQUIRED_FEATURE));
338
if (tmp_feature.isUndefined() == false) {
339
feature = tmp_feature.toString();
341
if (!feature.isEmpty()) {
342
return Core::Feature(feature.toLatin1().constData());
344
return Core::Feature(0);