未验证 提交 a23e74b6 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #32910 from sharwell/format-dry-run

dotnet-format --dry-run
......@@ -21,9 +21,9 @@ namespace Microsoft.CodeAnalysis.Tools.CodeFormatter
{
internal static class CodeFormatter
{
const int MaxLoggedWorkspaceWarnings = 5;
private const int MaxLoggedWorkspaceWarnings = 5;
public static async Task<int> FormatWorkspaceAsync(ILogger logger, string solutionOrProjectPath, bool isSolution, bool logAllWorkspaceWarnings, CancellationToken cancellationToken)
public static async Task<int> FormatWorkspaceAsync(ILogger logger, string solutionOrProjectPath, bool isSolution, bool logAllWorkspaceWarnings, bool saveFormattedFiles, CancellationToken cancellationToken)
{
logger.LogInformation(string.Format(Resources.Formatting_code_files_in_workspace_0, solutionOrProjectPath));
......@@ -61,7 +61,7 @@ public static async Task<int> FormatWorkspaceAsync(ILogger logger, string soluti
logger.LogTrace(Resources.Workspace_loaded_in_0_ms, workspaceStopwatch.ElapsedMilliseconds);
workspaceStopwatch.Restart();
exitCode = await FormatFilesInWorkspaceAsync(logger, workspace, projectPath, codingConventionsManager, cancellationToken).ConfigureAwait(false);
exitCode = await FormatFilesInWorkspaceAsync(logger, workspace, projectPath, codingConventionsManager, saveFormattedFiles, cancellationToken).ConfigureAwait(false);
logger.LogDebug(Resources.Format_complete_in_0_ms, workspaceStopwatch.ElapsedMilliseconds);
}
......@@ -92,7 +92,7 @@ void LogWorkspaceWarnings(object sender, WorkspaceDiagnosticEventArgs args)
}
}
private static async Task<int> FormatFilesInWorkspaceAsync(ILogger logger, Workspace workspace, string projectPath, ICodingConventionsManager codingConventionsManager, CancellationToken cancellationToken)
private static async Task<int> FormatFilesInWorkspaceAsync(ILogger logger, Workspace workspace, string projectPath, ICodingConventionsManager codingConventionsManager, bool saveFormattedFiles, CancellationToken cancellationToken)
{
var projectIds = workspace.CurrentSolution.ProjectIds.ToImmutableArray();
var optionsApplier = new EditorConfigOptionsApplier();
......@@ -115,7 +115,7 @@ private static async Task<int> FormatFilesInWorkspaceAsync(ILogger logger, Works
logger.LogInformation(Resources.Formatting_code_files_in_project_0, project.Name);
var formattedSolution = await FormatFilesInProjectAsync(logger, project, codingConventionsManager, optionsApplier, cancellationToken).ConfigureAwait(false);
if (!workspace.TryApplyChanges(formattedSolution))
if (saveFormattedFiles && !workspace.TryApplyChanges(formattedSolution))
{
logger.LogError(Resources.Failed_to_save_formatting_changes);
return 1;
......
......@@ -14,11 +14,11 @@
namespace Microsoft.CodeAnalysis.Tools.CodeFormatter
{
class Program
internal class Program
{
static readonly string[] _verbosityLevels = new[] { "q", "quiet", "m", "minimal", "n", "normal", "d", "detailed", "diag", "diagnostic" };
private static readonly string[] _verbosityLevels = new[] { "q", "quiet", "m", "minimal", "n", "normal", "d", "detailed", "diag", "diagnostic" };
static async Task<int> Main(string[] args)
private static async Task<int> Main(string[] args)
{
var parser = new CommandLineBuilder(new Command("dotnet-format", handler: CommandHandler.Create(typeof(Program).GetMethod(nameof(Run)))))
.UseParseDirective()
......@@ -30,13 +30,14 @@ static async Task<int> Main(string[] args)
.UseExceptionHandler()
.AddOption(new Option(new[] { "-w", "--workspace" }, Resources.The_solution_or_project_file_to_operate_on_If_a_file_is_not_specified_the_command_will_search_the_current_directory_for_one, new Argument<string>(() => null)))
.AddOption(new Option(new[] { "-v", "--verbosity" }, Resources.Set_the_verbosity_level_Allowed_values_are_quiet_minimal_normal_detailed_and_diagnostic, new Argument<string>() { Arity = ArgumentArity.ExactlyOne }.FromAmong(_verbosityLevels)))
.AddOption(new Option(new[] { "--dry-run" }, Resources.Format_files_but_do_not_save_changes_to_disk, new Argument<bool>()))
.UseVersionOption()
.Build();
return await parser.InvokeAsync(args).ConfigureAwait(false);
}
public static async Task<int> Run(string workspace, string verbosity, IConsole console = null)
public static async Task<int> Run(string workspace, string verbosity, bool dryRun, IConsole console = null)
{
var serviceCollection = new ServiceCollection();
var logLevel = GetLogLevel(verbosity);
......@@ -65,7 +66,12 @@ public static async Task<int> Run(string workspace, string verbosity, IConsole c
MSBuildCoreLoader.LoadDotnetInstance(workspaceDirectory);
return await CodeFormatter.FormatWorkspaceAsync(
logger, workspacePath, isSolution, logAllWorkspaceWarnings: logLevel == LogLevel.Trace, cancellationTokenSource.Token).ConfigureAwait(false);
logger,
workspacePath,
isSolution,
logAllWorkspaceWarnings: logLevel == LogLevel.Trace,
saveFormattedFiles: !dryRun,
cancellationTokenSource.Token).ConfigureAwait(false);
}
catch (FileNotFoundException fex)
{
......
{
"profiles": {
"dotnet-format (--dry-run)": {
"commandName": "Project",
"commandLineArgs": "-w Roslyn.sln --dry-run",
"workingDirectory": "$(SolutionDir)"
},
"dotnet-format": {
"commandName": "Project",
"commandLineArgs": "-w Roslyn.sln",
"workingDirectory": "$(SolutionDir)"
}
}
}
......@@ -117,6 +117,15 @@ internal class Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Format files, but do not save changes to disk..
/// </summary>
internal static string Format_files_but_do_not_save_changes_to_disk {
get {
return ResourceManager.GetString("Format_files_but_do_not_save_changes_to_disk", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Formatting code file &apos;{0}&apos;..
/// </summary>
......
......@@ -180,4 +180,7 @@
<data name="Maximum_number_of_workspace_warnings_to_log_has_been_reached_Set_the_verbosity_option_to_the_diagnostic_level_to_see_all_warnings" xml:space="preserve">
<value>Maximum number of workspace warnings to log has been reached. Set the --verbosity option to the 'diagnostic' level to see all warnings.</value>
</data>
<data name="Format_files_but_do_not_save_changes_to_disk" xml:space="preserve">
<value>Format files, but do not save changes to disk.</value>
</data>
</root>
\ No newline at end of file
......@@ -32,6 +32,11 @@
<target state="new">Format complete in {0}ms.</target>
<note />
</trans-unit>
<trans-unit id="Format_files_but_do_not_save_changes_to_disk">
<source>Format files, but do not save changes to disk.</source>
<target state="new">Format files, but do not save changes to disk.</target>
<note />
</trans-unit>
<trans-unit id="Formatting_code_file_0">
<source>Formatting code file '{0}'.</source>
<target state="new">Formatting code file '{0}'.</target>
......
......@@ -32,6 +32,11 @@
<target state="new">Format complete in {0}ms.</target>
<note />
</trans-unit>
<trans-unit id="Format_files_but_do_not_save_changes_to_disk">
<source>Format files, but do not save changes to disk.</source>
<target state="new">Format files, but do not save changes to disk.</target>
<note />
</trans-unit>
<trans-unit id="Formatting_code_file_0">
<source>Formatting code file '{0}'.</source>
<target state="new">Formatting code file '{0}'.</target>
......
......@@ -32,6 +32,11 @@
<target state="new">Format complete in {0}ms.</target>
<note />
</trans-unit>
<trans-unit id="Format_files_but_do_not_save_changes_to_disk">
<source>Format files, but do not save changes to disk.</source>
<target state="new">Format files, but do not save changes to disk.</target>
<note />
</trans-unit>
<trans-unit id="Formatting_code_file_0">
<source>Formatting code file '{0}'.</source>
<target state="new">Formatting code file '{0}'.</target>
......
......@@ -32,6 +32,11 @@
<target state="new">Format complete in {0}ms.</target>
<note />
</trans-unit>
<trans-unit id="Format_files_but_do_not_save_changes_to_disk">
<source>Format files, but do not save changes to disk.</source>
<target state="new">Format files, but do not save changes to disk.</target>
<note />
</trans-unit>
<trans-unit id="Formatting_code_file_0">
<source>Formatting code file '{0}'.</source>
<target state="new">Formatting code file '{0}'.</target>
......
......@@ -32,6 +32,11 @@
<target state="new">Format complete in {0}ms.</target>
<note />
</trans-unit>
<trans-unit id="Format_files_but_do_not_save_changes_to_disk">
<source>Format files, but do not save changes to disk.</source>
<target state="new">Format files, but do not save changes to disk.</target>
<note />
</trans-unit>
<trans-unit id="Formatting_code_file_0">
<source>Formatting code file '{0}'.</source>
<target state="new">Formatting code file '{0}'.</target>
......
......@@ -32,6 +32,11 @@
<target state="new">Format complete in {0}ms.</target>
<note />
</trans-unit>
<trans-unit id="Format_files_but_do_not_save_changes_to_disk">
<source>Format files, but do not save changes to disk.</source>
<target state="new">Format files, but do not save changes to disk.</target>
<note />
</trans-unit>
<trans-unit id="Formatting_code_file_0">
<source>Formatting code file '{0}'.</source>
<target state="new">Formatting code file '{0}'.</target>
......
......@@ -32,6 +32,11 @@
<target state="new">Format complete in {0}ms.</target>
<note />
</trans-unit>
<trans-unit id="Format_files_but_do_not_save_changes_to_disk">
<source>Format files, but do not save changes to disk.</source>
<target state="new">Format files, but do not save changes to disk.</target>
<note />
</trans-unit>
<trans-unit id="Formatting_code_file_0">
<source>Formatting code file '{0}'.</source>
<target state="new">Formatting code file '{0}'.</target>
......
......@@ -32,6 +32,11 @@
<target state="new">Format complete in {0}ms.</target>
<note />
</trans-unit>
<trans-unit id="Format_files_but_do_not_save_changes_to_disk">
<source>Format files, but do not save changes to disk.</source>
<target state="new">Format files, but do not save changes to disk.</target>
<note />
</trans-unit>
<trans-unit id="Formatting_code_file_0">
<source>Formatting code file '{0}'.</source>
<target state="new">Formatting code file '{0}'.</target>
......
......@@ -32,6 +32,11 @@
<target state="new">Format complete in {0}ms.</target>
<note />
</trans-unit>
<trans-unit id="Format_files_but_do_not_save_changes_to_disk">
<source>Format files, but do not save changes to disk.</source>
<target state="new">Format files, but do not save changes to disk.</target>
<note />
</trans-unit>
<trans-unit id="Formatting_code_file_0">
<source>Formatting code file '{0}'.</source>
<target state="new">Formatting code file '{0}'.</target>
......
......@@ -32,6 +32,11 @@
<target state="new">Format complete in {0}ms.</target>
<note />
</trans-unit>
<trans-unit id="Format_files_but_do_not_save_changes_to_disk">
<source>Format files, but do not save changes to disk.</source>
<target state="new">Format files, but do not save changes to disk.</target>
<note />
</trans-unit>
<trans-unit id="Formatting_code_file_0">
<source>Formatting code file '{0}'.</source>
<target state="new">Formatting code file '{0}'.</target>
......
......@@ -32,6 +32,11 @@
<target state="new">Format complete in {0}ms.</target>
<note />
</trans-unit>
<trans-unit id="Format_files_but_do_not_save_changes_to_disk">
<source>Format files, but do not save changes to disk.</source>
<target state="new">Format files, but do not save changes to disk.</target>
<note />
</trans-unit>
<trans-unit id="Formatting_code_file_0">
<source>Formatting code file '{0}'.</source>
<target state="new">Formatting code file '{0}'.</target>
......
......@@ -32,6 +32,11 @@
<target state="new">Format complete in {0}ms.</target>
<note />
</trans-unit>
<trans-unit id="Format_files_but_do_not_save_changes_to_disk">
<source>Format files, but do not save changes to disk.</source>
<target state="new">Format files, but do not save changes to disk.</target>
<note />
</trans-unit>
<trans-unit id="Formatting_code_file_0">
<source>Formatting code file '{0}'.</source>
<target state="new">Formatting code file '{0}'.</target>
......
......@@ -32,6 +32,11 @@
<target state="new">Format complete in {0}ms.</target>
<note />
</trans-unit>
<trans-unit id="Format_files_but_do_not_save_changes_to_disk">
<source>Format files, but do not save changes to disk.</source>
<target state="new">Format files, but do not save changes to disk.</target>
<note />
</trans-unit>
<trans-unit id="Formatting_code_file_0">
<source>Formatting code file '{0}'.</source>
<target state="new">Formatting code file '{0}'.</target>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册