Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Command line processor #285

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions src/Buildalyzer/AnalyzerResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,29 +142,11 @@
}
}

internal void ProcessCscCommandLine(string commandLine, bool coreCompile)
{
// Some projects can have multiple Csc calls (see #92) so if this is the one inside CoreCompile use it, otherwise use the first
if (string.IsNullOrWhiteSpace(commandLine) || (CompilerCommand != null && !coreCompile))
{
return;
}
CompilerCommand = Compiler.CommandLine.Parse(new FileInfo(ProjectFilePath).Directory, commandLine, CompilerLanguage.CSharp);
}

internal void ProcessVbcCommandLine(string commandLine)
{
CompilerCommand = Compiler.CommandLine.Parse(new FileInfo(ProjectFilePath).Directory, commandLine, CompilerLanguage.VisualBasic);
}

internal void ProcessFscCommandLine(string commandLine)
{
CompilerCommand = Compiler.CommandLine.Parse(new FileInfo(ProjectFilePath).Directory, commandLine, CompilerLanguage.FSharp);
}
internal void SetCompilerCommand(CompilerCommand cmd) => CompilerCommand = cmd;

private class ProjectItemItemSpecEqualityComparer : IEqualityComparer<IProjectItem>
{
public bool Equals(IProjectItem x, IProjectItem y) => x.ItemSpec.Equals(y.ItemSpec, StringComparison.OrdinalIgnoreCase);

Check warning on line 149 in src/Buildalyzer/AnalyzerResult.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Nullability of reference types in type of parameter 'x' of 'bool ProjectItemItemSpecEqualityComparer.Equals(IProjectItem x, IProjectItem y)' doesn't match implicitly implemented member 'bool IEqualityComparer<IProjectItem>.Equals(IProjectItem? x, IProjectItem? y)' (possibly because of nullability attributes).

Check warning on line 149 in src/Buildalyzer/AnalyzerResult.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Nullability of reference types in type of parameter 'y' of 'bool ProjectItemItemSpecEqualityComparer.Equals(IProjectItem x, IProjectItem y)' doesn't match implicitly implemented member 'bool IEqualityComparer<IProjectItem>.Equals(IProjectItem? x, IProjectItem? y)' (possibly because of nullability attributes).

Check warning on line 149 in src/Buildalyzer/AnalyzerResult.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Nullability of reference types in type of parameter 'x' of 'bool ProjectItemItemSpecEqualityComparer.Equals(IProjectItem x, IProjectItem y)' doesn't match implicitly implemented member 'bool IEqualityComparer<IProjectItem>.Equals(IProjectItem? x, IProjectItem? y)' (possibly because of nullability attributes).

Check warning on line 149 in src/Buildalyzer/AnalyzerResult.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Nullability of reference types in type of parameter 'y' of 'bool ProjectItemItemSpecEqualityComparer.Equals(IProjectItem x, IProjectItem y)' doesn't match implicitly implemented member 'bool IEqualityComparer<IProjectItem>.Equals(IProjectItem? x, IProjectItem? y)' (possibly because of nullability attributes).

public int GetHashCode(IProjectItem obj) => obj.ItemSpec.ToLowerInvariant().GetHashCode();
}
Expand Down
118 changes: 0 additions & 118 deletions src/Buildalyzer/Compiler/Compiler.cs

This file was deleted.

59 changes: 0 additions & 59 deletions src/Buildalyzer/Compiler/FSharpCommandLineParser.cs

This file was deleted.

28 changes: 0 additions & 28 deletions src/Buildalyzer/Compiler/RoslynCommandLineParser.cs

This file was deleted.

47 changes: 25 additions & 22 deletions src/Buildalyzer/Logging/EventProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
extern alias StructuredLogger;

using System.IO;
using Buildalyzer.Processors;
using Microsoft.Build.Framework;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Logging;

namespace Buildalyzer.Logging;

internal class EventProcessor : IDisposable
{
private static readonly List<CommandLineProcessor> Processors =
[
new CSharpCommandLineProcessor(),
new FSharpCommandLineProcessor(),
new VisualBasicCommandLineProcessor(),
];

private readonly Dictionary<string, AnalyzerResult> _results = new Dictionary<string, AnalyzerResult>();
private readonly Stack<AnalyzerResult> _currentResult = new Stack<AnalyzerResult>();
private readonly Stack<TargetStartedEventArgs> _targetStack = new Stack<TargetStartedEventArgs>();
Expand Down Expand Up @@ -145,31 +156,23 @@ private void TargetFinished(object sender, TargetFinishedEventArgs e)

private void MessageRaised(object sender, BuildMessageEventArgs e)
{
AnalyzerResult result = _currentResult.Count == 0 ? null : _currentResult.Peek();
if (result is object)
// Some projects can have multiple Csc calls (see #92) so if this is the one inside CoreCompile use it, otherwise use the first
if (!_currentResult.TryPeek(out var result)
|| result is null
|| !IsRelevant()
|| result.CompilerCommand is { }
|| !_targetStack.Any(x => x.TargetName == "CoreCompile")
|| e.Message is not { Length: > 0 })
{
// Process the command line arguments for the Fsc task
if (e.SenderName?.Equals("Fsc", StringComparison.OrdinalIgnoreCase) == true
&& !string.IsNullOrWhiteSpace(e.Message)
&& _targetStack.Any(x => x.TargetName == "CoreCompile")
&& result.CompilerCommand is null)
{
result.ProcessFscCommandLine(e.Message);
}

// Process the command line arguments for the Csc task
if (e is TaskCommandLineEventArgs cmd
&& string.Equals(cmd.TaskName, "Csc", StringComparison.OrdinalIgnoreCase))
{
result.ProcessCscCommandLine(cmd.CommandLine, _targetStack.Any(x => x.TargetName == "CoreCompile"));
}
return;
}

if (e is TaskCommandLineEventArgs cmdVbc &&
string.Equals(cmdVbc.TaskName, "Vbc", StringComparison.OrdinalIgnoreCase))
{
result.ProcessVbcCommandLine(cmdVbc.CommandLine);
}
if (Processors.Find(p => p.IsApplicable(e)) is { } processor)
{
result.SetCompilerCommand(processor.Parse(e.Message, new FileInfo(result.ProjectFilePath).Directory));
}

bool IsRelevant() => string.IsNullOrEmpty(result.Command) || AnalyzerManager.NormalizePath(e.ProjectFile) == _projectFilePath;
}

private void BuildFinished(object sender, BuildFinishedEventArgs e)
Expand Down
28 changes: 28 additions & 0 deletions src/Buildalyzer/Processors/CSharpCommandLineProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.Build.Framework;
using Microsoft.CodeAnalysis.CSharp;

namespace Buildalyzer.Processors;

public sealed class CSharpCommandLineProcessor : RoslynBasedCommandLineProcessor
{
[Pure]
public override bool IsApplicable(BuildMessageEventArgs e)
=> e is TaskCommandLineEventArgs cmd
&& cmd.CommandLine is { Length: > 0 }
&& cmd.TaskName?.ToUpperInvariant() == "CSC";

[Pure]
public override string[]? SplitCommandLineIntoArguments(string commandLine)
=> SplitCommandLineIntoArguments(commandLine, "csc.dll", "csc.exe");

[Pure]
protected override CompilerCommand Parse(string? baseDir, string? root, string[] args)
{
var arguments = CSharpCommandLineParser.Default.Parse(args, baseDir, root);
var command = new CSharpCompilerCommand()
{
CommandLineArguments = arguments,
};
return Enrich(command, arguments);
}
}
31 changes: 31 additions & 0 deletions src/Buildalyzer/Processors/CommandLineProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.IO;
using Microsoft.Build.Framework;

namespace Buildalyzer.Processors;

public abstract class CommandLineProcessor
{
[Pure]
public abstract bool IsApplicable(BuildMessageEventArgs e);

[Pure]
public abstract string[]? SplitCommandLineIntoArguments(string commandLine);

[Pure]
public CompilerCommand Parse(string commandLine, DirectoryInfo? baseDir)
{
var tokens = SplitCommandLineIntoArguments(commandLine) ?? throw new FormatException("Commandline could not be parsed.");
var location = new FileInfo(tokens[0]);
var args = tokens[1..];

return Parse(baseDir?.ToString(), location.Directory?.ToString(), args) with
{
Text = commandLine,
CompilerLocation = location,
Arguments = args.ToImmutableArray(),
};
}

[Pure]
protected abstract CompilerCommand Parse(string? baseDir, string? root, string[] args);
}
Loading
Loading