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

adding ability to produce a binlog #605

Merged
merged 2 commits into from Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/CodeFormatter.cs
Expand Up @@ -9,6 +9,7 @@
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Build.Logging;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Tools.Formatters;
Expand All @@ -33,7 +34,8 @@ internal static class CodeFormatter
public static async Task<WorkspaceFormatResult> FormatWorkspaceAsync(
FormatOptions options,
ILogger logger,
CancellationToken cancellationToken)
CancellationToken cancellationToken,
bool createBinaryLog = false)
{
var (workspaceFilePath, workspaceType, logLevel, saveFormattedFiles, _, fileMatcher, reportPath) = options;
var logWorkspaceWarnings = logLevel == LogLevel.Trace;
Expand All @@ -45,7 +47,7 @@ internal static class CodeFormatter
var workspaceStopwatch = Stopwatch.StartNew();

using (var workspace = await OpenWorkspaceAsync(
workspaceFilePath, workspaceType, fileMatcher, logWorkspaceWarnings, logger, cancellationToken).ConfigureAwait(false))
workspaceFilePath, workspaceType, fileMatcher, logWorkspaceWarnings, logger, cancellationToken, createBinaryLog).ConfigureAwait(false))
{
if (workspace is null)
{
Expand Down Expand Up @@ -147,7 +149,8 @@ private static string GetReportFilePath(string reportPath)
Matcher fileMatcher,
bool logWorkspaceWarnings,
ILogger logger,
CancellationToken cancellationToken)
CancellationToken cancellationToken,
bool createBinaryLog = false)
{
if (workspaceType == WorkspaceType.Folder)
{
Expand All @@ -156,15 +159,16 @@ private static string GetReportFilePath(string reportPath)
return folderWorkspace;
}

return await OpenMSBuildWorkspaceAsync(workspacePath, workspaceType, logWorkspaceWarnings, logger, cancellationToken);
return await OpenMSBuildWorkspaceAsync(workspacePath, workspaceType, logWorkspaceWarnings, logger, cancellationToken, createBinaryLog);
}

private static async Task<Workspace> OpenMSBuildWorkspaceAsync(
string solutionOrProjectPath,
WorkspaceType workspaceType,
bool logWorkspaceWarnings,
ILogger logger,
CancellationToken cancellationToken)
CancellationToken cancellationToken,
bool createBinaryLog = false)
{
var properties = new Dictionary<string, string>(StringComparer.Ordinal)
{
Expand All @@ -179,15 +183,25 @@ private static string GetReportFilePath(string reportPath)

var workspace = MSBuildWorkspace.Create(properties);

Build.Framework.ILogger binlog = null;
if (createBinaryLog)
{
binlog = new BinaryLogger()
{
Parameters = Path.Combine(Environment.CurrentDirectory, "formatDiagnosticLog.binlog"),
Verbosity = Build.Framework.LoggerVerbosity.Diagnostic,
};
}

if (workspaceType == WorkspaceType.Solution)
{
await workspace.OpenSolutionAsync(solutionOrProjectPath, cancellationToken: cancellationToken).ConfigureAwait(false);
await workspace.OpenSolutionAsync(solutionOrProjectPath, msbuildLogger: binlog, cancellationToken: cancellationToken).ConfigureAwait(false);
}
else
{
try
{
await workspace.OpenProjectAsync(solutionOrProjectPath, cancellationToken: cancellationToken).ConfigureAwait(false);
await workspace.OpenProjectAsync(solutionOrProjectPath, msbuildLogger: binlog, cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (InvalidOperationException)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Program.cs
Expand Up @@ -150,7 +150,8 @@ public static async Task<int> Run(string folder, string workspace, string verbos
var formatResult = await CodeFormatter.FormatWorkspaceAsync(
formatOptions,
logger,
cancellationTokenSource.Token).ConfigureAwait(false);
cancellationTokenSource.Token,
createBinaryLog: logLevel == LogLevel.Trace ? true : false).ConfigureAwait(false);

return GetExitCode(formatResult, check);
}
Expand Down
1 change: 1 addition & 0 deletions src/dotnet-format.csproj
Expand Up @@ -25,6 +25,7 @@
<ItemGroup>
<PackageReference Include="System.CommandLine" Version="$(SystemCommandLineVersion)" />
<PackageReference Include="System.CommandLine.Rendering" Version="$(SystemCommandLineRenderingVersion)" />
<PackageReference Include="Microsoft.Build" Version="$(MicrosoftBuildFrameworkVersion)" ExcludeAssets="Runtime" PrivateAssets="All" />
<PackageReference Include="Microsoft.Build.Framework" Version="$(MicrosoftBuildFrameworkVersion)" ExcludeAssets="Runtime" PrivateAssets="All" />
<PackageReference Include="Microsoft.Build.Locator" Version="$(MicrosoftBuildLocatorVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(MicrosoftExtensionsDependencyInjectionVersion)" />
Expand Down