Skip to content

Commit

Permalink
Merge pull request #710 from JoeRobich/update-options
Browse files Browse the repository at this point in the history
Remove deprecated options and aliases
  • Loading branch information
JoeRobich committed Jun 25, 2020
2 parents ea55ed1 + c0ca2c6 commit 95024cb
Show file tree
Hide file tree
Showing 26 changed files with 418 additions and 1,067 deletions.
96 changes: 13 additions & 83 deletions src/FormatCommand.cs
Expand Up @@ -9,125 +9,55 @@ namespace Microsoft.CodeAnalysis.Tools
{
internal static class FormatCommand
{
internal static string[] VerbosityLevels => new[] { "q", "quiet", "m", "minimal", "n", "normal", "d", "detailed", "diag", "diagnostic" };
internal static string[] SeverityLevels => new[] { FixSeverity.Info, FixSeverity.Warn, FixSeverity.Error };

internal static RootCommand CreateCommandLineOptions()
{
var rootCommand = new RootCommand
{
new Argument<string>("project")
new Argument<string?>("workspace")
{
Arity = ArgumentArity.ZeroOrOne,
Description = 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 Option(new[] { "--folder", "-f" }, Resources.Whether_to_treat_the_project_path_as_a_folder_of_files)
{
Argument = new Argument<string?>(() => null) { Arity = ArgumentArity.ZeroOrOne },
},
new Option(new[] { "--workspace", "-w" }, Resources.The_solution_or_project_file_to_operate_on_If_a_file_is_not_specified_the_command_will_search_the_current_directory_for_one)
{
Argument = new Argument<string?>(() => null),
IsHidden = true
},
Description = Resources.A_path_to_a_solution_file_a_project_file_or_a_folder_containing_a_solution_or_project_file_If_a_path_is_not_specified_then_the_current_directory_is_used
}.LegalFilePathsOnly(),
new Option(new[] { "--folder", "-f" }, Resources.Whether_to_treat_the_workspace_argument_as_a_simple_folder_of_files),
new Option(new[] { "--fix-style", "-fs" }, Resources.Run_code_style_analyzer_and_apply_fixes)
{
Argument = new Argument<string?>("severity") { Arity = ArgumentArity.ZeroOrOne }
Argument = new Argument<string?>("severity") { Arity = ArgumentArity.ZeroOrOne }.FromAmong(SeverityLevels)
},
new Option(new[] { "--fix-analyzers", "-fa" }, Resources.Run_code_style_analyzer_and_apply_fixes)
{
Argument = new Argument<string?>("severity") { Arity = ArgumentArity.ZeroOrOne }
Argument = new Argument<string?>("severity") { Arity = ArgumentArity.ZeroOrOne }.FromAmong(SeverityLevels)
},
new Option(new[] { "--include", "--files" }, Resources.A_list_of_relative_file_or_folder_paths_to_include_in_formatting_All_files_are_formatted_if_empty)
new Option(new[] { "--include" }, Resources.A_list_of_relative_file_or_folder_paths_to_include_in_formatting_All_files_are_formatted_if_empty)
{
Argument = new Argument<string[]>(() => Array.Empty<string>())
},
new Option(new[] { "--exclude" }, Resources.A_list_of_relative_file_or_folder_paths_to_exclude_from_formatting)
{
Argument = new Argument<string[]>(() => Array.Empty<string>())
},
new Option(new[] { "--check", "--dry-run" }, Resources.Formats_files_without_saving_changes_to_disk_Terminates_with_a_non_zero_exit_code_if_any_files_were_formatted)
{
Argument = new Argument<bool>()
},
new Option(new[] { "--check" }, Resources.Formats_files_without_saving_changes_to_disk_Terminates_with_a_non_zero_exit_code_if_any_files_were_formatted),
new Option(new[] { "--report" }, Resources.Accepts_a_file_path_which_if_provided_will_produce_a_format_report_json_file_in_the_given_directory)
{
Argument = new Argument<string?>(() => null)
Argument = new Argument<string?>(() => null).LegalFilePathsOnly()
},
new Option(new[] { "--verbosity", "-v" }, Resources.Set_the_verbosity_level_Allowed_values_are_quiet_minimal_normal_detailed_and_diagnostic)
{
Argument = new Argument<string?>() { Arity = ArgumentArity.ExactlyOne }
Argument = new Argument<string?>() { Arity = ArgumentArity.ExactlyOne }.FromAmong(VerbosityLevels)
},
new Option(new[] { "--include-generated" }, Resources.Include_generated_code_files_in_formatting_operations)
{
Argument = new Argument<bool>(),
IsHidden = true
},
};

rootCommand.Description = "dotnet-format";
rootCommand.AddValidator(ValidateProjectArgumentAndWorkspace);
rootCommand.AddValidator(ValidateProjectArgumentAndFolder);
rootCommand.AddValidator(ValidateWorkspaceAndFolder);

return rootCommand;
}

private static string? ValidateProjectArgumentAndWorkspace(CommandResult symbolResult)
{
try
{
var project = symbolResult.GetArgumentValueOrDefault<string>("project");
var workspace = symbolResult.ValueForOption<string>("workspace");

if (!string.IsNullOrEmpty(project) && !string.IsNullOrEmpty(workspace))
{
return Resources.Cannot_specify_both_project_argument_and_workspace_option;
}
}
catch (InvalidOperationException) // Parsing of arguments failed. This will be reported later.
{
}

return null;
}

private static string? ValidateProjectArgumentAndFolder(CommandResult symbolResult)
{
try
{
var project = symbolResult.GetArgumentValueOrDefault<string>("project");
var folder = symbolResult.ValueForOption<string>("folder");

if (!string.IsNullOrEmpty(project) && !string.IsNullOrEmpty(folder))
{
return Resources.Cannot_specify_both_project_argument_and_folder_options;
}
}
catch (InvalidOperationException) // Parsing of arguments failed. This will be reported later.
{
}

return null;
}

private static string? ValidateWorkspaceAndFolder(CommandResult symbolResult)
{
try
{
var workspace = symbolResult.ValueForOption<string>("workspace");
var folder = symbolResult.ValueForOption<string>("folder");


if (!string.IsNullOrEmpty(workspace) && !string.IsNullOrEmpty(folder))
{
return Resources.Cannot_specify_both_folder_and_workspace_options;
}
}
catch (InvalidOperationException) // Parsing of arguments failed. This will be reported later.
{
}

return null;
}

internal static bool WasOptionUsed(this ParseResult result, params string[] aliases)
{
return result.Tokens
Expand Down
12 changes: 11 additions & 1 deletion src/MSBuild/LooseVersionAssemblyLoader.cs
Expand Up @@ -63,7 +63,17 @@ public static void Register(string searchPath)
continue;
}

return LoadAndCache(context, candidatePath);
try
{
return LoadAndCache(context, candidatePath);
}
catch
{
// We were unable to load the assembly from the file path. It is likely that
// a different version of the assembly has already been loaded into the context.
// Be forgiving and attempt to load assembly by name without specifying a version.
return context.LoadFromAssemblyName(new AssemblyName(assemblyName.Name));
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/MSBuild/MSBuildWorkspaceFinder.cs
Expand Up @@ -36,12 +36,12 @@ public static (bool isSolution, string workspacePath) FindWorkspace(string searc
: FindFile(workspacePath!); // IsNullOrEmpty is not annotated on .NET Core 2.1
}

var foundSolution = FindMatchingFile(searchDirectory, FindSolutionFiles, Resources.Multiple_MSBuild_solution_files_found_in_0_Specify_which_to_use_with_the_workspace_option);
var foundProject = FindMatchingFile(searchDirectory, FindProjectFiles, Resources.Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_option);
var foundSolution = FindMatchingFile(searchDirectory, FindSolutionFiles, Resources.Multiple_MSBuild_solution_files_found_in_0_Specify_which_to_use_with_the_workspace_argument);
var foundProject = FindMatchingFile(searchDirectory, FindProjectFiles, Resources.Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_argument);

if (!string.IsNullOrEmpty(foundSolution) && !string.IsNullOrEmpty(foundProject))
{
throw new FileNotFoundException(string.Format(Resources.Both_a_MSBuild_project_file_and_solution_file_found_in_0_Specify_which_to_use_with_the_workspace_option, searchDirectory));
throw new FileNotFoundException(string.Format(Resources.Both_a_MSBuild_project_file_and_solution_file_found_in_0_Specify_which_to_use_with_the_workspace_argument, searchDirectory));
}

if (!string.IsNullOrEmpty(foundSolution))
Expand All @@ -54,7 +54,7 @@ public static (bool isSolution, string workspacePath) FindWorkspace(string searc
return (false, foundProject!); // IsNullOrEmpty is not annotated on .NET Core 2.1
}

throw new FileNotFoundException(string.Format(Resources.Could_not_find_a_MSBuild_project_or_solution_file_in_0_Specify_which_to_use_with_the_workspace_option, searchDirectory));
throw new FileNotFoundException(string.Format(Resources.Could_not_find_a_MSBuild_project_or_solution_file_in_0_Specify_which_to_use_with_the_workspace_argument, searchDirectory));
}

private static (bool isSolution, string workspacePath) FindFile(string workspacePath)
Expand Down
34 changes: 5 additions & 29 deletions src/Program.cs
Expand Up @@ -20,7 +20,6 @@ namespace Microsoft.CodeAnalysis.Tools
{
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;
Expand All @@ -40,9 +39,8 @@ private static async Task<int> Main(string[] args)
}

public static async Task<int> Run(
string? project,
string? folder,
string? workspace,
bool folder,
string? fixStyle,
string? fixAnalyzers,
string? verbosity,
Expand Down Expand Up @@ -76,44 +74,22 @@ private static async Task<int> Main(string[] args)
{
currentDirectory = Environment.CurrentDirectory;

// Check for deprecated options and assign package if specified via `-w | -f` options.
if (!string.IsNullOrEmpty(workspace) && string.IsNullOrEmpty(project))
{
logger.LogWarning(Resources.The_workspace_option_is_deprecated_Use_the_project_argument_instead);
project = workspace;
}
else if (!string.IsNullOrEmpty(folder) && string.IsNullOrEmpty(project))
{
logger.LogWarning(Resources.The_folder_option_is_deprecated_for_specifying_the_path_Pass_the_folder_option_but_specify_the_path_with_the_project_argument_instead);
project = folder;
}

if (s_parseResult.WasOptionUsed("--files"))
{
logger.LogWarning(Resources.The_files_option_is_deprecated_Use_the_include_option_instead);
}

if (s_parseResult.WasOptionUsed("--dry-run"))
{
logger.LogWarning(Resources.The_dry_run_option_is_deprecated_Use_the_check_option_instead);
}

string workspaceDirectory;
string workspacePath;
WorkspaceType workspaceType;

// The presence of the folder token means we should treat the project path as a folder path.
// This will change in the following version so that the folder option is a bool flag.
if (s_parseResult.WasOptionUsed("-f", "--folder"))
// The folder option means we should treat the project path as a folder path.
if (folder)
{
// If folder isn't populated, then use the current directory
workspacePath = Path.GetFullPath(project ?? ".", Environment.CurrentDirectory);
workspacePath = Path.GetFullPath(workspace ?? ".", Environment.CurrentDirectory);
workspaceDirectory = workspacePath;
workspaceType = WorkspaceType.Folder;
}
else
{
var (isSolution, workspaceFilePath) = MSBuildWorkspaceFinder.FindWorkspace(currentDirectory, project);
var (isSolution, workspaceFilePath) = MSBuildWorkspaceFinder.FindWorkspace(currentDirectory, workspace);

workspacePath = workspaceFilePath;
workspaceType = isSolution
Expand Down

0 comments on commit 95024cb

Please sign in to comment.