Skip to content

Commit

Permalink
housekeeping: code analysis to 4.8 and restrict to sln file on comman…
Browse files Browse the repository at this point in the history
…d line (#239)
  • Loading branch information
dpvreony authored Nov 23, 2023
1 parent f0e8ea6 commit 1b3e3c8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.7.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
</ItemGroup>

<Target Name="_AddAnalyzersToOutput">
Expand Down
61 changes: 61 additions & 0 deletions src/Dhgms.GripeWithRoslyn.Cmd/CommandLine/ArgumentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2019 DHGMS Solutions and Contributors. All rights reserved.
// This file is licensed to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.IO;
using System.Linq;

namespace Dhgms.GripeWithRoslyn.Cmd.CommandLine
{
internal static class ArgumentExtensions
{
internal static Argument<FileInfo> SpecificFileExtensionOnly(this Argument<FileInfo> argument, string extension)
{
argument.AddValidator(result => FileHasSupportedExtension(result, extension));
return argument;
}

internal static Argument<FileInfo> SpecificFileExtensionOnly(this Argument<FileInfo> argument, string[] extensions)
{
argument.AddValidator(result => FileHasSupportedExtension(result, extensions));
return argument;
}

internal static void FileHasSupportedExtension(ArgumentResult result, string extension)
{
for (var i = 0; i < result.Tokens.Count; i++)
{
var token = result.Tokens[i];

var tokenExtension = Path.GetExtension(token.Value);

if (string.IsNullOrWhiteSpace(tokenExtension)
|| !tokenExtension.Equals(extension, StringComparison.OrdinalIgnoreCase))
{
result.ErrorMessage = $"Filename does not have a supported extension of \"{extension}\".";
return;
}
}
}

internal static void FileHasSupportedExtension(ArgumentResult result, string[] extensions)
{
for (var i = 0; i < result.Tokens.Count; i++)
{
var token = result.Tokens[i];

var tokenExtension = Path.GetExtension(token.Value);

if (string.IsNullOrWhiteSpace(tokenExtension)
|| !extensions.Any(extension => tokenExtension.Equals(extension, StringComparison.OrdinalIgnoreCase)))
{
result.ErrorMessage = $"Filename does not have a supported extension of \"{string.Join(",", extensions)}\".";
return;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public static async Task<int> GetResultFromRootCommand<TCommandLineArg, TCommand
public static RootCommandAndBinderModel<CommandLineArgModelBinder> GetRootCommandAndBinder()
{
var solutionArgument = new Argument<FileInfo>("solution")
.ExistingOnly();
.ExistingOnly()
.SpecificFileExtensionOnly(".sln");

var msBuildInstanceNameArgument = new Argument<string?>("msbuild-instance-name");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<ItemGroup>
<PackageReference Include="Microsoft.Build.Locator" Version="1.6.10" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.7.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="4.7.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.7.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.8.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
Expand Down

0 comments on commit 1b3e3c8

Please sign in to comment.