-
Notifications
You must be signed in to change notification settings - Fork 20
/
SignatureGen.tt
128 lines (115 loc) · 4.29 KB
/
SignatureGen.tt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<#@ template language="C#" hostspecific="true" debug="false" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.ComponentModel" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ output extension=".cs" #>
using System;
using System.Reflection;
[assembly: AssemblyVersion("<#= GenerateVersion() #>")]
namespace RoliSoft.TVShowTracker
{
/// <summary>
/// Contains informations about the assembly.
/// </summary>
public static partial class Signature
{
/// <summary>
/// Gets the year when the executing assembly was compiled.
/// </summary>
/// <value>The compilation year.</value>
public const string CompileYear = "<#= DateTime.Now.Year #>";
/// <summary>
/// Gets the date and time when the executing assembly was compiled.
/// </summary>
/// <value>The compile time.</value>
public static DateTime CompileTime
{
get { return DateTime.FromBinary(<#= DateTime.Now.ToBinary() #>); }
}
/// <summary>
/// Gets the git commit hash.
/// </summary>
/// <value>The git commit hash.</value>
public static string GitRevision
{
get { return "<#= GetRevision() #>"; }
}
/// <summary>
/// Gets the directory in which the project was built.
/// </summary>
/// <value>The directory in which the project was built.</value>
public static string BuildDirectory
{
get { return @"<#= Path.GetDirectoryName(Host.TemplateFile) #>"; }
}
/// <summary>
/// Gets the name of the machine where the project was built.
/// </summary>
/// <value>The name of the machine where the project was built.</value>
public static string BuildMachine
{
get { return @"<#= Environment.MachineName #>"; }
}
}
}
<#+
public string Run(string process, string arguments = null)
{
var sb = new StringBuilder();
var p = new Process
{
EnableRaisingEvents = true,
StartInfo =
{
FileName = process,
Arguments = arguments ?? string.Empty,
WorkingDirectory = Path.GetDirectoryName(Host.TemplateFile),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
p.OutputDataReceived += (s, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
sb.AppendLine(e.Data);
}
};
p.ErrorDataReceived += (s, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
sb.AppendLine(e.Data);
}
};
try
{
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit();
}
catch (Win32Exception) { }
return sb.ToString();
}
public string GetRevision()
{
return Run("git", "rev-parse HEAD").Trim();
}
public string GenerateVersion()
{
var prefix = "2.2.0.";
var count = Regex.Matches(Run("git", "shortlog -s"), @"(\d+)").Cast<Match>().Select(c => c.Groups[1].Value).Select(int.Parse).Sum();
File.WriteAllText(Path.Combine(Path.GetDirectoryName(Host.TemplateFile), "version.txt"), prefix + count.ToString());
File.WriteAllText(Path.Combine(Path.GetDirectoryName(Host.TemplateFile), "version.nsh"), "!define VERSION \"" + prefix + count.ToString() + "\"");
File.WriteAllText(Path.Combine(Path.GetDirectoryName(Host.TemplateFile), "version.iss"), "[Setup]" + Environment.NewLine + "AppVersion=" + prefix + count.ToString() + Environment.NewLine + "VersionInfoVersion=" + prefix + count.ToString());
return prefix + count;
}
#>