dizel3d
dizel3d

Reputation: 3669

How to simplify MSBuild-targets?

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

This is the piece of target-file. How to simplify this code? How to reduce duplicating of string below?

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

Upvotes: 0

Views: 508

Answers (1)

BryanJ
BryanJ

Reputation: 8563

Since you already specified a property for that value like so:

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

You can just replace references to that string with the property $(protooutdir) like so:

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

and

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

Upvotes: 1

Related Questions