Skip to content

Commit

Permalink
Merge pull request #605 from jmarolf/feature/create-binlog-on-solutio…
Browse files Browse the repository at this point in the history
…n-load

adding ability to produce a binlog
  • Loading branch information
jmarolf committed Apr 13, 2020
2 parents aed51cf + e833443 commit 28d6f9d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
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 @@ -150,7 +152,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 @@ -159,15 +162,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 @@ -182,15 +186,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 @@ -154,7 +154,8 @@ public static async Task<int> Run(string? folder, string? workspace, string? ver
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 @@ -26,6 +26,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

0 comments on commit 28d6f9d

Please sign in to comment.