3

Constraints are: Using Visual Studio 2017. Needs to ultimately be called from a powershell script calling MSBuild.

Not sure its relevant, but needs to be able to build the following:

  • asp.net 461
  • asp.net-core 1.1 and 2.0 assemblies

Unsuccessful attempts so far:

Example attempt of "Code Generation in a Build Process" That works on build from VS but not MSBuild:

placed in root of project - handleVersioning.tt:

<#@ template language="C#" #>

using System.Reflection;

[assembly: AssemblyVersion("1.0.0.*")]
[assembly: AssemblyFileVersion("<#= this.Year #>.<#= this.Month #>.<#= this.Day #>.<#= this.Minute #>")]
<#+
    int Year = DateTime.UtcNow.Year;
    int Month = DateTime.UtcNow.Month;
    int Day = DateTime.UtcNow.Day;
    int Minute = unchecked((int)DateTime.UtcNow.TimeOfDay.TotalMinutes);
#>

.csproj:

<Import Project="...hardcoded...\Microsoft.CSharp.targets" />
<!-- This is the important line: -->  
<Import Project="...hardcoded...\TextTemplating\Microsoft.TextTemplating.targets" />

<PropertyGroup>  
    <TransformOnBuild>true</TransformOnBuild>  
    <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup> 

Called like so: msbuild myProject.csproj /t:TransformAll

ttugates
  • 5,818
  • 3
  • 44
  • 54
  • Did you tag "asp.net-core" on purpose? if you are using asp.net core, you already have a project system that can generate assembly info files based on msbuild values that can be passed on commandline or calculated from date and time in the project file. – Martin Ullrich Nov 21 '17 at 21:51
  • It needs to be able to build an asp.net 461 assembly as well as asp.net-core 1.1 and 2.0. I previously edited out the asp.net-core from the title, will remove tag as well. – ttugates Nov 21 '17 at 21:54
  • So your asp.net core would project already support that - `msbuild /p:Version=1.2.3.4` will set the assembly version, I recommend creating "polyfill targets" for classic projects based on the [existing source code](https://github.com/dotnet/sdk) for sdk-based projects. Don't have the time to do it now but maybe during the next days. would then behave like https://stackoverflow.com/questions/43274254 – Martin Ullrich Nov 21 '17 at 22:14
  • [@Martin Ullrich](https://stackoverflow.com/users/784387/martin-ullrich), thank you. I am already using powershell to call `msbuild` so `msbuild /p:Version=1.2.3.4` works perfect. It solves my *actual* problem, but had to disable the auto increment solution with t4 templates to work. Still would be great to know a way that works no matter where you build from.. All the same thanks! – ttugates Nov 22 '17 at 13:11
  • which MSBuild you are using? located in С:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin ? – Zam Apr 10 '18 at 14:10
  • @Zam - Ultimately I solved using T4 templates. That said any version of MSBuild that comes with VS2017 Community Edition. I believe it was `C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe` - I'll provide my solution as an answer later today. It was different for `.Net 461` vs `.Net Core` and I have now migrated to only `.Net Core` – ttugates Apr 10 '18 at 14:17
  • when i ran `msbuild` with this parameters ` /p:Version=1.2.3.4` version still stay the same. still looking what i did wrong. T4 -- yes, works good, but each time i need to re-save this file. – Zam Apr 10 '18 at 19:32
  • @Zam - Try this: Remove all T4 Templates, remove any edits to .csproj. Open console where .csproj resides and run `dotnet msbuild /p:Version=1.2.3.4`. That should work. – ttugates Apr 10 '18 at 23:23

1 Answers1

0

U can write simple console application and in Project Properties > Build Events, add a "Pre-build event command line" like this: "D:\SomePath\MyAssemblyInfoPatcher.exe" "$(ProjectDir)Properties\AssemblyInfo.cs"

sample application code (works on VS 2022)

using System;
using System.IO;
using System.Linq;

namespace MyAssemblyInfoPatcher
{
    internal class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                string path = args[0].ToString();
                Console.WriteLine(string.Format("Current App version is set to: {0}", path));
                string now_date = DateTime.Now.ToString("yyyy.MM.dd.HHmm");
                if (File.Exists(path))
                {
                    string _AssemblyVersion = string.Empty;
                    string _AssemblyFileVersion = string.Empty;

                    var lines = File.ReadLines(string.Format(path));
                    for (int i = 0; i < lines.Count(); i++)
                    {
                        if (lines.ElementAt(i).ToString().StartsWith("[assembly: AssemblyVersion"))
                        {
                            _AssemblyVersion = lines.ElementAt(i).ToString();
                        }
                        else if (lines.ElementAt(i).ToString().StartsWith("[assembly: AssemblyFileVersion"))
                        {
                            _AssemblyFileVersion = lines.ElementAt(i).ToString();
                        }
                    }

                    string _replace_assembly = File.ReadAllText(path);

                    if (_AssemblyVersion != string.Empty)
                    {
                        _replace_assembly = _replace_assembly.Replace(_AssemblyVersion, string.Format("[assembly: AssemblyVersion(\"{0}\")]", now_date));
                    }
                    if (_AssemblyFileVersion != string.Empty)
                    {
                        _replace_assembly = _replace_assembly.Replace(_AssemblyFileVersion, string.Format("[assembly: AssemblyFileVersion(\"{0}\")]", now_date));
                    }

                    File.WriteAllText(path, _replace_assembly);
                }
            }   
        }
    }
}