append %PATH% of many Visual Studio property sheets (.props) additively

独自空忆成欢 提交于 2020-04-18 01:45:06

问题


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?


回答1:


One way or another you'll need to inherit property values in each imported property sheet. You see if you write <A>someValue</A> then whatever A was it will now be set to someValue. You need <A>someValue;$(A)</A> to make the evaluated value consist of the 'old' one concatenated with the new one.

In this case you cannot simply concatenate the variables though becasue of the PATH= expression. Suppose you'd use

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

then you'd end up with

PATH=SOMEPATH2;PATH=SOMEPATH

So you need a workaround. One way is using a separate value for paths which you want to add:

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>

Disadvantage is that you need 2 variables and have to repeat the LocalDebuggerEnvironment part in each file otherwise you cannot use them standalone. However for the rest it's pretty usable and clear. And if you do not want to use B1 and B2 by themselves, you don't need the LocalDebuggerEnvironment in them at all. Then if you want just the value from B1, you'd add C and B1 to your project.

The only alternative I see currently would mean you'd have to do some parsing of the value to get the inheriting working without repating the PATH= part. But that won't be exactly pretty and can probably break in some cases. All in all I'd advice against it, just too complicated. Here's one variation; using Property Functions, take the current value of LocalDebuggerEnvironment, remove the PATH= part, remove the %PATH% part, then remove some consecutive semicolons (not strictly needed I guess). As you can see C doesn't need anything since the Bs already take care of inheriting the value and as such can be used standalone:

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" />


来源:https://stackoverflow.com/questions/60636185/append-path-of-many-visual-studio-property-sheets-props-additively

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!