1

To define PATH locally in a project from property sheet, I need to add it in LocalDebuggerEnvironment.

This approach works well when there is only 1 property sheet that define PATH.

If I have more than one property sheet, while I want to use PATH from every property sheet,
Visual Studio will consider only PATH of the last property sheet that I have included.

Example

If I create property sheet B1.props :-

<PropertyGroup Label="UserMacros"><LocalDebuggerEnvironment>
PATH=SOMEPATH1;%PATH%     
</LocalDebuggerEnvironment></PropertyGroup>

, property sheet B2.props :-

<PropertyGroup Label="UserMacros"><LocalDebuggerEnvironment>
PATH=SOMEPATH2;%PATH%       <!-- different only this line -->
</LocalDebuggerEnvironment></PropertyGroup> 

, property sheet C.props (=include B1.props & B2.props):-

<ImportGroup Label="PropertySheets">
    <Import Project="B1.props" />
    <Import Project="B2.props" />
  </ImportGroup>

, and set a Visual Studio project to use C.props

, I will get the result : PATH=SOMEPATH2;%PATH%.

Question

How to make Visual Studio use the summation of path e.g. PATH=SOMEPATH2;SOMEPATH1;%PATH%
... while maintain the nice property sheet modularity?

4

1 回答 1

3

您需要以一种或另一种方式继承每个导入的属性表中的属性值。你看如果你写<A>someValue</A>然后不管A它现在将被设置为someValue。您需要<A>someValue;$(A)</A>使评估值由与新值连接的“旧”值组成。

在这种情况下,由于 PATH= 表达式,您不能简单地连接变量。假设你会使用

<LocalDebuggerEnvironment>PATH=SOMEPATH;$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>
<LocalDebuggerEnvironment>PATH=SOMEPATH2;$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>

那么你最终会得到

PATH=SOMEPATH2;PATH=SOMEPATH

所以你需要一个解决方法。一种方法是为要添加的路径使用单独的值:

B1:

<PropertyGroup Label="UserMacros">
  <PathValue>SOMEPATH;$(PathValue)</PathValue>
  <LocalDebuggerEnvironment>PATH=$(PathValue);%PATH%</LocalDebuggerEnvironment>
</PropertyGroup>

B2:

<PropertyGroup Label="UserMacros">
  <PathValue>SOMEPATH2;$(PathValue)</PathValue>
  <LocalDebuggerEnvironment>PATH=$(PathValue);%PATH%</LocalDebuggerEnvironment>
</PropertyGroup>

C:

<PropertyGroup Label="UserMacros">
  <LocalDebuggerEnvironment>PATH=$(PathValue);%PATH%</LocalDebuggerEnvironment>
</PropertyGroup>

缺点是您需要 2 个变量并且必须LocalDebuggerEnvironment在每个文件中重复该部分,否则您不能单独使用它们。但是对于其余部分,它非常有用且清晰。如果您不想单独使用 B1 和 B2,则根本不需要LocalDebuggerEnvironment它们。然后,如果您只想要 B1 中的值,您可以将 C 和 B1 添加到您的项目中。

我目前看到的唯一替代方法意味着您必须对值进行一些解析才能使继承工作而不重复 PATH= 部分。但这不会很漂亮,并且在某些情况下可能会中断。总而言之,我建议不要这样做,太复杂了。这是一种变体;使用Property Functions,获取 的当前值LocalDebuggerEnvironment,删除 PATH= 部分,删除 %PATH% 部分,然后删除一些连续的分号(我猜不是严格需要)。如您所见,C 不需要任何东西,因为 B 已经负责继承该值,因此可以独立使用:

B1:

<PropertyGroup Label="UserMacros">
  <LocalDebuggerEnvironment>PATH=SOMEPATH;$(LocalDebuggerEnvironment.Replace('PATH=', '').Replace('%PATH%', '').Replace(';;', ';'));%PATH%</LocalDebuggerEnvironment>
</PropertyGroup>

B2:

<PropertyGroup Label="UserMacros">
  <LocalDebuggerEnvironment>PATH=SOMEPATH2;$(LocalDebuggerEnvironment.Replace('PATH=', '').Replace('%PATH%', '').Replace(';;', ';'));%PATH%</LocalDebuggerEnvironment>
</PropertyGroup>

C:

<PropertyGroup Label="UserMacros" />
于 2020-03-16T12:17:00.307 回答