Commit 02105ddf authored by Christian Kandeler's avatar Christian Kandeler

Utils: Untie the Environment class from the host system.

It is used e.g. in the RemoteLinux plugin
(via ProjectExplorer::EnvironmentAspect) and should
therefore not assume that it's meant for a host system process.

Change-Id: I9f44f862f133579b65951e43fc1e654c425fd3d7
Reviewed-by: default avatarDaniel Teske <daniel.teske@digia.com>
parent 6d684220
......@@ -29,8 +29,6 @@
#include "environment.h"
#include "hostosinfo.h"
#include <QDir>
#include <QProcessEnvironment>
#include <QCoreApplication>
......@@ -99,12 +97,12 @@ QStringList EnvironmentItem::toStringList(const QList<EnvironmentItem> &list)
return result;
}
Environment::Environment(const QStringList &env)
Environment::Environment(const QStringList &env, OsType osType) : m_osType(osType)
{
foreach (const QString &s, env) {
int i = s.indexOf(QLatin1Char('='));
if (i >= 0) {
if (HostOsInfo::isWindowsHost())
if (m_osType == OsTypeWindows)
m_values.insert(s.left(i).toUpper(), s.mid(i+1));
else
m_values.insert(s.left(i), s.mid(i+1));
......@@ -136,17 +134,17 @@ QProcessEnvironment Environment::toProcessEnvironment() const
void Environment::set(const QString &key, const QString &value)
{
m_values.insert(HostOsInfo::isWindowsHost() ? key.toUpper() : key, value);
m_values.insert(m_osType == OsTypeWindows ? key.toUpper() : key, value);
}
void Environment::unset(const QString &key)
{
m_values.remove(HostOsInfo::isWindowsHost() ? key.toUpper() : key);
m_values.remove(m_osType == OsTypeWindows ? key.toUpper() : key);
}
void Environment::appendOrSet(const QString &key, const QString &value, const QString &sep)
{
const QString &_key = HostOsInfo::isWindowsHost() ? key.toUpper() : key;
const QString &_key = m_osType == OsTypeWindows ? key.toUpper() : key;
QMap<QString, QString>::iterator it = m_values.find(_key);
if (it == m_values.end()) {
m_values.insert(_key, value);
......@@ -160,7 +158,7 @@ void Environment::appendOrSet(const QString &key, const QString &value, const QS
void Environment::prependOrSet(const QString&key, const QString &value, const QString &sep)
{
const QString &_key = HostOsInfo::isWindowsHost() ? key.toUpper() : key;
const QString &_key = m_osType == OsTypeWindows ? key.toUpper() : key;
QMap<QString, QString>::iterator it = m_values.find(_key);
if (it == m_values.end()) {
m_values.insert(_key, value);
......@@ -175,18 +173,18 @@ void Environment::prependOrSet(const QString&key, const QString &value, const QS
void Environment::appendOrSetPath(const QString &value)
{
appendOrSet(QLatin1String("PATH"), QDir::toNativeSeparators(value),
QString(HostOsInfo::pathListSeparator()));
QString(OsSpecificAspects(m_osType).pathListSeparator()));
}
void Environment::prependOrSetPath(const QString &value)
{
prependOrSet(QLatin1String("PATH"), QDir::toNativeSeparators(value),
QString(HostOsInfo::pathListSeparator()));
QString(OsSpecificAspects(m_osType).pathListSeparator()));
}
void Environment::prependOrSetLibrarySearchPath(const QString &value)
{
switch (HostOsInfo::hostOs()) {
switch (m_osType) {
case OsTypeWindows: {
const QChar sep = QLatin1Char(';');
const QLatin1String path("PATH");
......@@ -243,7 +241,7 @@ QString Environment::searchInPath(const QString &executable,
QFileInfo fi(exec);
QStringList execs(exec);
if (HostOsInfo::isWindowsHost()) {
if (m_osType == OsTypeWindows) {
// Check all the executable extensions on windows:
// PATHEXT is only used if the executable has no extension
if (fi.suffix().isEmpty()) {
......@@ -283,8 +281,8 @@ QString Environment::searchInPath(const QString &executable,
QStringList Environment::path() const
{
return m_values.value(QLatin1String("PATH")).split(HostOsInfo::pathListSeparator(),
QString::SkipEmptyParts);
return m_values.value(QLatin1String("PATH"))
.split(OsSpecificAspects(m_osType).pathListSeparator(), QString::SkipEmptyParts);
}
QString Environment::value(const QString &key) const
......@@ -398,7 +396,7 @@ bool Environment::hasKey(const QString &key) const
QString Environment::userName() const
{
return value(QLatin1String(HostOsInfo::isWindowsHost() ? "USERNAME" : "USER"));
return value(QLatin1String(m_osType == OsTypeWindows ? "USERNAME" : "USER"));
}
bool Environment::operator!=(const Environment &other) const
......@@ -408,7 +406,7 @@ bool Environment::operator!=(const Environment &other) const
bool Environment::operator==(const Environment &other) const
{
return m_values == other.m_values;
return m_osType == other.m_osType && m_values == other.m_values;
}
/** Expand environment variables in a string.
......@@ -422,7 +420,7 @@ QString Environment::expandVariables(const QString &input) const
{
QString result = input;
if (HostOsInfo::isWindowsHost()) {
if (m_osType == OsTypeWindows) {
for (int vStart = -1, i = 0; i < result.length(); ) {
if (result.at(i++) == QLatin1Char('%')) {
if (vStart > 0) {
......
......@@ -30,6 +30,7 @@
#ifndef UTILS_ENVIRONMENT_H
#define UTILS_ENVIRONMENT_H
#include "hostosinfo.h"
#include "utils_global.h"
#include <QMap>
......@@ -65,8 +66,8 @@ class QTCREATOR_UTILS_EXPORT Environment
public:
typedef QMap<QString, QString>::const_iterator const_iterator;
Environment() {}
explicit Environment(const QStringList &env);
explicit Environment(OsType osType = HostOsInfo::hostOs()) : m_osType(osType) {}
explicit Environment(const QStringList &env, OsType osType = HostOsInfo::hostOs());
static Environment systemEnvironment();
QStringList toStringList() const;
......@@ -112,6 +113,7 @@ public:
private:
QString searchInDirectory(const QStringList &execs, QString directory) const;
QMap<QString, QString> m_values;
OsType m_osType;
};
} // namespace Utils
......
......@@ -42,6 +42,7 @@ namespace Internal {
RemoteLinuxEnvironmentReader::RemoteLinuxEnvironmentReader(RunConfiguration *config, QObject *parent)
: QObject(parent)
, m_stop(false)
, m_env(Utils::OsTypeLinux)
, m_kit(config->target()->kit())
, m_remoteProcessRunner(0)
{
......@@ -104,7 +105,7 @@ void RemoteLinuxEnvironmentReader::remoteProcessFinished(int exitCode)
QString remoteOutput = QString::fromUtf8(m_remoteProcessRunner->readAllStandardOutput());
if (!remoteOutput.isEmpty()) {
m_env = Utils::Environment(remoteOutput.split(QLatin1Char('\n'),
QString::SkipEmptyParts));
QString::SkipEmptyParts), Utils::OsTypeLinux);
}
} else {
QString errorMsg = tr("Error running remote process: %1")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment