Skip to content

Instantly share code, notes, and snippets.

@shvydky
Last active December 13, 2023 05:28
Show Gist options
  • Select an option

  • Save shvydky/dceb83d34c942a3a95e336250c708334 to your computer and use it in GitHub Desktop.

Select an option

Save shvydky/dceb83d34c942a3a95e336250c708334 to your computer and use it in GitHub Desktop.

Revisions

  1. shvydky renamed this gist Dec 7, 2016. 1 changed file with 0 additions and 0 deletions.
  2. shvydky revised this gist Dec 7, 2016. 2 changed files with 65 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions "src\Common\CommonAssemblyInfo.cs.template
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

    #if DEBUG
    [assembly: AssemblyConfiguration("DEBUG")]
    #else
    [assembly: AssemblyConfiguration("RELEASE")]
    #endif
    [assembly: AssemblyCompany("BREEZE Software, Ltd")]
    [assembly: AssemblyProduct("Product Name")]
    [assembly: AssemblyCopyright("Copyright @ 2016 Breeze Software")]
    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyVersion("[VERSION]")]
    [assembly: AssemblyFileVersion("[VERSION]")]
    [assembly: AssemblyInformationalVersion("[REVISION]")]
    49 changes: 49 additions & 0 deletions main.proj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"
    DefaultTargets="Build">

    <Import Project="global.targets" />

    <PropertyGroup>
    <BuildRoot>$(MSBuildProjectDirectory)\build</BuildRoot>
    <Configuration>Debug</Configuration>
    <gitRevision>1</gitRevision>
    <gitRevisionHash>Unknown commit</gitRevisionHash>
    </PropertyGroup>

    <ItemGroup>
    <BuildProject Include="src\Solution.sln" />
    </ItemGroup>

    <Target Name="Build" DependsOnTargets="Version">
    <MsBuild
    Projects="@(BuildProject)"
    Targets="Build"
    Properties=""
    StopOnFirstFailure="true"
    />
    </Target>

    <Target Name="Version">
    <!--
    If .git folder is found than CommonAssemblyInfo.cs.part will be regenerated with new hash value.
    -->
    <ExtractRevisionHash WorkCopy="$(MSBuildProjectDirectory)" Condition="Exists('.git')">
    <Output TaskParameter="RevisionHash" PropertyName="gitRevisionHash"/>
    </ExtractRevisionHash>
    <ExtractGITRevision WorkCopy="$(MSBuildProjectDirectory)" Condition="Exists('.git')">
    <Output TaskParameter="Revision" PropertyName="gitRevision"/>
    </ExtractGITRevision>

    <ReplaceText
    InputFile="src\Common\CommonAssemblyInfo.cs.template"
    OutputFile="src\Common\CommonAssemblyInfo.cs.part"
    FindFor="\[REVISION\]"
    ReplaceBy="$(gitRevisionHash)" Condition="Exists('.git')"/>
    <ReplaceText
    InputFile="src\Common\CommonAssemblyInfo.cs.part"
    OutputFile="src\Common\CommonAssemblyInfo.cs"
    FindFor="\[VERSION\]"
    ReplaceBy="$(productVersion).$(gitRevision)" />
    </Target>

    </Project>
  3. shvydky created this gist Dec 7, 2016.
    177 changes: 177 additions & 0 deletions global.targets
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,177 @@
    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
    DefaultTargets="Settings">

    <PropertyGroup>
    <productVersion>0.1.1</productVersion>
    </PropertyGroup>

    <UsingTask
    TaskName="ExtractRevisionHash"
    TaskFactory="CodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >

    <ParameterGroup>
    <WorkCopy ParameterType="System.String" Required="true" />
    <RevisionHash ParameterType="System.String" Output="true" />
    </ParameterGroup>

    <Task>
    <Using Namespace="System" />
    <Using Namespace="System.Diagnostics" />
    <Using Namespace="System.IO" />
    <Code Type="Fragment" Language="cs">
    <![CDATA[
    try {
    ProcessStartInfo psi = new ProcessStartInfo("git", "rev-parse HEAD");
    psi.WorkingDirectory = WorkCopy;
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardError = true;
    psi.UseShellExecute = false;
    Process p = Process.Start(psi);
    string line;
    while ((line = p.StandardOutput.ReadLine()) != null) {
    RevisionHash = line;
    Log.LogMessage("Last Git Revision Hash: {0}", RevisionHash);
    }
    p.WaitForExit();
    if (p.ExitCode != 0)
    Log.LogError(p.StandardError.ReadLine());
    return p.ExitCode == 0;
    } catch (Exception ex) {
    Log.LogError(ex.Message);
    return false;
    }
    ]]>
    </Code>
    </Task>
    </UsingTask>

    <UsingTask
    TaskName="ExtractGITRevision"
    TaskFactory="CodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >

    <ParameterGroup>
    <WorkCopy ParameterType="System.String" Required="true" />
    <Revision ParameterType="System.String" Output="true" />
    </ParameterGroup>

    <Task>
    <Using Namespace="System" />
    <Using Namespace="System.Diagnostics" />
    <Using Namespace="System.IO" />
    <Code Type="Fragment" Language="cs">
    <![CDATA[
    try {
    ProcessStartInfo psi = new ProcessStartInfo("git", "rev-list HEAD --count");
    psi.WorkingDirectory = WorkCopy;
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardError = true;
    psi.UseShellExecute = false;
    Process p = Process.Start(psi);
    string line;
    while ((line = p.StandardOutput.ReadLine()) != null) {
    Revision = line;
    Log.LogMessage("Last Git Revision: {0}", Revision);
    }
    p.WaitForExit();
    if (p.ExitCode != 0)
    Log.LogError(p.StandardError.ReadLine());
    return p.ExitCode == 0;
    } catch (Exception ex) {
    Log.LogError(ex.Message);
    return false;
    }
    ]]>
    </Code>
    </Task>
    </UsingTask>

    <UsingTask
    TaskName="ExtractSVNRevision"
    TaskFactory="CodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >

    <ParameterGroup>
    <WorkCopy ParameterType="System.String" Required="true" />
    <Revision ParameterType="System.String" Output="true" />
    </ParameterGroup>

    <Task>
    <Using Namespace="System" />
    <Using Namespace="System.Diagnostics" />
    <Using Namespace="System.IO" />
    <Code Type="Fragment" Language="cs">
    <![CDATA[
    try {
    ProcessStartInfo psi = new ProcessStartInfo("svn", "info");
    psi.WorkingDirectory = WorkCopy;
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardError = true;
    psi.UseShellExecute = false;
    Process p = Process.Start(psi);
    string line;
    while ((line = p.StandardOutput.ReadLine()) != null) {
    if (line.ToLower().StartsWith("url:"))
    Log.LogMessage("Svn Url: {0}", line.Substring(5));
    if (line.ToLower().StartsWith("last changed rev:")) {
    Revision = line.Substring(18);
    Log.LogMessage("Last Revision: {0}", Revision);
    }
    }
    p.WaitForExit();
    if (p.ExitCode != 0)
    Log.LogError(p.StandardError.ReadLine());
    return p.ExitCode == 0;
    } catch (Exception ex) {
    Log.LogError(ex.Message);
    return false;
    }
    ]]>
    </Code>
    </Task>
    </UsingTask>

    <UsingTask
    TaskName="ReplaceText"
    TaskFactory="CodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >

    <ParameterGroup>
    <InputFile ParameterType="System.String" Required="true" />
    <OutputFile ParameterType="System.String" Required="true" />
    <FindFor ParameterType="System.String" Required="true" />
    <ReplaceBy ParameterType="System.String" Required="true" />
    <Revision ParameterType="System.String" Output="true" />
    </ParameterGroup>

    <Task>
    <Using Namespace="System" />
    <Using Namespace="System.Diagnostics" />
    <Using Namespace="System.IO" />
    <Using Namespace="System.Text.RegularExpressions" />
    <Code Type="Fragment" Language="cs">
    <![CDATA[
    try {
    StringBuilder sb = new StringBuilder();
    // in-memory replace
    Regex rx = new Regex(FindFor);
    string text = File.ReadAllText(InputFile);
    text = rx.Replace(text, ReplaceBy);
    if (File.Exists(OutputFile)) {
    string oldText = File.ReadAllText(OutputFile);
    if (oldText != text)
    File.WriteAllText(OutputFile, text, Encoding.UTF8);
    } else
    File.WriteAllText(OutputFile, text, Encoding.UTF8);
    return true;
    } catch (Exception ex) {
    Log.LogErrorFromException(ex);
    return false;
    }
    ]]>
    </Code>
    </Task>
    </UsingTask>

    </Project>