Skip to content

Commit

Permalink
launcherlib: Add additionalPathsVars() and pathSep() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jcfr committed May 22, 2017
1 parent 22a0fad commit b05bb88
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 47 deletions.
12 changes: 12 additions & 0 deletions Base/Testing/Cpp/ctkAppLauncherSettingsTest.cpp
Expand Up @@ -16,6 +16,7 @@ private slots:
void testLauncherDir();
void testLauncherName();
void testReadSettingsError();
void testPathSep();
};

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -66,6 +67,17 @@ void ctkAppLauncherSettingsTester::testReadSettingsError()
#endif
}

// ----------------------------------------------------------------------------
void ctkAppLauncherSettingsTester::testPathSep()
{
ctkAppLauncherSettings appLauncherSettings;
#if defined(Q_OS_WIN32)
QCOMPARE(appLauncherSettings.pathSep(), QString(";"));
#else
QCOMPARE(appLauncherSettings.pathSep(), QString(":"));
#endif
}

// ----------------------------------------------------------------------------
CTK_TEST_MAIN(ctkAppLauncherSettingsTest)
#include "moc_ctkAppLauncherSettingsTest.cpp"
33 changes: 19 additions & 14 deletions Base/ctkAppLauncher.cpp
Expand Up @@ -419,29 +419,34 @@ void ctkAppLauncherPrivate::buildEnvironment(QProcessEnvironment &env)
Q_Q(ctkAppLauncher);

this->reportInfo(QString("<APPLAUNCHER_DIR> -> [%1]").arg(this->LauncherDir));
this->reportInfo(QString("<APPLAUNCHER_NAME> -> [%1]").arg(this->LauncherName));
this->reportInfo(QString("<PATHSEP> -> [%1]").arg(this->PathSep));

QHash<QString, QString> newVars = q->envVars();

QSet<QString> appendVars = this->AdditionalPathVariables;
appendVars << "PATH" << this->LibraryPathVariableName;
// Regular environment variables
QHash<QString, QString> envVars = q->envVars();
foreach(const QString& key, envVars.keys())
{
this->reportInfo(QString("Setting env. variable [%1]:%2").arg(key, envVars[key]));
env.insert(key, envVars[key]);
}

// Add library path and PATH to map
// Path environment variables
QHash<QString, QStringList> envPathsVars = q->additionalPathsVars();
// Add LibraryPaths and Paths to map
#ifdef Q_OS_WIN32
newVars["PATH"] = (q->paths() + q->libraryPaths()).join(this->PathSep);
envPathsVars["PATH"] = (q->paths() + q->libraryPaths());
#else
newVars[this->LibraryPathVariableName] = q->libraryPaths().join(this->PathSep);
newVars["PATH"] = q->paths().join(this->PathSep);
envPathsVars[this->LibraryPathVariableName] = q->libraryPaths();
envPathsVars["PATH"] = q->paths();
#endif

// Set environment variables
foreach(const QString& key, newVars.keys())
foreach(const QString& key, envPathsVars.keys())
{
QString value = newVars[key];
if (appendVars.contains(key) && env.contains(key))
QString value = envPathsVars[key].join(this->PathSep);
this->reportInfo(QString("Setting env. variable [%1]:%2").arg(key, value));
if (env.contains(key))
{
value = QString("%1%2%3").arg(value, this->PathSep, env.value(key));
}
this->reportInfo(QString("Setting env. variable [%1]:%2").arg(key, value));
env.insert(key, value);
}
}
Expand Down
24 changes: 18 additions & 6 deletions Base/ctkAppLauncherSettings.cpp
Expand Up @@ -387,6 +387,13 @@ QString ctkAppLauncherSettings::envVar(const QString& variableName, bool expand
return value;
}

// --------------------------------------------------------------------------
QHash<QString, QString> ctkAppLauncherSettings::envVars(bool expand /* = true */) const
{
Q_D(const ctkAppLauncherSettings);
return expand ? d->MapOfExpandedEnvVars : d->MapOfEnvVars;
}

// --------------------------------------------------------------------------
QStringList ctkAppLauncherSettings::additionalPaths(const QString& variableName, bool expand /* = true */) const
{
Expand All @@ -400,15 +407,20 @@ QStringList ctkAppLauncherSettings::additionalPaths(const QString& variableName,
}

// --------------------------------------------------------------------------
QHash<QString, QString> ctkAppLauncherSettings::envVars(bool expand /* = true */) const
QHash<QString, QStringList> ctkAppLauncherSettings::additionalPathsVars(bool expand /* = true */) const
{
Q_D(const ctkAppLauncherSettings);
QHash<QString, QString> newVars;
QSet<QString> keys = QSet<QString>::fromList(
d->MapOfEnvVars.keys() + d->MapOfPathVars.keys());
foreach(const QString& key, keys)
QHash<QString, QStringList> newVars;
foreach(const QString& varName, d->MapOfPathVars.keys())
{
newVars.insert(key, this->envVar(key, expand));
newVars.insert(varName, this->additionalPaths(varName, expand));
}
return newVars;
}

// --------------------------------------------------------------------------
QString ctkAppLauncherSettings::pathSep() const
{
Q_D(const ctkAppLauncherSettings);
return d->PathSep;
}
25 changes: 19 additions & 6 deletions Base/ctkAppLauncherSettings.h
Expand Up @@ -120,7 +120,7 @@ class ctkAppLauncherSettingsPrivate;
/// // Get values (read from both the main settings file and the additional one).
/// qDebug() << appLauncherSettings.paths();
/// qDebug() << appLauncherSettings.libraryPaths();
/// qDebug() << appLauncherSettings.additionalPaths("PYTHONPATH");
/// qDebug() << appLauncherSettings.additionalPathsVars().value("PYTHONPATH");
/// qDebug() << appLauncherSettings.envVars().value("FOO");
/// \endcode
///
Expand Down Expand Up @@ -166,18 +166,31 @@ class ctkAppLauncherSettings : public QObject
/// by appending the environment variable to the matching path variable.
QString envVar(const QString& variableName, bool expand = true) const;

/// Get list of environment variables associated with \c EnvironmentVariables
/// group.
///
/// \sa envVar(const QString& variableName, bool expand)
QHash<QString, QString> envVars(bool expand = true) const;

/// \brief Get paths associated with \a variableName.
///
/// The returned list corresponds to the path matching one of the variable
/// associated with \c General/additionalPathVariables.
QStringList additionalPaths(const QString& variableName, bool expand = true) const;

/// Get list of environment variables associated with \a EnvironmentVariables
/// group or any path environment variables associated
/// with \c General/additionalPathVariables.
/// \brief Get dictionnary of path variable list associated with \c General/additionalPathVariables.
///
/// \sa envVar(const QString& variableName, bool expand)
QHash<QString, QString> envVars(bool expand = true) const;
/// \sa additionalPaths(const QString& variableName, bool expand)
QHash<QString, QStringList> additionalPathsVars(bool expand = true) const;

/// \brief Get current platform path separator.
///
/// Platform | Path separator
/// -------------| -------------
/// Unix | `:`
/// Windows | `;`
///
QString pathSep() const;

/// Return user specific additional settings file associated with the \c ApplicationOrganization,
/// \c ApplicationName and \c ApplicationRevision read from the main settings using
Expand Down
51 changes: 30 additions & 21 deletions Testing/LauncherLib/AppWithLauncherLib/main.cpp
Expand Up @@ -354,6 +354,20 @@ int checkReadSettingsWithExpand()
<< "/awesome/path/to/libexec/ASSOCIATION"
);

CHECK_QSTRINGLIST(
appLauncherSettings.additionalPathsVars().value("PYTHONPATH"),
QStringList()
<< "/awesome/path/to/lib/python/site-packages"
<< "/path/to/site-packages-2"
);

CHECK_QSTRINGLIST(
appLauncherSettings.additionalPathsVars().value("QT_PLUGIN_PATH"),
QStringList()
<< "/awesome/path/to/libexec/qt"
<< "/awesome/path/to/libexec/ASSOCIATION"
);

return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -444,30 +458,18 @@ int checkReadAdditionalSettingsWithExpand()
"dog"
);

#if defined(Q_OS_WIN32)
CHECK_QSTRING(
appLauncherSettings.envVar("PYTHONPATH"),
"/awesome/path/to/lib/python/site-packages;/awesome/path/to/lib/python/site-packages-3;/path/to/site-packages-RAB;/awesome/path/to/lib/python/site-packages;/path/to/site-packages-2"
);

CHECK_QSTRING(
appLauncherSettings.envVar("QT_PLUGIN_PATH"),
"/awesome/path/to/libexec/qt;/awesome/path/to/libexec/RAB"
);
#else
CHECK_QSTRING(
appLauncherSettings.envVar("PYTHONPATH"),
"/awesome/path/to/lib/python/site-packages:/awesome/path/to/lib/python/site-packages-3:/path/to/site-packages-RAB:/awesome/path/to/lib/python/site-packages:/path/to/site-packages-2"
);

CHECK_QSTRING(
appLauncherSettings.envVar("QT_PLUGIN_PATH"),
"/awesome/path/to/libexec/qt:/awesome/path/to/libexec/RAB"
CHECK_QSTRINGLIST(
appLauncherSettings.additionalPaths("PYTHONPATH"),
QStringList()
<< "/awesome/path/to/lib/python/site-packages"
<< "/awesome/path/to/lib/python/site-packages-3"
<< "/path/to/site-packages-RAB"
<< "/awesome/path/to/lib/python/site-packages"
<< "/path/to/site-packages-2"
);
#endif

CHECK_QSTRINGLIST(
appLauncherSettings.additionalPaths("PYTHONPATH"),
appLauncherSettings.additionalPathsVars().value("PYTHONPATH"),
QStringList()
<< "/awesome/path/to/lib/python/site-packages"
<< "/awesome/path/to/lib/python/site-packages-3"
Expand All @@ -483,5 +485,12 @@ int checkReadAdditionalSettingsWithExpand()
<< "/awesome/path/to/libexec/RAB"
);

CHECK_QSTRINGLIST(
appLauncherSettings.additionalPathsVars().value("QT_PLUGIN_PATH"),
QStringList()
<< "/awesome/path/to/libexec/qt"
<< "/awesome/path/to/libexec/RAB"
);

return EXIT_SUCCESS;
}

0 comments on commit b05bb88

Please sign in to comment.