This repository was archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathFormatOptions.cs
More file actions
196 lines (164 loc) · 6.66 KB
/
FormatOptions.cs
File metadata and controls
196 lines (164 loc) · 6.66 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
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
// 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.Diagnostics;
using System.IO;
using CommandLine;
using Microsoft.CodeAnalysis.Options;
using Microsoft.DotNet.CodeFormatter.Analyzers;
using Microsoft.DotNet.CodeFormatting;
namespace CodeFormatter
{
internal class CommandLineOptions
{
[Value(
0,
HelpText = "Project, solution, text file with target paths, or response file to drive code formatting.",
Required = true)]
public IEnumerable<string> Targets { get; set; }
[Option(
'o',
"options-file-path",
HelpText = "Path to an options file that should be used to configure analysis")]
public string OptionsFilePath { get; set; }
[Option(
"file-filters",
HelpText = "Only apply changes to files with specified name(s).",
Separator = ',')]
public IEnumerable<string> FileFilters { get; set; }
[Option(
'l', "lang",
Default = "C#",
HelpText = "Specifies the language to use when a response file is specified, e.g., 'C#', 'Visual Basic', ... (default: 'C#').")]
public string Language { get; set; }
[Option(
'c', "configs",
HelpText = "Comma-separated list of preprocessor configurations the formatter should run under.",
Separator = ',')]
public IEnumerable<string> PreprocessorConfigurations { get; set; }
[Option(
"copyright",
HelpText = "Specifies file containing copyright header.")]
public string CopyrightHeaderFile { get; set; }
[Option(
'v', "verbose",
HelpText = "Verbose output.")]
public bool Verbose { get; set; }
[Option(
"define-dotnet_formatter",
HelpText = "Define DOTNET_FORMATTER in order to allow #if !DOTNET_FORMATTER constructs in code (to opt out of reformatting).")]
public bool DefineDotNetFormatter { get; set; }
[Option(
"use-analyzers",
HelpText = "TEMPORARY: invoke built-in analyzers rather than rules to perform reformatting.")]
public bool UseAnalyzers { get; set; }
[Option(
"analyzers",
HelpText = "A path to an analyzer assembly or a file containing a newline separated list of analyzer assemblies to be run against the target source.")]
public string TargetAnalyzers { get; set; }
[Option(
"log-output-path",
HelpText = "Path to a file where analysis or format results will be logged.")]
public string LogOutputPath { get; set; }
public virtual bool ApplyFixes { get; }
private ImmutableArray<string> _targetAnalyzerText;
public ImmutableArray<string> TargetAnalyzerText
{
get
{
if (_targetAnalyzerText == null)
{
_targetAnalyzerText = InitializeTargetAnalyzerText(TargetAnalyzers);
}
return _targetAnalyzerText;
}
internal set
{
_targetAnalyzerText = value;
}
}
private static ImmutableArray<string> InitializeTargetAnalyzerText(string targetAnalyzers)
{
var fileType = Path.GetExtension(targetAnalyzers);
if (StringComparer.OrdinalIgnoreCase.Equals(fileType, ".dll"))
{
return ImmutableArray.Create(targetAnalyzers);
}
else if(StringComparer.OrdinalIgnoreCase.Equals(fileType, ".txt"))
{
ImmutableArray<string> analyzerText = new ImmutableArray<string>();
if (!String.IsNullOrEmpty(targetAnalyzers))
{
analyzerText = ImmutableArray.CreateRange(File.ReadAllLines(targetAnalyzers));
}
return analyzerText;
}
return ImmutableArray<string>.Empty;
}
private ImmutableArray<string> _copyrightHeaderText;
public ImmutableArray<string> CopyrightHeaderText
{
get
{
if (_copyrightHeaderText == null)
{
_copyrightHeaderText = InitializeCopyrightHeaderText(CopyrightHeaderFile);
}
return _copyrightHeaderText;
}
internal set
{
_copyrightHeaderText = value;
}
}
private static ImmutableArray<string> InitializeCopyrightHeaderText(string copyrightHeaderFile)
{
ImmutableArray<string> copyrightHeaderText = FormattingDefaults.DefaultCopyrightHeader;
if (!String.IsNullOrEmpty(copyrightHeaderFile))
{
copyrightHeaderText = ImmutableArray.CreateRange(File.ReadAllLines(copyrightHeaderFile));
}
return copyrightHeaderText;
}
private ImmutableDictionary<string, bool> _ruleMap;
public ImmutableDictionary<string, bool> RuleMap
{
get
{
return _ruleMap ?? BuildRuleMapFromOptions();
}
}
private ImmutableDictionary<string, bool> BuildRuleMapFromOptions()
{
var result = new Dictionary<string, bool>();
var propertyBag = OptionsHelper.BuildDefaultPropertyBag();
if (!string.IsNullOrEmpty(OptionsFilePath))
{
propertyBag.LoadFrom(OptionsFilePath);
}
propertyBag = (PropertyBag)propertyBag["CodeFormatterRules.Options"];
foreach (string key in propertyBag.Keys)
{
string[] tokens = key.Split('.');
Debug.Assert(tokens.Length == 2);
Debug.Assert(tokens[1].Equals("Enabled"));
string rule = tokens[0];
result[rule] = (bool)propertyBag[key];
}
_ruleMap = result.ToImmutableDictionary();
return _ruleMap;
}
}
[Verb("format", HelpText = "Apply code formatting rules and analyzers to specified targets.")]
internal class FormatOptions : CommandLineOptions
{
public override bool ApplyFixes { get { return true; } }
}
[Verb("analyze", HelpText = "Apply analyzers to specified targets but do not apply code fixes.")]
internal class AnalyzeOptions : CommandLineOptions
{
public override bool ApplyFixes { get { return false; } }
}
}