如何简化MSBuild-targets?

时间:2012-04-17 12:31:25

标签: msbuild

<Target Name="ProtobufCompile"
        Inputs="@(ProtocCompile)"
        Outputs="$(IntermediateOutputPath)$([System.Text.RegularExpressions.Regex]::Replace('%(ProtocCompile.RelativeDir)','\.\.[/\\]',''))%(ProtocCompile.Filename).cs">
  <PropertyGroup>
    <protooutdir>$(IntermediateOutputPath)$([System.Text.RegularExpressions.Regex]::Replace('%(ProtocCompile.RelativeDir)','\.\.[/\\]',''))</protooutdir>
  </PropertyGroup>
  <Message Text="%(ProtocCompile.Filename)%(ProtocCompile.Extension)" Importance="high" />
  <MakeDir Directories="$(protooutdir)" />
  <Exec Command="$(ProtobufCompiler) --protoc_dir=${PROTOBUF_PROTOC_EXECUTABLE}/.. --proto_path=%(ProtocCompile.RootDir)%(ProtocCompile.Directory) -output_directory=$(protooutdir) %(ProtocCompile.FullPath)" />
</Target>
<!-- set Intputs and Outputs -->
<Target Name="ProtobufCSharpCompile"
        DependsOnTargets="ProtobufCompile">
  <CreateItem Include="$(IntermediateOutputPath)$([System.Text.RegularExpressions.Regex]::Replace('%(ProtocCompile.RelativeDir)','\.\.[/\\]',''))%(ProtocCompile.Filename).cs">
    <Output TaskParameter="Include" ItemName="Compile"/>
  </CreateItem>
</Target>
<Target Name="ProtobufClean"
        BeforeTargets="Clean">
  <Delete Files="$(IntermediateOutputPath)$([System.Text.RegularExpressions.Regex]::Replace('%(ProtocCompile.RelativeDir)','\.\.[/\\]',''))%(ProtocCompile.Filename).cs" />
</Target>

这是目标文件。如何简化此代码?如何减少下面的字符串重复?

$(IntermediateOutputPath)$([System.Text.RegularExpressions.Regex]::Replace('%(ProtocCompile.RelativeDir)','\.\.[/\\]',''))

1 个答案:

答案 0 :(得分:1)

因为你已经为这个值指定了一个属性,如下所示:

  <PropertyGroup>
  <protooutdir>$(IntermediateOutputPath)$([System.Text.RegularExpressions.Regex]::Replace('%(ProtocCompile.RelativeDir)','\.\.[/\\]',''))</protooutdir>
  </PropertyGroup>

您可以使用属性 $(protooutdir)替换对该字符串的引用,如下所示:

<CreateItem Include="$(protooutdir)%(ProtocCompile.Filename).cs">

<Delete Files="$(protooutdir)%(ProtocCompile.Filename).cs" />