diff options
author | Eike Ziller <eike.ziller@qt.io> | 2023-01-04 15:30:43 +0100 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2023-01-09 12:05:47 +0000 |
commit | b7731b58a8e64a8faeb799c3fd9e409e8780a5b7 (patch) | |
tree | e737025ac8e30e3fa5889f5b69918e5d109afc42 /src/plugins/projectexplorer/project.cpp | |
parent | 23fa784e9c90fcc26413012f861becf696503fab (diff) | |
download | qt-creator-b7731b58a8e64a8faeb799c3fd9e409e8780a5b7.tar.gz |
MacroExpander: Generalize registration of project variables
We provide global macros for ActiveProject and CurrentDocument:Project,
but these were completely separate, and the CurrentDocument:Project one
comparatively incomplete. Unifiy them.
Change-Id: I2622012f2c760e9f5b328766eca9d64c0bf9c5b1
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Diffstat (limited to 'src/plugins/projectexplorer/project.cpp')
-rw-r--r-- | src/plugins/projectexplorer/project.cpp | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/src/plugins/projectexplorer/project.cpp b/src/plugins/projectexplorer/project.cpp index 35d15ecc36..256d8c17cd 100644 --- a/src/plugins/projectexplorer/project.cpp +++ b/src/plugins/projectexplorer/project.cpp @@ -8,12 +8,14 @@ #include "buildsystem.h" #include "deployconfiguration.h" #include "editorconfiguration.h" +#include "environmentaspect.h" #include "kit.h" #include "kitinformation.h" #include "projectexplorer.h" #include "projectexplorerconstants.h" #include "projectnodes.h" #include "runconfiguration.h" +#include "runconfigurationaspects.h" #include "session.h" #include "target.h" #include "taskhub.h" @@ -1087,6 +1089,129 @@ void Project::runGenerator(Utils::Id id) } } +void Project::addVariablesToMacroExpander(const QByteArray &prefix, + const QString &descriptor, + MacroExpander *expander, + const std::function<Project *()> &projectGetter) +{ + const auto targetGetter = [projectGetter]() -> Target * { + if (const Project *const project = projectGetter()) + return project->activeTarget(); + return nullptr; + }; + const auto bcGetter = [targetGetter]() -> BuildConfiguration * { + if (const Target *const target = targetGetter()) + return target->activeBuildConfiguration(); + return nullptr; + }; + const auto rcGetter = [targetGetter]() -> RunConfiguration * { + if (const Target *const target = targetGetter()) + return target->activeRunConfiguration(); + return nullptr; + }; + const QByteArray fullPrefix = (prefix.endsWith(':') ? prefix : prefix + ':'); + const QByteArray prefixWithoutColon = fullPrefix.chopped(1); + expander->registerVariable(fullPrefix + "Name", + //: %1 is something like "Active project" + tr("%1: Name.").arg(descriptor), + [projectGetter]() -> QString { + if (const Project *const project = projectGetter()) + return project->displayName(); + return {}; + }); + expander->registerFileVariables(prefixWithoutColon, + //: %1 is something like "Active project" + tr("%1: Full path to main file.").arg(descriptor), + [projectGetter]() -> FilePath { + if (const Project *const project = projectGetter()) + return project->projectFilePath(); + return {}; + }); + expander->registerVariable(fullPrefix + "Kit:Name", + //: %1 is something like "Active project" + tr("%1: The name the active kit.").arg(descriptor), + [targetGetter]() -> QString { + if (const Target *const target = targetGetter()) + return target->kit()->displayName(); + return {}; + }); + expander->registerVariable(fullPrefix + "BuildConfig:Name", + //: %1 is something like "Active project" + tr("%1: Name of the active build configuration.").arg(descriptor), + [bcGetter]() -> QString { + if (const BuildConfiguration *const bc = bcGetter()) + return bc->displayName(); + return {}; + }); + expander->registerVariable(fullPrefix + "BuildConfig:Type", + //: %1 is something like "Active project" + tr("%1: Type of the active build configuration.").arg(descriptor), + [bcGetter]() -> QString { + const BuildConfiguration *const bc = bcGetter(); + const BuildConfiguration::BuildType type + = bc ? bc->buildType() : BuildConfiguration::Unknown; + return BuildConfiguration::buildTypeName(type); + }); + expander + ->registerVariable(fullPrefix + "BuildConfig:Path", + //: %1 is something like "Active project" + tr("%1: Full build path of active build configuration.").arg(descriptor), + [bcGetter]() -> QString { + if (const BuildConfiguration *const bc = bcGetter()) + return bc->buildDirectory().toUserOutput(); + return {}; + }); + expander->registerPrefix(fullPrefix + "BuildConfig:Env", + //: %1 is something like "Active project" + tr("%1: Variables in the active build environment.").arg(descriptor), + [bcGetter](const QString &var) { + if (BuildConfiguration *const bc = bcGetter()) + return bc->environment().expandedValueForKey(var); + return QString(); + }); + + expander->registerVariable(fullPrefix + "RunConfig:Name", + //: %1 is something like "Active project" + tr("%1: Name of the active run configuration.").arg(descriptor), + [rcGetter]() -> QString { + if (const RunConfiguration *const rc = rcGetter()) + return rc->displayName(); + return QString(); + }); + expander->registerFileVariables(fullPrefix + "RunConfig:Executable", + //: %1 is something like "Active project" + tr("%1: Executable of the active run configuration.") + .arg(descriptor), + [rcGetter]() -> FilePath { + if (const RunConfiguration *const rc = rcGetter()) + return rc->commandLine().executable(); + return {}; + }); + expander->registerPrefix(fullPrefix + "RunConfig:Env", + //: %1 is something like "Active project" + tr("%1: Variables in the environment of the active run configuration.") + .arg(descriptor), + [rcGetter](const QString &var) { + if (const RunConfiguration *const rc = rcGetter()) { + if (const auto envAspect = rc->aspect<EnvironmentAspect>()) + return envAspect->environment().expandedValueForKey(var); + } + return QString(); + }); + expander->registerVariable(fullPrefix + "RunConfig:WorkingDir", + //: %1 is something like "Active project" + tr("%1: Working directory of the active run configuration.") + .arg(descriptor), + [rcGetter] { + if (const RunConfiguration *const rc = rcGetter()) { + if (const auto wdAspect + = rc->aspect<WorkingDirectoryAspect>()) + return wdAspect->workingDirectory().toString(); + } + return QString(); + }); +} + #if defined(WITH_TESTS) static FilePath constructTestPath(const QString &basePath) |