Skip to content

Commit

Permalink
Merge pull request #18840 from jmarolf/feature/add-code-cleanup-comma…
Browse files Browse the repository at this point in the history
…nd-handlers
  • Loading branch information
jmarolf committed Jul 15, 2021
2 parents 22ea0f5 + 2b0dba6 commit 1c0be9c
Show file tree
Hide file tree
Showing 53 changed files with 2,519 additions and 59 deletions.
5 changes: 3 additions & 2 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
/src/Assets/TestPackages/PackageLibraryTransitiveDependency/ @captainsafia, @pranavkm, @mkArtakMSFT
/src/src/Assets/TestProjects/Razor*/ @captainsafia, @pranavkm, @mkArtakMSFT

# Area-Cleanup
/src/Cli/dotnet/commands/dotnet-cleanup @jmarolf @JoeRobich
# Area-Format
/src/Cli/dotnet/commands/dotnet-format @jmarolf, @JoeRobich
/src/Tests/dotnet-format.Tests @jmarolf, @JoeRobich

# Area-NuGet
/src/Cli/dotnet/commands/dotnet-add/dotnet-add-package @dotnet/nuget-team
Expand Down
8 changes: 4 additions & 4 deletions src/Cli/dotnet/BuiltInCommandsCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public static class BuiltInCommandsCatalog
// aka.ms target: https://docs.microsoft.com/dotnet/articles/core/tools/dotnet-clean
DocLink = "https://aka.ms/dotnet-clean"
},
["cleanup"] = new BuiltInCommandMetadata
["format"] = new BuiltInCommandMetadata
{
Command = CleanupCommand.Run,
// aka.ms target: https://docs.microsoft.com/dotnet/articles/core/tools/dotnet-cleanup
DocLink = "https://aka.ms/dotnet-cleanup"
Command = FormatCommand.Run,
// aka.ms target: https://docs.microsoft.com/dotnet/articles/core/tools/dotnet-format
DocLink = "https://aka.ms/dotnet-format"
},
["fsi"] = new BuiltInCommandMetadata
{
Expand Down
12 changes: 10 additions & 2 deletions src/Cli/dotnet/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
using System.CommandLine.Invocation;
using System.CommandLine.IO;
using System.Reflection;

using Microsoft.DotNet.Cli.Format;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools;
using Microsoft.DotNet.Tools.Help;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Tools.New;
using Microsoft.DotNet.Tools.NuGet;

using Command = System.CommandLine.Command;
using ICommand = System.CommandLine.ICommand;

Expand All @@ -30,7 +33,7 @@ public static class Parser
BuildCommandParser.GetCommand(),
BuildServerCommandParser.GetCommand(),
CleanCommandParser.GetCommand(),
CleanupCommandParser.GetCommand(),
FormatCommandParser.GetCommand(),
CompleteCommandParser.GetCommand(),
FsiCommandParser.GetCommand(),
ListCommandParser.GetCommand(),
Expand Down Expand Up @@ -173,7 +176,12 @@ internal class DotnetHelpBuilder : HelpBuilder
{
windowWidth = int.MaxValue;
}
return new DotnetHelpBuilder(new SystemConsole(), windowWidth);
DotnetHelpBuilder dotnetHelpBuilder = new DotnetHelpBuilder(new SystemConsole(), windowWidth);
dotnetHelpBuilder.Customize(FormatCommandCommon.DiagnosticsOption, defaultValue: Tools.Format.LocalizableStrings.whichever_ids_are_listed_in_the_editorconfig_file);
dotnetHelpBuilder.Customize(FormatCommandCommon.IncludeOption, defaultValue: Tools.Format.LocalizableStrings.all_files_in_the_solution_or_project);
dotnetHelpBuilder.Customize(FormatCommandCommon.ExcludeOption, defaultValue: Tools.Format.LocalizableStrings.none);
return dotnetHelpBuilder;
});

public override void Write(ICommand command)
Expand Down
17 changes: 0 additions & 17 deletions src/Cli/dotnet/commands/dotnet-cleanup/CleanupCommand.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/Cli/dotnet/commands/dotnet-cleanup/CleanupCommandParser.cs

This file was deleted.

29 changes: 29 additions & 0 deletions src/Cli/dotnet/commands/dotnet-format/FormatCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.Collections.Generic;
using System.CommandLine.Parsing;

using Microsoft.DotNet.Cli.Format;

namespace Microsoft.DotNet.Cli
{
public class FormatCommand : AbstractFormatCommand
{
protected override string ParseFrom => "dotnet format";

protected override List<string> AddArgs(ParseResult parseResult)
{
var dotnetFormatArgs = new List<string>();
dotnetFormatArgs.AddProjectOrSolutionDotnetFormatArgs(parseResult);
dotnetFormatArgs.AddCommonDotnetFormatArgs(parseResult);
dotnetFormatArgs.AddFormattingDotnetFormatArgs(parseResult);
dotnetFormatArgs.AddStyleDotnetFormatArgs(parseResult);
dotnetFormatArgs.AddAnalyzerDotnetFormatArgs(parseResult);
return dotnetFormatArgs;
}

public static int Run(string[] args) => new FormatCommand().RunCommand(args);
}
}
40 changes: 40 additions & 0 deletions src/Cli/dotnet/commands/dotnet-format/FormatCommandParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.CommandLine;
using System.CommandLine.Invocation;

using Microsoft.DotNet.Cli.Format;
using LocalizableStrings = Microsoft.DotNet.Tools.Format.LocalizableStrings;
using static Microsoft.DotNet.Cli.Format.FormatCommandCommon;
using System.Threading.Tasks;

namespace Microsoft.DotNet.Cli
{
internal static partial class FormatCommandParser
{
private static readonly FormatCommandDefaultHandler s_formatCommandHandler = new();

public static Command GetCommand()
{
var formatCommand = new Command("format", LocalizableStrings.Formats_code_to_match_editorconfig_settings)
{
FormatWhitespaceCommandParser.GetCommand(),
FormatStyleCommandParser.GetCommand(),
FormatAnalyzersCommandParser.GetCommand(),
DiagnosticsOption,
SeverityOption,
};
formatCommand.AddCommonOptions();
formatCommand.Handler = s_formatCommandHandler;
return formatCommand;
}

class FormatCommandDefaultHandler : ICommandHandler
{
public Task<int> InvokeAsync(InvocationContext context)
=> Task.FromResult(new FormatCommand().FromArgs(context.ParseResult).Execute());
}
}
}
171 changes: 171 additions & 0 deletions src/Cli/dotnet/commands/dotnet-format/LocalizableStrings.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Accepts_a_file_path_which_if_provided_will_produce_a_json_report_in_the_given_directory" xml:space="preserve">
<value>Accepts a file path, which if provided, will produce a json report in the given directory.</value>
</data>
<data name="all_files_in_the_solution_or_project" xml:space="preserve">
<value>all files in the solution or project</value>
</data>
<data name="A_list_of_relative_file_or_folder_paths_to_exclude_from_formatting" xml:space="preserve">
<value>A list of relative file or folder paths to exclude from formatting.</value>
</data>
<data name="A_list_of_relative_file_or_folder_paths_to_include_in_formatting_All_files_are_formatted_if_empty" xml:space="preserve">
<value>A list of relative file or folder paths to include in formatting. All files are formatted if empty.</value>
</data>
<data name="A_space_separated_list_of_diagnostic_ids_to_use_as_a_filter_when_fixing_code_style_or_3rd_party_issues" xml:space="preserve">
<value>A space separated list of diagnostic ids to use as a filter when fixing code style or 3rd party issues.</value>
</data>
<data name="Doesnt_execute_an_implicit_restore_before_formatting" xml:space="preserve">
<value>Doesn't execute an implicit restore before formatting.</value>
</data>
<data name="Formats_code_to_match_editorconfig_settings" xml:space="preserve">
<value>Formats code to match editorconfig settings.</value>
</data>
<data name="Format_files_generated_by_the_SDK" xml:space="preserve">
<value>Format files generated by the SDK.</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="none" xml:space="preserve">
<value>none</value>
</data>
<data name="Run_3rd_party_analyzers_ and_apply_fixes" xml:space="preserve">
<value>Run 3rd party analyzers and apply fixes.</value>
</data>
<data name="Run_code_style_analyzers_and_apply_fixes" xml:space="preserve">
<value>Run code style analyzers and apply fixes.</value>
</data>
<data name="Run_whitespace_formatting" xml:space="preserve">
<value>Run whitespace formatting.</value>
</data>
<data name="Set_the_verbosity_level_Allowed_values_are_quiet_minimal_normal_detailed_and_diagnostic" xml:space="preserve">
<value>Set the verbosity level. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]</value>
</data>
<data name="The_severity_of_diagnostics_to_fix_Allowed_values_are_info_warn_and_error" xml:space="preserve">
<value>The severity of diagnostics to fix. Allowed values are info, warn, and error.</value>
</data>
<data name="Verify_no_formatting_changes_would_be_performed_Terminates_with_a_non_zero_exit_code_if_any_files_would_have_been_formatted" xml:space="preserve">
<value>Verify no formatting changes would be performed. Terminates with a non-zero exit code if any files would have been formatted.</value>
</data>
<data name="whichever_ids_are_listed_in_the_editorconfig_file" xml:space="preserve">
<value>whichever ids are listed in the editorconfig file</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.Collections.Generic;
using System.CommandLine.Parsing;

using Microsoft.DotNet.Cli.Format;

namespace Microsoft.DotNet.Cli
{
internal class FormatAnalyzersCommand : AbstractFormatCommand
{
protected override string ParseFrom => "dotnet format analyzers";

protected override List<string> AddArgs(ParseResult parseResult)
{
var dotnetFormatArgs = new List<string>();
dotnetFormatArgs.AddProjectOrSolutionDotnetFormatArgs(parseResult);
dotnetFormatArgs.AddCommonDotnetFormatArgs(parseResult);
dotnetFormatArgs.AddAnalyzerDotnetFormatArgs(parseResult);
return dotnetFormatArgs;
}

public static int Run(string[] args) => new FormatAnalyzersCommand().RunCommand(args);
}
}
Loading

0 comments on commit 1c0be9c

Please sign in to comment.