forked from dotnet/format
/
Program.cs
216 lines (189 loc) · 9.05 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Tools.Logging;
using Microsoft.CodeAnalysis.Tools.MSBuild;
using Microsoft.CodeAnalysis.Tools.Utilities;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
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;
private static async Task<int> Main(string[] args)
{
var rootCommand = new RootCommand
{
new Option(new[] { "--folder", "-f" }, Resources.The_folder_to_operate_on_Cannot_be_used_with_the_workspace_option)
{
Argument = new Argument<string>(() => null)
},
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)
},
new Option(new[] { "--include", "--files" }, 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_Terminate_with_a_non_zero_exit_code_if_any_files_were_formatted)
{
Argument = new Argument<bool>()
},
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)
},
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 }
},
};
rootCommand.Description = "dotnet-format";
rootCommand.Handler = CommandHandler.Create(typeof(Program).GetMethod(nameof(Run)));
// Parse the incoming args and invoke the handler
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)
{
// Setup logging.
var serviceCollection = new ServiceCollection();
var logLevel = GetLogLevel(verbosity);
ConfigureServices(serviceCollection, console, logLevel);
var serviceProvider = serviceCollection.BuildServiceProvider();
var logger = serviceProvider.GetService<ILogger<Program>>();
// Hook so we can cancel and exit when ctrl+c is pressed.
var cancellationTokenSource = new CancellationTokenSource();
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true;
cancellationTokenSource.Cancel();
};
var currentDirectory = string.Empty;
try
{
currentDirectory = Environment.CurrentDirectory;
string workspaceDirectory;
string workspacePath;
WorkspaceType workspaceType;
if (!string.IsNullOrEmpty(folder) && !string.IsNullOrEmpty(workspace))
{
logger.LogWarning(Resources.Cannot_specify_both_folder_and_workspace_options);
return 1;
}
if (!string.IsNullOrEmpty(folder))
{
folder = Path.GetFullPath(folder, Environment.CurrentDirectory);
workspacePath = folder;
workspaceDirectory = workspacePath;
workspaceType = WorkspaceType.Folder;
}
else
{
var (isSolution, workspaceFilePath) = MSBuildWorkspaceFinder.FindWorkspace(currentDirectory, workspace);
workspacePath = workspaceFilePath;
workspaceType = isSolution
? WorkspaceType.Solution
: WorkspaceType.Project;
// To ensure we get the version of MSBuild packaged with the dotnet SDK used by the
// workspace, use its directory as our working directory which will take into account
// a global.json if present.
workspaceDirectory = Path.GetDirectoryName(workspacePath);
}
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)
{
logger.LogInformation(Resources.Using_msbuildexe_located_in_0, msBuildInstance.MSBuildPath);
}
var fileMatcher = SourceFileMatcher.CreateMatcher(include, exclude);
var formatOptions = new FormatOptions(
workspacePath,
workspaceType,
logLevel,
saveFormattedFiles: !check,
changesAreErrors: check,
fileMatcher,
reportPath: report);
var formatResult = await CodeFormatter.FormatWorkspaceAsync(
formatOptions,
logger,
cancellationTokenSource.Token,
createBinaryLog: logLevel == LogLevel.Trace ? true : false).ConfigureAwait(false);
return GetExitCode(formatResult, check);
}
catch (FileNotFoundException fex)
{
logger.LogError(fex.Message);
return UnhandledExceptionExitCode;
}
catch (OperationCanceledException)
{
return UnhandledExceptionExitCode;
}
finally
{
if (!string.IsNullOrEmpty(currentDirectory))
{
Environment.CurrentDirectory = currentDirectory;
}
}
}
internal static int GetExitCode(WorkspaceFormatResult formatResult, bool check)
{
if (!check)
{
return formatResult.ExitCode;
}
return formatResult.FilesFormatted == 0 ? 0 : CheckFailedExitCode;
}
internal static LogLevel GetLogLevel(string verbosity)
{
switch (verbosity)
{
case "q":
case "quiet":
return LogLevel.Error;
case "m":
case "minimal":
return LogLevel.Warning;
case "n":
case "normal":
return LogLevel.Information;
case "d":
case "detailed":
return LogLevel.Debug;
case "diag":
case "diagnostic":
return LogLevel.Trace;
default:
return LogLevel.Information;
}
}
private static void ConfigureServices(ServiceCollection serviceCollection, IConsole console, LogLevel logLevel)
{
serviceCollection.AddSingleton(new LoggerFactory().AddSimpleConsole(console, logLevel));
serviceCollection.AddLogging();
}
}
}