2010-04-29 35 views
13

Tôi đang làm việc trên một Mục tiêu MSBuild có thể sử dụng lại sẽ được tiêu thụ bởi một số tác vụ khác. Mục tiêu này yêu cầu một số thuộc tính được định nghĩa. Cách tốt nhất để xác nhận rằng các thuộc tính được xác định là gì, ném một lỗi nếu không?Thuộc tính xác thực MSBuild

Hai nỗ lực mà tôi gần như:

<?xml version="1.0" encoding="utf-8" ?> 
    <Project ToolsVersion="3.5" DefaultTarget="Release" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 

    <Target Name="Release"> 
    <Error 
     Text="Property PropA required" 
     Condition="'$(PropA)' == ''"/> 
    <Error 
     Text="Property PropB required" 
     Condition="'$(PropB)' == ''"/> 

    <!-- The body of the task --> 

    </Target> 
</Project> 

Dưới đây là một nỗ lực tại trạm trộn. Rất xấu vì thông số "Tên" bổ sung. Có thể sử dụng thuộc tính Bao gồm thay thế không?

<?xml version="1.0" encoding="utf-8" ?> 
<Project ToolsVersion="3.5" DefaultTarget="Release" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <Target Name="Release"> 
    <!-- MSBuild BuildInParallel="true" Projects="@(ProjectsToBuild)"/ --> 
    <ItemGroup> 
     <RequiredProperty Include="PropA"><Name>PropA</Name></RequiredProperty> 
     <RequiredProperty Include="PropB"><Name>PropB</Name></RequiredProperty> 
     <RequiredProperty Include="PropC"><Name>PropC</Name></RequiredProperty> 
    </ItemGroup> 

    <Error 
     Text="Property %(RequiredProperty.Name) required" 
     Condition="'$(%(RequiredProperty.Name))' == ''" /> 

    </Target> 

</Project> 

Trả lời

16

Câu hỏi hay! Tôi đã viết về điều này theo chiều sâu trong số book và trong một bài đăng trên blog, Elements of Reusable MSBuild Scripts: Validation. Cách tiếp cận của tôi sẽ bao gồm các đặc tính và vật phẩm.

Đây là chương trình chạy xuống. Trong tệp .targets được chia sẻ tạo mục tiêu xác thực và đây là một trong những mục tiêu đầu tiên được khai báo trong tệp để người dùng có thể dễ dàng định vị nó.

Thuộc tính

Bên trong mục tiêu xác định tài sản của bạn như thế này:

<_RequiredProperties Include="Root"> 
    <Value>$(Root)</Value> 
</_RequiredProperties> 

tôi đặt tên của tài sản trong bao gồm và giá trị của nó bên trong metadata.The lý do tại sao Value Tôi làm điều này là để tôi có thể phát hiện khi Value trống và sau đó tôi sử dụng giá trị bao gồm để báo cáo tên của thuộc tính bị thiếu cho người dùng.

Items

Bên trong nơi mục tiêu các mục cần thiết bên trong một mục như:

<_RequiredItems Include="AllConfigurations"> 
    <RequiredValue>@(AllConfigurations)</RequiredValue> 
</_RequiredItems> 

Tương tự như các thuộc tính, bên trong bao gồm bạn đặt tên của mặt hàng đó và sau đó giá trị để kiểm tra bên trong của RequiredValue siêu dữ liệu. Trong ví dụ này, nó chỉ kiểm tra để đảm bảo mục AllConfiguraitons không rỗng. Nếu bạn muốn chắc chắn rằng một giá trị siêu dữ liệu cụ thể được quy định trên tất cả các mặt hàng sau đó làm điều gì đó như:

<_RequiredItems Include = "AllConfigurations.Configuration"> 
    <RequiredValue>%(AllConfigurations.Configuration </RequiredValue> 
</_RequiredItems> 

Nếu bạn muốn chắc chắn rằng một tập tin tồn tại sau đó thêm các siêu dữ liệu bổ sung, RequiredFilePath.

<_RequiredItems Include ="ProjectsToBuild"> 
    <RequiredValue>%(ProjectsToBuild.Identity)</RequiredValue> 
    <RequiredFilePath>%(ProjectsToBuild.Identity)</RequiredFilePath> 
</_RequiredItems> 

Validation

Dưới đây là những gì bạn cần phải thực hiện xác nhận

Hoàn dụ

Dưới đây là toàn bộ ví dụ

<Target Name="ValidateBuildSettings"> 
    <ItemGroup> 
    <_RequiredProperties Include="Root"> 
     <Value>$(Root)</Value> 
    </_RequiredProperties> 

    <_RequiredProperties Include="BuildInstallRoot"> 
     <Value>$(BuildInstallRoot)</Value> 
    </_RequiredProperties> 

    <_RequiredProperties Include="SourceRoot"> 
     <Value>$(SourceRoot)</Value> 
    </_RequiredProperties> 
    <!-- 
    _RequiredItems is the item where required items should be placed. 
    The following metadata is significant: 
     REQUIRED METADATA: 
     Identity   = This will basically be used to identify the specific required item 
     RequiredValue  = This is the specific value that will be validated to exist 

     OPTIONAL METADATA 
     RequiredFilePath = Populate this with a path that should exists, if it is not empty 
          then it will be checked to exist on disk. 
    --> 

    <_RequiredItems Include="AllConfigurations"> 
     <RequiredValue>@(AllConfigurations)</RequiredValue> 
    </_RequiredItems> 
    <_RequiredItems Include = "AllConfigurations.Configuration"> 
     <RequiredValue>%(AllConfigurations.Configuration </RequiredValue> 
    </_RequiredItems> 
    <_RequiredItems Include ="ProjectsToBuild"> 
     <RequiredValue>%(ProjectsToBuild.Identity)</RequiredValue> 
     <RequiredFilePath>%(ProjectsToBuild.Identity)</RequiredFilePath> 
    </_RequiredItems> 
    </ItemGroup> 
    <!-- Raise an error if any value in _RequiredProperties is missing --> 

    <Error Condition =" '%(_RequiredProperties.Value)'=='' " 
      Text=" Missing required property [%(_RequiredProperties.Identity)]" /> 

    <!-- Raise an error if any value in _RequiredItems is empty --> 
    <Error Condition = " '%(_RequiredItems.RequiredValue)'=='' " 
      Text = " Missing required item value [%(_RequiredItems.Identity)] " /> 

    <!-- Validate any file/directory that should exist --> 
    <Error Condition = " '%(_RequiredItems.RequiredFilePath)' != '' and !Exists('%(_RequiredItems.RequiredFilePath)') " 
      Text = " Unable to find expeceted path [%(_RequiredItems.RequiredFilePath)] on item [%(_RequiredItems.Identity)] " /> 
</Target> 
+0

Tuyệt vời - đây chỉ là những gì tôi đang tìm kiếm. Có vẻ như tôi cũng sẽ cần phải nhận một bản sao của cuốn sách của bạn :) –

Các vấn đề liên quan