forked from ReactiveX/RxSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate-headers.swift
executable file
·211 lines (167 loc) · 6.21 KB
/
validate-headers.swift
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/swift
//
// validate-headers.swift
// scripts
//
// Created by Krunoslav Zaher on 12/26/15.
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
//
import Foundation
/**
Validates that all headers are in this standard form
//
// {file}.swift
// Project
//
// Created by {Author} on 2/14/15.
// Copyright (c) 2015 Krunoslav Zaher. All rights reserved.
//
Only Project is not checked yet, but it will be soon.
*/
import Foundation
let fileManager = FileManager.default
let allowedExtensions = [
".swift",
".h",
".m",
]
let excludedRootPaths = [
".git",
"build",
"Rx.playground",
"vendor",
"Sources",
]
let excludePaths = [
"AllTestz/main.swift",
"Platform/Platform.Linux.swift",
"Platform/Platform.Darwin.swift",
"Platform/RecursiveLock.swift",
"Platform/DataStructures/Bag.swift",
"Platform/DataStructures/InfiniteSequence.swift",
"Platform/DataStructures/PriorityQueue.swift",
"Platform/DataStructures/Queue.swift",
"Platform/DispatchQueue+Extensions.swift",
"RxExample/Services/Reachability.swift",
"RxDataSources"
]
func isExtensionIncluded(path: String) -> Bool {
return (allowedExtensions.map { path.hasSuffix($0) }).reduce(false) { $0 || $1 }
}
let whitespace = NSCharacterSet.whitespacesAndNewlines
let identifier = "(?:\\w|\\+|\\_|\\.|-)+"
let fileLine = try NSRegularExpression(pattern: "// (\(identifier))", options: [])
let projectLine = try NSRegularExpression(pattern: "// (\(identifier))", options: [])
let createdBy = try NSRegularExpression(pattern: "// Created by .* on \\d+/\\d+/\\d+\\.", options: [])
let copyrightLine = try NSRegularExpression(pattern: "// Copyright © (\\d+) Krunoslav Zaher. All rights reserved.", options: [])
func validateRegexMatches(regularExpression: NSRegularExpression, content: String) -> ([String], Bool) {
let range = NSRange(location: 0, length: content.characters.count)
let matches = regularExpression.matches(in: content, options: [], range: range)
if matches.count == 0 {
print("ERROR: line `\(content)` is invalid: \(regularExpression.pattern)")
return ([], false)
}
for m in matches {
if m.numberOfRanges == 0 || !NSEqualRanges(m.range, range) {
print("ERROR: line `\(content)` is invalid: \(regularExpression.pattern)")
return ([], false)
}
}
return (matches[0 ..< matches.count].flatMap { m -> [String] in
return (1 ..< m.numberOfRanges).map { index in
return (content as NSString).substring(with: m.rangeAt(index))
}
}, true)
}
func validateHeader(path: String) throws -> Bool {
let contents = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
let rawLines = contents.components(separatedBy: "\n")
var lines = rawLines.map { $0.trimmingCharacters(in: whitespace) }
if (lines.first ?? "").hasPrefix("#") || (lines.first ?? "").hasPrefix("// This file is autogenerated.") {
lines.remove(at: 0)
}
if lines.count < 8 {
print("ERROR: Number of lines is less then 8, so the header can't be correct")
return false
}
for i in 0 ..< 7 {
if !lines[i].hasPrefix("//") {
print("ERROR: Line [\(i + 1)] (\(lines[i])) isn't prefixed with //")
return false
}
}
if lines[0] != "//" {
print("ERROR: Line[1] First line should be `//`")
return false
}
let (parsedFileLine, isValidFilename) = validateRegexMatches(regularExpression: fileLine, content: lines[1])
if !isValidFilename {
print("ERROR: Line[2] Filename line should match `\(fileLine.pattern)`")
return false
}
let fileNameInFile = parsedFileLine.first ?? ""
if fileNameInFile != (path as NSString).lastPathComponent {
print("ERROR: Line[2] invalid file name `\(fileNameInFile)`, correct content is `\((path as NSString).lastPathComponent)`")
return false
}
let (parsedProject, isValidProject) = validateRegexMatches(regularExpression: projectLine, content: lines[2])
let targetProject = path.components(separatedBy: "/")[0]
if !isValidProject || parsedProject.first != targetProject {
print("ERROR: Line[3] Line not equal to `// \(targetProject)`")
return false
}
if lines[3] != "//" {
print("ERROR: Line[4] Line should be `//`")
return false
}
let (_, isValidCreatedBy) = validateRegexMatches(regularExpression: createdBy, content: lines[4])
if !isValidCreatedBy {
print("ERROR: Line[5] Line not matching \(createdBy.pattern)")
return false
}
let (year, isValidCopyright) = validateRegexMatches(regularExpression: copyrightLine, content: lines[5])
if !isValidCopyright {
print("ERROR: Line[6] Line not matching \(copyrightLine.pattern)")
return false
}
let currentYear = Calendar.current.component(.year, from: Date())
if year.first == nil || !(2015...currentYear).contains(Int(year.first!) ?? 0) {
print("ERROR: Line[6] Wrong copyright year \(year.first ?? "?") instead of 2015...\(currentYear)")
return false
}
if lines[6] != "//" {
print("ERROR: Line[7] Line not matching \(copyrightLine.pattern)")
return false
}
if lines[7] != "" {
print("ERROR: Line[8] Should be blank and not `\(lines[7])`")
return false
}
return true
}
func verifyAll(root: String) throws -> Bool {
return try fileManager.subpathsOfDirectory(atPath: root).map { file -> Bool in
let excluded = excludePaths.map { file.hasPrefix($0) }.reduce(false) { $0 || $1 }
if excluded {
return true
}
if !isExtensionIncluded(path: file) {
return true
}
let isValid = try validateHeader(path: "\(root)/\(file)")
if !isValid {
print(" while Validating '\(root)/\(file)'")
}
return isValid
}.reduce(true) { $0 && $1 }
}
let allValid = try fileManager.contentsOfDirectory(atPath: ".").map { rootDir -> Bool in
if excludedRootPaths.contains(rootDir) {
print("Skipping \(rootDir)")
return true
}
return try verifyAll(root: rootDir)
}.reduce(true) { $0 && $1 }
if !allValid {
exit(-1)
}