अपडेट करें:
जब से मैंने मूल रूप से इस प्रश्न का उत्तर दिया है तब से चीजें विकसित हुई हैं। Microsoft.NET.Sdk(जिसका अर्थ है कि आप एक एसडीके शैली परियोजना का उपयोग करना चाहिए) अब जोड़ने के लिए समर्थन शामिल अच्छी तरह से nuget पैकेज मेटाडाटा के रूप में के रूप में विधानसभा सूचना के संस्करण दोनों को हैश प्रतिबद्ध है, अगर कुछ शर्तें पूरी की जाएं:
<SourceRevisionId>संपत्ति परिभाषित किया जाना चाहिए। यह इस तरह एक लक्ष्य को जोड़कर किया जा सकता है:
<Target Name="InitializeSourceControlInformation" BeforeTargets="AddSourceRevisionToInformationalVersion">
<Exec
Command="git describe --long --always --dirty --exclude=* --abbrev=8"
ConsoleToMSBuild="True"
IgnoreExitCode="False"
>
<Output PropertyName="SourceRevisionId" TaskParameter="ConsoleOutput"/>
</Exec>
</Target>
यह लक्ष्य एक कमांड निष्पादित करता SourceRevisionIdहै जो संक्षिप्त (8 वर्ण) हैश पर सेट होगा । इससे पहले कि विधानसभा सूचना संस्करण बनाया जाता है इससे पहले कि यह कारण चलाया जा सकता है।
हैग को नगेट पैकेज मेटाडेटा में शामिल करने के लिए <RepositoryUrl>भी परिभाषित किया जाना चाहिए।
<SourceControlInformationFeatureSupported>प्रॉपर्टी होनी चाहिए true, इससे नूगट पैक टास्क के लिए SourceRevisionId भी लिया जा सकता है।
मैं MSBuildGitHash पैकेज का उपयोग करने से लोगों को दूर कर दूंगा, क्योंकि यह नई तकनीक क्लीनर और सबसे सुसंगत है।
मूल:
मैंने एक साधारण नगेट पैकेज बनाया है जिसे आप अपनी परियोजना में शामिल कर सकते हैं जो आपके लिए इसका ध्यान रखेगा: https://www.nuget.org/packages/MSBuildGitHash/
यह नगेट पैकेज एक "शुद्ध" MSBuild समाधान लागू करता है। यदि आप एक नगेट पैकेज पर निर्भर नहीं हैं, तो आप बस इन टारगेट्स को अपनी csproj फाइल में कॉपी कर सकते हैं और इसमें git हैश को कस्टम असेंबली विशेषता के रूप में शामिल करना चाहिए:
<Target Name="GetGitHash" BeforeTargets="WriteGitHash" Condition="'$(BuildHash)' == ''">
<PropertyGroup>
<!-- temp file for the git version (lives in "obj" folder)-->
<VerFile>$(IntermediateOutputPath)gitver</VerFile>
</PropertyGroup>
<!-- write the hash to the temp file.-->
<Exec Command="git -C $(ProjectDir) describe --long --always --dirty > $(VerFile)" />
<!-- read the version into the GitVersion itemGroup-->
<ReadLinesFromFile File="$(VerFile)">
<Output TaskParameter="Lines" ItemName="GitVersion" />
</ReadLinesFromFile>
<!-- Set the BuildHash property to contain the GitVersion, if it wasn't already set.-->
<PropertyGroup>
<BuildHash>@(GitVersion)</BuildHash>
</PropertyGroup>
</Target>
<Target Name="WriteGitHash" BeforeTargets="CoreCompile">
<!-- names the obj/.../CustomAssemblyInfo.cs file -->
<PropertyGroup>
<CustomAssemblyInfoFile>$(IntermediateOutputPath)CustomAssemblyInfo.cs</CustomAssemblyInfoFile>
</PropertyGroup>
<!-- includes the CustomAssemblyInfo for compilation into your project -->
<ItemGroup>
<Compile Include="$(CustomAssemblyInfoFile)" />
</ItemGroup>
<!-- defines the AssemblyMetadata attribute that will be written -->
<ItemGroup>
<AssemblyAttributes Include="AssemblyMetadata">
<_Parameter1>GitHash</_Parameter1>
<_Parameter2>$(BuildHash)</_Parameter2>
</AssemblyAttributes>
</ItemGroup>
<!-- writes the attribute to the customAssemblyInfo file -->
<WriteCodeFragment Language="C#" OutputFile="$(CustomAssemblyInfoFile)" AssemblyAttributes="@(AssemblyAttributes)" />
</Target>
यहां दो लक्ष्य हैं। पहले एक, "गेटगिटहैश", बिल्ड हैश में एक MSBuild संपत्ति में git हैश को लोड करता है , यह केवल तभी करता है जब BuildHash पहले से परिभाषित नहीं है। यदि आप चाहें तो यह आपको MSBuild को कमांड लाइन पर पारित करने की अनुमति देता है। आप इसे MSBuild की तरह पास कर सकते हैं:
MSBuild.exe myproj.csproj /p:BuildHash=MYHASHVAL
दूसरा लक्ष्य, "WriteGitHash", "CustomAssemblyInfo.cs" नाम के अस्थायी "obj" फ़ोल्डर में एक फ़ाइल के लिए हैश मान लिखेगा। इस फ़ाइल में एक पंक्ति होगी जो इस तरह दिखाई देती है:
[assembly: AssemblyMetadata("GitHash", "MYHASHVAL")]
इस CustomAssemblyInfo.cs फ़ाइल को आपकी असेंबली में संकलित किया जाएगा, ताकि आप AssemblyMetadataरनटाइम को देखने के लिए प्रतिबिंब का उपयोग कर सकें । निम्न कोड दिखाता है कि जब AssemblyInfoकक्षा को एक ही विधानसभा में शामिल किया जाता है तो यह कैसे किया जा सकता है ।
using System.Linq;
using System.Reflection;
public static class AssemblyInfo
{
/// <summary> Gets the git hash value from the assembly
/// or null if it cannot be found. </summary>
public static string GetGitHash()
{
var asm = typeof(AssemblyInfo).Assembly;
var attrs = asm.GetCustomAttributes<AssemblyMetadataAttribute>();
return attrs.FirstOrDefault(a => a.Key == "GitHash")?.Value;
}
}
इस डिजाइन के कुछ लाभ यह है कि यह आपके प्रोजेक्ट फ़ोल्डर में किसी भी फाइल को नहीं छूता है, सभी उत्परिवर्तित फाइलें "obj" फ़ोल्डर के अंतर्गत हैं। आपका प्रोजेक्ट विजुअल स्टूडियो के भीतर या कमांड लाइन से भी पहचान बनाएगा। यह आपकी परियोजना के लिए भी आसानी से अनुकूलित किया जा सकता है, और आपके csproj फ़ाइल के साथ स्रोत को नियंत्रित करेगा।