ProjectExplorer: Simplify retrieval of WorkingDirectoryAspect values

Drop the macroExpander argument, at the cost of complicating internal
ProjectConfiguration setup a bit.

Simpler code at the user side.

Change-Id: Ie9ea0b719f6e402b44d9ba7ce6047aa4e15441fe
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2021-09-22 16:26:12 +02:00
parent d359a7902b
commit 3dd8831f14
8 changed files with 30 additions and 19 deletions

View File

@@ -98,8 +98,7 @@ bool CustomExecutableRunConfiguration::isEnabled() const
Runnable CustomExecutableRunConfiguration::runnable() const Runnable CustomExecutableRunConfiguration::runnable() const
{ {
FilePath workingDirectory = const FilePath workingDirectory = aspect<WorkingDirectoryAspect>()->workingDirectory();
aspect<WorkingDirectoryAspect>()->workingDirectory(macroExpander());
Runnable r; Runnable r;
r.command = commandLine(); r.command = commandLine();

View File

@@ -145,6 +145,12 @@ void ProjectConfiguration::acquaintAspects()
aspect->acquaintSiblings(m_aspects); aspect->acquaintSiblings(m_aspects);
} }
void ProjectConfiguration::doPostInit()
{
for (const std::function<void()> &postInit : qAsConst(m_postInit))
postInit();
}
FilePath ProjectConfiguration::mapFromBuildDeviceToGlobalPath(const FilePath &path) const FilePath ProjectConfiguration::mapFromBuildDeviceToGlobalPath(const FilePath &path) const
{ {
IDevice::ConstPtr dev = BuildDeviceKitAspect::device(kit()); IDevice::ConstPtr dev = BuildDeviceKitAspect::device(kit());

View File

@@ -91,6 +91,9 @@ public:
Utils::FilePath mapFromBuildDeviceToGlobalPath(const Utils::FilePath &path) const; Utils::FilePath mapFromBuildDeviceToGlobalPath(const Utils::FilePath &path) const;
void addPostInit(const std::function<void()> &fixup) { m_postInit.append(fixup); }
void doPostInit();
signals: signals:
void displayNameChanged(); void displayNameChanged();
void toolTipChanged(); void toolTipChanged();
@@ -103,6 +106,7 @@ private:
const Utils::Id m_id; const Utils::Id m_id;
Utils::DisplayName m_displayName; Utils::DisplayName m_displayName;
QString m_toolTip; QString m_toolTip;
QList<std::function<void()>> m_postInit;
}; };
// helper function: // helper function:

View File

@@ -1966,7 +1966,7 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
[] { [] {
if (const RunConfiguration * const rc = activeRunConfiguration()) { if (const RunConfiguration * const rc = activeRunConfiguration()) {
if (const auto wdAspect = rc->aspect<WorkingDirectoryAspect>()) if (const auto wdAspect = rc->aspect<WorkingDirectoryAspect>())
return wdAspect->workingDirectory(rc->macroExpander()).toString(); return wdAspect->workingDirectory().toString();
} }
return QString(); return QString();
}); });

View File

@@ -197,7 +197,7 @@ RunConfiguration::RunConfiguration(Target *target, Utils::Id id)
tr("The run configuration's working directory"), tr("The run configuration's working directory"),
[this] { [this] {
const auto wdAspect = aspect<WorkingDirectoryAspect>(); const auto wdAspect = aspect<WorkingDirectoryAspect>();
return wdAspect ? wdAspect->workingDirectory(&m_expander).toString() : QString(); return wdAspect ? wdAspect->workingDirectory().toString() : QString();
}); });
m_expander.registerVariable("RunConfig:Name", tr("The run configuration's name."), m_expander.registerVariable("RunConfig:Name", tr("The run configuration's name."),
[this] { return displayName(); }); [this] { return displayName(); });
@@ -215,6 +215,11 @@ RunConfiguration::RunConfiguration(Target *target, Utils::Id id)
arguments = argumentsAspect->arguments(macroExpander()); arguments = argumentsAspect->arguments(macroExpander());
return CommandLine{executable, arguments, CommandLine::Raw}; return CommandLine{executable, arguments, CommandLine::Raw};
}; };
addPostInit([this] {
if (const auto wdAspect = aspect<WorkingDirectoryAspect>())
wdAspect->setMacroExpander(&m_expander);
});
} }
RunConfiguration::~RunConfiguration() = default; RunConfiguration::~RunConfiguration() = default;
@@ -233,8 +238,6 @@ bool RunConfiguration::isEnabled() const
QWidget *RunConfiguration::createConfigurationWidget() QWidget *RunConfiguration::createConfigurationWidget()
{ {
if (const auto wdAspect = aspect<WorkingDirectoryAspect>())
wdAspect->setMacroExpanderProvider([this] { return &m_expander; });
Layouting::Form builder; Layouting::Form builder;
for (BaseAspect *aspect : qAsConst(m_aspects)) { for (BaseAspect *aspect : qAsConst(m_aspects)) {
if (aspect->isVisible()) if (aspect->isVisible())
@@ -402,7 +405,7 @@ Runnable RunConfiguration::runnable() const
Runnable r; Runnable r;
r.command = commandLine(); r.command = commandLine();
if (auto workingDirectoryAspect = aspect<WorkingDirectoryAspect>()) if (auto workingDirectoryAspect = aspect<WorkingDirectoryAspect>())
r.workingDirectory = workingDirectoryAspect->workingDirectory(macroExpander()); r.workingDirectory = workingDirectoryAspect->workingDirectory();
if (auto environmentAspect = aspect<EnvironmentAspect>()) if (auto environmentAspect = aspect<EnvironmentAspect>())
r.environment = environmentAspect->environment(); r.environment = environmentAspect->environment();
if (m_runnableModifier) if (m_runnableModifier)
@@ -571,6 +574,7 @@ RunConfiguration *RunConfigurationFactory::create(Target *target) const
rc->m_aspects.registerAspect(factory(target)); rc->m_aspects.registerAspect(factory(target));
rc->acquaintAspects(); rc->acquaintAspects();
rc->doPostInit();
return rc; return rc;
} }

View File

@@ -176,8 +176,8 @@ void WorkingDirectoryAspect::addToLayout(LayoutBuilder &builder)
{ {
QTC_CHECK(!m_chooser); QTC_CHECK(!m_chooser);
m_chooser = new PathChooser; m_chooser = new PathChooser;
if (m_expanderProvider) if (QTC_GUARD(m_macroExpander))
m_chooser->setMacroExpander(m_expanderProvider()); m_chooser->setMacroExpander(m_macroExpander);
m_chooser->setHistoryCompleter(settingsKey()); m_chooser->setHistoryCompleter(settingsKey());
m_chooser->setExpectedKind(Utils::PathChooser::Directory); m_chooser->setExpectedKind(Utils::PathChooser::Directory);
m_chooser->setPromptDialogTitle(tr("Select Working Directory")); m_chooser->setPromptDialogTitle(tr("Select Working Directory"));
@@ -246,14 +246,12 @@ void WorkingDirectoryAspect::toMap(QVariantMap &data) const
Macros in the value are expanded using \a expander. Macros in the value are expanded using \a expander.
*/ */
FilePath WorkingDirectoryAspect::workingDirectory(const MacroExpander *expander) const FilePath WorkingDirectoryAspect::workingDirectory() const
{ {
const Environment env = m_envAspect ? m_envAspect->environment() const Environment env = m_envAspect ? m_envAspect->environment()
: Environment::systemEnvironment(); : Environment::systemEnvironment();
FilePath res = m_workingDirectory; FilePath res = m_workingDirectory;
QString workingDir = m_workingDirectory.path(); const QString workingDir = m_workingDirectory.path();
if (expander)
workingDir = expander->expandProcessArgs(workingDir);
res.setPath(PathChooser::expandedDirectory(workingDir, env, QString())); res.setPath(PathChooser::expandedDirectory(workingDir, env, QString()));
return res; return res;
} }
@@ -293,9 +291,9 @@ void WorkingDirectoryAspect::setDefaultWorkingDirectory(const FilePath &defaultW
} }
} }
void WorkingDirectoryAspect::setMacroExpanderProvider(const MacroExpanderProvider &expanderProvider) void WorkingDirectoryAspect::setMacroExpander(MacroExpander *macroExpander)
{ {
m_expanderProvider = expanderProvider; m_macroExpander = macroExpander;
} }
/*! /*!

View File

@@ -78,11 +78,11 @@ public:
void addToLayout(Utils::LayoutBuilder &builder) override; void addToLayout(Utils::LayoutBuilder &builder) override;
void acquaintSiblings(const Utils::AspectContainer &) override; void acquaintSiblings(const Utils::AspectContainer &) override;
Utils::FilePath workingDirectory(const Utils::MacroExpander *expander) const; Utils::FilePath workingDirectory() const;
Utils::FilePath defaultWorkingDirectory() const; Utils::FilePath defaultWorkingDirectory() const;
Utils::FilePath unexpandedWorkingDirectory() const; Utils::FilePath unexpandedWorkingDirectory() const;
void setDefaultWorkingDirectory(const Utils::FilePath &defaultWorkingDirectory); void setDefaultWorkingDirectory(const Utils::FilePath &defaultWorkingDirectory);
void setMacroExpanderProvider(const Utils::MacroExpanderProvider &expanderProvider); void setMacroExpander(Utils::MacroExpander *macroExpander);
Utils::PathChooser *pathChooser() const; Utils::PathChooser *pathChooser() const;
private: private:
@@ -96,7 +96,7 @@ private:
Utils::FilePath m_defaultWorkingDirectory; Utils::FilePath m_defaultWorkingDirectory;
QPointer<Utils::PathChooser> m_chooser; QPointer<Utils::PathChooser> m_chooser;
QPointer<QToolButton> m_resetButton; QPointer<QToolButton> m_resetButton;
Utils::MacroExpanderProvider m_expanderProvider; Utils::MacroExpander *m_macroExpander = nullptr;
}; };
class PROJECTEXPLORER_EXPORT ArgumentsAspect : public Utils::BaseAspect class PROJECTEXPLORER_EXPORT ArgumentsAspect : public Utils::BaseAspect

View File

@@ -203,7 +203,7 @@ Target::Target(Project *project, Kit *k, _constructor_tag) :
[this] { [this] {
if (RunConfiguration * const rc = activeRunConfiguration()) { if (RunConfiguration * const rc = activeRunConfiguration()) {
if (const auto wdAspect = rc->aspect<WorkingDirectoryAspect>()) if (const auto wdAspect = rc->aspect<WorkingDirectoryAspect>())
return wdAspect->workingDirectory(&d->m_macroExpander).toString(); return wdAspect->workingDirectory().toString();
} }
return QString(); return QString();
}, false); }, false);