forked from dotnet/codeformatter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
149 lines (136 loc) · 6.35 KB
/
Program.cs
File metadata and controls
149 lines (136 loc) · 6.35 KB
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.DotNet.CodeFormatting;
namespace CodeFormatter
{
internal static class Program
{
private const string FileSwitch = "/file:";
private const string ConfigSwitch = "/c:";
private const string CopyrightSwitch = "/copyright:";
private static int Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("CodeFormatter <project or solution> [<rule types>] [/file:<filename>] [/nocopyright] [/c:<config1,config2> [/copyright:file]");
Console.WriteLine(" <rule types> - Rule types to use in addition to the default ones.");
Console.WriteLine(" Use ConvertTests to convert MSTest tests to xUnit.");
Console.WriteLine(" <filename> - Only apply changes to files with specified name.");
Console.WriteLine(" <configs> - Additional preprocessor configurations the formatter");
Console.WriteLine(" should run under.");
Console.WriteLine(" <copyright> - Specifies file containing copyright header.");
return -1;
}
var projectOrSolutionPath = args[0];
if (!File.Exists(projectOrSolutionPath))
{
Console.Error.WriteLine("Project or solution {0} doesn't exist.", projectOrSolutionPath);
return -1;
}
var fileNamesBuilder = ImmutableArray.CreateBuilder<string>();
var ruleTypeBuilder = ImmutableArray.CreateBuilder<string>();
var configBuilder = ImmutableArray.CreateBuilder<string[]>();
var copyrightHeader = FormattingConstants.DefaultCopyrightHeader;
var comparer = StringComparer.OrdinalIgnoreCase;
for (int i = 1; i < args.Length; i++)
{
string arg = args[i];
if (arg.StartsWith(FileSwitch, StringComparison.OrdinalIgnoreCase))
{
var all = arg.Substring(FileSwitch.Length);
var files = all.Split(new[] { ','}, StringSplitOptions.RemoveEmptyEntries);
fileNamesBuilder.AddRange(files);
}
else if (arg.StartsWith(ConfigSwitch, StringComparison.OrdinalIgnoreCase))
{
var all = arg.Substring(ConfigSwitch.Length);
var configs = all.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
configBuilder.Add(configs);
}
else if (arg.StartsWith(CopyrightSwitch, StringComparison.OrdinalIgnoreCase))
{
var fileName = arg.Substring(CopyrightSwitch.Length);
try
{
copyrightHeader = ImmutableArray.CreateRange(File.ReadAllLines(fileName));
}
catch (Exception ex)
{
Console.Error.WriteLine("Could not read {0}", fileName);
Console.Error.WriteLine(ex.Message);
return -1;
}
}
else if (comparer.Equals(arg, "/nocopyright"))
{
copyrightHeader = ImmutableArray<string>.Empty;
}
else
{
ruleTypeBuilder.Add(arg);
}
}
var cts = new CancellationTokenSource();
var ct = cts.Token;
Console.CancelKeyPress += delegate { cts.Cancel(); };
try
{
RunAsync(
projectOrSolutionPath,
ruleTypeBuilder.ToImmutableArray(),
fileNamesBuilder.ToImmutableArray(),
configBuilder.ToImmutableArray(),
copyrightHeader,
ct).Wait(ct);
Console.WriteLine("Completed formatting.");
return 0;
}
catch (AggregateException ex)
{
var typeLoadException = ex.InnerExceptions.FirstOrDefault() as ReflectionTypeLoadException;
if (typeLoadException == null)
throw;
Console.WriteLine("ERROR: Type loading error detected. In order to run this tool you need either Visual Studio 2015 or Microsoft Build Tools 2015 tools installed.");
var messages = typeLoadException.LoaderExceptions.Select(e => e.Message).Distinct();
foreach (var message in messages)
Console.WriteLine("- {0}", message);
return 1;
}
}
private static async Task RunAsync(
string projectOrSolutionPath,
ImmutableArray<string> ruleTypes,
ImmutableArray<string> fileNames,
ImmutableArray<string[]> preprocessorConfigurations,
ImmutableArray<string> copyrightHeader,
CancellationToken cancellationToken)
{
var workspace = MSBuildWorkspace.Create();
var engine = FormattingEngine.Create(ruleTypes);
engine.PreprocessorConfigurations = preprocessorConfigurations;
engine.FileNames = fileNames;
engine.CopyrightHeader = copyrightHeader;
string extension = Path.GetExtension(projectOrSolutionPath);
if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln"))
{
var solution = await workspace.OpenSolutionAsync(projectOrSolutionPath, cancellationToken);
await engine.FormatSolutionAsync(solution, cancellationToken);
}
else
{
workspace.LoadMetadataForReferencedProjects = true;
var project = await workspace.OpenProjectAsync(projectOrSolutionPath, cancellationToken);
await engine.FormatProjectAsync(project, cancellationToken);
}
}
}
}