~zeller-benjamin/qtcreator-plugin-ubuntu/qtc41-beta

« back to all changes in this revision

Viewing changes to ubuntuprojectapp.cpp

  • Committer: Juhapekka Piiroinen
  • Date: 2013-09-04 15:30:00 UTC
  • mto: (23.1.14 binary-plugin)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: juhapekka.piiroinen@canonical.com-20130904153000-r4lhfhrjlwmop277
Added cordova plugin from ubuntu-qtcreator-plugins.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2013 Canonical Ltd.
3
 
 *
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.
7
 
 *
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.
12
 
 *
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/>.
15
 
 *
16
 
 * Author: Juhapekka Piiroinen <juhapekka.piiroinen@canonical.com>
17
 
 */
18
 
 
19
 
#include "ubuntuprojectapp.h"
20
 
 
21
 
#include "ubuntushared.h"
22
 
#include "ubuntuprojectapplicationwizard.h"
23
 
#include "ubuntuconstants.h"
24
 
#include "ubuntuproject.h"
25
 
 
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>
36
 
#include <QtGlobal>
37
 
 
38
 
#include <QIcon>
39
 
#include <QDir>
40
 
#include <QDebug>
41
 
#include <QTextStream>
42
 
#include <QDeclarativeEngine>
43
 
#include <QJsonArray>
44
 
 
45
 
using namespace Ubuntu;
46
 
using namespace Ubuntu::Internal;
47
 
 
48
 
UbuntuProjectApp::UbuntuProjectApp(QObject *parent) :
49
 
    QObject(parent)
50
 
{
51
 
}
52
 
 
53
 
Core::GeneratedFiles UbuntuProjectApp::generateFiles(const QWizard *w, QString *errorMessage) {
54
 
    Q_UNUSED(errorMessage);
55
 
 
56
 
    QByteArray contents;
57
 
    Core::GeneratedFiles files;
58
 
 
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();
63
 
    }
64
 
 
65
 
    setProjectType(projectType);
66
 
 
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();
71
 
    }
72
 
 
73
 
    const UbuntuProjectApplicationWizardDialog *wizard = qobject_cast<const UbuntuProjectApplicationWizardDialog *>(w);
74
 
    const QString projectName = wizard->projectName();
75
 
    const QString projectPath = wizard->path() + QLatin1Char('/') + projectName;
76
 
 
77
 
 
78
 
    QString folder;
79
 
    QJsonValue tmp_folder = m_obj.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_FOLDER));
80
 
    if (tmp_folder.isUndefined() == false) {
81
 
        folder = tmp_folder.toString();
82
 
    }
83
 
 
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)) {
89
 
 
90
 
        const QString creatorFileName = Core::BaseFileWizard::buildFileName(projectPath,
91
 
                                                                            projectName,
92
 
                                                                            projectType);
93
 
 
94
 
        QString mainFileName;
95
 
 
96
 
        // load the mainFile
97
 
        if (m_projectType == QLatin1String(Constants::UBUNTU_CORDOVAUBUNTU_TYPE)) {
98
 
            mainFileName = Core::BaseFileWizard::buildFileName(projectPath,
99
 
                                                              QLatin1String("index"),
100
 
                                                              QLatin1String("html"));
101
 
        } else {
102
 
            mainFileName = Core::BaseFileWizard::buildFileName(projectPath,
103
 
                                                                         projectName,
104
 
                                                                         QLatin1String(Constants::UBUNTU_QML_TYPE));
105
 
        }
106
 
        QJsonValue tmp_mainFile = m_obj.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_MAINFILE));
107
 
 
108
 
        if (tmp_mainFile.isUndefined() == false && tmp_folder.isUndefined() == false) {
109
 
            QString errorMsg;
110
 
            QString fileName = tmp_mainFile.toString();
111
 
 
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;
115
 
            }
116
 
        }
117
 
 
118
 
        contents = processReservedWords(contents,projectPath,projectName);
119
 
        mainFileName = processReservedWordsInFileName(mainFileName,projectName);
120
 
 
121
 
        Core::GeneratedFile generatedMainFile(mainFileName);
122
 
        generatedMainFile.setContents(QString::fromLatin1(contents));
123
 
        generatedMainFile.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
124
 
 
125
 
        // create the project file
126
 
        QByteArray projectContents;
127
 
        {
128
 
            QTextStream out(&projectContents);
129
 
 
130
 
            QDeclarativeEngine engine; // QQmlEngine engine;
131
 
 
132
 
            out << "/* File generated by Qt Creator (with Ubuntu Plugin), version " << Core::Constants::IDE_VERSION_LONG << " */" << endl
133
 
                << endl
134
 
                << "import QmlProject 1.1" << endl
135
 
                << endl
136
 
                << "Project {" << endl
137
 
                << "    mainFile: \"" << QDir(projectPath).relativeFilePath(mainFileName) << '"' << endl
138
 
                << endl
139
 
                << "    /* Include .qml, .js, and image files from current directory and subdirectories */" << endl
140
 
                << "    QmlFiles {" << endl
141
 
                << "        directory: \".\"" << endl
142
 
                << "    }" << endl
143
 
                << "    JavaScriptFiles {" << endl
144
 
                << "        directory: \".\"" << endl
145
 
                << "    }" << endl
146
 
                << "    ImageFiles {" << endl
147
 
                << "        directory: \".\"" << endl
148
 
                << "    }" << endl
149
 
                << "    Files {" << endl
150
 
                << "        filter: \"*.desktop\"" << endl
151
 
                << "    }" << endl
152
 
                << "    Files {" << endl
153
 
                << "        filter: \"Makefile\"" << endl
154
 
                << "    }" << endl;
155
 
 
156
 
                out << "    Files {" << endl
157
 
                    << "        directory: \"html\"" << endl
158
 
                    << "        filter: \"*\"" << endl
159
 
                    << "    }" << endl;
160
 
 
161
 
                out << "    Files {" << endl
162
 
                    << "        directory: \"img/\"" << endl
163
 
                    << "        filter: \"*\"" << endl
164
 
                    << "    }" << endl;
165
 
 
166
 
                out << "    Files {" << endl
167
 
                    << "        directory: \"css/\"" << endl
168
 
                    << "        filter: \"*\"" << endl
169
 
                    << "    }" << endl;
170
 
 
171
 
                out << "    Files {" << endl
172
 
                    << "        directory: \"tests/\"" << endl
173
 
                    << "        filter: \"*\"" << endl
174
 
                    << "    }" << endl;
175
 
           // }
176
 
            out << "    Files {" << endl
177
 
                << "        directory: \"debian\"" << endl
178
 
                << "        filter: \"*\"" << endl
179
 
                << "    }" << endl
180
 
                << "    /* List of plugin directories passed to QML runtime */" << endl
181
 
                << "    importPaths: [ \".\" ,\""
182
 
 
183
 
                // FIX ME: use QQmlEngine instead of QDeclarativeEngine. Then remove the replace.
184
 
                <<  engine.importPathList().join(QLatin1String("\",\"")).replace(QLatin1String("imports"),QLatin1String("qml"))
185
 
 
186
 
                << "\" ]" << endl
187
 
 
188
 
            << "}" << endl;
189
 
        }
190
 
 
191
 
        setProjectFileName(creatorFileName);
192
 
        Core::GeneratedFile generatedCreatorFile(creatorFileName);
193
 
        generatedCreatorFile.setContents(QLatin1String(projectContents));
194
 
        generatedCreatorFile.setAttributes(Core::GeneratedFile::OpenProjectAttribute);
195
 
 
196
 
        // add created files
197
 
        files.append(generatedMainFile);
198
 
        files.append(generatedCreatorFile);
199
 
    } else {
200
 
        QString errorMsg;
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;
208
 
            }
209
 
 
210
 
            contents = processReservedWords(contents,projectPath,projectName);
211
 
 
212
 
            fileName_target = processReservedWordsInFileName(fileName_target,projectName);
213
 
 
214
 
 
215
 
            Core::GeneratedFile generatedMainFile(fileName_target);
216
 
            generatedMainFile.setContents(QLatin1String(contents));
217
 
            generatedMainFile.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
218
 
            files.append(generatedMainFile);
219
 
        }
220
 
 
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;
228
 
            }
229
 
 
230
 
            contents = processReservedWords(contents,projectPath,projectName);
231
 
 
232
 
            fileName_target = processReservedWordsInFileName(fileName_target,projectName);
233
 
 
234
 
            setProjectFileName(fileName_target);
235
 
 
236
 
            Core::GeneratedFile generatedCreatorFile(fileName_target);
237
 
            generatedCreatorFile.setContents(QLatin1String(contents));
238
 
            generatedCreatorFile.setAttributes(Core::GeneratedFile::OpenProjectAttribute);
239
 
            files.append(generatedCreatorFile);
240
 
        }
241
 
    }
242
 
 
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();
253
 
                    QString errorMsg;
254
 
                    QByteArray contents;
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;
258
 
                    } else {
259
 
                        contents = processReservedWords(contents,projectPath,projectName);
260
 
 
261
 
                        fileName = processReservedWordsInFileName(fileName,projectName);
262
 
 
263
 
                        Core::GeneratedFile generatedFile(Core::BaseFileWizard::buildFileName(projectPath,fileName,QString()));
264
 
                        generatedFile.setBinaryContents(contents);
265
 
                        files.append(generatedFile);
266
 
                    }
267
 
                }
268
 
            }
269
 
        }
270
 
    }
271
 
    return files;
272
 
}
273
 
 
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());
279
 
    return data;
280
 
}
281
 
 
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());
290
 
    return data;
291
 
}
292
 
 
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);
297
 
 
298
 
    QJsonValue tmp = params.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_DISPLAYNAME));
299
 
    if (tmp.isUndefined() == false) {
300
 
        displayName = tmp.toString();
301
 
    }
302
 
 
303
 
    tmp = params.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_ID));
304
 
    if (tmp.isUndefined() == false) {
305
 
        id = tmp.toString();
306
 
    }
307
 
 
308
 
    tmp = params.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_DESCRIPTION));
309
 
    if (tmp.isUndefined() == false) {
310
 
        description = tmp.toString();
311
 
    }
312
 
 
313
 
    tmp = params.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_CATEGORY));
314
 
    if (tmp.isUndefined() == false) {
315
 
        category = tmp.toString();
316
 
    }
317
 
 
318
 
    tmp = params.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_CATEGORY_DISPLAY));
319
 
    if (tmp.isUndefined() == false) {
320
 
        displayCategory = tmp.toString();
321
 
    }
322
 
 
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);
332
 
    return parameters;
333
 
}
334
 
 
335
 
Core::Feature UbuntuProjectApp::requiredFeature() {
336
 
    QJsonValue tmp_feature = m_obj.value(QLatin1String(Constants::UBUNTU_PROJECTJSON_REQUIRED_FEATURE));
337
 
    QString feature;
338
 
    if (tmp_feature.isUndefined() == false) {
339
 
        feature = tmp_feature.toString();
340
 
    }
341
 
    if (!feature.isEmpty()) {
342
 
        return Core::Feature(feature.toLatin1().constData());
343
 
    }
344
 
    return Core::Feature(0);
345
 
}