|
| 1 | +#include "toolsupport.hpp" |
| 2 | + |
| 3 | +#include <qcontainerfwd.h> |
| 4 | +#include <qdebug.h> |
| 5 | +#include <qdir.h> |
| 6 | +#include <qfileinfo.h> |
| 7 | +#include <qlist.h> |
| 8 | +#include <qlogging.h> |
| 9 | +#include <qloggingcategory.h> |
| 10 | +#include <qnamespace.h> |
| 11 | +#include <qtenvironmentvariables.h> |
| 12 | + |
| 13 | +#include "logcat.hpp" |
| 14 | +#include "paths.hpp" |
| 15 | +#include "scan.hpp" |
| 16 | + |
| 17 | +namespace qs::core { |
| 18 | + |
| 19 | +namespace { |
| 20 | +QS_LOGGING_CATEGORY(logTooling, "quickshell.tooling", QtWarningMsg); |
| 21 | +} |
| 22 | + |
| 23 | +bool QmlToolingSupport::updateTooling(const QDir& configRoot, QmlScanner& scanner) { |
| 24 | + auto* vfs = QsPaths::instance()->shellVfsDir(); |
| 25 | + |
| 26 | + if (!vfs) { |
| 27 | + qCCritical(logTooling) << "Tooling dir could not be created"; |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + if (!QmlToolingSupport::updateQmllsConfig(configRoot, false)) { |
| 32 | + QDir(vfs->filePath("qs")).removeRecursively(); |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + QmlToolingSupport::updateToolingFs(scanner, configRoot, vfs->filePath("qs")); |
| 37 | + return true; |
| 38 | +} |
| 39 | + |
| 40 | +QString QmlToolingSupport::getQmllsConfig() { |
| 41 | + static auto config = []() { |
| 42 | + QList<QString> importPaths; |
| 43 | + |
| 44 | + auto addPaths = [&](const QList<QString>& paths) { |
| 45 | + for (const auto& path: paths) { |
| 46 | + if (!importPaths.contains(path)) importPaths.append(path); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + addPaths(qEnvironmentVariable("QML_IMPORT_PATH").split(u':', Qt::SkipEmptyParts)); |
| 51 | + addPaths(qEnvironmentVariable("QML2_IMPORT_PATH").split(u':', Qt::SkipEmptyParts)); |
| 52 | + |
| 53 | + auto vfsPath = QsPaths::instance()->shellVfsDir()->path(); |
| 54 | + auto importPathsStr = importPaths.join(u':'); |
| 55 | + |
| 56 | + QString qmllsConfig; |
| 57 | + auto print = QDebug(&qmllsConfig).nospace(); |
| 58 | + print << "[General]\nno-cmake-calls=true\nbuildDir=" << vfsPath |
| 59 | + << "\nimportPaths=" << importPathsStr << '\n'; |
| 60 | + |
| 61 | + return qmllsConfig; |
| 62 | + }(); |
| 63 | + |
| 64 | + return config; |
| 65 | +} |
| 66 | + |
| 67 | +bool QmlToolingSupport::updateQmllsConfig(const QDir& configRoot, bool create) { |
| 68 | + auto shellConfigPath = configRoot.filePath(".qmlls.ini"); |
| 69 | + auto vfsConfigPath = QsPaths::instance()->shellVfsDir()->filePath(".qmlls.ini"); |
| 70 | + |
| 71 | + auto shellFileInfo = QFileInfo(shellConfigPath); |
| 72 | + if (!create && !shellFileInfo.exists()) { |
| 73 | + if (QmlToolingSupport::toolingEnabled) { |
| 74 | + qInfo() << "QML tooling support disabled"; |
| 75 | + QmlToolingSupport::toolingEnabled = false; |
| 76 | + } |
| 77 | + |
| 78 | + QFile::remove(vfsConfigPath); |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + auto vfsFile = QFile(vfsConfigPath); |
| 83 | + |
| 84 | + if (!vfsFile.open(QFile::ReadWrite | QFile::Text)) { |
| 85 | + qCCritical(logTooling) << "Failed to create qmlls config in vfs"; |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + auto config = QmlToolingSupport::getQmllsConfig(); |
| 90 | + |
| 91 | + if (vfsFile.readAll() != config) { |
| 92 | + if (!vfsFile.resize(0) || !vfsFile.write(config.toUtf8())) { |
| 93 | + qCCritical(logTooling) << "Failed to write qmlls config in vfs"; |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + qCDebug(logTooling) << "Wrote qmlls config in vfs"; |
| 98 | + } |
| 99 | + |
| 100 | + if (!shellFileInfo.isSymLink() || shellFileInfo.symLinkTarget() != vfsConfigPath) { |
| 101 | + QFile::remove(shellConfigPath); |
| 102 | + |
| 103 | + if (!QFile::link(vfsConfigPath, shellConfigPath)) { |
| 104 | + qCCritical(logTooling) << "Failed to create qmlls config symlink"; |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + qCDebug(logTooling) << "Created qmlls config symlink"; |
| 109 | + } |
| 110 | + |
| 111 | + if (!QmlToolingSupport::toolingEnabled) { |
| 112 | + qInfo() << "QML tooling support enabled"; |
| 113 | + QmlToolingSupport::toolingEnabled = true; |
| 114 | + } |
| 115 | + |
| 116 | + return true; |
| 117 | +} |
| 118 | + |
| 119 | +void QmlToolingSupport::updateToolingFs( |
| 120 | + QmlScanner& scanner, |
| 121 | + const QDir& scanDir, |
| 122 | + const QDir& linkDir |
| 123 | +) { |
| 124 | + QList<QString> files; |
| 125 | + QSet<QString> subdirs; |
| 126 | + |
| 127 | + auto scanPath = scanDir.path(); |
| 128 | + |
| 129 | + linkDir.mkpath("."); |
| 130 | + |
| 131 | + for (auto& path: scanner.scannedFiles) { |
| 132 | + if (path.length() < scanPath.length() + 1 || !path.startsWith(scanPath)) continue; |
| 133 | + auto name = path.sliced(scanPath.length() + 1); |
| 134 | + |
| 135 | + if (name.contains('/')) { |
| 136 | + auto dirname = name.first(name.indexOf('/')); |
| 137 | + subdirs.insert(dirname); |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + auto fileInfo = QFileInfo(path); |
| 142 | + if (!fileInfo.isFile()) continue; |
| 143 | + |
| 144 | + auto spath = linkDir.filePath(name); |
| 145 | + auto sFileInfo = QFileInfo(spath); |
| 146 | + |
| 147 | + if (!sFileInfo.isSymLink() || sFileInfo.symLinkTarget() != path) { |
| 148 | + QFile::remove(spath); |
| 149 | + |
| 150 | + if (QFile::link(path, spath)) { |
| 151 | + qCDebug(logTooling) << "Created symlink to" << path << "at" << spath; |
| 152 | + files.append(spath); |
| 153 | + } else { |
| 154 | + qCCritical(logTooling) << "Could not create symlink to" << path << "at" << spath; |
| 155 | + } |
| 156 | + } else { |
| 157 | + files.append(spath); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + for (auto [path, text]: scanner.fileIntercepts.asKeyValueRange()) { |
| 162 | + if (path.length() < scanPath.length() + 1 || !path.startsWith(scanPath)) continue; |
| 163 | + auto name = path.sliced(scanPath.length() + 1); |
| 164 | + |
| 165 | + if (name.contains('/')) { |
| 166 | + auto dirname = name.first(name.indexOf('/')); |
| 167 | + subdirs.insert(dirname); |
| 168 | + continue; |
| 169 | + } |
| 170 | + |
| 171 | + auto spath = linkDir.filePath(name); |
| 172 | + auto file = QFile(spath); |
| 173 | + if (!file.open(QFile::ReadWrite | QFile::Text)) { |
| 174 | + qCCritical(logTooling) << "Failed to open injected file" << spath; |
| 175 | + continue; |
| 176 | + } |
| 177 | + |
| 178 | + if (file.readAll() == text) { |
| 179 | + files.append(spath); |
| 180 | + continue; |
| 181 | + } |
| 182 | + |
| 183 | + if (file.resize(0) && file.write(text.toUtf8())) { |
| 184 | + files.append(spath); |
| 185 | + qCDebug(logTooling) << "Wrote injected file" << spath; |
| 186 | + } else { |
| 187 | + qCCritical(logTooling) << "Failed to write injected file" << spath; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + for (auto& name: linkDir.entryList(QDir::Files | QDir::System)) { // System = broken symlinks |
| 192 | + auto path = linkDir.filePath(name); |
| 193 | + |
| 194 | + if (!files.contains(path)) { |
| 195 | + if (QFile::remove(path)) qCDebug(logTooling) << "Removed old file at" << path; |
| 196 | + else qCWarning(logTooling) << "Failed to remove old file at" << path; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + for (const auto& subdir: subdirs) { |
| 201 | + QmlToolingSupport::updateToolingFs(scanner, scanDir.filePath(subdir), linkDir.filePath(subdir)); |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +} // namespace qs::core |
0 commit comments