Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit 48ab78f

Browse files
committed
Merge pull request #221 from davkean/NoCopyright
Turned copyright header off by default
2 parents fe6f6c8 + 819f3a8 commit 48ab78f

File tree

4 files changed

+71
-45
lines changed

4 files changed

+71
-45
lines changed

src/CodeFormatter/CommandLineParser.cs

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -117,31 +117,34 @@ public static class CommandLineParser
117117
{
118118
private const string FileSwitch = "/file:";
119119
private const string ConfigSwitch = "/c:";
120-
private const string CopyrightSwitch = "/copyright:";
120+
private const string CopyrightWithFileSwitch = "/copyright:";
121121
private const string LanguageSwitch = "/lang:";
122122
private const string RuleEnabledSwitch1 = "/rule+:";
123123
private const string RuleEnabledSwitch2 = "/rule:";
124124
private const string RuleDisabledSwitch = "/rule-:";
125-
private const string Usage =
125+
private const string Usage =
126126
@"CodeFormatter [/file:<filename>] [/lang:<language>] [/c:<config>[,<config>...]>]
127-
[/copyright:<file> | /nocopyright] [/tables] [/nounicode]
127+
[/copyright(+|-):[<file>]] [/tables] [/nounicode]
128128
[/rule(+|-):rule1,rule2,...] [/verbose]
129129
<project, solution or response file>
130130
131-
/file - Only apply changes to files with specified name
132-
/lang - Specifies the language to use when a responsefile is
133-
specified. i.e. 'C#', 'Visual Basic', ... (default: 'C#')
134-
/c - Additional preprocessor configurations the formatter
135-
should run under.
136-
/copyright - Specifies file containing copyright header.
137-
Use ConvertTests to convert MSTest tests to xUnit.
138-
/nocopyright - Do not update the copyright message.
139-
/tables - Let tables opt out of formatting by defining
140-
DOTNET_FORMATTER
141-
/nounicode - Do not convert unicode strings to escape sequences
142-
/rule(+|-) - Enable (default) or disable the specified rule
143-
/rules - List the available rules
144-
/verbose - Verbose output
131+
/file - Only apply changes to files with specified name
132+
/lang - Specifies the language to use when a responsefile is
133+
specified. i.e. 'C#', 'Visual Basic', ... (default: 'C#')
134+
/c - Additional preprocessor configurations the formatter
135+
should run under.
136+
/copyright(+|-) - Enables or disables (default) updating the copyright
137+
header in files, optionally specifying a file
138+
containing a custom copyright header.
139+
/nocopyright - Do not update the copyright message.
140+
/tables - Let tables opt out of formatting by defining
141+
DOTNET_FORMATTER
142+
/nounicode - Do not convert unicode strings to escape sequences
143+
/rule(+|-) - Enable (default) or disable the specified rule
144+
/rules - List the available rules
145+
/verbose - Verbose output
146+
147+
Use ConvertTests to convert MSTest tests to xUnit.
145148
";
146149

147150
public static void PrintUsage()
@@ -163,7 +166,7 @@ public static CommandLineParseResult Parse(string[] args)
163166
var formatTargets = new List<string>();
164167
var fileNames = new List<string>();
165168
var configBuilder = ImmutableArray.CreateBuilder<string[]>();
166-
var copyrightHeader = FormattingDefaults.DefaultCopyrightHeader;
169+
var copyrightHeader = ImmutableArray<string>.Empty;
167170
var ruleMap = ImmutableDictionary<string, bool>.Empty;
168171
var language = LanguageNames.CSharp;
169172
var allowTables = false;
@@ -172,15 +175,22 @@ public static CommandLineParseResult Parse(string[] args)
172175
for (int i = 0; i < args.Length; i++)
173176
{
174177
string arg = args[i];
175-
if (arg.StartsWith(ConfigSwitch, StringComparison.OrdinalIgnoreCase))
178+
if (arg.StartsWith(ConfigSwitch, comparison))
176179
{
177180
var all = arg.Substring(ConfigSwitch.Length);
178181
var configs = all.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
179182
configBuilder.Add(configs);
180183
}
181-
else if (arg.StartsWith(CopyrightSwitch, StringComparison.OrdinalIgnoreCase))
184+
else if (comparer.Equals(arg, "/copyright+") || comparer.Equals(arg, "/copyright"))
185+
{
186+
ruleMap = ruleMap.SetItem(FormattingDefaults.CopyrightRuleName, true);
187+
copyrightHeader = FormattingDefaults.DefaultCopyrightHeader;
188+
}
189+
else if (arg.StartsWith(CopyrightWithFileSwitch, comparison))
182190
{
183-
var fileName = arg.Substring(CopyrightSwitch.Length);
191+
ruleMap = ruleMap.SetItem(FormattingDefaults.CopyrightRuleName, true);
192+
193+
var fileName = arg.Substring(CopyrightWithFileSwitch.Length);
184194
try
185195
{
186196
copyrightHeader = ImmutableArray.CreateRange(File.ReadAllLines(fileName));
@@ -194,13 +204,14 @@ public static CommandLineParseResult Parse(string[] args)
194204
return CommandLineParseResult.CreateError(error);
195205
}
196206
}
197-
else if (arg.StartsWith(LanguageSwitch, StringComparison.OrdinalIgnoreCase))
198-
{
199-
language = arg.Substring(LanguageSwitch.Length);
207+
else if (comparer.Equals(arg, "/copyright-") || comparer.Equals(arg, "/nocopyright"))
208+
{ // We still check /nocopyright for backwards compat
209+
210+
ruleMap = ruleMap.SetItem(FormattingDefaults.CopyrightRuleName, false);
200211
}
201-
else if (comparer.Equals(arg, "/nocopyright"))
212+
else if (arg.StartsWith(LanguageSwitch, comparison))
202213
{
203-
ruleMap = ruleMap.SetItem(FormattingDefaults.CopyrightRuleName, false);
214+
language = arg.Substring(LanguageSwitch.Length);
204215
}
205216
else if (comparer.Equals(arg, "/nounicode"))
206217
{

src/Microsoft.DotNet.CodeFormatting.Tests/CommandLineParserTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,36 @@ public void NoUnicode()
8383
Assert.Equal(new[] { "test.csproj" }, options.FormatTargets);
8484
}
8585

86+
[Fact]
87+
public void CopyrightDisable()
88+
{
89+
var options = Parse("/copyright-", "test.csproj");
90+
Assert.False(options.RuleMap[FormattingDefaults.CopyrightRuleName]);
91+
Assert.Equal(new[] { "test.csproj" }, options.FormatTargets);
92+
}
93+
8694
[Fact]
8795
public void NoCopyright()
8896
{
8997
var options = Parse("/nocopyright", "test.csproj");
9098
Assert.False(options.RuleMap[FormattingDefaults.CopyrightRuleName]);
9199
Assert.Equal(new[] { "test.csproj" }, options.FormatTargets);
92100
}
101+
102+
[Fact]
103+
public void CopyrightEnable1()
104+
{
105+
var options = Parse("/copyright+", "test.csproj");
106+
Assert.True(options.RuleMap[FormattingDefaults.CopyrightRuleName]);
107+
Assert.Equal(new[] { "test.csproj" }, options.FormatTargets);
108+
}
109+
110+
[Fact]
111+
public void CopyrightEnable2()
112+
{
113+
var options = Parse("/copyright", "test.csproj");
114+
Assert.True(options.RuleMap[FormattingDefaults.CopyrightRuleName]);
115+
Assert.Equal(new[] { "test.csproj" }, options.FormatTargets);
116+
}
93117
}
94118
}

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/CombinationTest.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ void M() {
6666
}
6767
}";
6868

69-
var expected = @"// header
70-
69+
var expected = @"
7170
internal class C
7271
{
7372
private int _field;
@@ -123,8 +122,7 @@ void M() {
123122
}
124123
}";
125124

126-
var expected = @"// header
127-
125+
var expected = @"
128126
internal class C
129127
{
130128
private int field;
@@ -151,8 +149,7 @@ void M() {
151149
}
152150
}";
153151

154-
var expected = @"// header
155-
152+
var expected = @"
156153
internal class C
157154
{
158155
private int _field;
@@ -177,8 +174,7 @@ void M() { }
177174
#endif
178175
}";
179176

180-
var expected = @"// header
181-
177+
var expected = @"
182178
internal class C
183179
{
184180
#if DOG
@@ -201,8 +197,7 @@ internal void M() {
201197
#endif
202198
}";
203199

204-
var expected = @"// header
205-
200+
var expected = @"
206201
internal class C
207202
{
208203
#if DOG
@@ -232,8 +227,7 @@ void M() {
232227
#endif
233228
}";
234229

235-
var expected = @"// header
236-
230+
var expected = @"
237231
internal class C
238232
{
239233
private void G()
@@ -270,8 +264,7 @@ void M() {
270264
#endif
271265
}";
272266

273-
var expected = @"// header
274-
267+
var expected = @"
275268
internal class C
276269
{
277270
#if TEST
@@ -310,8 +303,7 @@ private void M()
310303
}
311304
}";
312305

313-
var expected = @"// header
314-
306+
var expected = @"
315307
internal class C
316308
{
317309
private void M()
@@ -348,8 +340,7 @@ public void RequiredRuntimeAttribute()
348340
{}
349341
}
350342
}";
351-
var expected = @"// header
352-
343+
var expected = @"
353344
using System;
354345
using System.Reflection;
355346

src/Microsoft.DotNet.CodeFormatting/Rules/CopyrightHeaderRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Microsoft.DotNet.CodeFormatting.Rules
1515
{
16-
[SyntaxRule(CopyrightHeaderRule.Name, CopyrightHeaderRule.Description, SyntaxRuleOrder.CopyrightHeaderRule)]
16+
[SyntaxRule(CopyrightHeaderRule.Name, CopyrightHeaderRule.Description, SyntaxRuleOrder.CopyrightHeaderRule, DefaultRule=false)]
1717
internal sealed partial class CopyrightHeaderRule : SyntaxFormattingRule, ISyntaxFormattingRule
1818
{
1919
internal const string Name = FormattingDefaults.CopyrightRuleName;

0 commit comments

Comments
 (0)