Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/BuiltInTools/dotnet-format/CodeFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Immutable;
Expand Down Expand Up @@ -38,7 +38,7 @@ public static async Task<WorkspaceFormatResult> FormatWorkspaceAsync(

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

if (workspace is null)
{
Expand Down Expand Up @@ -126,7 +126,8 @@ private static Workspace OpenFolderWorkspace(string workspacePath, SourceFileMat
string? binaryLogPath,
bool logWorkspaceWarnings,
ILogger logger,
CancellationToken cancellationToken)
CancellationToken cancellationToken,
string? targetFramework = null)
{
if (requiresSemantics &&
!noRestore &&
Expand All @@ -135,7 +136,7 @@ await DotNetHelper.PerformRestoreAsync(solutionOrProjectPath, logger) != 0)
throw new Exception("Restore operation failed.");
}

return await MSBuildWorkspaceLoader.LoadAsync(solutionOrProjectPath, workspaceType, binaryLogPath, logWorkspaceWarnings, logger, cancellationToken);
return await MSBuildWorkspaceLoader.LoadAsync(solutionOrProjectPath, workspaceType, binaryLogPath, logWorkspaceWarnings, logger, cancellationToken, targetFramework);
}

private static async Task<Solution> RunCodeFormattersAsync(
Expand Down
12 changes: 12 additions & 0 deletions src/BuiltInTools/dotnet-format/Commands/FormatCommandCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ internal static class FormatCommandCommon
{
Description = Resources.Doesnt_execute_an_implicit_restore_before_formatting,
};
internal static readonly Option<string> FrameworkOption = new Option<string>("--framework", "-f")
{
HelpName = "framework",
Description = Resources.The_target_framework_to_use_when_loading_the_workspace,
};
internal static readonly Option<bool> VerifyNoChanges = new("--verify-no-changes")
{
Description = Resources.Verify_no_formatting_changes_would_be_performed_Terminates_with_a_non_zero_exit_code_if_any_files_would_have_been_formatted,
Expand Down Expand Up @@ -115,6 +120,7 @@ public static void AddCommonOptions(this Command command)
{
command.Arguments.Add(SlnOrProjectArgument);
command.Options.Add(NoRestoreOption);
command.Options.Add(FrameworkOption);
command.Options.Add(VerifyNoChanges);
command.Options.Add(IncludeOption);
command.Options.Add(ExcludeOption);
Expand Down Expand Up @@ -211,6 +217,12 @@ public static FormatOptions ParseCommonOptions(this ParseResult parseResult, For
}
}

if (parseResult.GetResult(FrameworkOption) is not null &&
parseResult.GetValue(FrameworkOption) is string { Length: > 0 } framework)
{
formatOptions = formatOptions with { TargetFramework = framework };
}

return formatOptions;

static void HandleStandardInput(ILogger logger, ref string[] include, ref string[] exclude)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal static Command GetCommand()
command.AddCommonOptions();
command.Validators.Add(EnsureFolderNotSpecifiedWithNoRestore);
command.Validators.Add(EnsureFolderNotSpecifiedWhenLoggingBinlog);
command.Validators.Add(EnsureFolderNotSpecifiedWithFramework);
command.Action = s_formattingHandler;
return command;
}
Expand All @@ -46,6 +47,16 @@ internal static void EnsureFolderNotSpecifiedWhenLoggingBinlog(CommandResult sym
}
}

internal static void EnsureFolderNotSpecifiedWithFramework(CommandResult symbolResult)
{
var folder = symbolResult.GetValue(FolderOption);
var framework = symbolResult.GetResult(FrameworkOption);
if (folder && framework is not null && !framework.Implicit)
{
symbolResult.AddError(Resources.Cannot_specify_the_folder_option_with_framework);
}
}

private class FormatWhitespaceHandler : AsynchronousCommandLineAction
{
public override async Task<int> InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)
Expand Down
6 changes: 4 additions & 2 deletions src/BuiltInTools/dotnet-format/FormatOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ internal record FormatOptions(
SourceFileMatcher FileMatcher,
string? ReportPath,
string? BinaryLogPath,
bool IncludeGeneratedFiles)
bool IncludeGeneratedFiles,
string? TargetFramework)
{
public static FormatOptions Instance = new(
WorkspaceFilePath: null!, // must be supplied
Expand All @@ -39,6 +40,7 @@ internal record FormatOptions(
FileMatcher: SourceFileMatcher.CreateMatcher(Array.Empty<string>(), Array.Empty<string>()),
ReportPath: null,
BinaryLogPath: null,
IncludeGeneratedFiles: false);
IncludeGeneratedFiles: false,
TargetFramework: null);
}
}
6 changes: 6 additions & 0 deletions src/BuiltInTools/dotnet-format/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@
<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>
<data name="The_target_framework_to_use_when_loading_the_workspace" xml:space="preserve">
<value>The target framework (TFM) to use when loading the workspace. Restricts restore and analysis to the specified framework only.</value>
</data>
<data name="Cannot_specify_the_folder_option_with_framework" xml:space="preserve">
<value>Cannot specify the '--folder' option with '--framework'.</value>
</data>
<data name="SolutionOrProjectArgumentName" xml:space="preserve">
<value>PROJECT | SOLUTION</value>
</data>
Expand Down
3 changes: 2 additions & 1 deletion src/BuiltInTools/dotnet-format/Utilities/DotNetHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Logging;
Expand All @@ -7,6 +7,7 @@ namespace Microsoft.CodeAnalysis.Tools.Utilities
{
internal static class DotNetHelper
{

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove blank line

Suggested change

public static async Task<int> PerformRestoreAsync(string workspaceFilePath, ILogger logger)
{
var processInfo = ProcessRunner.CreateProcess("dotnet", $"restore \"{workspaceFilePath}\"", captureOutput: true, displayWindow: false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ internal static class MSBuildWorkspaceLoader
string? binaryLogPath,
bool logWorkspaceWarnings,
ILogger logger,
CancellationToken cancellationToken)
CancellationToken cancellationToken,
string? targetFramework = null)
Comment on lines +21 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep cancellationToken as the last parameter.

{
var properties = new Dictionary<string, string>(StringComparer.Ordinal)
{
Expand All @@ -28,6 +29,11 @@ internal static class MSBuildWorkspaceLoader
{ "AlwaysCompileMarkupFilesInSeparateDomain", bool.FalseString },
};

if (targetFramework is not null)
{
properties["TargetFramework"] = targetFramework;
}

var workspace = MSBuildWorkspace.Create(properties);

Build.Framework.ILogger? binlog = null;
Expand Down
10 changes: 10 additions & 0 deletions src/BuiltInTools/dotnet-format/xlf/Resources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/BuiltInTools/dotnet-format/xlf/Resources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/BuiltInTools/dotnet-format/xlf/Resources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/BuiltInTools/dotnet-format/xlf/Resources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/BuiltInTools/dotnet-format/xlf/Resources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/BuiltInTools/dotnet-format/xlf/Resources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/BuiltInTools/dotnet-format/xlf/Resources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading