Skip to content

Commit

Permalink
Traverse app and config parent directories when looking up modules
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MartinBriza authored and patrickelectric committed Oct 19, 2023
1 parent 6c50b53 commit 50e2091
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/commandline/commandlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <utility>

#define MAX_RECURSIVE_SEARCH_DEPTH 3

CommandLineParser::CommandLineParser(int argc, char *argv[])
: QCommandLineParser()
{
Expand Down Expand Up @@ -138,15 +140,33 @@ 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;
}
}
}

auto pluginPaths = jsonObject.value(QLatin1String{"plugin-path"});
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;
}
}
}

Expand Down Expand Up @@ -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 {};
}
1 change: 1 addition & 0 deletions src/commandline/commandlineparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 50e2091

Please sign in to comment.