Skip to content

Commit

Permalink
Drop F# services dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Mar 2, 2024
1 parent 99452cc commit 0f6515b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 38 deletions.
1 change: 0 additions & 1 deletion src/Buildalyzer/Buildalyzer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FSharp.Compiler.Service" Version="43.8.200" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="4.4.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
Expand Down
20 changes: 9 additions & 11 deletions src/Buildalyzer/Compiler/FSharpCommandLineParser.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
#nullable enable

using FSharp.Compiler.CodeAnalysis;
using Microsoft.FSharp.Collections;

namespace Buildalyzer;

internal static class FSharpCommandLineParser
{
[Pure]
public static FSharpCompilerCommand Parse(string[] args)
{
var references = args.Where(a => a.StartsWith("-r:")).Select(a => a[3..]);
// TODO: find the best way to initiate an F# checker.
var checker = FSharpChecker.Instance;
var result = checker.GetParsingOptionsFromCommandLineArgs(ListModule.OfArray(args), isInteractive: true, isEditing: false);
var sourceFiles = args.Where(a => a[0] != '-');
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(
result.Item1,
result.Item2,
references);
sourceFiles,
preprocessorSymbolNames,
metadataReferences);
}

[Pure]
public static string[]? SplitCommandLineIntoArguments(string? commandLine)
=> commandLine?.Split(Splitters, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries) is { Length: > 0 } args
? First(args[0]).Concat(args[1..]).ToArray()
&& First(args[0]).ToArray() is { Length: >= 1 } first
? first.Concat(args[1..]).ToArray()
: null;

[Pure]
Expand Down
30 changes: 5 additions & 25 deletions src/Buildalyzer/Compiler/FSharpCompilerCommand.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,26 @@
using System.Collections.Immutable;
using FSharp.Compiler.CodeAnalysis;
using FSharp.Compiler.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.FSharp.Collections;
using Microsoft.CodeAnalysis;

namespace Buildalyzer;

public sealed record FSharpCompilerCommand : CompilerCommand
{
public FSharpCompilerCommand(
FSharpParsingOptions options,
FSharpList<FSharpDiagnostic> diagnostics,
IEnumerable<string> sourceFiles,
IEnumerable<string> preprocessorSymbolNames,
IEnumerable<string> metadataReferences)
{
Options = Guard.NotNull(options);
Errors = diagnostics.Select(AsDiagnostic).ToImmutableArray();
SourceFiles = Options.SourceFiles.Select(AsCommandLineSourceFile).ToImmutableArray();
PreprocessorSymbolNames = Options.ConditionalDefines.ToImmutableArray();
SourceFiles = sourceFiles.Select(AsCommandLineSourceFile).ToImmutableArray();
PreprocessorSymbolNames = preprocessorSymbolNames.ToImmutableArray();
MetadataReferences = metadataReferences.Select(AsMetadataReference).ToImmutableArray();
}

[Pure]
public static CommandLineReference AsMetadataReference(string r)
=> new CommandLineReference(r, new MetadataReferenceProperties(MetadataImageKind.Assembly));

[Pure]
private static Diagnostic AsDiagnostic(FSharpDiagnostic d)
=> Diagnostic.Create(
id: d.ErrorNumberText,
category: d.Subcategory,
message: d.Message,
severity: DiagnosticSeverity.Error, // d.Severity,
defaultSeverity: DiagnosticSeverity.Error, ///d.Severity,
isEnabledByDefault: true, // TODO
warningLevel: 3, //TODO
location: Location.Create(d.FileName, default, default)); // TODO: map location

[Pure]
private static CommandLineSourceFile AsCommandLineSourceFile(string path) => new(path, isScript: false); // TODO: resolve when it is a script.

public FSharpParsingOptions Options { get; }

/// <inheritdoc />
public override CompilerLanguage Language => CompilerLanguage.FSharp;

Expand Down
2 changes: 1 addition & 1 deletion tests/Buildalyzer.Tests/Compiler/CompilerCommandFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void Parse_FSharp()
{
Text = commandline,
Language = CompilerLanguage.FSharp,
PreprocessorSymbolNames = new[] { "NETCOREAPP3_1_OR_GREATER", "NETCOREAPP3_0_OR_GREATER", "NETCOREAPP2_2_OR_GREATER", "NETCOREAPP2_1_OR_GREATER", "NETCOREAPP2_0_OR_GREATER", "NETCOREAPP1_1_OR_GREATER", "NETCOREAPP1_0_OR_GREATER", "NETCOREAPP3_1", "NETCOREAPP", "DEBUG", "TRACE", "INTERACTIVE" },
PreprocessorSymbolNames = new[] { "NETCOREAPP3_1_OR_GREATER", "NETCOREAPP3_0_OR_GREATER", "NETCOREAPP2_2_OR_GREATER", "NETCOREAPP2_1_OR_GREATER", "NETCOREAPP2_0_OR_GREATER", "NETCOREAPP1_1_OR_GREATER", "NETCOREAPP1_0_OR_GREATER", "NETCOREAPP3_1", "NETCOREAPP", "DEBUG", "TRACE" },
SourceFiles = Files("obj\\Debug\\netcoreapp3.1\\.NETCoreApp,Version=v3.1.AssemblyAttributes.fs", "obj\\Debug\\netcoreapp3.1\\FSharpProject.AssemblyInfo.fs", "Program.fs"),
MetadataReferences = MetaReferences("C:\\Program Files\\dotnet\\packs\\Microsoft.NETCore.App.Ref\\3.1.0\\ref\\netcoreapp3.1\\Microsoft.CSharp.dll", "C:\\Program Files\\dotnet\\packs\\Microsoft.NETCore.App.Ref\\3.1.0\\ref\\netcoreapp3.1\\Microsoft.VisualBasic.Core.dll", "C:\\Program Files\\dotnet\\packs\\Microsoft.NETCore.App.Ref\\3.1.0\\ref\\netcoreapp3.1\\Microsoft.VisualBasic.dll", "C:\\Program Files\\dotnet\\packs\\Microsoft.NETCore.App.Ref\\3.1.0\\ref\\netcoreapp3.1\\Microsoft.Win32.Primitives.dll", "C:\\Program Files\\dotnet\\packs\\Microsoft.NETCore.App.Ref\\3.1.0\\ref\\netcoreapp3.1\\mscorlib.dll", "C:\\Program Files\\dotnet\\packs\\Microsoft.NETCore.App.Ref\\3.1.0\\ref\\netcoreapp3.1\\netstandard.dll"),
});
Expand Down

0 comments on commit 0f6515b

Please sign in to comment.