forked from nunit/nunit-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader-check.cake
98 lines (82 loc) · 2.89 KB
/
header-check.cake
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
//////////////////////////////////////////////////////////////////////
// CHECK FOR MISSING AND NON-STANDARD FILE HEADERS
//////////////////////////////////////////////////////////////////////
static readonly int CD_LENGTH = Environment.CurrentDirectory.Length + 1;
static readonly string[] EXEMPT_FILES = new [] {
"AssemblyInfo.cs",
"Options.cs",
"ProcessUtils.cs",
"ProcessUtilsTests.cs"
};
// Standard Header. Change this for each project as needed.
static readonly string[] STD_HDR = new [] {
"// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt"
};
Task("CheckHeaders")
.Does(() =>
{
var NoHeader = new List<FilePath>();
var NonStandard = new List<FilePath>();
var Exempted = new List<FilePath>();
int examined = 0;
foreach(var file in GetFiles("src/**/*.cs"))
{
// Ignore autogenerated files in an obj directory
if (file.ToString().Contains("/obj/"))
continue;
examined++;
var header = GetHeader(file);
if (EXEMPT_FILES.Contains(file.GetFilename().ToString()))
Exempted.Add(file);
else if (header.Count == 0)
NoHeader.Add(file);
else if (!header.SequenceEqual(STD_HDR))
NonStandard.Add(file);
}
if (NoHeader.Count > 0)
{
Information("\nFILES WITH NO HEADER\n");
foreach(var file in NoHeader)
Information(RelPathTo(file));
}
if (NonStandard.Count > 0)
{
Information("\nFILES WITH A NON-STANDARD HEADER\n");
foreach(var file in NonStandard)
{
Information(RelPathTo(file));
Information("");
foreach(string line in GetHeader(file))
Information(line);
Information("");
}
}
if (Exempted.Count > 0)
{
Information("\nEXEMPTED FILES (NO CHECK MADE)\n");
foreach(var file in Exempted)
Information(RelPathTo(file));
}
Information($"\nFiles Examined: {examined}");
Information($"Missing Headers: {NoHeader.Count}");
Information($"Non-Standard Headers: {NonStandard.Count}");
Information($"Exempted Files: {Exempted.Count}");
if (NoHeader.Count > 0 || NonStandard.Count > 0)
throw new Exception("Missing or invalid file headers found");
});
private List<string> GetHeader(FilePath file)
{
var header = new List<string>();
var lines = System.IO.File.ReadLines(file.ToString());
foreach(string line in lines)
{
if (!line.StartsWith("//"))
break;
header.Add(line);
}
return header;
}
private string RelPathTo(FilePath file)
{
return file.ToString().Substring(CD_LENGTH);
}