Skip to content

Commit

Permalink
adjust cmd app for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvreony committed Apr 28, 2024
1 parent 2a47bd9 commit c3c936a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Dhgms.GripeWithRoslyn.Cmd/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Dhgms.GripeWithRoslyn.Analyzer.Project;
using Dhgms.GripeWithRoslyn.Cmd.CommandLine;
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.MSBuild;

Expand Down Expand Up @@ -92,6 +93,8 @@ public Job(JobLogMessageActionsWrapper logMessageActionsWrapper)
var compilationWithAnalyzers = compilation.WithAnalyzers(analyzers);
var diagnostics = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync().ConfigureAwait(false);
hasIssues |= !diagnostics.IsEmpty;

OutputDiagnostics(diagnostics);
}
}

Expand Down Expand Up @@ -185,6 +188,41 @@ private ImmutableArray<DiagnosticAnalyzer> GetDiagnosticAnalyzers()
return analyzers;
}

private void OutputDiagnostics(ImmutableArray<Diagnostic> diagnostics)
{
foreach (var diagnostic in diagnostics)
{
OutputDiagnostic(diagnostic);
}
}

private void OutputDiagnostic(Diagnostic diagnostic)
{
try
{
var message = diagnostic.ToString();
switch (diagnostic.Severity)
{
case DiagnosticSeverity.Error:
_logMessageActionsWrapper.DiagnosticError(message);
break;
case DiagnosticSeverity.Hidden:
_logMessageActionsWrapper.DiagnosticHidden(message);
break;
case DiagnosticSeverity.Info:
_logMessageActionsWrapper.DiagnosticInfo(message);
break;
case DiagnosticSeverity.Warning:
_logMessageActionsWrapper.DiagnosticWarning(message);
break;
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

private class ConsoleProgressReporter : IProgress<ProjectLoadProgress>
{
public void Report(ProjectLoadProgress loadProgress)
Expand Down
44 changes: 44 additions & 0 deletions src/Dhgms.GripeWithRoslyn.Cmd/JobLogMessageActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public sealed class JobLogMessageActions
private readonly Action<ILogger, int, Exception?> _multipleMsBuildInstancesFound;
private readonly Action<ILogger, WorkspaceDiagnosticEventArgs, Exception?> _workspaceFailed;
private readonly Action<ILogger, string, string, Exception?> _foundMsBuildInstance;
private readonly Action<ILogger, string, Exception?> _diagnosticError;
private readonly Action<ILogger, string, Exception?> _diagnosticHidden;
private readonly Action<ILogger, string, Exception?> _diagnosticInfo;
private readonly Action<ILogger, string, Exception?> _diagnosticWarning;

/// <summary>
/// Initializes a new instance of the <see cref="JobLogMessageActions"/> class.
Expand Down Expand Up @@ -84,6 +88,26 @@ public JobLogMessageActions()
LogLevel.Information,
new EventId(10, nameof(WorkspaceFailed)),
"MSBuild Instance: {Name} - {Location}");

_diagnosticError = LoggerMessage.Define<string>(
LogLevel.Error,
new EventId(11, nameof(DiagnosticError)),
"Diagnostic Error: {Message}");

_diagnosticHidden = LoggerMessage.Define<string>(
LogLevel.Information,
new EventId(12, nameof(DiagnosticHidden)),
"Diagnostic Hidden: {Message}");

_diagnosticInfo = LoggerMessage.Define<string>(
LogLevel.Error,
new EventId(13, nameof(DiagnosticInfo)),
"Diagnostic Info: {Message}");

_diagnosticWarning = LoggerMessage.Define<string>(
LogLevel.Warning,
new EventId(14, nameof(DiagnosticWarning)),
"Diagnostic Warning: {Message}");
}

/// <summary>
Expand Down Expand Up @@ -183,5 +207,25 @@ internal void FoundMsBuildInstance(ILogger<Job> logger, string instanceName, str
{
_foundMsBuildInstance(logger, instanceName, instancePath, null);
}

internal void DiagnosticError(ILogger<Job> logger, string message)
{
_diagnosticError(logger, message, null);
}

internal void DiagnosticHidden(ILogger<Job> logger, string message)
{
_diagnosticHidden(logger, message, null);
}

internal void DiagnosticInfo(ILogger<Job> logger, string message)
{
_diagnosticInfo(logger, message, null);
}

internal void DiagnosticWarning(ILogger<Job> logger, string message)
{
_diagnosticWarning(logger, message, null);
}
}
}
36 changes: 36 additions & 0 deletions src/Dhgms.GripeWithRoslyn.Cmd/JobLogMessageActionsWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,41 @@ public void WorkspaceFailed(WorkspaceDiagnosticEventArgs e)
{
_logMessageActions.WorkspaceFailed(_logger, e);
}

/// <summary>
/// Logging action for a Roslyn diagnostic error report.
/// </summary>
/// <param name="message">Message from the diagnostic.</param>
public void DiagnosticError(string message)
{
_logMessageActions.DiagnosticError(_logger, message);
}

/// <summary>
/// Logging action for a Roslyn diagnostic hidden report.
/// </summary>
/// <param name="message">Message from the diagnostic.</param>
public void DiagnosticHidden(string message)
{
_logMessageActions.DiagnosticHidden(_logger, message);
}

/// <summary>
/// Logging action for a Roslyn diagnostic information report.
/// </summary>
/// <param name="message">Message from the diagnostic.</param>
public void DiagnosticInfo(string message)
{
_logMessageActions.DiagnosticInfo(_logger, message);
}

/// <summary>
/// Logging action for a Roslyn diagnostic warning report.
/// </summary>
/// <param name="message">Message from the diagnostic.</param>
public void DiagnosticWarning(string message)
{
_logMessageActions.DiagnosticWarning(_logger, message);
}
}
}

0 comments on commit c3c936a

Please sign in to comment.