genericprojectwizard.cpp 7.96 KB
Newer Older
1
2
3
4
5
6
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, 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.
**
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
27
28
29
**
**************************************************************************/

Roberto Raggi's avatar
Roberto Raggi committed
30
#include "genericprojectwizard.h"
Roberto Raggi's avatar
Roberto Raggi committed
31
32
33

#include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h>
Roberto Raggi's avatar
Roberto Raggi committed
34
#include <projectexplorer/projectexplorer.h>
Friedemann Kleint's avatar
Friedemann Kleint committed
35
#include <projectexplorer/projectexplorerconstants.h>
Roberto Raggi's avatar
Roberto Raggi committed
36

37
#include <utils/filewizardpage.h>
Roberto Raggi's avatar
Roberto Raggi committed
38

Roberto Raggi's avatar
Roberto Raggi committed
39
#include <QtCore/QDir>
Friedemann Kleint's avatar
Friedemann Kleint committed
40
#include <QtCore/QFileInfo>
Roberto Raggi's avatar
Roberto Raggi committed
41
#include <QtCore/QtDebug>
Friedemann Kleint's avatar
Friedemann Kleint committed
42
#include <QtCore/QCoreApplication>
Roberto Raggi's avatar
Roberto Raggi committed
43

Roberto Raggi's avatar
Roberto Raggi committed
44
using namespace GenericProjectManager::Internal;
45
using namespace Utils;
Roberto Raggi's avatar
Roberto Raggi committed
46

Roberto Raggi's avatar
Roberto Raggi committed
47
48
49
50
51
52
//////////////////////////////////////////////////////////////////////////////
// GenericProjectWizardDialog
//////////////////////////////////////////////////////////////////////////////

GenericProjectWizardDialog::GenericProjectWizardDialog(QWidget *parent)
    : QWizard(parent)
Roberto Raggi's avatar
Roberto Raggi committed
53
{
54
    setWindowTitle(tr("Import of Makefile-based Project"));
Roberto Raggi's avatar
Roberto Raggi committed
55
56

    // first page
57
    m_firstPage = new FileWizardPage;
58
    m_firstPage->setTitle(tr("Generic Project"));
59
    m_firstPage->setFileNameLabel(tr("Project name:"));
60
    m_firstPage->setPathLabel(tr("Location:"));
Roberto Raggi's avatar
Roberto Raggi committed
61

62
    addPage(m_firstPage);
Roberto Raggi's avatar
Roberto Raggi committed
63
64
}

Roberto Raggi's avatar
Roberto Raggi committed
65
66
67
68
GenericProjectWizardDialog::~GenericProjectWizardDialog()
{ }

QString GenericProjectWizardDialog::path() const
69
70
71
72
{
    return m_firstPage->path();
}

73
74
75
76
77
void GenericProjectWizardDialog::setPath(const QString &path)
{
    m_firstPage->setPath(path);
}

78
79
QString GenericProjectWizardDialog::projectName() const
{
80
    return m_firstPage->fileName();
81
}
Roberto Raggi's avatar
Roberto Raggi committed
82
83
84
85
86
87
88
89

GenericProjectWizard::GenericProjectWizard()
    : Core::BaseFileWizard(parameters())
{ }

GenericProjectWizard::~GenericProjectWizard()
{ }

Roberto Raggi's avatar
Roberto Raggi committed
90
91
Core::BaseFileWizardParameters GenericProjectWizard::parameters()
{
92
93
    Core::BaseFileWizardParameters parameters(ProjectWizard);
    parameters.setIcon(QIcon(QLatin1String(":/wizards/images/console.png")));
94
    parameters.setDisplayName(tr("Import of Makefile-based Project"));
Friedemann Kleint's avatar
Friedemann Kleint committed
95
    parameters.setId(QLatin1String("Z.Makefile"));
96
    parameters.setDescription(tr("Creates a generic project, supporting any build system."));
Friedemann Kleint's avatar
Friedemann Kleint committed
97
    parameters.setCategory(QLatin1String(ProjectExplorer::Constants::PROJECT_WIZARD_CATEGORY));
98
    parameters.setDisplayCategory(QCoreApplication::translate("ProjectExplorer", ProjectExplorer::Constants::PROJECT_WIZARD_TR_CATEGORY));
Roberto Raggi's avatar
Roberto Raggi committed
99
100
101
102
103
104
105
    return parameters;
}

QWizard *GenericProjectWizard::createWizardDialog(QWidget *parent,
                                                  const QString &defaultPath,
                                                  const WizardPageList &extensionPages) const
{
Roberto Raggi's avatar
Roberto Raggi committed
106
    GenericProjectWizardDialog *wizard = new GenericProjectWizardDialog(parent);
Roberto Raggi's avatar
Roberto Raggi committed
107
108
    setupWizard(wizard);

109
110
    wizard->setPath(defaultPath);

Roberto Raggi's avatar
Roberto Raggi committed
111
112
    foreach (QWizardPage *p, extensionPages)
        wizard->addPage(p);
Roberto Raggi's avatar
Roberto Raggi committed
113

Roberto Raggi's avatar
Roberto Raggi committed
114
115
    return wizard;
}
Roberto Raggi's avatar
Roberto Raggi committed
116

Roberto Raggi's avatar
Roberto Raggi committed
117
118
119
120
121
122
123
124
void GenericProjectWizard::getFileList(const QDir &dir, const QString &projectRoot,
                                       const QStringList &suffixes,
                                       QStringList *files, QStringList *paths) const
{
    const QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files |
                                                         QDir::Dirs |
                                                         QDir::NoDotAndDotDot |
                                                         QDir::NoSymLinks);
Roberto Raggi's avatar
Roberto Raggi committed
125

Roberto Raggi's avatar
Roberto Raggi committed
126
127
128
    foreach (const QFileInfo &fileInfo, fileInfoList) {
        QString filePath = fileInfo.absoluteFilePath();
        filePath = filePath.mid(projectRoot.length() + 1);
Roberto Raggi's avatar
Roberto Raggi committed
129

Roberto Raggi's avatar
Roberto Raggi committed
130
131
132
        if (fileInfo.isDir() && isValidDir(fileInfo)) {
            getFileList(QDir(fileInfo.absoluteFilePath()), projectRoot,
                        suffixes, files, paths);
Roberto Raggi's avatar
Roberto Raggi committed
133

Roberto Raggi's avatar
Roberto Raggi committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
            if (! paths->contains(filePath))
                paths->append(filePath);
        }

        else if (suffixes.contains(fileInfo.suffix()))
            files->append(filePath);
    }
}

bool GenericProjectWizard::isValidDir(const QFileInfo &fileInfo) const
{
    const QString fileName = fileInfo.fileName();
    const QString suffix = fileInfo.suffix();

    if (fileName.startsWith(QLatin1Char('.')))
        return false;

    else if (fileName == QLatin1String("CVS"))
152
        return false;
Roberto Raggi's avatar
Roberto Raggi committed
153
154
155
156

    // ### user include/exclude

    return true;
Roberto Raggi's avatar
Roberto Raggi committed
157
158
159
160
161
}

Core::GeneratedFiles GenericProjectWizard::generateFiles(const QWizard *w,
                                                         QString *errorMessage) const
{
162
163
    Q_UNUSED(errorMessage)

Roberto Raggi's avatar
Roberto Raggi committed
164
165
166
    const GenericProjectWizardDialog *wizard = qobject_cast<const GenericProjectWizardDialog *>(w);
    const QString projectPath = wizard->path();
    const QDir dir(projectPath);
167
    const QString projectName = wizard->projectName();
168
169
170
171
    const QString creatorFileName = QFileInfo(dir, projectName + QLatin1String(".creator")).absoluteFilePath();
    const QString filesFileName = QFileInfo(dir, projectName + QLatin1String(".files")).absoluteFilePath();
    const QString includesFileName = QFileInfo(dir, projectName + QLatin1String(".includes")).absoluteFilePath();
    const QString configFileName = QFileInfo(dir, projectName + QLatin1String(".config")).absoluteFilePath();
Roberto Raggi's avatar
Roberto Raggi committed
172
173
174
175
176

    Core::ICore *core = Core::ICore::instance();
    Core::MimeDatabase *mimeDatabase = core->mimeDatabase();

    const QStringList suffixes = mimeDatabase->suffixes();
Roberto Raggi's avatar
Roberto Raggi committed
177

Roberto Raggi's avatar
Roberto Raggi committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    QStringList sources, paths;
    getFileList(dir, projectPath, suffixes, &sources, &paths);

    Core::MimeType headerTy = mimeDatabase->findByType(QLatin1String("text/x-chdr"));

    QStringList nameFilters;
    foreach (const QRegExp &rx, headerTy.globPatterns())
        nameFilters.append(rx.pattern());

    QStringList includePaths;
    foreach (const QString &path, paths) {
        QFileInfo fileInfo(dir, path);
        QDir thisDir(fileInfo.absoluteFilePath());

        if (! thisDir.entryList(nameFilters, QDir::Files).isEmpty())
            includePaths.append(path);
    }
Roberto Raggi's avatar
Roberto Raggi committed
195

196
197
    Core::GeneratedFile generatedCreatorFile(creatorFileName);
    generatedCreatorFile.setContents(QLatin1String("[General]\n"));
Roberto Raggi's avatar
Roberto Raggi committed
198

199
200
201
202
203
204
205
206
    Core::GeneratedFile generatedFilesFile(filesFileName);
    generatedFilesFile.setContents(sources.join(QLatin1String("\n")));

    Core::GeneratedFile generatedIncludesFile(includesFileName);
    generatedIncludesFile.setContents(includePaths.join(QLatin1String("\n")));

    Core::GeneratedFile generatedConfigFile(configFileName);
    generatedConfigFile.setContents(QLatin1String("// ADD PREDEFINED MACROS HERE!\n"));
Roberto Raggi's avatar
Roberto Raggi committed
207

Roberto Raggi's avatar
Roberto Raggi committed
208
    Core::GeneratedFiles files;
209
210
211
212
    files.append(generatedFilesFile);
    files.append(generatedIncludesFile);
    files.append(generatedConfigFile);
    files.append(generatedCreatorFile);
Roberto Raggi's avatar
Roberto Raggi committed
213
214
215
216
217
218
219
220
221
222
223
224
225

    return files;
}

bool GenericProjectWizard::postGenerateFiles(const Core::GeneratedFiles &l, QString *errorMessage)
{
    // Post-Generate: Open the project
    const QString proFileName = l.back().path();
    if (!ProjectExplorer::ProjectExplorerPlugin::instance()->openProject(proFileName)) {
        *errorMessage = tr("The project %1 could not be opened.").arg(proFileName);
        return false;
    }
    return true;
Roberto Raggi's avatar
Roberto Raggi committed
226
227
}