Skip to content

Commit 7dfa398

Browse files
committed
Refactor the finding and filtering of non-source files into a new function
Pass all the extra file paths to `isGitIgnored` at once to improve performance
1 parent bdc6f70 commit 7dfa398

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

Sources/Xcodeproj/generate().swift

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,11 @@ public func generate(
7979
// references in the project.
8080
let extraDirs = try findDirectoryReferences(path: srcroot)
8181

82-
// Find non-source files in the source directories and root that should be added
83-
// as a reference to the project.
84-
var extraFiles = try findNonSourceFiles(path: srcroot)
85-
86-
let sourcesDirectory = srcroot.appending(component: "Sources")
87-
if localFileSystem.isDirectory(sourcesDirectory) {
88-
let sourcesExtraFiles = try findNonSourceFiles(path: sourcesDirectory, recursively: true)
89-
extraFiles.append(contentsOf: sourcesExtraFiles)
90-
}
91-
92-
let testsDirectory = srcroot.appending(component: "Tests")
93-
if localFileSystem.isDirectory(testsDirectory) {
94-
let testsExtraFiles = try findNonSourceFiles(path: testsDirectory, recursively: true)
95-
extraFiles.append(contentsOf: testsExtraFiles)
96-
}
97-
82+
var workingCheckout: WorkingCheckout?
9883
if try repositoryProvider.checkoutExists(at: srcroot) {
99-
// Get the ignored files and exclude them
100-
let repository = try repositoryProvider.openCheckout(at: srcroot)
101-
extraFiles = try extraFiles.filter({ try !repository.isIgnored($0) })
84+
workingCheckout = try repositoryProvider.openCheckout(at: srcroot)
10285
}
86+
let extraFiles = try getExtraFilesFor(package: graph.rootPackages[0], in: workingCheckout)
10387

10488
/// Generate the contents of project.xcodeproj (inside the .xcodeproj).
10589
// FIXME: This could be more efficient by directly writing to a stream
@@ -243,6 +227,28 @@ func generateSchemes(
243227
}
244228
}
245229

230+
// Find and return non-source files in the source directories and root that should be added
231+
// as a reference to the project.
232+
func getExtraFilesFor(package: ResolvedPackage, in workingCheckout: WorkingCheckout? = nil) throws -> [AbsolutePath] {
233+
let srcroot = package.path
234+
var extraFiles = try findNonSourceFiles(path: srcroot)
235+
236+
for target in package.targets {
237+
let sourcesDirectory = target.sources.root
238+
if localFileSystem.isDirectory(sourcesDirectory) {
239+
let sourcesExtraFiles = try findNonSourceFiles(path: sourcesDirectory, recursively: true)
240+
extraFiles.append(contentsOf: sourcesExtraFiles)
241+
}
242+
}
243+
244+
if let checkout = workingCheckout {
245+
let isIgnored = try checkout.isGitIgnored(extraFiles)
246+
extraFiles = extraFiles.enumerated().filter({ !isIgnored[$0.offset] }).map({ $0.element })
247+
}
248+
249+
return extraFiles
250+
}
251+
246252
/// Finds the non-source files from `path`
247253
/// - parameters:
248254
/// - path: The path of the directory to get the files from

0 commit comments

Comments
 (0)