Skip to content

Commit a2d2ad8

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 6e2ce11 commit a2d2ad8

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

Sources/Xcodeproj/generate().swift

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,11 @@ public func generate(
8484
// references in the project.
8585
let extraDirs = try findDirectoryReferences(path: srcroot)
8686

87-
// Find non-source files in the source directories and root that should be added
88-
// as a reference to the project.
89-
var extraFiles = try findNonSourceFiles(path: srcroot)
90-
91-
let sourcesDirectory = srcroot.appending(component: "Sources")
92-
if localFileSystem.isDirectory(sourcesDirectory) {
93-
let sourcesExtraFiles = try findNonSourceFiles(path: sourcesDirectory, recursively: true)
94-
extraFiles.append(contentsOf: sourcesExtraFiles)
95-
}
96-
97-
let testsDirectory = srcroot.appending(component: "Tests")
98-
if localFileSystem.isDirectory(testsDirectory) {
99-
let testsExtraFiles = try findNonSourceFiles(path: testsDirectory, recursively: true)
100-
extraFiles.append(contentsOf: testsExtraFiles)
101-
}
102-
103-
// Get the ignored files and exclude them
87+
var workingCheckout: WorkingCheckout?
10488
if try repositoryProvider.checkoutExists(at: srcroot) {
105-
let repository = try repositoryProvider.openCheckout(at: srcroot)
106-
let areIgnored = try repository.areIgnored(extraFiles)
107-
extraFiles = extraFiles.enumerated().filter({ !areIgnored[$0.offset] }).map({ $0.element })
89+
workingCheckout = try repositoryProvider.openCheckout(at: srcroot)
10890
}
91+
let extraFiles = try getExtraFilesFor(package: graph.rootPackages[0], in: workingCheckout)
10992

11093
/// Generate the contents of project.xcodeproj (inside the .xcodeproj).
11194
// FIXME: This could be more efficient by directly writing to a stream
@@ -249,7 +232,28 @@ func generateSchemes(
249232
}
250233
}
251234

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

Tests/XcodeprojTests/GenerateXcodeprojTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,8 @@ class GenerateXcodeprojTests: XCTestCase {
183183
("testXcodebuildCanParseIt", testXcodebuildCanParseIt),
184184
("testXcconfigOverrideValidatesPath", testXcconfigOverrideValidatesPath),
185185
("testGenerateXcodeprojWithInvalidModuleNames", testGenerateXcodeprojWithInvalidModuleNames),
186+
("testGenerateXcodeprojWithRootFiles", testGenerateXcodeprojWithRootFiles),
187+
("testGenerateXcodeprojWithNonSourceFilesInSourceDirectories", testGenerateXcodeprojWithNonSourceFilesInSourceDirectories),
188+
("testGenerateXcodeprojWithFilesIgnoredByGit", testGenerateXcodeprojWithFilesIgnoredByGit),
186189
]
187190
}

0 commit comments

Comments
 (0)