From 50e209142046e9b73a7a075721293c47e4a95595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20B=C5=99=C3=ADza?= Date: Fri, 8 Sep 2023 18:00:19 +0200 Subject: [PATCH] Traverse app and config parent directories when looking up modules When running something from the build folder, it was really hard to set up the paths properly and it would have to be done manually in most cases. Traversing up from the binary makes it a lot easier. It also takes preference over the config dir because there may be clashes between dir names and the ones in build dir should be preferred. --- src/commandline/commandlineparser.cpp | 38 +++++++++++++++++++++++++-- src/commandline/commandlineparser.h | 1 + 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/commandline/commandlineparser.cpp b/src/commandline/commandlineparser.cpp index 863bfe2..1a59474 100644 --- a/src/commandline/commandlineparser.cpp +++ b/src/commandline/commandlineparser.cpp @@ -8,6 +8,8 @@ #include +#define MAX_RECURSIVE_SEARCH_DEPTH 3 + CommandLineParser::CommandLineParser(int argc, char *argv[]) : QCommandLineParser() { @@ -138,7 +140,16 @@ void CommandLineParser::_parseQHotProfile(const QString& profilePath) if (importPaths.isArray()) { const auto paths = importPaths.toArray(); for (const auto &path : paths) { - _importPaths.append(profileDir.absoluteFilePath(path.toString())); + auto potentialPath = _lookupDirectory(path.toString(), qApp->applicationDirPath(), MAX_RECURSIVE_SEARCH_DEPTH); + if (!potentialPath.isEmpty()) { + _importPaths.append(potentialPath); + continue; + } + potentialPath = _lookupDirectory(path.toString(), profilePath, MAX_RECURSIVE_SEARCH_DEPTH); + if (!potentialPath.isEmpty()) { + _importPaths.append(potentialPath); + continue; + } } } @@ -146,7 +157,16 @@ void CommandLineParser::_parseQHotProfile(const QString& profilePath) if (pluginPaths.isArray()) { const auto paths = pluginPaths.toArray(); for (const auto &path : paths) { - _pluginPaths.append(profileDir.absoluteFilePath(path.toString())); + auto potentialPath = _lookupDirectory(path.toString(), qApp->applicationDirPath(), MAX_RECURSIVE_SEARCH_DEPTH); + if (!potentialPath.isEmpty()) { + _pluginPaths.append(potentialPath); + continue; + } + potentialPath = _lookupDirectory(path.toString(), profilePath, MAX_RECURSIVE_SEARCH_DEPTH); + if (!potentialPath.isEmpty()) { + _pluginPaths.append(potentialPath); + continue; + } } } @@ -188,3 +208,17 @@ void CommandLineParser::_translate(const QString &translationFile) #endif } + +QString CommandLineParser::_lookupDirectory(const QString &needle, const QString &parentDirPath, int maximumDepth) +{ + QDir parentDir(parentDirPath); + QString parentPrefix = QStringLiteral(""); + for (int i = 0; i < maximumDepth; i++) { + auto relativeToBinaryPath = parentDir.absoluteFilePath(parentPrefix + needle); + if (QDir(relativeToBinaryPath).exists()) { + return relativeToBinaryPath; + } + parentPrefix += QStringLiteral("../"); + } + return {}; +} diff --git a/src/commandline/commandlineparser.h b/src/commandline/commandlineparser.h index 9186bcc..2e1ec33 100644 --- a/src/commandline/commandlineparser.h +++ b/src/commandline/commandlineparser.h @@ -59,6 +59,7 @@ class CommandLineParser : public QCommandLineParser { void printHelp(); void _parseQHotProfile(const QString& profilePath); void _translate(const QString& translationFile); + static QString _lookupDirectory(const QString& needle, const QString& parentDirPath, int maximumDepth = 1); QStringList _importPaths; QStringList _pluginPaths;