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>
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 :) –