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

Add separate command for binary log #1044

Merged
merged 6 commits into from Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 4 additions & 4 deletions src/CodeFormatter.cs
Expand Up @@ -32,7 +32,7 @@ internal static class CodeFormatter
FormatOptions formatOptions,
ILogger logger,
CancellationToken cancellationToken,
bool createBinaryLog = false)
string? binaryLogPath = null)
{
var logWorkspaceWarnings = formatOptions.LogLevel == LogLevel.Trace;

Expand All @@ -44,7 +44,7 @@ internal static class CodeFormatter

using var workspace = formatOptions.WorkspaceType == WorkspaceType.Folder
? OpenFolderWorkspace(formatOptions.WorkspaceFilePath, formatOptions.FileMatcher)
: await OpenMSBuildWorkspaceAsync(formatOptions.WorkspaceFilePath, formatOptions.WorkspaceType, createBinaryLog, logWorkspaceWarnings, logger, cancellationToken).ConfigureAwait(false);
: await OpenMSBuildWorkspaceAsync(formatOptions.WorkspaceFilePath, formatOptions.WorkspaceType, binaryLogPath, logWorkspaceWarnings, logger, cancellationToken).ConfigureAwait(false);

if (workspace is null)
{
Expand Down Expand Up @@ -123,12 +123,12 @@ private static Workspace OpenFolderWorkspace(string workspacePath, SourceFileMat
private static Task<Workspace?> OpenMSBuildWorkspaceAsync(
string solutionOrProjectPath,
WorkspaceType workspaceType,
bool createBinaryLog,
string? binaryLogPath,
bool logWorkspaceWarnings,
ILogger logger,
CancellationToken cancellationToken)
{
return MSBuildWorkspaceLoader.LoadAsync(solutionOrProjectPath, workspaceType, createBinaryLog, logWorkspaceWarnings, logger, cancellationToken);
return MSBuildWorkspaceLoader.LoadAsync(solutionOrProjectPath, workspaceType, binaryLogPath, logWorkspaceWarnings, logger, cancellationToken);
}

private static async Task<Solution> RunCodeFormattersAsync(
Expand Down
17 changes: 16 additions & 1 deletion src/FormatCommand.cs
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.CodeAnalysis.Tools
{
internal static class FormatCommand
{
// This delegate should be kept in Sync with the FormatCommand options and arguement names
// This delegate should be kept in Sync with the FormatCommand options and argument names
// so that values bind correctly.
internal delegate Task<int> Handler(
string? workspace,
Expand All @@ -25,6 +25,7 @@ internal static class FormatCommand
string[] exclude,
string? report,
bool includeGenerated,
string? binarylog,
IConsole console);

internal static string[] VerbosityLevels => new[] { "q", "quiet", "m", "minimal", "n", "normal", "d", "detailed", "diag", "diagnostic" };
Expand Down Expand Up @@ -75,11 +76,16 @@ internal static RootCommand CreateCommandLineOptions()
{
IsHidden = true
},
new Option(new[] { "--binarylog" }, Resources.Log_all_project_or_solution_load_information_to_a_binary_log_file)
{
Argument = new Argument<string?>(() => null) { Name = "binary-log-path", Arity = ArgumentArity.ZeroOrOne }.LegalFilePathsOnly()
},
};

rootCommand.Description = "dotnet-format";
rootCommand.AddValidator(EnsureFolderNotSpecifiedWhenFixingStyle);
rootCommand.AddValidator(EnsureFolderNotSpecifiedWhenFixingAnalyzers);
rootCommand.AddValidator(EnsureFolderNotSpecifiedWhenLoggingBinlog);

return rootCommand;
}
Expand All @@ -102,6 +108,15 @@ internal static RootCommand CreateCommandLineOptions()
: null;
}

internal static string? EnsureFolderNotSpecifiedWhenLoggingBinlog(CommandResult symbolResult)
{
var folder = symbolResult.ValueForOption<bool>("--folder");
var binarylog = symbolResult.OptionResult("--binarylog");
return folder && binarylog is not null && !binarylog.IsImplicit
? Resources.Cannot_specify_the_folder_option_when_writing_a_binary_log
: null;
}

internal static bool WasOptionUsed(this ParseResult result, params string[] aliases)
{
return result.Tokens
Expand Down
23 changes: 22 additions & 1 deletion src/Program.cs
Expand Up @@ -12,6 +12,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.CodeAnalysis.Tools.Logging;
using Microsoft.CodeAnalysis.Tools.MSBuild;
using Microsoft.CodeAnalysis.Tools.Utilities;
Expand Down Expand Up @@ -55,6 +56,7 @@ private static async Task<int> Main(string[] args)
string[] exclude,
string? report,
bool includeGenerated,
string? binarylog,
IConsole console = null!)
{
if (s_parseResult == null)
Expand Down Expand Up @@ -182,9 +184,28 @@ private static async Task<int> Main(string[] args)
formatOptions,
logger,
cancellationTokenSource.Token,
createBinaryLog: logLevel == LogLevel.Trace).ConfigureAwait(false);
binaryLogPath: GetBinaryLogPath(s_parseResult, binarylog)).ConfigureAwait(false);

return GetExitCode(formatResult, check);

static string? GetBinaryLogPath(ParseResult parseResult, string? binarylog)
{
if (parseResult.WasOptionUsed("--binarylog"))
{
if (binarylog is null)
{
return Path.Combine(Directory.GetCurrentDirectory(), "format.binlog");
}
else if (Path.GetExtension(binarylog)?.Equals(".binlog") == false)
{
return Path.ChangeExtension(binarylog, ".binlog");
}

return binarylog;
}

return null;
}
}
catch (FileNotFoundException fex)
{
Expand Down
8 changes: 7 additions & 1 deletion src/Resources.resx
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Expand Down Expand Up @@ -321,4 +321,10 @@
<data name="Replace_0_characters_with_1" xml:space="preserve">
<value>Replace {0} characters with '{1}'.</value>
</data>
<data name="Log_all_project_or_solution_load_information_to_a_binary_log_file" xml:space="preserve">
<value>Log all project or solution load information to a binary log file.</value>
</data>
<data name="Cannot_specify_the_folder_option_when_writing_a_binary_log" xml:space="preserve">
<value>Cannot specify the '--folder' option when writing a binary log.</value>
</data>
</root>
11 changes: 5 additions & 6 deletions src/Workspaces/MSBuildWorkspaceLoader.cs
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.MSBuild;
Expand All @@ -19,7 +18,7 @@ internal static class MSBuildWorkspaceLoader
public static async Task<Workspace?> LoadAsync(
string solutionOrProjectPath,
WorkspaceType workspaceType,
bool createBinaryLog,
string? binaryLogPath,
bool logWorkspaceWarnings,
ILogger logger,
CancellationToken cancellationToken)
Expand All @@ -35,11 +34,11 @@ internal static class MSBuildWorkspaceLoader
var workspace = MSBuildWorkspace.Create(properties);

Build.Framework.ILogger? binlog = null;
if (createBinaryLog)
if (binaryLogPath is not null)
{
binlog = new Build.Logging.BinaryLogger()
{
Parameters = Path.Combine(Environment.CurrentDirectory, "formatDiagnosticLog.binlog"),
Parameters = binaryLogPath,
Verbosity = Build.Framework.LoggerVerbosity.Diagnostic,
};
}
Expand Down Expand Up @@ -98,15 +97,15 @@ static void LogWorkspaceDiagnostics(ILogger logger, bool logWorkspaceWarnings, I
internal static async Task<Workspace?> LockedLoadAsync(
string solutionOrProjectPath,
WorkspaceType workspaceType,
bool createBinaryLog,
string? binaryLogPath,
bool logWorkspaceWarnings,
ILogger logger,
CancellationToken cancellationToken)
{
await Guard.WaitAsync();
try
{
return await LoadAsync(solutionOrProjectPath, workspaceType, createBinaryLog, logWorkspaceWarnings, logger, cancellationToken);
return await LoadAsync(solutionOrProjectPath, workspaceType, binaryLogPath, logWorkspaceWarnings, logger, cancellationToken);
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-format.csproj
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<OutputType>Exe</OutputType>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RootNamespace>Microsoft.CodeAnalysis.Tools</RootNamespace>
<ServerGarbageCollection>true</ServerGarbageCollection>
Expand Down
10 changes: 10 additions & 0 deletions src/xlf/Resources.cs.xlf
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">Styl kódu</target>
Expand Down Expand Up @@ -167,6 +172,11 @@
<target state="translated">Načítá se pracovní prostor.</target>
<note />
</trans-unit>
<trans-unit id="Log_all_project_or_solution_load_information_to_a_binary_log_file">
<source>Log all project or solution load information to a binary log file.</source>
<target state="new">Log all project or solution load information to a binary log file.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_argument">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the &lt;workspace&gt; argument.</source>
<target state="translated">{0} obsahuje více souborů projektů MSBuild. Určete, který soubor chcete použít, pomocí argumentu &lt;workspace&gt;.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/xlf/Resources.de.xlf
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">Codeformat</target>
Expand Down Expand Up @@ -167,6 +172,11 @@
<target state="translated">Arbeitsbereich wird geladen.</target>
<note />
</trans-unit>
<trans-unit id="Log_all_project_or_solution_load_information_to_a_binary_log_file">
<source>Log all project or solution load information to a binary log file.</source>
<target state="new">Log all project or solution load information to a binary log file.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_argument">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the &lt;workspace&gt; argument.</source>
<target state="translated">In "{0}" wurden mehrere MSBuild-Projektdateien gefunden. Geben Sie die zu verwendende Datei mit dem &lt;workspace&gt;-Argument an.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/xlf/Resources.es.xlf
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">Estilo de código</target>
Expand Down Expand Up @@ -167,6 +172,11 @@
<target state="translated">Cargando área de trabajo.</target>
<note />
</trans-unit>
<trans-unit id="Log_all_project_or_solution_load_information_to_a_binary_log_file">
<source>Log all project or solution load information to a binary log file.</source>
<target state="new">Log all project or solution load information to a binary log file.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_argument">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the &lt;workspace&gt; argument.</source>
<target state="translated">Se encontraron varios archivos del proyecto MSBuild en "{0}". Especifique cuál debe usarse con el argumento &lt;workspace&gt;.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/xlf/Resources.fr.xlf
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">Style de code</target>
Expand Down Expand Up @@ -167,6 +172,11 @@
<target state="translated">Chargement de l'espace de travail.</target>
<note />
</trans-unit>
<trans-unit id="Log_all_project_or_solution_load_information_to_a_binary_log_file">
<source>Log all project or solution load information to a binary log file.</source>
<target state="new">Log all project or solution load information to a binary log file.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_argument">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the &lt;workspace&gt; argument.</source>
<target state="translated">Plusieurs fichiers projet MSBuild trouvés dans '{0}'. Spécifiez celui qui doit être utilisé avec l'argument &lt;espace de travail&gt;.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/xlf/Resources.it.xlf
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">Stile codice</target>
Expand Down Expand Up @@ -167,6 +172,11 @@
<target state="translated">Caricamento dell'area di lavoro.</target>
<note />
</trans-unit>
<trans-unit id="Log_all_project_or_solution_load_information_to_a_binary_log_file">
<source>Log all project or solution load information to a binary log file.</source>
<target state="new">Log all project or solution load information to a binary log file.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_argument">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the &lt;workspace&gt; argument.</source>
<target state="translated">In '{0}' sono stati trovati più file di progetto MSBuild. Per specificare quello desiderato, usare l'argomento &lt;workspace&gt;.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/xlf/Resources.ja.xlf
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">コード スタイル</target>
Expand Down Expand Up @@ -167,6 +172,11 @@
<target state="translated">ワークスペースを読み込んでいます。</target>
<note />
</trans-unit>
<trans-unit id="Log_all_project_or_solution_load_information_to_a_binary_log_file">
<source>Log all project or solution load information to a binary log file.</source>
<target state="new">Log all project or solution load information to a binary log file.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_argument">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the &lt;workspace&gt; argument.</source>
<target state="translated">複数の MSBuild プロジェクト ファイルが '{0}' で見つかりました。使用するものを &lt;workspace&gt; 引数で指定してください。</target>
Expand Down
10 changes: 10 additions & 0 deletions src/xlf/Resources.ko.xlf
Expand Up @@ -52,6 +52,11 @@
<target state="new">Cannot specify the '--folder' option when running analyzers.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_the_folder_option_when_writing_a_binary_log">
<source>Cannot specify the '--folder' option when writing a binary log.</source>
<target state="new">Cannot specify the '--folder' option when writing a binary log.</target>
<note />
</trans-unit>
<trans-unit id="Code_Style">
<source>Code Style</source>
<target state="translated">코드 스타일</target>
Expand Down Expand Up @@ -167,6 +172,11 @@
<target state="translated">작업 영역을 로드하는 중입니다.</target>
<note />
</trans-unit>
<trans-unit id="Log_all_project_or_solution_load_information_to_a_binary_log_file">
<source>Log all project or solution load information to a binary log file.</source>
<target state="new">Log all project or solution load information to a binary log file.</target>
<note />
</trans-unit>
<trans-unit id="Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_argument">
<source>Multiple MSBuild project files found in '{0}'. Specify which to use with the &lt;workspace&gt; argument.</source>
<target state="translated">'{0}'에 여러 MSBuild 프로젝트 파일이 있습니다. &lt;workspace&gt; 인수를 사용하여 사용할 파일을 지정하세요.</target>
Expand Down