Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 5738c2a

Browse files
matttLukas-Stuehrk
andauthored
Improve file enumeration in generate subcommand (#229)
* Include isDirectory key in file enumerator Skip hidden files and package descendents during file enumeration * Skip top-level Tests directory * Update end-to-end tests to pass current directory as argument * Add changelog entry for #229 * Use skipDescendants, which is available on Linux. * Document new directory enumeration behavior in README Co-authored-by: Lukas Stührk <[email protected]>
1 parent 467cf38 commit 5738c2a

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525

2626
- Changed display of code declarations in HTML.
2727
#204 by @mattt.
28+
- Changed the `generate` command to skip hidden files
29+
and top-level `Tests` directories.
30+
#229 by @mattt.
2831

2932
## [1.0.0-beta.5] - 2020-09-29
3033

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,16 @@ $ apt-get install -y libxml2-dev graphviz
107107
be included. (default: public)
108108
-h, --help Show help information.
109109

110-
The `generate` subcommand
110+
The `generate` subcommand
111111
takes one or more paths and enumerates them recursively,
112112
collecting all Swift files into a single "module"
113113
and generating documentation accordingly.
114+
Any hidden directories are skipped,
115+
including `.git` and other directories with paths starting with a dot (`.`).
116+
Top-level `Tests` directories are skipped as well.
114117

115118
```terminal
116-
$ swift doc generate path/to/SwiftProject/Sources --module-name SwiftProject
119+
$ swift doc generate path/to/SwiftProject --module-name SwiftProject
117120
$ tree .build/documentation
118121
$ documentation/
119122
├── Home

Sources/SwiftDoc/Module.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,27 @@ public final class Module {
2222
let fileManager = FileManager.default
2323
for path in paths {
2424
let directory = URL(fileURLWithPath: path)
25-
guard let directoryEnumerator = fileManager.enumerator(at: directory, includingPropertiesForKeys: nil) else { continue }
25+
guard let directoryEnumerator = fileManager.enumerator(at: directory, includingPropertiesForKeys: [.isDirectoryKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) else { continue }
2626
for case let url as URL in directoryEnumerator {
2727
var isDirectory: ObjCBool = false
2828
guard url.pathExtension == "swift",
2929
fileManager.isReadableFile(atPath: url.path),
30-
fileManager.fileExists(atPath: url.path, isDirectory: &isDirectory),
31-
isDirectory.boolValue == false
32-
else { continue }
33-
sources.append((url, directory))
30+
fileManager.fileExists(atPath: url.path, isDirectory: &isDirectory)
31+
else {
32+
// Skip top-level Tests directory
33+
if isDirectory.boolValue == true,
34+
url.lastPathComponent == "Tests",
35+
directory.appendingPathComponent("Tests").path == url.path
36+
{
37+
directoryEnumerator.skipDescendants()
38+
}
39+
40+
continue
41+
}
42+
43+
if isDirectory.boolValue == false {
44+
sources.append((url, directory))
45+
}
3446
}
3547
}
3648

Tests/EndToEndTests/GenerateSubcommandTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class GenerateSubcommandTests: XCTestCase {
1313
"--module-name", "SwiftDoc",
1414
"--format", "commonmark",
1515
"--output", outputDirectory.path,
16-
"Sources"
16+
"."
1717
]
1818
) { result in
1919
XCTAssertEqual(result.terminationStatus, EXIT_SUCCESS)
@@ -54,7 +54,7 @@ final class GenerateSubcommandTests: XCTestCase {
5454
"--module-name", "SwiftDoc",
5555
"--format", "html",
5656
"--output", outputDirectory.path,
57-
"Sources"
57+
"."
5858
]
5959
) { result in
6060
XCTAssertEqual(result.terminationStatus, EXIT_SUCCESS)

0 commit comments

Comments
 (0)