Skip to content

Commit d40ebfa

Browse files
committed
Merge branch 'main' into 1.0.0
# Conflicts: # Src/CSharpier.Cli/CommandLineOptions.cs # Src/CSharpier.Cli/Program.cs # Src/CSharpier.Generators/NodePrinterGenerator.cs # Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets
2 parents 4c2ae02 + a5648ea commit d40ebfa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1187
-541
lines changed

.github/workflows/publish_nuget.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
- name: Publish CSharpier dotnet tool on version change
3535
uses: alirezanet/[email protected]
3636
with:
37+
PACKAGE_NAME: CSharpier
3738
PROJECT_FILE_PATH: Src/CSharpier.Cli/CSharpier.Cli.csproj
3839
TAG_FORMAT: "*"
3940
- name: Publish CSharpier.MsBuild library on version change

Src/CSharpier.Cli/CommandLineFormatter.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ CancellationToken cancellationToken
2121
{
2222
try
2323
{
24-
var stopwatch = Stopwatch.StartNew();
24+
var timestamp = Stopwatch.GetTimestamp();
2525
var commandLineFormatterResult = new CommandLineFormatterResult();
2626

2727
if (commandLineOptions.StandardInFileContents != null)
@@ -60,7 +60,8 @@ CancellationToken cancellationToken
6060
{
6161
var fileIssueLogger = new FileIssueLogger(
6262
commandLineOptions.OriginalDirectoryOrFilePaths[0],
63-
logger
63+
logger,
64+
commandLineOptions.LogFormat
6465
);
6566

6667
var printerOptions = await optionsProvider.GetPrinterOptionsForAsync(
@@ -101,7 +102,8 @@ await PerformFormattingSteps(
101102
}
102103
}
103104

104-
commandLineFormatterResult.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds;
105+
commandLineFormatterResult.ElapsedMilliseconds = (long)
106+
Stopwatch.GetElapsedTime(timestamp).TotalMilliseconds;
105107
if (!commandLineOptions.WriteStdout)
106108
{
107109
logger.LogInformation(
@@ -236,7 +238,11 @@ await FormatPhysicalFile(
236238
}
237239
else if (warnForUnsupported)
238240
{
239-
var fileIssueLogger = new FileIssueLogger(originalFilePath, logger);
241+
var fileIssueLogger = new FileIssueLogger(
242+
originalFilePath,
243+
logger,
244+
logFormat: LogFormat.Console
245+
);
240246
fileIssueLogger.WriteWarning("Is an unsupported file type.");
241247
}
242248
}
@@ -271,7 +277,7 @@ await FormatPhysicalFile(
271277
.ToArray();
272278
try
273279
{
274-
Task.WaitAll(tasks, cancellationToken);
280+
await Task.WhenAll(tasks).WaitAsync(cancellationToken);
275281
}
276282
catch (OperationCanceledException ex)
277283
{
@@ -307,7 +313,11 @@ CancellationToken cancellationToken
307313
cancellationToken
308314
);
309315

310-
var fileIssueLogger = new FileIssueLogger(originalFilePath, logger);
316+
var fileIssueLogger = new FileIssueLogger(
317+
originalFilePath,
318+
logger,
319+
commandLineOptions.LogFormat
320+
);
311321

312322
logger.LogDebug(
313323
commandLineOptions.Check

Src/CSharpier.Cli/CommandLineOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ internal class CommandLineOptions
88
public string[] DirectoryOrFilePaths { get; init; } = [];
99
public bool Check { get; init; }
1010
public bool SkipValidation { get; init; }
11+
public LogFormat LogFormat { get; init; }
12+
public bool Fast { get; init; }
1113
public bool SkipWrite { get; init; }
1214
public bool WriteStdout { get; init; }
1315
public bool NoCache { get; init; }

Src/CSharpier.Cli/ConsoleLogger.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace CSharpier.Cli;
44

5-
internal class ConsoleLogger(IConsole console, LogLevel loggingLevel) : ILogger
5+
internal class ConsoleLogger(IConsole console, LogLevel loggingLevel, LogFormat logFormat) : ILogger
66
{
77
private static readonly object ConsoleLock = new();
88

@@ -52,7 +52,7 @@ void WriteLine(string? value = null)
5252
{
5353
var message = formatter(state, exception!);
5454

55-
if (logLevel >= LogLevel.Warning)
55+
if (logFormat == LogFormat.Console && logLevel >= LogLevel.Warning)
5656
{
5757
console.ForegroundColor = GetColorLevel(logLevel);
5858
Write($"{logLevel} ");

Src/CSharpier.Cli/FileIssueLogger.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@
22

33
namespace CSharpier.Cli;
44

5-
internal class FileIssueLogger(string filePath, ILogger logger)
5+
internal class FileIssueLogger(string filePath, ILogger logger, LogFormat logFormat)
66
{
77
public void WriteError(string value, Exception? exception = null)
88
{
9-
logger.LogError(exception, $"{this.GetPath()} - {value}");
9+
logger.LogError(exception, GetMessageTemplate(), this.GetPath(), value);
1010
}
1111

1212
public void WriteWarning(string value)
1313
{
14-
logger.LogWarning($"{this.GetPath()} - {value}");
14+
logger.LogWarning(GetMessageTemplate(), this.GetPath(), value);
15+
}
16+
17+
private string GetMessageTemplate()
18+
{
19+
return logFormat switch
20+
{
21+
LogFormat.MsBuild => "{Path}: error: {Message}",
22+
LogFormat.Console => "{Path} - {Message}",
23+
_ => throw new NotImplementedException($"LogFormat: {Enum.GetName(logFormat)}"),
24+
};
1525
}
1626

1727
private string GetPath()

Src/CSharpier.Cli/FormattingCommands.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static Command CreateFormatCommand()
3535
formatCommand.AddOption(writeStdoutOption);
3636
formatCommand.AddOption(CompilationErrorsAsWarningsOption);
3737
formatCommand.AddOption(ConfigPathOption);
38+
formatCommand.AddOption(LogFormatOption);
3839
formatCommand.AddOption(LogLevelOption);
3940

4041
formatCommand.SetHandler(async context =>
@@ -51,6 +52,7 @@ public static Command CreateFormatCommand()
5152
);
5253
var configPath = context.ParseResult.GetValueForOption(ConfigPathOption);
5354
var logLevel = context.ParseResult.GetValueForOption(LogLevelOption);
55+
var logFormat = context.ParseResult.GetValueForOption(LogFormatOption);
5456
var token = context.GetCancellationToken();
5557

5658
context.ExitCode = await FormatOrCheck(
@@ -65,6 +67,7 @@ public static Command CreateFormatCommand()
6567
compilationErrorsAsWarnings,
6668
configPath,
6769
logLevel,
70+
logFormat,
6871
token
6972
);
7073
});
@@ -81,6 +84,7 @@ public static Command CreateCheckCommand()
8184
var directoryOrFileArgument = AddDirectoryOrFileArgument(checkCommand);
8285

8386
checkCommand.AddOption(ConfigPathOption);
87+
checkCommand.AddOption(LogFormatOption);
8488
checkCommand.AddOption(LogLevelOption);
8589
checkCommand.AddOption(IncludeGeneratedOption);
8690
checkCommand.AddOption(NoMsBuildCheckOption);
@@ -96,6 +100,7 @@ public static Command CreateCheckCommand()
96100
);
97101
var configPath = context.ParseResult.GetValueForOption(ConfigPathOption);
98102
var logLevel = context.ParseResult.GetValueForOption(LogLevelOption);
103+
var logFormat = context.ParseResult.GetValueForOption(LogFormatOption);
99104
var token = context.GetCancellationToken();
100105

101106
context.ExitCode = await FormatOrCheck(
@@ -110,6 +115,7 @@ public static Command CreateCheckCommand()
110115
compilationErrorsAsWarnings,
111116
configPath,
112117
logLevel,
118+
logFormat,
113119
token
114120
);
115121
});
@@ -129,11 +135,12 @@ private static async Task<int> FormatOrCheck(
129135
bool compilationErrorsAsWarnings,
130136
string? configPath,
131137
LogLevel logLevel,
138+
LogFormat logFormat,
132139
CancellationToken cancellationToken
133140
)
134141
{
135142
var console = new SystemConsole();
136-
var logger = new ConsoleLogger(console, logLevel);
143+
var logger = new ConsoleLogger(console, logLevel, logFormat);
137144

138145
var directoryOrFileNotProvided = directoryOrFile is null or { Length: 0 };
139146
var originalDirectoryOrFile = directoryOrFile;
@@ -210,8 +217,18 @@ private static Argument<string[]> AddDirectoryOrFileArgument(Command command)
210217
"Path to the CSharpier configuration file"
211218
);
212219

220+
private static readonly Option<LogFormat> LogFormatOption = new(
221+
["--log-format"],
222+
() => LogFormat.Console,
223+
"""
224+
Log output format
225+
Console (default) - Formats messages in a human readable way for console interaction.
226+
MsBuild - Formats messages in standard error/warning format for MSBuild.
227+
"""
228+
);
229+
213230
private static readonly Option<LogLevel> LogLevelOption = new(
214-
["--loglevel"],
231+
["--log-level"],
215232
() => LogLevel.Information,
216233
"Specify the log level - Debug, Information (default), Warning, Error, None"
217234
);

Src/CSharpier.Cli/LogFormat.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace CSharpier.Cli;
2+
3+
internal enum LogFormat
4+
{
5+
/// <summary>
6+
/// Formats messages in a human readable way for console interaction.
7+
/// </summary>
8+
Console,
9+
10+
/// <summary>
11+
/// Formats messages in standard error/warning format for MSBuild.
12+
/// </summary>
13+
MsBuild,
14+
}

Src/CSharpier.Cli/PipeCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static Command Create()
2525
var logLevel = LogLevel.Information;
2626

2727
var console = new SystemConsole();
28-
var logger = new ConsoleLogger(console, logLevel);
28+
var logger = new ConsoleLogger(console, logLevel, LogFormat.Console);
2929

3030
var cancellationToken = context.GetCancellationToken();
3131
context.ExitCode = await PipeMultipleFilesFormatter.StartServer(

Src/CSharpier.Cli/ServerCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static Command Create()
2525
var serverPort = context.ParseResult.GetValueForOption(serverPortOption);
2626
context.ExitCode = await ServerFormatter.StartServer(
2727
serverPort,
28-
new ConsoleLogger(new SystemConsole(), logLevel)
28+
new ConsoleLogger(new SystemConsole(), logLevel, LogFormat.Console)
2929
);
3030
});
3131

0 commit comments

Comments
 (0)