Skip to content

Commit 02a5e5a

Browse files
committed
Added --ignore-filename-regex
1 parent c1cdab2 commit 02a5e5a

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

Sources/SwiftDocCoverage/Coverage.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ extension String : LocalizedError {
2828
public var errorDescription: String? { self }
2929
}
3030

31-
fileprivate func findFiles(path: String, ext: String, skipsHiddenFiles: Bool) throws -> [URL] {
31+
fileprivate func findFiles(path: String, ext: String, skipsHiddenFiles: Bool, ignoreFilenameRegex: String) throws -> [URL] {
3232
var isDirectory: ObjCBool = false
3333
guard FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory) else {
3434
throw "Path not found."
3535
}
3636

3737
if isDirectory.boolValue {
3838
var urls = [URL]()
39+
40+
let regex: NSRegularExpression? = ignoreFilenameRegex.isEmpty ? nil : try NSRegularExpression(pattern: ignoreFilenameRegex)
41+
3942
let url = URL(fileURLWithPath: path)
4043
let resourceKeys = Set<URLResourceKey>([.nameKey, .isDirectoryKey])
4144
let options: FileManager.DirectoryEnumerationOptions = skipsHiddenFiles ? [.skipsHiddenFiles] : []
@@ -49,6 +52,15 @@ fileprivate func findFiles(path: String, ext: String, skipsHiddenFiles: Bool) th
4952
continue
5053
}
5154

55+
// Skip by regex
56+
if let regex = regex {
57+
let fileName = fileURL.lastPathComponent
58+
let range = NSRange(location: 0, length: fileName.utf16.count)
59+
if regex.firstMatch(in: fileName, range: range) != nil {
60+
continue
61+
}
62+
}
63+
5264
urls.append(fileURL)
5365
}
5466
}
@@ -74,8 +86,10 @@ public struct Coverage {
7486
return formatter
7587
}()
7688

77-
public init(paths: [String], skipsHiddenFiles: Bool = true, minAccessLevel: AccessLevel = .public, output: Output = TerminalOutput()) throws {
78-
self.urls = try paths.flatMap { try findFiles(path: $0, ext: ".swift", skipsHiddenFiles: skipsHiddenFiles) }
89+
public init(paths: [String], skipsHiddenFiles: Bool = true, ignoreFilenameRegex: String = "", minAccessLevel: AccessLevel = .public, output: Output = TerminalOutput()) throws {
90+
self.urls = try paths.flatMap {
91+
try findFiles(path: $0, ext: ".swift", skipsHiddenFiles: skipsHiddenFiles, ignoreFilenameRegex: ignoreFilenameRegex)
92+
}
7993
guard urls.count > 0 else {
8094
throw "Swift files not found."
8195
}

Sources/swift-doc-coverage/main.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ struct SwiftDocCoverage: ParsableCommand {
5959
@Option(name: .shortAndLong, help: "An option to skip hidden files.")
6060
var skipsHiddenFiles: Bool = true
6161

62+
@Option(name: .shortAndLong, help: "Skip source code files with file paths that match the given regular expression.")
63+
var ignoreFilenameRegex: String = ""
64+
6265
@Option(name: .shortAndLong, help: "The minimum access level of the symbols considered for coverage statistics: \(AccessLevelArgument.open), \(AccessLevelArgument.public), \(AccessLevelArgument.internal), \(AccessLevelArgument.fileprivate), \(AccessLevelArgument.private).")
6366
var minimumAccessLevel: AccessLevelArgument = .public
6467

@@ -74,7 +77,12 @@ struct SwiftDocCoverage: ParsableCommand {
7477
out = try FileOutput(path: path)
7578
}
7679

77-
let coverage = try Coverage(paths: inputs, skipsHiddenFiles: skipsHiddenFiles, minAccessLevel: minimumAccessLevel.accessLevel, output: out)
80+
let coverage = try Coverage(
81+
paths: inputs,
82+
skipsHiddenFiles: skipsHiddenFiles,
83+
ignoreFilenameRegex: ignoreFilenameRegex,
84+
minAccessLevel: minimumAccessLevel.accessLevel,
85+
output: out)
7886

7987
switch report {
8088
case .statistics:

0 commit comments

Comments
 (0)