Skip to content

Commit

Permalink
Some refactoring that was in the csproj branch but didn't really beln…
Browse files Browse the repository at this point in the history
…g there (#1415)
  • Loading branch information
belav authored Dec 26, 2024
1 parent bfedce4 commit baa3138
Show file tree
Hide file tree
Showing 245 changed files with 607 additions and 723 deletions.
26 changes: 26 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,31 @@
indent_size = 2
indent_style = space

csharp_using_directive_placement = outside_namespace
csharp_style_inlined_variable_declaration = true
csharp_style_pattern_matching_over_is_with_cast_check = true
csharp_style_var_elsewhere = true
csharp_style_var_for_built_in_types = true
csharp_style_var_when_type_is_apparent = true

[*.{cs,vb}]
dotnet_diagnostic.RS0041.severity = warning
dotnet_diagnostic.IDE0007.severity = warning
dotnet_diagnostic.IDE0018.severity = warning
dotnet_diagnostic.IDE0019.severity = warning
dotnet_diagnostic.IDE0020.severity = warning
dotnet_diagnostic.IDE0029.severity = warning
dotnet_diagnostic.IDE0030.severity = warning
dotnet_diagnostic.IDE0031.severity = warning
dotnet_diagnostic.IDE0032.severity = warning
dotnet_diagnostic.IDE0036.severity = warning
dotnet_diagnostic.IDE0038.severity = warning
dotnet_diagnostic.IDE0065.severity = warning
dotnet_diagnostic.IDE0075.severity = warning
dotnet_diagnostic.IDE0078.severity = warning
dotnet_diagnostic.IDE0150.severity = warning
dotnet_diagnostic.IDE0290.severity = warning
dotnet_diagnostic.IDE0300.severity = warning
dotnet_diagnostic.IDE0301.severity = warning


1 change: 1 addition & 0 deletions CSharpier.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
Nuget\Build.props = Nuget\Build.props
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpier.Benchmarks", "Src\CSharpier.Benchmarks\CSharpier.Benchmarks.csproj", "{A338903F-69AD-4950-B827-8EE75F98B620}"
Expand Down
2 changes: 2 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
<!-- this is the warning generators get when they fail to generate a source file this makes sure the error for it failing is obvious -->
<WarningsAsErrors>CS8032</WarningsAsErrors>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
</Project>
3 changes: 1 addition & 2 deletions Src/CSharpier.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
using System.Threading;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.CodeAnalysis;

namespace CSharpier.Benchmarks;

using Microsoft.CodeAnalysis;

[MemoryDiagnoser]
public class Benchmarks
{
Expand Down
4 changes: 2 additions & 2 deletions Src/CSharpier.Cli.Tests/CliTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
namespace CSharpier.Cli.Tests;

using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -8,6 +6,8 @@ namespace CSharpier.Cli.Tests;
using FluentAssertions;
using NUnit.Framework;

namespace CSharpier.Cli.Tests;

// these tests are kind of nice as c# because they run in the same place.
// except the one test that has issues with console input redirection
// they used to be powershell, but doing the multiple file thing didn't work
Expand Down
4 changes: 2 additions & 2 deletions Src/CSharpier.Cli.Tests/EditorConfig/SectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace CSharpier.Cli.Tests.EditorConfig;

using CSharpier.Cli.EditorConfig;
using FluentAssertions;
using IniParser.Model;
using NUnit.Framework;

namespace CSharpier.Cli.Tests.EditorConfig;

[TestFixture]
public class SectionTests
{
Expand Down
7 changes: 4 additions & 3 deletions Src/CSharpier.Cli.Tests/ServerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
namespace CSharpier.Cli.Tests;

using System.Diagnostics;
using System.Net.Http.Json;
using System.Text;
Expand All @@ -9,6 +7,8 @@ namespace CSharpier.Cli.Tests;
using FluentAssertions;
using NUnit.Framework;

namespace CSharpier.Cli.Tests;

[TestFixture]
public class ServerTests
{
Expand Down Expand Up @@ -79,7 +79,8 @@ public void RunTwo()

async Task NewFunction()
{
var command = Cli.Wrap("dotnet")
var command = CliWrap
.Cli.Wrap("dotnet")
.WithArguments(path + " --server")
.WithValidation(CommandResultValidation.None);

Expand Down
3 changes: 1 addition & 2 deletions Src/CSharpier.Cli/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
using System.Text;
using CSharpier.Cli.Options;
using CSharpier.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Logging;

namespace CSharpier.Cli;

using Microsoft.CodeAnalysis;

internal static class CommandLineFormatter
{
public static async Task<int> Format(
Expand Down
42 changes: 16 additions & 26 deletions Src/CSharpier.Cli/CommandLineOptions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.CommandLine;
using Microsoft.Extensions.Logging;

namespace CSharpier.Cli;

using Microsoft.Extensions.Logging;

internal class CommandLineOptions
{
public string[] DirectoryOrFilePaths { get; init; } = Array.Empty<string>();
public string[] DirectoryOrFilePaths { get; init; } = [];
public bool Check { get; init; }
public bool Fast { get; init; }
public bool SkipWrite { get; init; }
Expand All @@ -17,7 +16,7 @@ internal class CommandLineOptions
public bool IncludeGenerated { get; init; }
public string? StandardInFileContents { get; init; }
public string? ConfigPath { get; init; }
public string[] OriginalDirectoryOrFilePaths { get; init; } = Array.Empty<string>();
public string[] OriginalDirectoryOrFilePaths { get; init; } = [];

internal delegate Task<int> Handler(
string[] directoryOrFile,
Expand Down Expand Up @@ -47,57 +46,48 @@ public static RootCommand Create()
Description =
"One or more paths to a directory containing C# files to format or a C# file to format. It may be ommited when piping data via stdin.",
}.LegalFilePathsOnly(),
new Option(
new[] { "--check" },
"Check that files are formatted. Will not write any changes."
),
new Option(["--check"], "Check that files are formatted. Will not write any changes."),
new Option<string>(
new[] { "--loglevel" },
["--loglevel"],
() => LogLevel.Information.ToString(),
"Specify the log level - Debug, Information (default), Warning, Error, None"
),
new Option(
new[] { "--no-cache" },
["--no-cache"],
"Bypass the cache to determine if a file needs to be formatted."
),
new Option(
new[] { "--no-msbuild-check" },
["--no-msbuild-check"],
"Bypass the check to determine if a csproj files references a different version of CSharpier.MsBuild."
),
new Option(
new[] { "--include-generated" },
["--include-generated"],
"Include files generated by the SDK and files that begin with <autogenerated /> comments"
),
new Option(
new[] { "--fast" },
["--fast"],
"Skip comparing syntax tree of formatted file to original file to validate changes."
),
new Option(
new[] { "--skip-write" },
["--skip-write"],
"Skip writing changes. Generally used for testing to ensure csharpier doesn't throw any errors or cause syntax tree validation failures."
),
new Option(["--write-stdout"], "Write the results of formatting any files to stdout."),
new Option(
new[] { "--write-stdout" },
"Write the results of formatting any files to stdout."
),
new Option(
new[] { "--pipe-multiple-files" },
["--pipe-multiple-files"],
"Keep csharpier running so that multiples files can be piped to it via stdin."
),
new Option(
new[] { "--server" },
["--server"],
"Run CSharpier as a server so that multiple files may be formatted."
),
new Option<int?>(
new[] { "--server-port" },
["--server-port"],
"Specify the port that CSharpier should start on. Defaults to a random unused port."
),
new Option<string>(
new[] { "--config-path" },
"Path to the CSharpier configuration file"
),
new Option<string>(["--config-path"], "Path to the CSharpier configuration file"),
new Option(
new[] { "--compilation-errors-as-warnings" },
["--compilation-errors-as-warnings"],
"Treat compilation errors from files as warnings instead of errors."
),
};
Expand Down
25 changes: 8 additions & 17 deletions Src/CSharpier.Cli/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@

namespace CSharpier.Cli;

internal class ConsoleLogger : ILogger
internal class ConsoleLogger(IConsole console, LogLevel loggingLevel) : ILogger
{
private static readonly object ConsoleLock = new();

private readonly IConsole console;
private readonly LogLevel loggingLevel;

public ConsoleLogger(IConsole console, LogLevel loggingLevel)
{
this.console = console;
this.loggingLevel = loggingLevel;
}

public virtual void Log<TState>(
LogLevel logLevel,
EventId eventId,
Expand All @@ -23,7 +14,7 @@ public virtual void Log<TState>(
Func<TState, Exception, string> formatter
)
{
if (logLevel < this.loggingLevel)
if (logLevel < loggingLevel)
{
return;
}
Expand All @@ -32,23 +23,23 @@ void Write(string value)
{
if (logLevel >= LogLevel.Error)
{
this.console.WriteError(value);
console.WriteError(value);
}
else
{
this.console.Write(value);
console.Write(value);
}
}

void WriteLine(string? value = null)
{
if (logLevel >= LogLevel.Error)
{
this.console.WriteErrorLine(value);
console.WriteErrorLine(value);
}
else
{
this.console.WriteLine(value);
console.WriteLine(value);
}
}

Expand All @@ -63,9 +54,9 @@ void WriteLine(string? value = null)

if (logLevel >= LogLevel.Warning)
{
this.console.ForegroundColor = GetColorLevel(logLevel);
console.ForegroundColor = GetColorLevel(logLevel);
Write($"{logLevel} ");
this.console.ResetColor();
console.ResetColor();
}

var stringReader = new StringReader(message);
Expand Down
Loading

0 comments on commit baa3138

Please sign in to comment.