Вопрос

I am compiling a DLL using Visual Studio 2013 and for technical reasons cannot use System.Version since it is not supported in one of my target runtime environments.

Is there a way to automatically place the assembly "FileVersion" into a constant at compile-time?

public const string MyFileVersion = "{{ PLEASE REPLACE ME }}";

I am not using a build server or anything fancy like that, just Visual Studio 2013.

Это было полезно?

Решение

Seems like t4 templates could help you. It's template generation engine. There are enough info about it which will be helpful.

I made a simple template which does what you want. It's just an example and probably you need to make some adjustments in the code:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="Microsoft.CSharp.dll" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="System.Reflection" #>
<#@ output extension=".cs" #>

<#
        var assemblyVersionRegex = new Regex(@"AssemblyVersion\(\""(?<version>(.*?))\""\)");
        var pathBase = new FileInfo(Host.TemplateFile).DirectoryName;
        var assemblyInfoPath = Path.Combine(pathBase, @"Properties\AssemblyInfo.cs");

        var assemblyInfoSources = File.ReadAllText(assemblyInfoPath);
        var match = assemblyVersionRegex.Match(assemblyInfoSources);
        var version = match.Success 
                ? match.Groups["version"].Value
                : "unknow";
#>

public static class SomeAssemblyInfo
{
    public const string Version = "<#= version #>";
}

So, it reads file AssemblyInfo.cs (or another if you want) get version from it and prepare class SomeAssemblyInfo :

public static class SomeAssemblyInfo
{
    public const string Version = "1.0.0.0";
}

Last thing what you need to do is run this template on build. Here you can find how to do it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top