Skip to content

Commit

Permalink
Added Azure Pipelines logging and better command line parsing support.
Browse files Browse the repository at this point in the history
  • Loading branch information
EraYaN committed Jul 28, 2019
1 parent a5f216a commit 73be82d
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 73 deletions.
24 changes: 24 additions & 0 deletions CompatibilityChecker/AzurePipelinesMessageLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace CompatibilityChecker
{
using System;

public class AzurePipelinesMessageLogger : IMessageLogger
{
public virtual void Report(Message message)
{
switch (message.Severity)
{
case Severity.Error:
Console.Error.WriteLine("##vso[task.logissue type=error]{0}", message);
break;
case Severity.Warning:
Console.Error.WriteLine("##vso[task.logissue type=warning]{0}", message);
break;
default:
Console.WriteLine("##vso[task.logdetail]{0}",message);
break;
}

}
}
}
2 changes: 1 addition & 1 deletion CompatibilityChecker/ConsoleMessageLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
using System;

internal class ConsoleMessageLogger : IMessageLogger
public class ConsoleMessageLogger : IMessageLogger
{
public virtual void Report(Message message)
{
Expand Down
3 changes: 3 additions & 0 deletions CompatibilityCheckerCLI/CompatibilityCheckerCLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser">
<Version>2.5.0</Version>
</PackageReference>
<PackageReference Include="System.Reflection.Metadata">
<Version>1.6.0</Version>
</PackageReference>
Expand Down
105 changes: 71 additions & 34 deletions CompatibilityCheckerCLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Reflection;
using System.Reflection.PortableExecutable;
using CompatibilityChecker;
using CommandLine;
using System.Collections.Generic;
using CommandLine.Text;

namespace CompatibilityCheckerCoreCLI
{
Expand All @@ -10,58 +13,92 @@ namespace CompatibilityCheckerCoreCLI

internal class Program
{
public class Options
{
[Value(0, MetaName = "reference assembly", Required = true, HelpText = "The reference assembly.")]
public string ReferenceAssembly { get; set; }

[Value(1, MetaName = "new assembly", Required = true, HelpText = "The new assembly.")]
public string NewAssembly { get; set; }

[Option('a', "azure-pipelines", Required = false, Default = false, HelpText = "Include the logging prefixes for Azure Pipelines.")]
public bool AzurePipelines { get; set; }

[Usage(ApplicationAlias = "cccc")]
public static IEnumerable<Example> Examples {
get {
yield return new Example("Compare versions", new Options { ReferenceAssembly = "Assembly-1.0.0.dll", NewAssembly = "Assembly-1.0.1.dll" });
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 });
}
}

}

private static void Main(string[] args)
{
if (args.Length != 2)
var result = CommandLine.Parser.Default.ParseArguments<Options>(args)
.WithParsed(opts => RunWithOptions(opts));
#if DEBUG
Console.WriteLine("Done. Press any key to exit.");
Console.ReadKey();
#endif
if (result.Tag == ParserResultType.NotParsed)
{
Console.WriteLine("Usage: reference.dll new.dll");
Environment.ExitCode = 1;
}
else
{
}

FileInfo referenceFile = new FileInfo(args[0]);
FileInfo newFile = new FileInfo(args[1]);
if (referenceFile.Exists && newFile.Exists)
private static void RunWithOptions(Options opts)
{
FileInfo referenceFile = new FileInfo(opts.ReferenceAssembly);
FileInfo newFile = new FileInfo(opts.NewAssembly);
if (referenceFile.Exists && newFile.Exists)
{
var refName = AssemblyName.GetAssemblyName(referenceFile.FullName);
var newName = AssemblyName.GetAssemblyName(newFile.FullName);
Console.WriteLine("{1}Using '{0}' as the reference assembly.", refName.FullName, opts.AzurePipelines ? "##vso[task.logdetail]" : string.Empty);
Console.WriteLine("{1}Using '{0}' as the new assembly.", refName.FullName, opts.AzurePipelines ? "##vso[task.logdetail]" : string.Empty);
using (PEReader referenceAssembly = new PEReader(File.OpenRead(referenceFile.FullName)))
{
var refName = AssemblyName.GetAssemblyName(referenceFile.FullName);
var newName = AssemblyName.GetAssemblyName(newFile.FullName);
Console.WriteLine("Using '{0}' as the reference assembly.", refName.FullName);
Console.WriteLine("Using '{0}' as the new assembly.", refName.FullName);
using (PEReader referenceAssembly = new PEReader(File.OpenRead(referenceFile.FullName)))
using (PEReader newAssembly = new PEReader(File.OpenRead(newFile.FullName)))
{
using (PEReader newAssembly = new PEReader(File.OpenRead(newFile.FullName)))
IMessageLogger logger = opts.AzurePipelines ? (IMessageLogger)new AzurePipelinesMessageLogger() : new ConsoleMessageLogger();
Analyzer analyzer = new Analyzer(referenceAssembly, newAssembly, null, logger);
analyzer.Run();
if (analyzer.HasRun)
{
Analyzer analyzer = new Analyzer(referenceAssembly, newAssembly, null, null);
analyzer.Run();
if (analyzer.HasRun)
if (analyzer.ResultStatistics.SeverityCounts.error > 0)
{
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));
if (analyzer.ResultStatistics.SeverityCounts.error > 0)
{
Environment.ExitCode = -2;
}
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));
Environment.ExitCode = -2;
return;
}
else
{
Environment.ExitCode = -1;
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));
return;
}
}
}
else
{
Console.WriteLine(string.Format("{0}Analyzer failed to run.", opts.AzurePipelines ? "##vso[task.complete result=Failed]" : string.Empty));

Environment.ExitCode = -1;
return;
}
}
}
else
{
if (!referenceFile.Exists)
Console.Error.WriteLine("Reference file '{0}' not found or inaccessible.", referenceFile.FullName);
if (!newFile.Exists)
Console.Error.WriteLine("New file '{0}' not found or inaccessible.", newFile.FullName);
}

}
else
{
if (!referenceFile.Exists)
Console.Error.WriteLine("{1}Reference file '{0}' not found or inaccessible.", referenceFile.FullName, opts.AzurePipelines ? "##vso[task.logissue type=error]" : string.Empty);
if (!newFile.Exists)
Console.Error.WriteLine("{1}New file '{0}' not found or inaccessible.", newFile.FullName, opts.AzurePipelines ? "##vso[task.logissue type=error]" : string.Empty);
Environment.ExitCode = 2;
return;
}
#if DEBUG
Console.WriteLine("Done. Press any key to exit.");
Console.ReadKey();
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.5.0" />
<PackageReference Include="System.Reflection.Metadata" Version="1.6.0" />
</ItemGroup>

Expand Down
109 changes: 72 additions & 37 deletions CompatibilityCheckerCoreCLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Reflection;
using System.Reflection.PortableExecutable;
using CompatibilityChecker;
using CommandLine;
using System.Collections.Generic;
using CommandLine.Text;

namespace CompatibilityCheckerCoreCLI
{
Expand All @@ -10,59 +13,91 @@ namespace CompatibilityCheckerCoreCLI

internal class Program
{
public class Options
{
[Value(0, MetaName = "reference assembly", Required = true, HelpText = "The reference assembly.")]
public string ReferenceAssembly { get; set; }

[Value(1, MetaName = "new assembly", Required = true, HelpText = "The new assembly.")]
public string NewAssembly { get; set; }

[Option('a', "azure-pipelines", Required = false, Default = false, HelpText = "Include the logging prefixes for Azure Pipelines.")]
public bool AzurePipelines { get; set; }

[Usage(ApplicationAlias = "cccc")]
public static IEnumerable<Example> Examples {
get {
yield return new Example("Compare versions", new Options { ReferenceAssembly = "Assembly-1.0.0.dll", NewAssembly = "Assembly-1.0.1.dll" });
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 });
}
}

}

private static void Main(string[] args)
{
if (args.Length != 2)
var result = CommandLine.Parser.Default.ParseArguments<Options>(args)
.WithParsed(opts => RunWithOptions(opts));
#if DEBUG
Console.WriteLine("Done. Press any key to exit.");
Console.ReadKey();
#endif
if (result.Tag == ParserResultType.NotParsed)
{
Console.WriteLine("Usage: reference.dll new.dll");
Environment.ExitCode = 1;
}
else
{
}

FileInfo referenceFile = new FileInfo(args[0]);
FileInfo newFile = new FileInfo(args[1]);
if (referenceFile.Exists && newFile.Exists)
private static void RunWithOptions(Options opts)
{
FileInfo referenceFile = new FileInfo(opts.ReferenceAssembly);
FileInfo newFile = new FileInfo(opts.NewAssembly);
if (referenceFile.Exists && newFile.Exists)
{
var refName = AssemblyName.GetAssemblyName(referenceFile.FullName);
var newName = AssemblyName.GetAssemblyName(newFile.FullName);
Console.WriteLine("{1}Using '{0}' as the reference assembly.", refName.FullName, opts.AzurePipelines ? "##vso[task.logdetail]" : string.Empty);
Console.WriteLine("{1}Using '{0}' as the new assembly.", refName.FullName, opts.AzurePipelines ? "##vso[task.logdetail]" : string.Empty);
using (PEReader referenceAssembly = new PEReader(File.OpenRead(referenceFile.FullName)))
{
var refName = AssemblyName.GetAssemblyName(referenceFile.FullName);
var newName = AssemblyName.GetAssemblyName(newFile.FullName);
Console.WriteLine("Using '{0}' as the reference assembly.", refName.FullName);
Console.WriteLine("Using '{0}' as the new assembly.", refName.FullName);
using (PEReader referenceAssembly = new PEReader(File.OpenRead(referenceFile.FullName)))
using (PEReader newAssembly = new PEReader(File.OpenRead(newFile.FullName)))
{
using (PEReader newAssembly = new PEReader(File.OpenRead(newFile.FullName)))
IMessageLogger logger = opts.AzurePipelines ? (IMessageLogger)new AzurePipelinesMessageLogger() : new ConsoleMessageLogger();
Analyzer analyzer = new Analyzer(referenceAssembly, newAssembly, null, logger);
analyzer.Run();
if (analyzer.HasRun)
{
Analyzer analyzer = new Analyzer(referenceAssembly, newAssembly, null, null);
analyzer.Run();
if (analyzer.HasRun)
if (analyzer.ResultStatistics.SeverityCounts.error > 0)
{
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));
if (analyzer.ResultStatistics.SeverityCounts.error > 0)
{
Environment.ExitCode = -2;
}
}
else
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));
Environment.ExitCode = -2;
return;
} else
{
Environment.ExitCode = -1;
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));
return;
}
}
}
else
{
Console.WriteLine(string.Format("{0}Analyzer failed to run.", opts.AzurePipelines ? "##vso[task.complete result=Failed]" : string.Empty));

Environment.ExitCode = -1;
return;
}
}
}
else
{
if (!referenceFile.Exists)
Console.Error.WriteLine("Reference file '{0}' not found or inaccessible.", referenceFile.FullName);
if (!newFile.Exists)
Console.Error.WriteLine("New file '{0}' not found or inaccessible.", newFile.FullName);
Environment.ExitCode = 2;
}

}
else
{
if (!referenceFile.Exists)
Console.Error.WriteLine("{1}Reference file '{0}' not found or inaccessible.", referenceFile.FullName, opts.AzurePipelines ? "##vso[task.logissue type=error]" : string.Empty);
if (!newFile.Exists)
Console.Error.WriteLine("{1}New file '{0}' not found or inaccessible.", newFile.FullName, opts.AzurePipelines ? "##vso[task.logissue type=error]" : string.Empty);
Environment.ExitCode = 2;
return;
}
#if DEBUG
Console.WriteLine("Done. Press any key to exit.");
Console.ReadKey();
#endif
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp2.2</TargetFramework>
<PublishDir>bin\Release\netcoreapp2.2\publish\</PublishDir>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<_IsPortable>true</_IsPortable>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion CompatibilityCheckerCoreCLI/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"CompatibilityCheckerCoreExample": {
"commandName": "Project",
"commandLineArgs": "\"..\\..\\..\\..\\MediaBrowser.Common.10.1.0.dll\" \"..\\..\\..\\..\\MediaBrowser.Common.10.2.0.dll\""
"commandLineArgs": "\"..\\..\\..\\..\\MediaBrowser.Common_nuget.dll\" \"..\\..\\..\\..\\MediaBrowser.Common_artifact.dll\" --azure-pipelines"
}
}
}

0 comments on commit 73be82d

Please sign in to comment.