-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildVersion.cs
More file actions
223 lines (197 loc) · 9.03 KB
/
Copy pathBuildVersion.cs
File metadata and controls
223 lines (197 loc) · 9.03 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Copyright (c) 2022 Robert Adams
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace org.herbal3d.buildVersion
{
class BuildVersion {
// Notes on the new net6 templating changes
// https://docs.microsoft.com/en-us/dotnet/core/tutorials/top-level-templates
// This project has disabled implicit usings and nullable contexts.
AppParams appParams;
BLogger log;
static void Main(string[] args) {
var parms = GetParams(args);
if (parms is not null) {
var logger = new LoggerConsole(parms);
BuildVersion app = new BuildVersion(parms, logger);
app.Start();
}
else {
System.Console.WriteLine("Failed processing parameters");
}
}
public BuildVersion(AppParams pParams, BLogger pLog) {
appParams = pParams;
log = pLog;
}
public void Start() {
log.Info("BuildVersion {0}. See https://github.com/Misterblue/BuildVersion", VersionInfo.longVersion);
// Verify passed version number is in good format
string[] versionParts = appParams.Version.Split('.');
if (versionParts.Length != 3) {
log.Error("Specified version number must be in form 'num.num.num'. Given version = {0}", appParams.Version);
return;
}
OptionallyIncrementBuildNumber(ref versionParts);
// Get the Git version of the current HEAD
string? gitVersion = GetGitVersion();
if (gitVersion is not null) {
// Built date is assumed to be when BuildVersion is run
if (appParams.BuildDate == null) {
appParams.BuildDate = DateTime.UtcNow.ToString("yyyyMMdd");
}
// Long version string is app version, build date, and git commit
if (appParams.LongVersion == null) {
appParams.LongVersion = appParams.Version + "-" + appParams.BuildDate + "-" + gitVersion.Substring(0, 8);
}
if (appParams.Print) {
// Print out the version if that what was asked for
System.Console.WriteLine(appParams.LongVersion);
}
else {
WriteVersionFile();
UpdateAssemblyFile();
WriteAppVersion();
}
}
}
/// <summary>
/// Fetch the long form of the current selected GIT version.
/// </summary>
/// <returns>The long form of current Git HEAD version or 'null' if cannot be read</returns>
public string? GetGitVersion() {
string? gitVersion = null;
string? gitDir = appParams.GitDir;
if (gitDir is not null) {
string headFile = gitDir + "/" + "HEAD";
if (File.Exists(headFile)) {
string refFile = ".git/" + File.ReadAllText(headFile);
var refFilePieces = refFile.Split(' ');
if (refFilePieces.Length > 1) {
if (refFilePieces[0] == ".git/ref:") {
refFile = gitDir + "/" + refFilePieces[1].Trim();
}
}
if (File.Exists(refFile)) {
gitVersion = File.ReadAllText(refFile).Trim();
}
else {
log.Error("Cannot open GIT ref file named {0}", refFile);
}
}
else {
log.Error("Cannot open GIT HEAD file named {0}", headFile);
}
}
else {
log.Error("gitDir not specified in app parameters");
}
return gitVersion;
}
public void OptionallyIncrementBuildNumber(ref string[] pVersionParts) {
if (appParams.IncrementBuild) {
try {
var buildnum = Convert.ToInt32(pVersionParts[2]);
pVersionParts[2] = (buildnum + 1).ToString();
appParams.Version = String.Format("{0}.{1}.{2}", pVersionParts);
log.Debug("Incremented build number. New version = {0}", appParams.Version);
}
catch (Exception ex) {
log.Error("Exception incrementing build number: {0}", ex);
return;
}
}
}
public void WriteVersionFile() {
// Write VersionFile
if (appParams.VersionFile is not null) {
log.Debug("Creating version file {0}", appParams.VersionFile);
try {
var buff = new StringBuilder();
buff.AppendLine("// This file is auto-generated by BuildVersion");
buff.AppendLine("// Before editting, check out the application's build environment for use of BuildVersion");
buff.AppendLine("using System;");
buff.AppendLine(String.Format("namespace {0} {{", appParams.NameSpace ?? "UNKNOWN"));
buff.AppendLine(" public class VersionInfo {");
buff.AppendLine(String.Format(" public static string appVersion = \"{0}\";", appParams.Version));
buff.AppendLine(String.Format(" public static string longVersion = \"{0}\";", appParams.LongVersion));
buff.AppendLine(String.Format(" public static string buildDate = \"{0}\";", appParams.BuildDate));
buff.AppendLine(" }");
buff.AppendLine("}");
File.WriteAllText(appParams.VersionFile ?? "", buff.ToString());
}
catch (Exception e) {
log.Error("Exception writing version file {0}: {1}", appParams.VersionFile, e);
}
}
}
public void UpdateAssemblyFile() {
if (appParams.AssemblyFile is not null) {
log.Debug("Adding version {0} to {1}", appParams.Version, appParams.AssemblyFile);
try {
string ambly = File.ReadAllText(appParams.AssemblyFile);
ambly = Regex.Replace(ambly, @"Version\(""[0-9\.]*""\)", @"Version(""" + appParams.Version + @".0"")");
File.WriteAllText(appParams.AssemblyFile, ambly);
}
catch (IOException e) {
log.Error("Exception writing assembly file {0}: {1}", appParams.AssemblyFile, e);
}
}
return;
}
public void WriteAppVersion() {
if (appParams.WriteAppVersion is not null) {
File.WriteAllText(appParams.WriteAppVersion, appParams.Version);
}
}
/// <summary>
/// Process the command line parameters and return an AppParams instance with
/// the configuration parameters for this session.
/// </summary>
/// <param name="args">command line parameters</param>
/// <returns>initialized AppParams or 'null' if there were errors</returns>
public static AppParams? GetParams(string[] args) {
var parms = new AppParams();
// A single parameter of '--help' outputs the invocation parameters
if (args.Length > 0 && args[0] == "--help") {
System.Console.WriteLine(Invocation(parms));
return null;
}
try {
parms.MergeCommandLine(args);
}
catch (Exception e) {
System.Console.WriteLine("ERROR: bad parameters: " + e.Message);
System.Console.WriteLine(Invocation(parms));
return null;
}
return parms;
}
public static string Invocation(AppParams pParams) {
StringBuilder buff = new StringBuilder();
buff.AppendLine("Invocation: BuildVersion <parameters>");
buff.AppendLine(" Possible parameters are (negate bool parameters by prepending 'no'):");
string[] paramDescs = pParams.ListParameters().Select(kvp => { return kvp.Key + ": " + kvp.Value; }).ToArray();
buff.AppendLine(String.Join(Environment.NewLine, paramDescs));
return buff.ToString();
}
}
}