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

Making the library language/compiler agnostic #279

Open
wants to merge 8 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
8 changes: 6 additions & 2 deletions src/Buildalyzer/AnalyzerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public class AnalyzerManager : IAnalyzerManager

public SolutionFile SolutionFile { get; }

public List<ICompilerOptionsParser> CompilerOptionsParsers { get; }

public AnalyzerManager(AnalyzerManagerOptions options = null)
: this(null, options)
{
}

public AnalyzerManager(string solutionFilePath, AnalyzerManagerOptions options = null)
public AnalyzerManager(string solutionFilePath, AnalyzerManagerOptions? options = null)
{
options ??= new AnalyzerManagerOptions();
LoggerFactory = options.LoggerFactory;
Expand All @@ -58,13 +60,15 @@ public AnalyzerManager(string solutionFilePath, AnalyzerManagerOptions options =
foreach (ProjectInSolution projectInSolution in SolutionFile.ProjectsInOrder)
{
if (!SupportedProjectTypes.Contains(projectInSolution.ProjectType)
|| (options?.ProjectFilter != null && !options.ProjectFilter(projectInSolution)))
|| (options.ProjectFilter != null && !options.ProjectFilter(projectInSolution)))
{
continue;
}
GetProject(projectInSolution.AbsolutePath, projectInSolution);
}
}

CompilerOptionsParsers = options.CompilerOptionsParsers;
}

public void SetGlobalProperty(string key, string value)
Expand Down
5 changes: 5 additions & 0 deletions src/Buildalyzer/AnalyzerManagerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public TextWriter LogWriter
LoggerFactory.AddProvider(new TextWriterLoggerProvider(value));
}
}

public List<ICompilerOptionsParser> CompilerOptionsParsers { get; set; } = [
CscOptionsParser.Instance,
VbcOptionsParser.Instance,
FscOptionsParser.Instance];
}
22 changes: 1 addition & 21 deletions src/Buildalyzer/AnalyzerResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class AnalyzerResult : IAnalyzerResult
private readonly Dictionary<string, IProjectItem[]> _items = new Dictionary<string, IProjectItem[]>(StringComparer.OrdinalIgnoreCase);
private readonly Guid _projectGuid;

public CompilerCommand CompilerCommand { get; private set; }
public CompilerCommand CompilerCommand { get; internal set; }

internal AnalyzerResult(string projectFilePath, AnalyzerManager manager, ProjectAnalyzer analyzer)
{
Expand Down Expand Up @@ -107,26 +107,6 @@ internal void ProcessProject(PropertiesAndItems propertiesAndItems)
}
}

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);
}

private class ProjectItemItemSpecEqualityComparer : IEqualityComparer<IProjectItem>
{
public bool Equals(IProjectItem x, IProjectItem y) => x.ItemSpec.Equals(y.ItemSpec, StringComparison.OrdinalIgnoreCase);
Expand Down
2 changes: 1 addition & 1 deletion src/Buildalyzer/Compiler/CSharpCompilerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ namespace Buildalyzer;
public sealed record CSharpCompilerCommand : RoslynBasedCompilerCommand<CSharpCommandLineArguments>
{
/// <inheritdoc />
public override CompilerLanguage Language => CompilerLanguage.CSharp;
public override string Language => "C#";
}
118 changes: 0 additions & 118 deletions src/Buildalyzer/Compiler/Compiler.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Buildalyzer/Compiler/CompilerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Buildalyzer;
public abstract record CompilerCommand
{
/// <summary>The compiler lanuague.</summary>
public abstract CompilerLanguage Language { get; }
public abstract string Language { get; }

/// <summary>The original text of the compiler command.</summary>
public string Text { get; init; } = string.Empty;
Expand Down
17 changes: 0 additions & 17 deletions src/Buildalyzer/Compiler/CompilerLanguage.cs

This file was deleted.

52 changes: 52 additions & 0 deletions src/Buildalyzer/Compiler/CscOptionsParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.IO;
using Buildalyzer.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace Buildalyzer;

/// <summary>
/// A parser for the csc compiler options (Roslyn).
/// </summary>
public sealed class CscOptionsParser : ICompilerOptionsParser
{
/// <summary>
/// A singleton instance of the parser.
/// </summary>
public static CscOptionsParser Instance { get; } = new CscOptionsParser();

public string Language => "C#";

private CscOptionsParser()
{
}

public bool IsSupportedInvocation(object sender, BuildMessageEventArgs eventArgs, CompilerOptionsContext context) =>
eventArgs is TaskCommandLineEventArgs cmd
&& string.Equals(cmd.TaskName, "Csc", StringComparison.OrdinalIgnoreCase);

public CompilerCommand? Parse(string commandLine, CompilerOptionsContext context)
{
if (string.IsNullOrWhiteSpace(commandLine) || (!context.IsFirstInvocation && !context.CoreCompile))
{
return null;
}

var tokens = RoslynParser.SplitCommandLineIntoArguments(commandLine, "csc.dll", "csc.exe")
?? throw new FormatException("Commandline could not be parsed.");
var location = new FileInfo(tokens[0]);
var args = tokens[1..];

var arguments = CSharpCommandLineParser.Default.Parse(args, context.BaseDirectory?.ToString(), location.Directory?.ToString());
var command = new CSharpCompilerCommand()
{
CommandLineArguments = arguments,
Text = commandLine,
CompilerLocation = location,
Arguments = args.ToImmutableArray(),
};
return RoslynParser.Enrich(command, arguments);
}
}
2 changes: 1 addition & 1 deletion src/Buildalyzer/Compiler/FSharpCompilerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace Buildalyzer;
public sealed record FSharpCompilerCommand : CompilerCommand
{
/// <inheritdoc />
public override CompilerLanguage Language => CompilerLanguage.FSharp;
public override string Language => "F#";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Buildalyzer;

internal static class FSharpCommandLineParser
internal static class FSharpParser
{
[Pure]
public static string[]? SplitCommandLineIntoArguments(string? commandLine)
Expand Down
52 changes: 52 additions & 0 deletions src/Buildalyzer/Compiler/FscOptionsParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.IO;
using Buildalyzer.IO;
using Microsoft.Build.Framework;
using Microsoft.CodeAnalysis.VisualBasic;

namespace Buildalyzer;

/// <summary>
/// A parser for the fsc compiler options (F#).
/// </summary>
public sealed class FscOptionsParser : ICompilerOptionsParser
{
/// <summary>
/// A singleton instance of the parser.
/// </summary>
public static FscOptionsParser Instance { get; } = new FscOptionsParser();

public string Language => "F#";

private FscOptionsParser()
{
}

public bool IsSupportedInvocation(object sender, BuildMessageEventArgs eventArgs, CompilerOptionsContext context) =>
eventArgs.SenderName?.Equals("Fsc", StringComparison.OrdinalIgnoreCase) == true
&& !string.IsNullOrWhiteSpace(eventArgs.Message)
&& context.TargetStack.Any(x => x.TargetName == "CoreCompile")
&& context.IsFirstInvocation;

public CompilerCommand? Parse(string commandLine, CompilerOptionsContext context)
{
var tokens = FSharpParser.SplitCommandLineIntoArguments(commandLine)
?? throw new FormatException("Commandline could not be parsed.");

var location = new FileInfo(tokens[0]);
var args = tokens[1..];

var sourceFiles = args.Where(a => a[0] != '-').Select(IOPath.Parse);
var preprocessorSymbolNames = args.Where(a => a.StartsWith("--define:")).Select(a => a[9..]);
var metadataReferences = args.Where(a => a.StartsWith("-r:")).Select(a => a[3..]);

return new FSharpCompilerCommand()
{
MetadataReferences = metadataReferences.ToImmutableArray(),
PreprocessorSymbolNames = preprocessorSymbolNames.ToImmutableArray(),
SourceFiles = sourceFiles.ToImmutableArray(),
Text = commandLine,
CompilerLocation = location,
Arguments = args.ToImmutableArray(),
};
}
}
Loading