Skip to content

Commit 73be82d

Browse files
committed
Added Azure Pipelines logging and better command line parsing support.
1 parent a5f216a commit 73be82d

File tree

8 files changed

+189
-73
lines changed

8 files changed

+189
-73
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace CompatibilityChecker
2+
{
3+
using System;
4+
5+
public class AzurePipelinesMessageLogger : IMessageLogger
6+
{
7+
public virtual void Report(Message message)
8+
{
9+
switch (message.Severity)
10+
{
11+
case Severity.Error:
12+
Console.Error.WriteLine("##vso[task.logissue type=error]{0}", message);
13+
break;
14+
case Severity.Warning:
15+
Console.Error.WriteLine("##vso[task.logissue type=warning]{0}", message);
16+
break;
17+
default:
18+
Console.WriteLine("##vso[task.logdetail]{0}",message);
19+
break;
20+
}
21+
22+
}
23+
}
24+
}

CompatibilityChecker/ConsoleMessageLogger.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
using System;
44

5-
internal class ConsoleMessageLogger : IMessageLogger
5+
public class ConsoleMessageLogger : IMessageLogger
66
{
77
public virtual void Report(Message message)
88
{

CompatibilityCheckerCLI/CompatibilityCheckerCLI.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
</ProjectReference>
5353
</ItemGroup>
5454
<ItemGroup>
55+
<PackageReference Include="CommandLineParser">
56+
<Version>2.5.0</Version>
57+
</PackageReference>
5558
<PackageReference Include="System.Reflection.Metadata">
5659
<Version>1.6.0</Version>
5760
</PackageReference>

CompatibilityCheckerCLI/Program.cs

+71-34
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using System.Reflection;
33
using System.Reflection.PortableExecutable;
44
using CompatibilityChecker;
5+
using CommandLine;
6+
using System.Collections.Generic;
7+
using CommandLine.Text;
58

69
namespace CompatibilityCheckerCoreCLI
710
{
@@ -10,58 +13,92 @@ namespace CompatibilityCheckerCoreCLI
1013

1114
internal class Program
1215
{
16+
public class Options
17+
{
18+
[Value(0, MetaName = "reference assembly", Required = true, HelpText = "The reference assembly.")]
19+
public string ReferenceAssembly { get; set; }
20+
21+
[Value(1, MetaName = "new assembly", Required = true, HelpText = "The new assembly.")]
22+
public string NewAssembly { get; set; }
23+
24+
[Option('a', "azure-pipelines", Required = false, Default = false, HelpText = "Include the logging prefixes for Azure Pipelines.")]
25+
public bool AzurePipelines { get; set; }
26+
27+
[Usage(ApplicationAlias = "cccc")]
28+
public static IEnumerable<Example> Examples {
29+
get {
30+
yield return new Example("Compare versions", new Options { ReferenceAssembly = "Assembly-1.0.0.dll", NewAssembly = "Assembly-1.0.1.dll" });
31+
yield return new Example("Compare versions in Azure Pipelines as CI", new Options { ReferenceAssembly = "Assembly-1.0.0.dll", NewAssembly = "Assembly-1.0.1.dll", AzurePipelines = true });
32+
}
33+
}
34+
35+
}
36+
1337
private static void Main(string[] args)
1438
{
15-
if (args.Length != 2)
39+
var result = CommandLine.Parser.Default.ParseArguments<Options>(args)
40+
.WithParsed(opts => RunWithOptions(opts));
41+
#if DEBUG
42+
Console.WriteLine("Done. Press any key to exit.");
43+
Console.ReadKey();
44+
#endif
45+
if (result.Tag == ParserResultType.NotParsed)
1646
{
17-
Console.WriteLine("Usage: reference.dll new.dll");
1847
Environment.ExitCode = 1;
1948
}
20-
else
21-
{
49+
}
2250

23-
FileInfo referenceFile = new FileInfo(args[0]);
24-
FileInfo newFile = new FileInfo(args[1]);
25-
if (referenceFile.Exists && newFile.Exists)
51+
private static void RunWithOptions(Options opts)
52+
{
53+
FileInfo referenceFile = new FileInfo(opts.ReferenceAssembly);
54+
FileInfo newFile = new FileInfo(opts.NewAssembly);
55+
if (referenceFile.Exists && newFile.Exists)
56+
{
57+
var refName = AssemblyName.GetAssemblyName(referenceFile.FullName);
58+
var newName = AssemblyName.GetAssemblyName(newFile.FullName);
59+
Console.WriteLine("{1}Using '{0}' as the reference assembly.", refName.FullName, opts.AzurePipelines ? "##vso[task.logdetail]" : string.Empty);
60+
Console.WriteLine("{1}Using '{0}' as the new assembly.", refName.FullName, opts.AzurePipelines ? "##vso[task.logdetail]" : string.Empty);
61+
using (PEReader referenceAssembly = new PEReader(File.OpenRead(referenceFile.FullName)))
2662
{
27-
var refName = AssemblyName.GetAssemblyName(referenceFile.FullName);
28-
var newName = AssemblyName.GetAssemblyName(newFile.FullName);
29-
Console.WriteLine("Using '{0}' as the reference assembly.", refName.FullName);
30-
Console.WriteLine("Using '{0}' as the new assembly.", refName.FullName);
31-
using (PEReader referenceAssembly = new PEReader(File.OpenRead(referenceFile.FullName)))
63+
using (PEReader newAssembly = new PEReader(File.OpenRead(newFile.FullName)))
3264
{
33-
using (PEReader newAssembly = new PEReader(File.OpenRead(newFile.FullName)))
65+
IMessageLogger logger = opts.AzurePipelines ? (IMessageLogger)new AzurePipelinesMessageLogger() : new ConsoleMessageLogger();
66+
Analyzer analyzer = new Analyzer(referenceAssembly, newAssembly, null, logger);
67+
analyzer.Run();
68+
if (analyzer.HasRun)
3469
{
35-
Analyzer analyzer = new Analyzer(referenceAssembly, newAssembly, null, null);
36-
analyzer.Run();
37-
if (analyzer.HasRun)
70+
if (analyzer.ResultStatistics.SeverityCounts.error > 0)
3871
{
39-
Console.Error.WriteLine(string.Format("Analyzer done. {0} errors, {1} warnings, {2} informational items.", analyzer.ResultStatistics.SeverityCounts.error, analyzer.ResultStatistics.SeverityCounts.warning, analyzer.ResultStatistics.SeverityCounts.information));
40-
if (analyzer.ResultStatistics.SeverityCounts.error > 0)
41-
{
42-
Environment.ExitCode = -2;
43-
}
72+
Console.WriteLine(string.Format("{3}Analyzer done. {0} errors, {1} warnings, {2} informational items.", analyzer.ResultStatistics.SeverityCounts.error, analyzer.ResultStatistics.SeverityCounts.warning, analyzer.ResultStatistics.SeverityCounts.information, opts.AzurePipelines ? "##vso[task.complete result=SucceededWithIssues]" : string.Empty));
73+
Environment.ExitCode = -2;
74+
return;
4475
}
4576
else
4677
{
47-
Environment.ExitCode = -1;
78+
Console.WriteLine(string.Format("{3}Analyzer done. {0} errors, {1} warnings, {2} informational items.", analyzer.ResultStatistics.SeverityCounts.error, analyzer.ResultStatistics.SeverityCounts.warning, analyzer.ResultStatistics.SeverityCounts.information, opts.AzurePipelines ? "##vso[task.complete result=Succeeded]" : string.Empty));
79+
return;
4880
}
4981
}
50-
}
82+
else
83+
{
84+
Console.WriteLine(string.Format("{0}Analyzer failed to run.", opts.AzurePipelines ? "##vso[task.complete result=Failed]" : string.Empty));
5185

86+
Environment.ExitCode = -1;
87+
return;
88+
}
89+
}
5290
}
53-
else
54-
{
55-
if (!referenceFile.Exists)
56-
Console.Error.WriteLine("Reference file '{0}' not found or inaccessible.", referenceFile.FullName);
57-
if (!newFile.Exists)
58-
Console.Error.WriteLine("New file '{0}' not found or inaccessible.", newFile.FullName);
59-
}
91+
92+
}
93+
else
94+
{
95+
if (!referenceFile.Exists)
96+
Console.Error.WriteLine("{1}Reference file '{0}' not found or inaccessible.", referenceFile.FullName, opts.AzurePipelines ? "##vso[task.logissue type=error]" : string.Empty);
97+
if (!newFile.Exists)
98+
Console.Error.WriteLine("{1}New file '{0}' not found or inaccessible.", newFile.FullName, opts.AzurePipelines ? "##vso[task.logissue type=error]" : string.Empty);
99+
Environment.ExitCode = 2;
100+
return;
60101
}
61-
#if DEBUG
62-
Console.WriteLine("Done. Press any key to exit.");
63-
Console.ReadKey();
64-
#endif
65102
}
66103
}
67104
}

CompatibilityCheckerCoreCLI/CompatibilityCheckerCoreCLI.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</PropertyGroup>
1717

1818
<ItemGroup>
19+
<PackageReference Include="CommandLineParser" Version="2.5.0" />
1920
<PackageReference Include="System.Reflection.Metadata" Version="1.6.0" />
2021
</ItemGroup>
2122

CompatibilityCheckerCoreCLI/Program.cs

+72-37
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using System.Reflection;
33
using System.Reflection.PortableExecutable;
44
using CompatibilityChecker;
5+
using CommandLine;
6+
using System.Collections.Generic;
7+
using CommandLine.Text;
58

69
namespace CompatibilityCheckerCoreCLI
710
{
@@ -10,59 +13,91 @@ namespace CompatibilityCheckerCoreCLI
1013

1114
internal class Program
1215
{
16+
public class Options
17+
{
18+
[Value(0, MetaName = "reference assembly", Required = true, HelpText = "The reference assembly.")]
19+
public string ReferenceAssembly { get; set; }
20+
21+
[Value(1, MetaName = "new assembly", Required = true, HelpText = "The new assembly.")]
22+
public string NewAssembly { get; set; }
23+
24+
[Option('a', "azure-pipelines", Required = false, Default = false, HelpText = "Include the logging prefixes for Azure Pipelines.")]
25+
public bool AzurePipelines { get; set; }
26+
27+
[Usage(ApplicationAlias = "cccc")]
28+
public static IEnumerable<Example> Examples {
29+
get {
30+
yield return new Example("Compare versions", new Options { ReferenceAssembly = "Assembly-1.0.0.dll", NewAssembly = "Assembly-1.0.1.dll" });
31+
yield return new Example("Compare versions in Azure Pipelines as CI", new Options { ReferenceAssembly = "Assembly-1.0.0.dll", NewAssembly = "Assembly-1.0.1.dll", AzurePipelines = true });
32+
}
33+
}
34+
35+
}
36+
1337
private static void Main(string[] args)
1438
{
15-
if (args.Length != 2)
39+
var result = CommandLine.Parser.Default.ParseArguments<Options>(args)
40+
.WithParsed(opts => RunWithOptions(opts));
41+
#if DEBUG
42+
Console.WriteLine("Done. Press any key to exit.");
43+
Console.ReadKey();
44+
#endif
45+
if (result.Tag == ParserResultType.NotParsed)
1646
{
17-
Console.WriteLine("Usage: reference.dll new.dll");
1847
Environment.ExitCode = 1;
1948
}
20-
else
21-
{
49+
}
2250

23-
FileInfo referenceFile = new FileInfo(args[0]);
24-
FileInfo newFile = new FileInfo(args[1]);
25-
if (referenceFile.Exists && newFile.Exists)
51+
private static void RunWithOptions(Options opts)
52+
{
53+
FileInfo referenceFile = new FileInfo(opts.ReferenceAssembly);
54+
FileInfo newFile = new FileInfo(opts.NewAssembly);
55+
if (referenceFile.Exists && newFile.Exists)
56+
{
57+
var refName = AssemblyName.GetAssemblyName(referenceFile.FullName);
58+
var newName = AssemblyName.GetAssemblyName(newFile.FullName);
59+
Console.WriteLine("{1}Using '{0}' as the reference assembly.", refName.FullName, opts.AzurePipelines ? "##vso[task.logdetail]" : string.Empty);
60+
Console.WriteLine("{1}Using '{0}' as the new assembly.", refName.FullName, opts.AzurePipelines ? "##vso[task.logdetail]" : string.Empty);
61+
using (PEReader referenceAssembly = new PEReader(File.OpenRead(referenceFile.FullName)))
2662
{
27-
var refName = AssemblyName.GetAssemblyName(referenceFile.FullName);
28-
var newName = AssemblyName.GetAssemblyName(newFile.FullName);
29-
Console.WriteLine("Using '{0}' as the reference assembly.", refName.FullName);
30-
Console.WriteLine("Using '{0}' as the new assembly.", refName.FullName);
31-
using (PEReader referenceAssembly = new PEReader(File.OpenRead(referenceFile.FullName)))
63+
using (PEReader newAssembly = new PEReader(File.OpenRead(newFile.FullName)))
3264
{
33-
using (PEReader newAssembly = new PEReader(File.OpenRead(newFile.FullName)))
65+
IMessageLogger logger = opts.AzurePipelines ? (IMessageLogger)new AzurePipelinesMessageLogger() : new ConsoleMessageLogger();
66+
Analyzer analyzer = new Analyzer(referenceAssembly, newAssembly, null, logger);
67+
analyzer.Run();
68+
if (analyzer.HasRun)
3469
{
35-
Analyzer analyzer = new Analyzer(referenceAssembly, newAssembly, null, null);
36-
analyzer.Run();
37-
if (analyzer.HasRun)
70+
if (analyzer.ResultStatistics.SeverityCounts.error > 0)
3871
{
39-
Console.Error.WriteLine(string.Format("Analyzer done. {0} errors, {1} warnings, {2} informational items.", analyzer.ResultStatistics.SeverityCounts.error, analyzer.ResultStatistics.SeverityCounts.warning, analyzer.ResultStatistics.SeverityCounts.information));
40-
if (analyzer.ResultStatistics.SeverityCounts.error > 0)
41-
{
42-
Environment.ExitCode = -2;
43-
}
44-
}
45-
else
72+
Console.WriteLine(string.Format("{3}Analyzer done. {0} errors, {1} warnings, {2} informational items.", analyzer.ResultStatistics.SeverityCounts.error, analyzer.ResultStatistics.SeverityCounts.warning, analyzer.ResultStatistics.SeverityCounts.information, opts.AzurePipelines ? "##vso[task.complete result=SucceededWithIssues]" : string.Empty));
73+
Environment.ExitCode = -2;
74+
return;
75+
} else
4676
{
47-
Environment.ExitCode = -1;
77+
Console.WriteLine(string.Format("{3}Analyzer done. {0} errors, {1} warnings, {2} informational items.", analyzer.ResultStatistics.SeverityCounts.error, analyzer.ResultStatistics.SeverityCounts.warning, analyzer.ResultStatistics.SeverityCounts.information, opts.AzurePipelines ? "##vso[task.complete result=Succeeded]" : string.Empty));
78+
return;
4879
}
4980
}
50-
}
81+
else
82+
{
83+
Console.WriteLine(string.Format("{0}Analyzer failed to run.", opts.AzurePipelines ? "##vso[task.complete result=Failed]" : string.Empty));
5184

85+
Environment.ExitCode = -1;
86+
return;
87+
}
88+
}
5289
}
53-
else
54-
{
55-
if (!referenceFile.Exists)
56-
Console.Error.WriteLine("Reference file '{0}' not found or inaccessible.", referenceFile.FullName);
57-
if (!newFile.Exists)
58-
Console.Error.WriteLine("New file '{0}' not found or inaccessible.", newFile.FullName);
59-
Environment.ExitCode = 2;
60-
}
90+
91+
}
92+
else
93+
{
94+
if (!referenceFile.Exists)
95+
Console.Error.WriteLine("{1}Reference file '{0}' not found or inaccessible.", referenceFile.FullName, opts.AzurePipelines ? "##vso[task.logissue type=error]" : string.Empty);
96+
if (!newFile.Exists)
97+
Console.Error.WriteLine("{1}New file '{0}' not found or inaccessible.", newFile.FullName, opts.AzurePipelines ? "##vso[task.logissue type=error]" : string.Empty);
98+
Environment.ExitCode = 2;
99+
return;
61100
}
62-
#if DEBUG
63-
Console.WriteLine("Done. Press any key to exit.");
64-
Console.ReadKey();
65-
#endif
66101
}
67102
}
68103
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
https://go.microsoft.com/fwlink/?LinkID=208121.
4+
-->
5+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
6+
<PropertyGroup>
7+
<PublishProtocol>FileSystem</PublishProtocol>
8+
<Configuration>Release</Configuration>
9+
<Platform>Any CPU</Platform>
10+
<TargetFramework>netcoreapp2.2</TargetFramework>
11+
<PublishDir>bin\Release\netcoreapp2.2\publish\</PublishDir>
12+
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
13+
<SelfContained>false</SelfContained>
14+
<_IsPortable>true</_IsPortable>
15+
</PropertyGroup>
16+
</Project>

CompatibilityCheckerCoreCLI/Properties/launchSettings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"CompatibilityCheckerCoreExample": {
44
"commandName": "Project",
5-
"commandLineArgs": "\"..\\..\\..\\..\\MediaBrowser.Common.10.1.0.dll\" \"..\\..\\..\\..\\MediaBrowser.Common.10.2.0.dll\""
5+
"commandLineArgs": "\"..\\..\\..\\..\\MediaBrowser.Common_nuget.dll\" \"..\\..\\..\\..\\MediaBrowser.Common_artifact.dll\" --azure-pipelines"
66
}
77
}
88
}

0 commit comments

Comments
 (0)