Skip to content

Commit

Permalink
use the system environment when no source is given
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrauch committed Mar 29, 2024
1 parent f9f16c6 commit d82ee2b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
57 changes: 28 additions & 29 deletions src/project_manager/ros_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ QString ROSUtils::buildTypeName(const ROSUtils::BuildType &buildType)

bool ROSUtils::sourceROS(QProcess *process, const Utils::FilePath &rosDistribution)
{
bool results = sourceWorkspaceHelper(process, Utils::FilePath(rosDistribution).pathAppended(QLatin1String("setup.bash")).toString());
if (!results)
Core::MessageManager::writeSilently(QObject::tr("[ROS Warning] Faild to source ROS Distribution: %1.").arg(rosDistribution.toString()));

return results;
sourceWorkspaceHelper(process, Utils::FilePath(rosDistribution).pathAppended(QLatin1String("setup.bash")).toString());
return true;
}

bool ROSUtils::sourceWorkspace(QProcess *process, const WorkspaceInfo &workspaceInfo)
Expand All @@ -79,27 +76,26 @@ bool ROSUtils::sourceWorkspace(QProcess *process, const WorkspaceInfo &workspace

Utils::FilePath source_bash_file = sourcePath.pathAppended("setup.bash");
Utils::FilePath source_shell_file = sourcePath.pathAppended("setup.sh");
QString source_path;
if (source_bash_file.exists())
{
Core::MessageManager::writeSilently(QObject::tr("[ROS Debug] Sourced workspace: %1.").arg(source_bash_file.toString()));
if (sourceWorkspaceHelper(process, source_bash_file.toString()))
return true;
source_path = source_bash_file.toString();
}
else if (source_shell_file.exists())
{
// Some reason if a workspace does not contain at least one catkin package it does not generate a setup.bash only a setup.sh
Core::MessageManager::writeSilently(QObject::tr("[ROS Debug] Sourced workspace: %1.").arg(source_shell_file.toString()));
if (sourceWorkspaceHelper(process, source_shell_file.toString()))
return true;
source_path = source_shell_file.toString();
}
else
{
Core::MessageManager::writeSilently(QObject::tr("[ROS Warning] Failed to source workspace because either of these files do not exist: %1 or %2.").arg(source_bash_file.toString(), source_shell_file.toString()));
return true;
source_path = QString{};
}

Core::MessageManager::writeSilently(QObject::tr("[ROS Warning] Failed to source workspace: %1.").arg(workspaceInfo.path.toString()));
return false;
sourceWorkspaceHelper(process, source_path);
return true;
}

bool ROSUtils::isWorkspaceInitialized(const WorkspaceInfo &workspaceInfo)
Expand Down Expand Up @@ -319,27 +315,30 @@ QList<Utils::FilePath> ROSUtils::installedDistributions()
return distributions;
}

bool ROSUtils::sourceWorkspaceHelper(QProcess *process, const QString &path)
void ROSUtils::sourceWorkspaceHelper(QProcess *process, const QString &path)
{
QStringList env_list;
QString cmd = QLatin1String("source ") + path + QLatin1String(" && env");

process->start(QLatin1String("bash"), QStringList());
process->waitForStarted();
process->write(cmd.toLatin1());
process->closeWriteChannel();
process->waitForFinished();
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();

if (process->exitStatus() != QProcess::CrashExit)
if (!path.isEmpty())
{
QString output = QString::fromStdString(process->readAllStandardOutput().toStdString());
env_list = output.split(QRegularExpression("[\r\n]"), Qt::SkipEmptyParts);

Utils::Environment env(env_list);
process->setProcessEnvironment(env.toProcessEnvironment());
return true;
const QString cmd = QLatin1String("source ") + path + QLatin1String(" && env");
process->start(QLatin1String("bash"), QStringList());
process->waitForStarted();
process->write(cmd.toLatin1());
process->closeWriteChannel();
process->waitForFinished();

if (process->exitStatus() != QProcess::CrashExit)
{
while (process->canReadLine()) {
const QStringList env_kv = QString::fromLocal8Bit(process->readLine().trimmed())
.split('=', Qt::SkipEmptyParts);
env.insert(env_kv[0], env_kv[1]);
}
}
}
return false;

process->setProcessEnvironment(env);
}

bool ROSUtils::generateQtCreatorWorkspaceFile(QXmlStreamWriter &xmlFile, const ROSProjectFileContent &content)
Expand Down
3 changes: 1 addition & 2 deletions src/project_manager/ros_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,8 @@ class ROSUtils {
* @brief sourceWorkspaceHelper - Source workspace helper function
* @param process - QProcess to execute source bash command
* @param path - Path to workspace setup.bash
* @return True if successful
*/
static bool sourceWorkspaceHelper(QProcess *process, const QString &path);
static void sourceWorkspaceHelper(QProcess *process, const QString &path);

/**
* @brief This will parse the CodeBlock file and get the build info (incudes, Cxx Flags, etc.)
Expand Down

0 comments on commit d82ee2b

Please sign in to comment.