महान प्रश्न! मैंने इसके बारे में book में और ब्लॉग पोस्ट में Elements of Reusable MSBuild Scripts: Validation में गहराई से लिखा है। मेरा दृष्टिकोण गुणों और वस्तुओं को कवर करेगा।
यहां रन डाउन है। साझा .targets फ़ाइल में एक सत्यापन लक्ष्य बनाते हैं, और यह फ़ाइल में घोषित पहले लक्ष्यों में से एक होना चाहिए ताकि उपयोगकर्ता आसानी से इसका पता लगा सकें।
गुण
सत्यापन लक्ष्य के अंदर इस तरह अपने गुण को परिभाषित:
<_RequiredProperties Include="Root">
<Value>$(Root)</Value>
</_RequiredProperties>
मैं में संपत्ति के नाम पर जगह शामिल हैं और Value
metadata.The कारण है कि के अंदर अपने मूल्य मैं ऐसा करता हूं ताकि मैं पता लगा सकूं कि Value
खाली है और फिर मैं अनुपलब्ध संपत्ति के नाम को उपयोगकर्ता को वापस रिपोर्ट करने के लिए शामिल मूल्य का उपयोग करता हूं।
आइटम
लक्ष्य जगह अंदर की तरह एक आइटम के अंदर आवश्यक वस्तुओं:
<_RequiredItems Include="AllConfigurations">
<RequiredValue>@(AllConfigurations)</RequiredValue>
</_RequiredItems>
गुण की तरह, अंदर आप आइटम का नाम और उसके बाद मूल्य जगह शामिल RequiredValue
मेटाडेटा के अंदर जांचने के लिए। इस उदाहरण में यह सुनिश्चित करने के लिए जांच करता है कि AllConfiguraitons
आइटम खाली नहीं है। आप यह सुनिश्चित करें कि एक फाइल तो अतिरिक्त मेटाडेटा, RequiredFilePath जोड़ने मौजूद है बनाना चाहते हैं
<_RequiredItems Include = "AllConfigurations.Configuration">
<RequiredValue>%(AllConfigurations.Configuration </RequiredValue>
</_RequiredItems>
: आप यह सुनिश्चित करें कि किसी दिए गए मेटाडाटा मूल्य सभी वस्तुओं पर निर्दिष्ट किया जाता है तो कुछ इस तरह करते हैं बनाना चाहते हैं।
<_RequiredItems Include ="ProjectsToBuild">
<RequiredValue>%(ProjectsToBuild.Identity)</RequiredValue>
<RequiredFilePath>%(ProjectsToBuild.Identity)</RequiredFilePath>
</_RequiredItems>
मान्यता
यहाँ तुम क्या मान्यता
करने की आवश्यकता है यह सिर्फ मैं क्या देख रहा था है - पूरा उदाहरण
यहाँ पूर्ण उदाहरण
<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>
बहुत बढ़िया है। ऐसा लगता है कि मुझे आपकी पुस्तक की एक प्रति भी लेने की आवश्यकता होगी :) –