Skip to content

Commit

Permalink
Added --ignore-filename-regex
Browse files Browse the repository at this point in the history
  • Loading branch information
khvorost-dish committed Feb 7, 2024
1 parent c1cdab2 commit 02a5e5a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
20 changes: 17 additions & 3 deletions Sources/SwiftDocCoverage/Coverage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ extension String : LocalizedError {
public var errorDescription: String? { self }
}

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

if isDirectory.boolValue {
var urls = [URL]()

let regex: NSRegularExpression? = ignoreFilenameRegex.isEmpty ? nil : try NSRegularExpression(pattern: ignoreFilenameRegex)

let url = URL(fileURLWithPath: path)
let resourceKeys = Set<URLResourceKey>([.nameKey, .isDirectoryKey])
let options: FileManager.DirectoryEnumerationOptions = skipsHiddenFiles ? [.skipsHiddenFiles] : []
Expand All @@ -49,6 +52,15 @@ fileprivate func findFiles(path: String, ext: String, skipsHiddenFiles: Bool) th
continue
}

// Skip by regex
if let regex = regex {
let fileName = fileURL.lastPathComponent
let range = NSRange(location: 0, length: fileName.utf16.count)
if regex.firstMatch(in: fileName, range: range) != nil {
continue
}
}

urls.append(fileURL)
}
}
Expand All @@ -74,8 +86,10 @@ public struct Coverage {
return formatter
}()

public init(paths: [String], skipsHiddenFiles: Bool = true, minAccessLevel: AccessLevel = .public, output: Output = TerminalOutput()) throws {
self.urls = try paths.flatMap { try findFiles(path: $0, ext: ".swift", skipsHiddenFiles: skipsHiddenFiles) }
public init(paths: [String], skipsHiddenFiles: Bool = true, ignoreFilenameRegex: String = "", minAccessLevel: AccessLevel = .public, output: Output = TerminalOutput()) throws {
self.urls = try paths.flatMap {
try findFiles(path: $0, ext: ".swift", skipsHiddenFiles: skipsHiddenFiles, ignoreFilenameRegex: ignoreFilenameRegex)
}
guard urls.count > 0 else {
throw "Swift files not found."
}
Expand Down
10 changes: 9 additions & 1 deletion Sources/swift-doc-coverage/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ struct SwiftDocCoverage: ParsableCommand {
@Option(name: .shortAndLong, help: "An option to skip hidden files.")
var skipsHiddenFiles: Bool = true

@Option(name: .shortAndLong, help: "Skip source code files with file paths that match the given regular expression.")
var ignoreFilenameRegex: String = ""

@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).")
var minimumAccessLevel: AccessLevelArgument = .public

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

let coverage = try Coverage(paths: inputs, skipsHiddenFiles: skipsHiddenFiles, minAccessLevel: minimumAccessLevel.accessLevel, output: out)
let coverage = try Coverage(
paths: inputs,
skipsHiddenFiles: skipsHiddenFiles,
ignoreFilenameRegex: ignoreFilenameRegex,
minAccessLevel: minimumAccessLevel.accessLevel,
output: out)

switch report {
case .statistics:
Expand Down

0 comments on commit 02a5e5a

Please sign in to comment.