-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
219 lines (200 loc) · 8.23 KB
/
Program.cs
File metadata and controls
219 lines (200 loc) · 8.23 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
// Copyright (c) DEMA Consulting
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System.Reflection;
using DemaConsulting.BuildMark.RepoConnectors;
namespace DemaConsulting.BuildMark;
/// <summary>
/// Main program entry point for the BuildMark tool.
/// </summary>
internal static class Program
{
/// <summary>
/// Gets the application version string.
/// </summary>
public static string Version
{
get
{
// Get the assembly containing this program
var assembly = typeof(Program).Assembly;
// Try to get version from assembly attributes, fallback to AssemblyVersion, or default to 0.0.0
return assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
?? assembly.GetName().Version?.ToString()
?? "0.0.0";
}
}
/// <summary>
/// Main entry point for the BuildMark tool.
/// </summary>
/// <param name="args">Command-line arguments.</param>
/// <returns>Exit code: 0 for success, non-zero for failure.</returns>
private static int Main(string[] args)
{
try
{
// Create context from command-line arguments
using var context = Context.Create(args);
// Run the program logic
Run(context);
// Return the exit code from the context
return context.ExitCode;
}
catch (ArgumentException ex)
{
// Print expected argument exceptions and return error code
Console.WriteLine($"Error: {ex.Message}");
return 1;
}
catch (InvalidOperationException ex)
{
// Print expected operation exceptions and return error code
Console.WriteLine($"Error: {ex.Message}");
return 1;
}
catch (Exception ex)
{
// Print unexpected exceptions and re-throw to generate event logs
Console.WriteLine($"Unexpected error: {ex.Message}");
throw;
}
}
/// <summary>
/// Runs the program logic based on the provided context.
/// </summary>
/// <param name="context">The context containing command line arguments and program state.</param>
public static void Run(Context context)
{
// Priority 1: Version query
if (context.Version)
{
context.WriteLine(Version);
return;
}
// Print application banner
PrintBanner(context);
// Priority 2: Help
if (context.Help)
{
PrintHelp(context);
return;
}
// Priority 3: Self-Validation
if (context.Validate)
{
Validation.Run(context);
return;
}
// Priority 4: Build notes processing
ProcessBuildNotes(context);
}
/// <summary>
/// Prints the application banner.
/// </summary>
/// <param name="context">The context for output.</param>
private static void PrintBanner(Context context)
{
context.WriteLine($"BuildMark version {Version}");
context.WriteLine("Copyright (c) DEMA Consulting");
context.WriteLine("");
}
/// <summary>
/// Prints usage information.
/// </summary>
/// <param name="context">The context for output.</param>
private static void PrintHelp(Context context)
{
context.WriteLine("Usage: buildmark [options]");
context.WriteLine("");
context.WriteLine("Options:");
context.WriteLine(" -v, --version Display version information");
context.WriteLine(" -?, -h, --help Display this help message");
context.WriteLine(" --silent Suppress console output");
context.WriteLine(" --validate Run self-validation");
context.WriteLine(" --results <file> Write validation results (TRX or JUnit format)");
context.WriteLine(" --log <file> Write output to log file");
context.WriteLine(" --build-version <version> Specify the build version");
context.WriteLine(" --report <file> Specify the report file name");
context.WriteLine(" --report-depth <depth> Specify the report markdown depth (default: 1)");
context.WriteLine(" --include-known-issues Include known issues in the report");
}
/// <summary>
/// Processes build notes and generates markdown output.
/// </summary>
/// <param name="context">The context containing command line arguments and program state.</param>
private static void ProcessBuildNotes(Context context)
{
// Create repository connector using factory if provided, otherwise use default
var connector = context.ConnectorFactory?.Invoke() ?? RepoConnectorFactory.Create();
// Parse build version if provided
DemaConsulting.BuildMark.Version? buildVersion = null;
if (context.BuildVersion != null)
{
try
{
buildVersion = DemaConsulting.BuildMark.Version.Create(context.BuildVersion);
}
catch (ArgumentException)
{
context.WriteError($"Error: Invalid build version format: {context.BuildVersion}");
return;
}
}
// Create build information
context.WriteLine("Generating build information...");
BuildInformation buildInfo;
try
{
buildInfo = connector.GetBuildInformationAsync(buildVersion).GetAwaiter().GetResult();
}
catch (InvalidOperationException ex)
{
context.WriteError($"Error: {ex.Message}");
return;
}
// Display build information summary
context.WriteLine($"Build Version: {buildInfo.CurrentVersionTag.VersionInfo.Tag}");
context.WriteLine($"Commit Hash: {buildInfo.CurrentVersionTag.CommitHash}");
if (buildInfo.BaselineVersionTag != null)
{
context.WriteLine($"Previous Version: {buildInfo.BaselineVersionTag.VersionInfo.Tag}");
context.WriteLine($"Previous Commit Hash: {buildInfo.BaselineVersionTag.CommitHash}");
}
context.WriteLine($"Changes: {buildInfo.Changes.Count}");
context.WriteLine($"Bugs Fixed: {buildInfo.Bugs.Count}");
context.WriteLine($"Known Issues: {buildInfo.KnownIssues.Count}");
// Export markdown report if requested
if (context.ReportFile != null)
{
context.WriteLine($"Writing build report to {context.ReportFile}...");
try
{
var markdown = buildInfo.ToMarkdown(context.ReportDepth, context.IncludeKnownIssues);
File.WriteAllText(context.ReportFile, markdown);
context.WriteLine("Build report generated successfully.");
}
// Generic catch is justified here to handle any file system exception gracefully
// and allow the program to continue execution rather than crashing
catch (Exception ex)
{
context.WriteError($"Error: Failed to write report: {ex.Message}");
}
}
}
}