Skip to content

Commit ef063a7

Browse files
committed
Add non-source files from source directories
1 parent b2d803e commit ef063a7

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

Sources/Xcodeproj/generate().swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,16 @@ public func generate(
6969
// references in the project.
7070
let extraDirs = try findDirectoryReferences(path: srcroot)
7171

72+
// Find non-source files in the source directories that should be added
73+
// as a reference to the project.
74+
var extraFiles = try findNonSourceFiles(path: srcroot.appending(component: "Sources"))
75+
extraFiles.append(contentsOf: try findNonSourceFiles(path: srcroot.appending(component: "Tests")))
76+
7277
/// Generate the contents of project.xcodeproj (inside the .xcodeproj).
7378
try open(xcodeprojPath.appending(component: "project.pbxproj")) { stream in
7479
// FIXME: This could be more efficient by directly writing to a stream
7580
// instead of first creating a string.
76-
let str = try pbxproj(xcodeprojPath: xcodeprojPath, graph: graph, extraDirs: extraDirs, options: options)
81+
let str = try pbxproj(xcodeprojPath: xcodeprojPath, graph: graph, extraDirs: extraDirs, extraFiles: extraFiles, options: options)
7782
stream(str)
7883
}
7984

@@ -181,3 +186,17 @@ func findDirectoryReferences(path: AbsolutePath) throws -> [AbsolutePath] {
181186
return isDirectory($0)
182187
})
183188
}
189+
190+
/// Finds the non-source files from `path` recursively
191+
func findNonSourceFiles(path: AbsolutePath) throws -> [AbsolutePath] {
192+
let filesFromPath = try walk(path, recursively: true)
193+
194+
return filesFromPath.filter {
195+
if !isFile($0) { return false }
196+
if let `extension` = $0.extension {
197+
if SupportedLanguageExtension.cFamilyExtensions.contains(`extension`) { return false }
198+
if SupportedLanguageExtension.swiftExtensions.contains(`extension`) { return false }
199+
}
200+
return true
201+
}
202+
}

Sources/Xcodeproj/pbxproj().swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ public func pbxproj(
3232
xcodeprojPath: AbsolutePath,
3333
graph: PackageGraph,
3434
extraDirs: [AbsolutePath],
35+
extraFiles: [AbsolutePath],
3536
options: XcodeprojOptions,
3637
fileSystem: FileSystem = localFileSystem
3738
) throws -> String {
3839
let project = try xcodeProject(
3940
xcodeprojPath: xcodeprojPath,
4041
graph: graph,
4142
extraDirs: extraDirs,
43+
extraFiles: extraFiles,
4244
options: options,
4345
fileSystem: fileSystem)
4446
// Serialize the project model we created to a plist, and return
@@ -56,6 +58,7 @@ func xcodeProject(
5658
xcodeprojPath: AbsolutePath,
5759
graph: PackageGraph,
5860
extraDirs: [AbsolutePath],
61+
extraFiles: [AbsolutePath],
5962
options: XcodeprojOptions,
6063
fileSystem: FileSystem,
6164
warningStream: OutputByteStream = stdoutStream
@@ -294,7 +297,6 @@ func xcodeProject(
294297
let name = (sourcesGroup == nil ? groupName : target.name)
295298
let group = (sourcesGroup ?? parentGroup)
296299
.addGroup(path: (path == "." ? "" : path), pathBase: .projectDir, name: name)
297-
298300
// Associate the group with the target's root path.
299301
srcPathsToGroups[target.sources.root] = group
300302
}
@@ -320,6 +322,11 @@ func xcodeProject(
320322
project.mainGroup.addFileReference(path: extraDir.relative(to: sourceRootDir).asString, pathBase: .projectDir)
321323
}
322324

325+
for extraFile in extraFiles {
326+
let groupOfFile = srcPathsToGroups[extraFile.parentDirectory]
327+
groupOfFile?.addFileReference(path: extraFile.basename)
328+
}
329+
323330
// Determine the set of targets to generate in the project by excluding
324331
// any system targets.
325332
let targets = graph.reachableTargets.filter({ $0.type != .systemModule })

0 commit comments

Comments
 (0)