jsonwizardfilegenerator.cpp 9.22 KB
Newer Older
Tobias Hunger's avatar
Tobias Hunger committed
1
2
/****************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
Tobias Hunger's avatar
Tobias Hunger committed
5
6
7
8
9
10
11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
Tobias Hunger's avatar
Tobias Hunger committed
15
16
17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
Tobias Hunger's avatar
Tobias Hunger committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
Tobias Hunger's avatar
Tobias Hunger committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "jsonwizardfilegenerator.h"

#include "../customwizard/customwizardpreprocessor.h"
#include "../projectexplorer.h"
#include "jsonwizard.h"
#include "jsonwizardfactory.h"

#include <coreplugin/editormanager/editormanager.h>

#include <utils/fileutils.h>
#include <utils/qtcassert.h>
42
#include <utils/macroexpander.h>
Tobias Hunger's avatar
Tobias Hunger committed
43
44
45
46
47
48
49
50

#include <QCoreApplication>
#include <QDir>
#include <QVariant>

namespace ProjectExplorer {
namespace Internal {

51
static QString processTextFileContents(Utils::MacroExpander *expander,
Tobias Hunger's avatar
Tobias Hunger committed
52
53
54
55
56
57
58
                                       const QString &input, QString *errorMessage)
{
    errorMessage->clear();

    if (input.isEmpty())
        return input;

59
    QString tmp;
60
    if (!customWizardPreprocess(expander->expand(input), &tmp, errorMessage))
Tobias Hunger's avatar
Tobias Hunger committed
61
        return QString();
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

    // Expand \n, \t and handle line continuation:
    QString result;
    result.reserve(tmp.count());
    bool isEscaped = false;
    for (int i = 0; i < tmp.count(); ++i) {
        const QChar c = tmp.at(i);

        if (isEscaped) {
            if (c == QLatin1Char('n'))
                result.append(QLatin1Char('\n'));
            else if (c == QLatin1Char('t'))
                result.append(QLatin1Char('\t'));
            else if (c != QLatin1Char('\n'))
                result.append(c);
            isEscaped = false;
        } else {
            if (c == QLatin1Char('\\'))
                isEscaped = true;
            else
                result.append(c);
        }
    }
Tobias Hunger's avatar
Tobias Hunger committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    return result;
}

bool JsonWizardFileGenerator::setup(const QVariant &data, QString *errorMessage)
{
    QVariantList list = JsonWizardFactory::objectOrList(data, errorMessage);
    if (list.isEmpty())
        return false;

    foreach (const QVariant &d, list) {
        if (d.type() != QVariant::Map) {
            *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                        "Files data list entry is not an object.");
            return false;
        }

        File f;

103
        const QVariantMap tmp = d.toMap();
Tobias Hunger's avatar
Tobias Hunger committed
104
105
106
107
108
109
110
111
        f.source = tmp.value(QLatin1String("source")).toString();
        f.target = tmp.value(QLatin1String("target")).toString();
        f.condition = tmp.value(QLatin1String("condition"), true);
        f.isBinary = tmp.value(QLatin1String("isBinary"), false);
        f.overwrite = tmp.value(QLatin1String("overwrite"), false);
        f.openInEditor = tmp.value(QLatin1String("openInEditor"), false);
        f.openAsProject = tmp.value(QLatin1String("openAsProject"), false);

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        const QVariant options = tmp.value(QLatin1String("options"));
        if (!options.isNull()) {
            const QVariantList optList = JsonWizardFactory::objectOrList(options, errorMessage);
            if (optList.isEmpty())
                return false;

            foreach (const QVariant &o, optList) {
                QVariantMap optionObject = o.toMap();
                File::OptionDefinition odef;
                odef.key = optionObject.value(QLatin1String("key")).toString();
                odef.value = optionObject.value(QLatin1String("value")).toString();
                odef.condition = optionObject.value(QLatin1String("condition"), QLatin1String("true")).toString();
                if (odef.key.isEmpty()) {
                    *errorMessage = QCoreApplication::translate("ProjectExplorer::Internal::JsonWizardFileGenerator",
                                                                "No 'key' in options object.");
                    return false;
                }
                f.options.append(odef);
            }
        }

133
        if (f.source.isEmpty() && f.target.isEmpty()) {
Tobias Hunger's avatar
Tobias Hunger committed
134
            *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
135
                                                        "Source and target are both empty.");
Tobias Hunger's avatar
Tobias Hunger committed
136
137
138
139
140
141
142
143
144
145
146
147
            return false;
        }

        if (f.target.isEmpty())
            f.target = f.source;

        m_fileList << f;
    }

    return true;
}

148
Core::GeneratedFiles JsonWizardFileGenerator::fileList(Utils::MacroExpander *expander,
Tobias Hunger's avatar
Tobias Hunger committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
                                                       const QString &wizardDir, const QString &projectDir,
                                                       QString *errorMessage)
{
    errorMessage->clear();

    QDir wizard(wizardDir);
    QDir project(projectDir);

    Core::GeneratedFiles result;

    foreach (const File &f, m_fileList) {
        if (!JsonWizard::boolFromVariant(f.condition, expander))
            continue;

163
164
165
166
167
168
        const bool keepExisting = f.source.isEmpty();
        const QString targetPath = project.absoluteFilePath(expander->expand(f.target));
        const QString sourcePath
                = keepExisting ? targetPath : wizard.absoluteFilePath(expander->expand(f.source));
        const bool isBinary = JsonWizard::boolFromVariant(f.isBinary, expander);

Tobias Hunger's avatar
Tobias Hunger committed
169
170
        // Read contents of source file
        const QFile::OpenMode openMode
171
                = JsonWizard::boolFromVariant(isBinary, expander)
Tobias Hunger's avatar
Tobias Hunger committed
172
173
174
                ? QIODevice::ReadOnly : (QIODevice::ReadOnly|QIODevice::Text);

        Utils::FileReader reader;
175
        if (!reader.fetch(sourcePath, openMode, errorMessage))
Tobias Hunger's avatar
Tobias Hunger committed
176
177
178
179
            return Core::GeneratedFiles();

        // Generate file information:
        Core::GeneratedFile gf;
180
181
182
183
184
185
186
187
188
        gf.setPath(targetPath);

        if (!keepExisting) {
            if (isBinary) {
                gf.setBinary(true);
                gf.setBinaryContents(reader.data());
            } else {
                // TODO: Document that input files are UTF8 encoded!
                gf.setBinary(false);
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
                Utils::MacroExpander nested;
                const File *fPtr = &f;
                Utils::MacroExpander *thisExpander = &nested;
                nested.registerExtraResolver([fPtr, thisExpander](QString n, QString *ret) -> bool {
                    foreach (const File::OptionDefinition &od, fPtr->options) {
                        if (!JsonWizard::boolFromVariant(od.condition, thisExpander))
                            continue;
                        if (n == od.key) {
                            *ret = od.value;
                            return true;
                        }
                    }
                    return false;
                });
                nested.registerExtraResolver([expander](QString n, QString *ret) { return expander->resolveMacro(n, ret); });

                gf.setContents(processTextFileContents(&nested, QString::fromUtf8(reader.data()), errorMessage));
206
207
208
209
210
                if (!errorMessage->isEmpty()) {
                    *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", "When processing \"%1\":<br>%2")
                            .arg(sourcePath, *errorMessage);
                    return Core::GeneratedFiles();
                }
Tobias Hunger's avatar
Tobias Hunger committed
211
212
213
214
215
216
217
218
219
220
            }
        }

        Core::GeneratedFile::Attributes attributes = 0;
        if (JsonWizard::boolFromVariant(f.openInEditor, expander))
            attributes |= Core::GeneratedFile::OpenEditorAttribute;
        if (JsonWizard::boolFromVariant(f.openAsProject, expander))
            attributes |= Core::GeneratedFile::OpenProjectAttribute;
        if (JsonWizard::boolFromVariant(f.overwrite, expander))
            attributes |= Core::GeneratedFile::ForceOverwrite;
221
222
223
224

        if (keepExisting)
            attributes |= Core::GeneratedFile::KeepExistingFileAttribute;

Tobias Hunger's avatar
Tobias Hunger committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
        gf.setAttributes(attributes);
        result.append(gf);
    }

    return result;
}

bool JsonWizardFileGenerator::writeFile(const JsonWizard *wizard, Core::GeneratedFile *file, QString *errorMessage)
{
    Q_UNUSED(wizard);
    if (!(file->attributes() & Core::GeneratedFile::KeepExistingFileAttribute)) {
        if (!file->write(errorMessage))
            return false;
    }
    return true;
}

} // namespace Internal
} // namespace ProjectExplorer