| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "qqmltoolingutils_p.h" |
| 5 | |
| 6 | #include <QtCore/qfileinfo.h> |
| 7 | #include <QtCore/qdir.h> |
| 8 | |
| 9 | using namespace Qt::StringLiterals; |
| 10 | |
| 11 | /*! |
| 12 | \internal |
| 13 | |
| 14 | Helper utils to help QQmlTooling retrieve certain values from the environment or command line |
| 15 | options. |
| 16 | It helps to keep the warning messages consistent between tools like qmlls and qmllint when |
| 17 | they use environment variables, for examples. |
| 18 | */ |
| 19 | |
| 20 | void QQmlToolingUtils::warnForInvalidDirs(const QStringList &dirs, const QString &origin) |
| 21 | { |
| 22 | for (const QString &path : dirs) { |
| 23 | QFileInfo info(path); |
| 24 | if (!info.exists()) { |
| 25 | qWarning().noquote().nospace() |
| 26 | << u"Argument \"%1\" %2 does not exist."_s.arg(args: path, args: origin); |
| 27 | continue; |
| 28 | } |
| 29 | if (!info.isDir()) { |
| 30 | qWarning().noquote().nospace() |
| 31 | << "Argument \""<< path << "\" "<< origin << " is not a directory."; |
| 32 | continue; |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | QStringList |
| 38 | QQmlToolingUtils::getAndWarnForInvalidDirsFromEnv(const QString &environmentVariableName) |
| 39 | { |
| 40 | const QStringList envPaths = qEnvironmentVariable(varName: environmentVariableName.toUtf8()) |
| 41 | .split(sep: QDir::listSeparator(), behavior: Qt::SkipEmptyParts); |
| 42 | warnForInvalidDirs(dirs: envPaths, |
| 43 | origin: u"from environment variable \"%1\""_s.arg(a: environmentVariableName)); |
| 44 | return envPaths; |
| 45 | } |
| 46 | |
| 47 | QStringList QQmlToolingUtils::getAndWarnForInvalidDirsFromOption(const QCommandLineParser &parser, |
| 48 | const QCommandLineOption &option) |
| 49 | { |
| 50 | if (!parser.isSet(option)) |
| 51 | return {}; |
| 52 | |
| 53 | const QStringList dirs = parser.values(option); |
| 54 | const QString optionName = option.names().constFirst(); |
| 55 | warnForInvalidDirs(dirs, origin: u"passed to -%1"_s.arg(a: optionName)); |
| 56 | return dirs; |
| 57 | } |
| 58 |
