2012-04-17 37 views
0
<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> 

這是一塊目標文件。如何簡化此代碼?如何減少以下字符串的重複?如何簡化MSBuild目標?

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

回答

1

既然你已經指定等,以使值的屬性:

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

你可以只與物業替換該字符串引用$(protooutdir)像這樣:

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

and

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

謝謝。我認爲$(protooutdir)可以在的範圍內訪問。所有屬性和項目是否存在於全局範圍內? – dizel3d 2012-04-18 12:06:52

+0

如何修復屬性「protooutdir」和屬性「Outputs」之間的重複? – dizel3d 2012-04-18 12:11:48

+0

我認爲如果您在其他目標之前調用ProtobufCompile目標,那麼其他目標將能夠訪問它。但是,您始終可以在目標外定義protooutdir屬性以使其成爲全局目標。 – BryanJ 2012-04-18 14:59:56