如何重新评估 MSBuild 属性

发布于 2024-10-26 03:28:31 字数 764 浏览 1 评论 0原文

我可以创建 MSBuild 属性,其中包含未扩展的属性引用。以下是示例:

文本文件 property.txt 包含单行

$(一些财产)

它可以用 msbuild 脚本进行处理,如下所示:

<ItemGroup>
  <PropertyFile Include="property.txt"/>
</ItemGroup>

<!-- Standart task for file reading -->
<ReadLinesFromFile File="@(PropertyFile)" >
  <Output
      TaskParameter="Lines"
      ItemName="ItemsFromFile"/>
</ReadLinesFromFile>

<!-- Property LastLine now has value $(SomeProperty) -->
<PropertyGroup>
  <LastLine>%(ItemsFromFile.Identity)</LastLine>     
</PropertyGroup>

<Message Text="$(LastLine)"/>

现在我想扩展包含在 CurrentLine 中的属性引用。是否可以?

I can create MSBuild property, which contain unexpanded property reference. Here is example of it:

Text file property.txt contains single line


$(SomeProperty)

It can be processed with msbuild script like this:

<ItemGroup>
  <PropertyFile Include="property.txt"/>
</ItemGroup>

<!-- Standart task for file reading -->
<ReadLinesFromFile File="@(PropertyFile)" >
  <Output
      TaskParameter="Lines"
      ItemName="ItemsFromFile"/>
</ReadLinesFromFile>

<!-- Property LastLine now has value $(SomeProperty) -->
<PropertyGroup>
  <LastLine>%(ItemsFromFile.Identity)</LastLine>     
</PropertyGroup>

<Message Text="$(LastLine)"/>

Now I want to expand property reference, which contains in CurrentLine. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光①夏 2024-11-02 03:28:31

如果您事先知道作为文件中列出的单个属性的候选属性的所有属性,则可以通过项目过滤来实现此目的。
注意事项:

1) 仅指定文件中的 PropertyName,而不是 $(PropertyName),除非您想使用属性函数解析字符串。

2)使用下面的方法,文件中只能有一行,对于多行,您需要额外的批处理级别。

<ItemGroup>
  <PropertyFile Include="property.txt" />
</ItemGroup>

<PropertyGroup>
  <SomeProperty>1</SomeProperty>
  <SomeOtherProperty>2</SomeOtherProperty>
</PropertyGroup>
<ItemGroup>
  <ChooseProperty Include="SomeProperty">
    <Value>$(SomeProperty)</Value>
  </ChooseProperty>
  <ChooseProperty Include="SomeOtherProperty">
    <Value>$(SomeOtherProperty)</Value>
  </ChooseProperty>
</ItemGroup>

上面的构造建立了两个候选属性:$(SomeProperty) 和$(SomeOtherProperty)。对于此示例,property.txt 的内容是一行...

SomeProperty

...与值为 1 的 $(SomeProperty) 相关。

<Target Name="ReadItems">
  <ReadLinesFromFile File="@(PropertyFile)">
    <Output
      TaskParameter="Lines"
      ItemName="ItemsFromFile"
      />
  </ReadLinesFromFile>
</Target>

<Target Name="FilterItems"
  Outputs="%(ChooseProperty.Identity)">
  <PropertyGroup>
    <_ThisProperty>%(ChooseProperty.Identity)</_ThisProperty>
    <_ThisValue>%(ChooseProperty.Value)</_ThisValue>
    <_ItemFromFile>%(ItemsFromFile.Identity)</_ItemFromFile>
  </PropertyGroup>
  <ItemGroup Condition="'$(_ItemFromFile)' == '$(_ThisProperty)'">
    <_FilteredItems Include="$(_ThisProperty)">
      <Value>$(_ThisValue)</Value>
    </_FilteredItems>
  </ItemGroup>
</Target>

上面是下面主目标的两个依赖目标。他们使用依赖目标来读取文件,以便将其发布到项目组中,以便与 @(ChooseProperty) 项目组上的目标批处理一起使用。需要注意的关键是创建 @(_FilteredItems) 项目组的条件,该组将包含单个成员,因为......

Condition="'$(_ItemFromFile)' == '$(_ThisProperty)'

请注意,批处理的项目元数据被传输到这些临时属性为了使条件起作用(这就是为什么文件只能包含一行,因此只有一个项目)。

<Target Name="ChoosePropertyFromItem"
  DependsOnTargets="ReadItems;FilterItems">
  <!-- Standard task for file reading -->
  <PropertyGroup>
    <LastLine>%(_FilteredItems.Value)</LastLine>
  </PropertyGroup>
  <Message Text="LastLine='$(LastLine)'" />
</Target>

...最后 $(LastLine) 属性是从 @(_FilteredItems) 中的单个项目中提取的。结果输出如下:

ChoosePropertyFromItem:
  LastLine='1'

更改 property.txt 以包含“SomeOtherProperty”会导致以下结果:

ChoosePropertyFromItem:
  LastLine='2'

If you know in advance all of the properties that are candidates for being the single property listed in the file there is a way to do this with item filtering.
Caveats:

1) Specify only the PropertyName in the file, not $(PropertyName), unless you want to parse the string using a property function.

2) There can only be a single line in the file with the approach below, for multiple lines you'll need an extra level of batching.

<ItemGroup>
  <PropertyFile Include="property.txt" />
</ItemGroup>

<PropertyGroup>
  <SomeProperty>1</SomeProperty>
  <SomeOtherProperty>2</SomeOtherProperty>
</PropertyGroup>
<ItemGroup>
  <ChooseProperty Include="SomeProperty">
    <Value>$(SomeProperty)</Value>
  </ChooseProperty>
  <ChooseProperty Include="SomeOtherProperty">
    <Value>$(SomeOtherProperty)</Value>
  </ChooseProperty>
</ItemGroup>

The constructs above establish two candidate properties, $(SomeProperty) and $(SomeOtherProperty). For this example the contents of property.txt was a single line...

SomeProperty

...which correlates to $(SomeProperty) which has a value of 1

<Target Name="ReadItems">
  <ReadLinesFromFile File="@(PropertyFile)">
    <Output
      TaskParameter="Lines"
      ItemName="ItemsFromFile"
      />
  </ReadLinesFromFile>
</Target>

<Target Name="FilterItems"
  Outputs="%(ChooseProperty.Identity)">
  <PropertyGroup>
    <_ThisProperty>%(ChooseProperty.Identity)</_ThisProperty>
    <_ThisValue>%(ChooseProperty.Value)</_ThisValue>
    <_ItemFromFile>%(ItemsFromFile.Identity)</_ItemFromFile>
  </PropertyGroup>
  <ItemGroup Condition="'$(_ItemFromFile)' == '$(_ThisProperty)'">
    <_FilteredItems Include="$(_ThisProperty)">
      <Value>$(_ThisValue)</Value>
    </_FilteredItems>
  </ItemGroup>
</Target>

Above are the two dependent targets for the main target below. They use a dependent target to read the file so that it is published into an item group to be used with target batching on the @(ChooseProperty) item group. The key thing to note is the condition on the creation of the @(_FilteredItems) item group, which will contain a single member, due to this...

Condition="'$(_ItemFromFile)' == '$(_ThisProperty)'

...notice that the batched item meta data is transferred to these temporary properties in order to make the condition work (and this is why the file can only contain a single line, so that there is only a single item).

<Target Name="ChoosePropertyFromItem"
  DependsOnTargets="ReadItems;FilterItems">
  <!-- Standard task for file reading -->
  <PropertyGroup>
    <LastLine>%(_FilteredItems.Value)</LastLine>
  </PropertyGroup>
  <Message Text="LastLine='$(LastLine)'" />
</Target>

...finally the $(LastLine) Property is pulled from the single item in @(_FilteredItems). The resulting output is below:

ChoosePropertyFromItem:
  LastLine='1'

Changing the property.txt to contain 'SomeOtherProperty' results in this:

ChoosePropertyFromItem:
  LastLine='2'
若沐 2024-11-02 03:28:31

您不能以这种方式创建新属性。您无法动态创建属性名称。您可以修改构建过程。例子

 msbuild.exe yourproject.sln /p:UseSomeProp=true

You can't create new properties in such a way. You can't create property names dynamically. You can modify build process. Example

 msbuild.exe yourproject.sln /p:UseSomeProp=true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文