Created
September 21, 2021 10:14
-
-
Save karenpayneoregon/afc14f14985269a0f5c1b49ece65017c to your computer and use it in GitHub Desktop.
Create class for build date using a T4 template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template debug="false" hostspecific="true" language="C#" #> | |
<#@ output extension=".cs" #> | |
<#@ Assembly Name="EnvDTE.dll" #> | |
<#@ Assembly Name="System.Data" #> | |
<#@ import namespace="EnvDTE" #> | |
<#@ import namespace="System.Data" #> | |
<#@ import namespace="System.Globalization" #> | |
<#@ import namespace="System.IO" #> | |
<#@ import namespace="System.Text.RegularExpressions" #> | |
<#@ output extension=".cs" #> | |
<# | |
var tableName = Path.GetFileNameWithoutExtension(Host.TemplateFile); | |
var path = Path.GetDirectoryName(Host.TemplateFile); | |
const string FMT_DATE_FORMAL = ""; | |
const string FMT_DATE_COMMENT = "MMMM d, yyyy h:mm:ss.ffffff tt"; | |
DateTime dtBuildDate = DateTime.UtcNow; | |
string strNamespace = "BogusStuff"; | |
string strFormatDisplay = String.IsNullOrEmpty(FMT_DATE_FORMAL) ? CultureInfo.InstalledUICulture.DateTimeFormat.FullDateTimePattern : FMT_DATE_FORMAL; | |
// Get containing project | |
IServiceProvider serviceProvider = (IServiceProvider)Host; | |
DTE dte = (DTE)serviceProvider.GetService(typeof(DTE)); | |
Project project = dte.Solution.FindProjectItem(Host.TemplateFile).ContainingProject; | |
#> | |
///////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// NOTICE: DO NOT EDIT THIS FILE! | |
// | |
// This file is autogenerated and your changes will be OVERWRITTEN! | |
// Edit the corresponding .tt file instead. | |
// | |
// Or, better yet, make a lasting contribution by submitting a Pull Request: | |
// https://github.com/dwcullop/BuildInfo | |
// | |
// This version was modified by Karen Payne | |
///////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
using System; | |
using System.Reflection; | |
namespace <#= project.Properties.Item("DefaultNamespace").Value #><#= Path.GetDirectoryName(Host.TemplateFile).Remove(0, Path.GetDirectoryName(project.FileName).Length).Replace("\\", ".") #> | |
{ | |
public static class BuildInfo | |
{ | |
private const long BUILD_DATE_BINARY_UTC = 0x<#=dtBuildDate.ToBinary().ToString("x16") #>; // <#=dtBuildDate.ToString(FMT_DATE_COMMENT) #> UTC | |
private static AssemblyName BuildAssemblyName { get; } = Assembly.GetExecutingAssembly().GetName(); | |
public static DateTimeOffset BuildDateUtc { get; } = DateTime.FromBinary(BUILD_DATE_BINARY_UTC); | |
public static string ModuleText { get; } = BuildAssemblyName.Name; | |
public static string VersionText { get; } = "v" + BuildAssemblyName.Version.ToString() | |
#if DEBUG | |
+ " [DEBUG]" | |
#endif | |
; | |
public static string BuildDateText { get; } = "<#=dtBuildDate.ToString(strFormatDisplay) #> UTC"; | |
public static string DisplayText { get; } = $"{ModuleText} {VersionText} (Build Date: {BuildDateText})"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Under properties for BuildInfo.tt set
Custom Tool
toTextTemplatingFileGenerator
, save the file and BuildInfo.cs is generated.Usage
Note
The original code at the original repository worked except it didn't pickup on the default namespace, this version does for .NET Core. So first try the original if using .NET traditional Framework e.g. .NET 4.8 and if it fails use this version.