Commit 5f3b9dd8 authored by Christian Kandeler's avatar Christian Kandeler

QbsProjectManager: Fix working directory handling.

Commit 2dbc5b10 introduced usage of WorkingDirectoryAspect into the
qbs run configurations for Desktop targets. However, the design of that
class was not suitable for the way our working directory logic works:
There typically is a user-provided value and, as a fallback if the user
does not provide anything, a default value managed by (build system
specific) code. The WorkingDirectoryAspect class could not differentiate
between these two values and so the value stayed forever at the initial
default if the user did not override it, instead of adapting to the
location of the executable.
This patch makes the necessary changes to the WorkingDirectoryAspect
class so that it matches the actual use case.

Task-number: QTCREATORBUG-14891
Change-Id: I7555d0a9cb4b04b75c9215a988278db32eb1ca10
Reviewed-by: default avatarJake Petroules <jake.petroules@petroules.com>
parent d0655e5c
......@@ -135,8 +135,8 @@ void TerminalAspect::setRunMode(ApplicationLauncher::Mode runMode)
\class ProjectExplorer::WorkingDirectoryAspect
*/
WorkingDirectoryAspect::WorkingDirectoryAspect(RunConfiguration *runConfig, const QString &key, const QString &dir)
: IRunConfigurationAspect(runConfig), m_workingDirectory(dir), m_chooser(0), m_key(key)
WorkingDirectoryAspect::WorkingDirectoryAspect(RunConfiguration *runConfig, const QString &key)
: IRunConfigurationAspect(runConfig), m_chooser(0), m_key(key)
{
setDisplayName(tr("Working Directory"));
setId("WorkingDirectoryAspect");
......@@ -149,7 +149,10 @@ WorkingDirectoryAspect *WorkingDirectoryAspect::create(RunConfiguration *runConf
WorkingDirectoryAspect *WorkingDirectoryAspect::clone(RunConfiguration *runConfig) const
{
return new WorkingDirectoryAspect(runConfig, m_key, m_workingDirectory);
auto * const aspect = new WorkingDirectoryAspect(runConfig, m_key);
aspect->m_defaultWorkingDirectory = m_defaultWorkingDirectory;
aspect->m_workingDirectory = m_workingDirectory;
return aspect;
}
void WorkingDirectoryAspect::addToMainConfigurationWidget(QWidget *parent, QFormLayout *layout)
......@@ -159,8 +162,10 @@ void WorkingDirectoryAspect::addToMainConfigurationWidget(QWidget *parent, QForm
m_chooser->setHistoryCompleter(m_key);
m_chooser->setExpectedKind(Utils::PathChooser::Directory);
m_chooser->setPromptDialogTitle(tr("Select Working Directory"));
connect(m_chooser.data(), &PathChooser::pathChanged,
this, &WorkingDirectoryAspect::setWorkingDirectory);
m_chooser->lineEdit()->setPlaceholderText(m_defaultWorkingDirectory);
m_chooser->setPath(m_workingDirectory);
connect(m_chooser.data(), &PathChooser::pathChanged, this,
[this]() { m_workingDirectory = m_chooser->rawPath(); });
auto resetButton = new QToolButton(parent);
resetButton->setToolTip(tr("Reset to Default"));
......@@ -187,25 +192,39 @@ void WorkingDirectoryAspect::updateEnvironment()
m_chooser->setEnvironment(envAspect->environment());
}
QString WorkingDirectoryAspect::keyForDefaultWd() const
{
return m_key + QLatin1String(".default");
}
void WorkingDirectoryAspect::resetPath()
{
QTC_ASSERT(m_chooser, return);
m_chooser->setPath(QString());
}
void WorkingDirectoryAspect::fromMap(const QVariantMap &map)
{
m_workingDirectory = map.value(m_key).toString();
m_defaultWorkingDirectory = map.value(keyForDefaultWd()).toString();
}
void WorkingDirectoryAspect::toMap(QVariantMap &data) const
{
data.insert(m_key, m_workingDirectory);
data.insert(keyForDefaultWd(), m_defaultWorkingDirectory);
}
QString WorkingDirectoryAspect::workingDirectory() const
{
return runConfiguration()->macroExpander()->expandProcessArgs(m_workingDirectory);
QTC_ASSERT(m_chooser, return m_defaultWorkingDirectory);
return m_workingDirectory.isEmpty()
? m_defaultWorkingDirectory
: runConfiguration()->macroExpander()->expandProcessArgs(m_chooser->path());
}
QString WorkingDirectoryAspect::defaultWorkingDirectory() const
{
return m_defaultWorkingDirectory;
}
QString WorkingDirectoryAspect::unexpandedWorkingDirectory() const
......@@ -213,9 +232,11 @@ QString WorkingDirectoryAspect::unexpandedWorkingDirectory() const
return m_workingDirectory;
}
void WorkingDirectoryAspect::setWorkingDirectory(const QString &workingDirectory)
void WorkingDirectoryAspect::setDefaultWorkingDirectory(const QString &defaultWorkingDir)
{
m_workingDirectory = workingDirectory;
m_defaultWorkingDirectory = defaultWorkingDir;
if (m_chooser)
m_chooser->lineEdit()->setPlaceholderText(m_defaultWorkingDirectory);
}
PathChooser *WorkingDirectoryAspect::pathChooser() const
......
......@@ -85,7 +85,7 @@ class PROJECTEXPLORER_EXPORT WorkingDirectoryAspect : public IRunConfigurationAs
Q_OBJECT
public:
explicit WorkingDirectoryAspect(RunConfiguration *runConfig, const QString &key, const QString &dir = QString());
explicit WorkingDirectoryAspect(RunConfiguration *runConfig, const QString &key);
WorkingDirectoryAspect *create(RunConfiguration *runConfig) const override;
WorkingDirectoryAspect *clone(RunConfiguration *runConfig) const override;
......@@ -93,8 +93,9 @@ public:
void addToMainConfigurationWidget(QWidget *parent, QFormLayout *layout);
QString workingDirectory() const;
QString defaultWorkingDirectory() const;
QString unexpandedWorkingDirectory() const;
void setWorkingDirectory(const QString &workingDirectory);
void setDefaultWorkingDirectory(const QString &defaultWorkingDir);
Utils::PathChooser *pathChooser() const;
private:
......@@ -103,8 +104,10 @@ private:
void resetPath();
void updateEnvironment();
QString keyForDefaultWd() const;
QString m_workingDirectory;
QString m_defaultWorkingDirectory;
QPointer<Utils::PathChooser> m_chooser;
QString m_key;
};
......
......@@ -246,21 +246,13 @@ bool QbsRunConfiguration::isConsoleApplication() const
QString QbsRunConfiguration::workingDirectory() const
{
EnvironmentAspect *aspect = extraAspect<EnvironmentAspect>();
QTC_ASSERT(aspect, return baseWorkingDirectory());
return QDir::cleanPath(aspect->environment().expandVariables(
macroExpander()->expand(baseWorkingDirectory())));
const auto *wdAspect = extraAspect<WorkingDirectoryAspect>();
QTC_ASSERT(wdAspect, return baseWorkingDirectory());
return wdAspect->workingDirectory();
}
QString QbsRunConfiguration::baseWorkingDirectory() const
{
WorkingDirectoryAspect *aspect = extraAspect<WorkingDirectoryAspect>();
// if the user overrode us, then return his working directory
QString wd = aspect->unexpandedWorkingDirectory();
if (!wd.isEmpty())
return wd;
// else what the pro file reader tells us
const QString exe = executable();
if (!exe.isEmpty())
return QFileInfo(executable()).absolutePath();
......@@ -272,18 +264,6 @@ QString QbsRunConfiguration::commandLineArguments() const
return extraAspect<ArgumentsAspect>()->arguments();
}
void QbsRunConfiguration::setBaseWorkingDirectory(const QString &wd)
{
WorkingDirectoryAspect *aspect = extraAspect<WorkingDirectoryAspect>();
const QString &oldWorkingDirectory = workingDirectory();
aspect->setWorkingDirectory(wd);
const QString &newWorkingDirectory = workingDirectory();
if (oldWorkingDirectory != newWorkingDirectory)
emit baseWorkingDirectoryChanged(newWorkingDirectory);
}
void QbsRunConfiguration::setRunMode(ApplicationLauncher::Mode runMode)
{
extraAspect<TerminalAspect>()->setRunMode(runMode);
......@@ -413,7 +393,7 @@ void QbsRunConfigurationWidget::targetInformationHasChanged()
setExecutableLineText(m_rc->executable());
WorkingDirectoryAspect *aspect = m_rc->extraAspect<WorkingDirectoryAspect>();
aspect->pathChooser()->setPath(m_rc->baseWorkingDirectory());
aspect->setDefaultWorkingDirectory(m_rc->baseWorkingDirectory());
aspect->pathChooser()->setBaseFileName(m_rc->target()->project()->projectDirectory());
m_ignoreChange = false;
}
......
......@@ -92,7 +92,6 @@ public:
bool isConsoleApplication() const;
signals:
void baseWorkingDirectoryChanged(const QString&);
void targetInformationChanged();
void usingDyldImageSuffixChanged(bool);
......@@ -104,7 +103,6 @@ private slots:
void installStepToBeRemoved(int pos);
private:
void setBaseWorkingDirectory(const QString &workingDirectory);
QString baseWorkingDirectory() const;
QString defaultDisplayName();
qbs::InstallOptions installOptions() const;
......
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