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

Commit 3b6afc9

Browse files
committed
Copyright can now be specified on command line
The copyright text can now be specified on the command line by passing in a file path that contains the relevant text.
1 parent a390bb6 commit 3b6afc9

File tree

4 files changed

+57
-22
lines changed

4 files changed

+57
-22
lines changed

src/CodeFormatter/Program.cs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ namespace CodeFormatter
1515
{
1616
internal static class Program
1717
{
18+
private const string FileSwitch = "/file:";
19+
private const string ConfigSwitch = "/c:";
20+
private const string CopyrightSwitch = "/copyright:";
21+
1822
private static int Main(string[] args)
1923
{
2024
if (args.Length < 1)
2125
{
22-
Console.WriteLine("CodeFormatter <project or solution> [<rule types>] [/file:<filename>] [/nocopyright] [/c:<config1,config2>");
26+
Console.WriteLine("CodeFormatter <project or solution> [<rule types>] [/file:<filename>] [/nocopyright] [/c:<config1,config2> [/copyright:file]");
2327
Console.WriteLine(" <rule types> - Rule types to use in addition to the default ones.");
2428
Console.WriteLine(" Use ConvertTests to convert MSTest tests to xUnit.");
2529
Console.WriteLine(" <filename> - Only apply changes to files with specified name.");
26-
Console.WriteLine(" <configs> - Additional preprocessor configurations the formatter")
30+
Console.WriteLine(" <configs> - Additional preprocessor configurations the formatter");
2731
Console.WriteLine(" should run under");
2832
return -1;
2933
}
@@ -38,27 +42,41 @@ private static int Main(string[] args)
3842
var fileNamesBuilder = ImmutableArray.CreateBuilder<string>();
3943
var ruleTypeBuilder = ImmutableArray.CreateBuilder<string>();
4044
var configBuilder = ImmutableArray.CreateBuilder<string[]>();
45+
var copyrightHeader = FormattingConstants.DefaultCopyrightHeader;
4146
var comparer = StringComparer.OrdinalIgnoreCase;
42-
var disableCopyright = false;
4347

4448
for (int i = 1; i < args.Length; i++)
4549
{
4650
string arg = args[i];
47-
if (arg.StartsWith("/file:", StringComparison.OrdinalIgnoreCase))
51+
if (arg.StartsWith(FileSwitch, StringComparison.OrdinalIgnoreCase))
4852
{
49-
var all = arg.Substring(6);
53+
var all = arg.Substring(FileSwitch.Length);
5054
var files = all.Split(new[] { ','}, StringSplitOptions.RemoveEmptyEntries);
5155
fileNamesBuilder.AddRange(files);
5256
}
53-
else if (arg.StartsWith("/c:", StringComparison.OrdinalIgnoreCase))
57+
else if (arg.StartsWith(ConfigSwitch, StringComparison.OrdinalIgnoreCase))
5458
{
55-
var all = arg.Substring(3);
59+
var all = arg.Substring(ConfigSwitch.Length);
5660
var configs = all.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
5761
configBuilder.Add(configs);
5862
}
63+
else if (arg.StartsWith(CopyrightSwitch, StringComparison.OrdinalIgnoreCase))
64+
{
65+
var fileName = arg.Substring(CopyrightSwitch.Length);
66+
try
67+
{
68+
copyrightHeader = ImmutableArray.CreateRange(File.ReadAllLines(fileName));
69+
}
70+
catch (Exception ex)
71+
{
72+
Console.Error.WriteLine("Could not read {0}", fileName);
73+
Console.Error.WriteLine(ex.Message);
74+
return -1;
75+
}
76+
}
5977
else if (comparer.Equals(arg, "/nocopyright"))
6078
{
61-
disableCopyright = true;
79+
copyrightHeader = ImmutableArray<string>.Empty;
6280
}
6381
else
6482
{
@@ -76,7 +94,7 @@ private static int Main(string[] args)
7694
ruleTypeBuilder.ToImmutableArray(),
7795
fileNamesBuilder.ToImmutableArray(),
7896
configBuilder.ToImmutableArray(),
79-
disableCopyright,
97+
copyrightHeader,
8098
ct).Wait(ct);
8199
Console.WriteLine("Completed formatting.");
82100
return 0;
@@ -87,18 +105,14 @@ private static async Task RunAsync(
87105
ImmutableArray<string> ruleTypes,
88106
ImmutableArray<string> fileNames,
89107
ImmutableArray<string[]> preprocessorConfigurations,
90-
bool disableCopright,
108+
ImmutableArray<string> copyrightHeader,
91109
CancellationToken cancellationToken)
92110
{
93111
var workspace = MSBuildWorkspace.Create();
94112
var engine = FormattingEngine.Create(ruleTypes);
95113
engine.PreprocessorConfigurations = preprocessorConfigurations;
96114
engine.FileNames = fileNames;
97-
98-
if (disableCopright)
99-
{
100-
engine.CopyrightHeader = ImmutableArray<string>.Empty;
101-
}
115+
engine.CopyrightHeader = copyrightHeader;
102116

103117
string extension = Path.GetExtension(projectOrSolutionPath);
104118
if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln"))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Immutable;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.DotNet.CodeFormatting
9+
{
10+
public static class FormattingConstants
11+
{
12+
private static readonly string[] s_defaultCopyrightHeader =
13+
{
14+
"// Copyright (c) Microsoft. All rights reserved.",
15+
"// Licensed under the MIT license. See LICENSE file in the project root for full license information."
16+
};
17+
18+
public static readonly ImmutableArray<string> DefaultCopyrightHeader;
19+
20+
static FormattingConstants()
21+
{
22+
DefaultCopyrightHeader = ImmutableArray.CreateRange(s_defaultCopyrightHeader);
23+
}
24+
}
25+
}

src/Microsoft.DotNet.CodeFormatting/Microsoft.DotNet.CodeFormatting.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<Compile Include="Extensions.cs" />
7575
<Compile Include="Filters\FilenameFilter.cs" />
7676
<Compile Include="Filters\UsableFileFilter.cs" />
77+
<Compile Include="FormattingConstants.cs" />
7778
<Compile Include="FormattingEngine.cs" />
7879
<Compile Include="FormattingEngineImplementation.cs" />
7980
<Compile Include="IFormatLogger.cs" />

src/Microsoft.DotNet.CodeFormatting/Options.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ namespace Microsoft.DotNet.CodeFormatting
1414
[Export(typeof(Options))]
1515
internal sealed class Options
1616
{
17-
private static readonly string[] s_defaultCopyrightHeader =
18-
{
19-
"// Copyright (c) Microsoft. All rights reserved.",
20-
"// Licensed under the MIT license. See LICENSE file in the project root for full license information."
21-
};
22-
2317
internal ImmutableArray<string> CopyrightHeader { get; set; }
2418
internal ImmutableArray<string[]> PreprocessorConfigurations { get; set; }
2519

@@ -33,7 +27,8 @@ internal sealed class Options
3327
[ImportingConstructor]
3428
internal Options()
3529
{
36-
CopyrightHeader = ImmutableArray.Create(s_defaultCopyrightHeader);
30+
CopyrightHeader = FormattingConstants.DefaultCopyrightHeader;
31+
FileNames = ImmutableArray<string>.Empty;
3732
PreprocessorConfigurations = ImmutableArray<string[]>.Empty;
3833
FormatLogger = new ConsoleFormatLogger();
3934
}

0 commit comments

Comments
 (0)