Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into roslyn-editorconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeRobich committed May 21, 2020
2 parents 2b88e4e + 6a54c4a commit 9f87221
Show file tree
Hide file tree
Showing 26 changed files with 271 additions and 65 deletions.
4 changes: 2 additions & 2 deletions eng/Version.Details.xml
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Dependencies>
<ProductDependencies>
<Dependency Name="Microsoft.NETCore.Compilers" Version="3.7.0-1.20216.3">
<Dependency Name="Microsoft.NETCore.Compilers" Version="3.7.0-3.20269.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>b1270719390efb196bef99d51a6817b69f39f8a5</Sha>
<Sha>c8d525c5aab8cfdbc6511d5a3a3e1cd4c7481b81</Sha>
</Dependency>
</ProductDependencies>
<ToolsetDependencies>
Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Expand Up @@ -9,7 +9,7 @@
<PropertyGroup>
<MicrosoftBuildVersion>15.3.409</MicrosoftBuildVersion>
<!-- Dependencies from https://github.com/dotnet/roslyn -->
<MicrosoftNETCoreCompilersPackageVersion>3.7.0-1.20216.3</MicrosoftNETCoreCompilersPackageVersion>
<MicrosoftNETCoreCompilersPackageVersion>3.7.0-3.20269.8</MicrosoftNETCoreCompilersPackageVersion>
<MicrosoftExtensionsVersion>3.1.1</MicrosoftExtensionsVersion>
</PropertyGroup>
<!--
Expand Down
9 changes: 6 additions & 3 deletions perf/FormattedFiles.cs
Expand Up @@ -37,7 +37,8 @@ public void FilesFormattedFolder()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand All @@ -52,7 +53,8 @@ public void FilesFormattedProject()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand All @@ -67,7 +69,8 @@ public void FilesFormattedSolution()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand Down
9 changes: 6 additions & 3 deletions perf/NoFilesFormatted.cs
Expand Up @@ -37,7 +37,8 @@ public void NoFilesFormattedFolder()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand All @@ -52,7 +53,8 @@ public void NoFilesFormattedProject()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand All @@ -67,7 +69,8 @@ public void NoFilesFormattedSolution()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand Down
6 changes: 4 additions & 2 deletions perf/RealWorldSolution.cs
Expand Up @@ -39,7 +39,8 @@ public void FilesFormattedSolution()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand All @@ -54,7 +55,8 @@ public void FilesFormattedFolder()
saveFormattedFiles: false,
changesAreErrors: false,
AllFileMatcher,
reportPath: string.Empty);
reportPath: string.Empty,
includeGeneratedFiles: false);
_ = CodeFormatter.FormatWorkspaceAsync(options, EmptyLogger, default).GetAwaiter().GetResult();
}

Expand Down
10 changes: 6 additions & 4 deletions src/CodeFormatter.cs
Expand Up @@ -43,7 +43,7 @@ internal static class CodeFormatter
CancellationToken cancellationToken,
bool createBinaryLog = false)
{
var (workspaceFilePath, workspaceType, logLevel, saveFormattedFiles, _, fileMatcher, reportPath) = options;
var (workspaceFilePath, workspaceType, logLevel, saveFormattedFiles, _, fileMatcher, reportPath, includeGeneratedFiles) = options;
var logWorkspaceWarnings = logLevel == LogLevel.Trace;

logger.LogInformation(string.Format(Resources.Formatting_code_files_in_workspace_0, workspaceFilePath));
Expand All @@ -68,7 +68,7 @@ internal static class CodeFormatter
logger.LogTrace(Resources.Determining_formattable_files);

var (fileCount, formatableFiles) = await DetermineFormattableFiles(
solution, projectPath, fileMatcher, logger, cancellationToken).ConfigureAwait(false);
solution, projectPath, fileMatcher, includeGeneratedFiles, logger, cancellationToken).ConfigureAwait(false);

var determineFilesMS = workspaceStopwatch.ElapsedMilliseconds - loadWorkspaceMS;
logger.LogTrace(Resources.Complete_in_0_ms, determineFilesMS);
Expand Down Expand Up @@ -270,6 +270,7 @@ private static void LogWorkspaceDiagnostics(ILogger logger, bool logWorkspaceWar
Solution solution,
string projectPath,
Matcher fileMatcher,
bool includeGeneratedFiles,
ILogger logger,
CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -299,7 +300,7 @@ private static void LogWorkspaceDiagnostics(ILogger logger, bool logWorkspaceWar

// Get project documents and options with .editorconfig settings applied.
var getProjectDocuments = project.DocumentIds.Select(
documentId => GetDocumentAndOptions(project, documentId, fileMatcher, cancellationToken));
documentId => GetDocumentAndOptions(project, documentId, fileMatcher, includeGeneratedFiles, cancellationToken));
getDocumentsAndOptions.AddRange(getProjectDocuments);
}

Expand Down Expand Up @@ -339,6 +340,7 @@ private static void LogWorkspaceDiagnostics(ILogger logger, bool logWorkspaceWar
Project project,
DocumentId documentId,
Matcher fileMatcher,
bool includeGeneratedFiles,
CancellationToken cancellationToken)
{
var document = project.Solution.GetDocument(documentId);
Expand All @@ -357,7 +359,7 @@ private static void LogWorkspaceDiagnostics(ILogger logger, bool logWorkspaceWar
}

var isGeneratedCode = await GeneratedCodeUtilities.IsGeneratedCodeAsync(syntaxTree, cancellationToken).ConfigureAwait(false);
if (isGeneratedCode)
if (!includeGeneratedFiles && isGeneratedCode)
{
return null;
}
Expand Down
9 changes: 7 additions & 2 deletions src/FormatOptions.cs
Expand Up @@ -14,6 +14,7 @@ internal class FormatOptions
public bool ChangesAreErrors { get; }
public Matcher FileMatcher { get; }
public string? ReportPath { get; }
public bool IncludeGeneratedFiles { get; }

public FormatOptions(
string workspaceFilePath,
Expand All @@ -22,7 +23,8 @@ internal class FormatOptions
bool saveFormattedFiles,
bool changesAreErrors,
Matcher fileMatcher,
string? reportPath)
string? reportPath,
bool includeGeneratedFiles)
{
WorkspaceFilePath = workspaceFilePath;
WorkspaceType = workspaceType;
Expand All @@ -31,6 +33,7 @@ internal class FormatOptions
ChangesAreErrors = changesAreErrors;
FileMatcher = fileMatcher;
ReportPath = reportPath;
IncludeGeneratedFiles = includeGeneratedFiles;
}

public void Deconstruct(
Expand All @@ -40,7 +43,8 @@ internal class FormatOptions
out bool saveFormattedFiles,
out bool changesAreErrors,
out Matcher fileMatcher,
out string? reportPath)
out string? reportPath,
out bool includeGeneratedFiles)
{
workspaceFilePath = WorkspaceFilePath;
workspaceType = WorkspaceType;
Expand All @@ -49,6 +53,7 @@ internal class FormatOptions
changesAreErrors = ChangesAreErrors;
fileMatcher = FileMatcher;
reportPath = ReportPath;
includeGeneratedFiles = IncludeGeneratedFiles;
}
}
}
2 changes: 1 addition & 1 deletion src/Formatters/DocumentFormatter.cs
Expand Up @@ -140,7 +140,7 @@ private IEnumerable<FileChange> GetFileChanges(FormatOptions formatOptions, stri
var changes = formattedText.GetChangeRanges(originalText);
if (workspaceFolder is null)
{
throw new Exception($"Unable to fine directory name for '{workspacePath}'");
throw new Exception($"Unable to find directory name for '{workspacePath}'");
}

foreach (var change in changes)
Expand Down
46 changes: 31 additions & 15 deletions src/Program.cs
Expand Up @@ -22,6 +22,7 @@ internal class Program
private static string[] VerbosityLevels => new[] { "q", "quiet", "m", "minimal", "n", "normal", "d", "detailed", "diag", "diagnostic" };
internal const int UnhandledExceptionExitCode = 1;
internal const int CheckFailedExitCode = 2;
internal const int UnableToLocateMSBuildExitCode = 3;

private static async Task<int> Main(string[] args)
{
Expand Down Expand Up @@ -57,6 +58,11 @@ private static async Task<int> Main(string[] args)
{
Argument = new Argument<string?>() { Arity = ArgumentArity.ExactlyOne }
},
new Option(new[] { "--include-generated" }, Resources.Include_generated_code_files_in_formatting_operations)
{
Argument = new Argument<bool>(),
IsHidden = true
},
};

rootCommand.Description = "dotnet-format";
Expand All @@ -66,7 +72,7 @@ private static async Task<int> Main(string[] args)
return await rootCommand.InvokeAsync(args);
}

public static async Task<int> Run(string? folder, string? workspace, string? verbosity, bool check, string[] include, string[] exclude, string? report, IConsole console = null!)
public static async Task<int> Run(string? folder, string? workspace, string? verbosity, bool check, string[] include, string[] exclude, string? report, bool includeGenerated, IConsole console = null!)
{
// Setup logging.
var serviceCollection = new ServiceCollection();
Expand Down Expand Up @@ -127,20 +133,29 @@ public static async Task<int> Run(string? folder, string? workspace, string? ver

Environment.CurrentDirectory = workspaceDirectory;

// Since we are running as a dotnet tool we should be able to find an instance of
// MSBuild in a .NET Core SDK.
var msBuildInstance = Build.Locator.MSBuildLocator.QueryVisualStudioInstances().First();

// Since we do not inherit msbuild.deps.json when referencing the SDK copy
// of MSBuild and because the SDK no longer ships with version matched assemblies, we
// register an assembly loader that will load assemblies from the msbuild path with
// equal or higher version numbers than requested.
LooseVersionAssemblyLoader.Register(msBuildInstance.MSBuildPath);
Build.Locator.MSBuildLocator.RegisterInstance(msBuildInstance);

if (logLevel == LogLevel.Trace)
try
{
// Since we are running as a dotnet tool we should be able to find an instance of
// MSBuild in a .NET Core SDK.
var msBuildInstance = Build.Locator.MSBuildLocator.QueryVisualStudioInstances().First();

// Since we do not inherit msbuild.deps.json when referencing the SDK copy
// of MSBuild and because the SDK no longer ships with version matched assemblies, we
// register an assembly loader that will load assemblies from the msbuild path with
// equal or higher version numbers than requested.
LooseVersionAssemblyLoader.Register(msBuildInstance.MSBuildPath);
Build.Locator.MSBuildLocator.RegisterInstance(msBuildInstance);

if (logLevel == LogLevel.Trace)
{
logger.LogInformation(Resources.Using_msbuildexe_located_in_0, msBuildInstance.MSBuildPath);
}
}
catch (InvalidOperationException)
{
logger.LogInformation(Resources.Using_msbuildexe_located_in_0, msBuildInstance.MSBuildPath);
// Unable to find MSBuild instance.
logger.LogError(Resources.Unable_to_locate_MSBuild_Ensure_the_NET_SDK_was_installed_with_the_official_installer);
return UnableToLocateMSBuildExitCode;
}

var fileMatcher = SourceFileMatcher.CreateMatcher(include, exclude);
Expand All @@ -152,7 +167,8 @@ public static async Task<int> Run(string? folder, string? workspace, string? ver
saveFormattedFiles: !check,
changesAreErrors: check,
fileMatcher,
reportPath: report);
reportPath: report,
includeGenerated);

var formatResult = await CodeFormatter.FormatWorkspaceAsync(
formatOptions,
Expand Down
60 changes: 33 additions & 27 deletions src/Resources.resx
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
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
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>
Expand All @@ -26,36 +26,36 @@
<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
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
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
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
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
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
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
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -222,4 +222,10 @@
<data name="Using_msbuildexe_located_in_0" xml:space="preserve">
<value>Using MSBuild.exe located in '{0}'</value>
</data>
<data name="Unable_to_locate_MSBuild_Ensure_the_NET_SDK_was_installed_with_the_official_installer" xml:space="preserve">
<value>Unable to locate MSBuild. Ensure the .NET SDK was installed with the official installer.</value>
</data>
<data name="Include_generated_code_files_in_formatting_operations" xml:space="preserve">
<value>Include generated code files in formatting operations.</value>
</data>
</root>

0 comments on commit 9f87221

Please sign in to comment.