aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmljstools/qmlformatsettings.cpp
blob: a8977e1878781c37017b237fffa6c6ceb8d4043a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qmlformatsettings.h"
#include "qmljstoolstr.h"

#include <coreplugin/messagemanager.h>

#include <qmljs/qmljsmodelmanagerinterface.h>

#include <qtsupport/qtversionmanager.h>

#include <utils/commandline.h>
#include <utils/processinterface.h>
#include <utils/qtcprocess.h>
#include <utils/temporaryfile.h>

#include <QLoggingCategory>
#include <QStandardPaths>
#include <QTemporaryDir>

using namespace QtSupport;
using namespace Utils;
using namespace ProjectExplorer;

namespace QmlJSTools {

static Q_LOGGING_CATEGORY(qmlformatlog, "qtc.qmljstools.qmlformat", QtWarningMsg)

class QmlFormatProcess : public QObject
{
    Q_OBJECT

public:
    QmlFormatProcess(const CommandLine &cmd);

signals:
    void qmlformatIniCreated(const FilePath &qmlformatIniFile);

public:
    Process m_process;
    TemporaryFile m_logFile;
    QTemporaryDir m_tempDir;
};

// TODO: Mostly borrowed from qmllsclientsettings.cpp
// Unify them.
void QmlFormatSettings::evaluateLatestQmlFormat()
{
    if (!QtVersionManager::isLoaded())
        return;

    const QtVersions versions = QtVersionManager::versions();
    FilePath latestQmlFormat;
    QVersionNumber latestVersion;
    int latestUniqueId = std::numeric_limits<int>::min();

    for (QtVersion *qtVersion : versions) {
        if (!qtVersion->qmakeFilePath().isLocal())
            continue;

        const QVersionNumber version = qtVersion->qtVersion();
        const int uniqueId = qtVersion->uniqueId();

        // note: break ties between qt kits of same versions by selecting the qt kit with the highest id
        if (std::tie(version, uniqueId) < std::tie(latestVersion, latestUniqueId))
            continue;

        const FilePath qmlformat
            = QmlJS::ModelManagerInterface::qmlformatForBinPath(qtVersion->hostBinPath(), version);
        if (!qmlformat.isExecutableFile())
            continue;

        latestVersion = version;
        latestQmlFormat = qmlformat;
        latestUniqueId = uniqueId;
    }

    if (m_latestQmlFormat != latestQmlFormat || m_latestVersion != latestVersion) {
        m_latestQmlFormat = latestQmlFormat;
        m_latestVersion = latestVersion;
        generateQmlFormatIniContent();
    }
}

QmlFormatSettings::QmlFormatSettings()
{
    connect(QtVersionManager::instance(), &QtVersionManager::qtVersionsLoaded,
            this, &QmlFormatSettings::evaluateLatestQmlFormat);
}

QmlFormatSettings::~QmlFormatSettings() = default;

QmlFormatSettings& QmlFormatSettings::instance()
{
    static QmlFormatSettings instance;
    return instance;
}

FilePath QmlFormatSettings::globalQmlFormatIniFile()
{
    return FilePath::fromString(
        QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/.qmlformat.ini") ;
}

FilePath QmlFormatSettings::currentQmlFormatIniFile(const FilePath &path)
{
    const FilePath iniFile = path.searchHereAndInParents(".qmlformat.ini", QDir::Files);
    if (!iniFile.isEmpty())
        return iniFile;
    return globalQmlFormatIniFile();
}

FilePath QmlFormatSettings::latestQmlFormatPath() const
{
    return m_latestQmlFormat;
}

QVersionNumber QmlFormatSettings::latestQmlFormatVersion() const
{
    return m_latestVersion;
}

void QmlFormatSettings::generateQmlFormatIniContent()
{
    if (m_latestQmlFormat.isEmpty() || !m_latestQmlFormat.isExecutableFile()) {
        Core::MessageManager::writeSilently(Tr::tr("No qmlformat executable found."));
        return;
    }

    CommandLine cmd(m_latestQmlFormat);
    cmd.addArg("--write-defaults");

    if (cmd.executable().isEmpty()) {
        Core::MessageManager::writeSilently(Tr::tr("No qmlformat executable found."));
        return;
    }

    auto qmlFormatProcess = new QmlFormatProcess(cmd);

    connect(qmlFormatProcess, &QmlFormatProcess::qmlformatIniCreated,
            this, &QmlFormatSettings::qmlformatIniCreated);

    qmlFormatProcess->m_process.start();
}

QmlFormatProcess::QmlFormatProcess(const CommandLine &cmd)
    : m_logFile("qmlformat.qtc.log")
{
    m_logFile.setAutoRemove(false);

    m_process.setCommand(cmd);
    m_process.setWorkingDirectory(FilePath::fromString(m_tempDir.path()));
    m_process.setProcessMode(ProcessMode::Writer);

    if (m_logFile.open()) {
        connect(&m_process, &Process::readyReadStandardOutput, [this] {
            const QString output = m_process.readAllStandardOutput();
            if (!output.isEmpty()) {
                qCInfo(qmlformatlog) << "qmlformat stdout is written to: " << m_logFile.filePath();
                QTextStream(&m_logFile) << "STDOUT: " << output << '\n';
            }
        });
        connect(&m_process, &Process::readyReadStandardError, [this] {
            const QString output = m_process.readAllStandardError();
            if (!output.isEmpty()) {
                qCInfo(qmlformatlog) << "qmlformat stderr is written to: " << m_logFile.filePath();
                QTextStream(&m_logFile) << "STDERR: " << output << '\n';
            }
        });
    }

    connect(&m_process, &Process::done, this, [this] {
        ProcessResultData result = m_process.resultData();
        FilePath qmlformatIniFile = FilePath::fromString(m_tempDir.filePath(".qmlformat.ini"));
        if (result.m_exitStatus == QProcess::NormalExit && result.m_exitCode == 0)
            emit qmlformatIniCreated(qmlformatIniFile);
        else
            Core::MessageManager::writeSilently(
                Tr::tr("Failed to generate qmlformat.ini file."));
        deleteLater();
    });
}

} // namespace QmlJSTools

#include "qmlformatsettings.moc"