Skip to content

Commit 54d1b13

Browse files
committed
Improve handling of "extra" files
In swiftlang#1507, we started adding "extra" files to the generated Xcode project. This makes some enhancements to that feature: - Introduce a new flag `--skip-extra-files` to go back to the pre-4.2 behaviour (best behaviour) of not including these files at all - Do not add a file reference for "Package.resolved" since this shouldn't be edited by hand and most people also never have to read it - Move the "extra" file references after the products group. This may be debatable, but I think it more clean to have all file references related to the package manifest grouped together at the top instead of having these interspersed throughout the main group.
1 parent 5f63b90 commit 54d1b13

File tree

4 files changed

+38
-19
lines changed

4 files changed

+38
-19
lines changed

Sources/Commands/SwiftPackageTool.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,13 @@ public class SwiftPackageTool: SwiftTool<PackageToolOptions> {
373373
generateXcodeParser.add(
374374
option: "--watch", kind: Bool.self,
375375
usage: "Watch for changes to the Package manifest to regenerate the Xcode project"),
376+
generateXcodeParser.add(
377+
option: "--skip-extra-files", kind: Bool.self,
378+
usage: "Do not add file references for extra files to the generated Xcode project"),
376379
to: {
377380
$0.xcodeprojOptions.useLegacySchemeGenerator = $1 ?? false
378381
$0.xcodeprojOptions.enableAutogeneration = $2 ?? false
382+
$0.xcodeprojOptions.addExtraFiles = !($3 ?? false)
379383
})
380384

381385
let completionToolParser = parser.add(

Sources/Xcodeproj/generate().swift

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public struct XcodeprojOptions {
3434
/// Run watchman to auto-generate the project file on changes.
3535
public var enableAutogeneration: Bool
3636

37+
/// Whether to add extra files to the generated project.
38+
public var addExtraFiles: Bool
39+
3740
/// Reference to manifest loader, if present.
3841
public var manifestLoader: ManifestLoader?
3942

@@ -42,13 +45,15 @@ public struct XcodeprojOptions {
4245
xcconfigOverrides: AbsolutePath? = nil,
4346
isCodeCoverageEnabled: Bool? = nil,
4447
useLegacySchemeGenerator: Bool? = nil,
45-
enableAutogeneration: Bool? = nil
48+
enableAutogeneration: Bool? = nil,
49+
addExtraFiles: Bool? = nil
4650
) {
4751
self.flags = flags
4852
self.xcconfigOverrides = xcconfigOverrides
4953
self.isCodeCoverageEnabled = isCodeCoverageEnabled ?? false
5054
self.useLegacySchemeGenerator = useLegacySchemeGenerator ?? false
5155
self.enableAutogeneration = enableAutogeneration ?? false
56+
self.addExtraFiles = addExtraFiles ?? true
5257
}
5358
}
5459

@@ -83,14 +88,20 @@ public func generate(
8388
try makeDirectories(xcodeprojPath)
8489
try makeDirectories(schemesDir)
8590

86-
// Find the paths of any extra directories that should be added as folder
87-
// references in the project.
88-
let extraDirs = try findDirectoryReferences(path: srcroot)
89-
91+
let extraDirs: [AbsolutePath]
9092
var extraFiles = [AbsolutePath]()
91-
if try repositoryProvider.checkoutExists(at: srcroot) {
92-
let workingCheckout = try repositoryProvider.openCheckout(at: srcroot)
93-
extraFiles = try getExtraFilesFor(package: graph.rootPackages[0], in: workingCheckout)
93+
94+
if options.addExtraFiles {
95+
// Find the paths of any extra directories that should be added as folder
96+
// references in the project.
97+
extraDirs = try findDirectoryReferences(path: srcroot)
98+
99+
if try repositoryProvider.checkoutExists(at: srcroot) {
100+
let workingCheckout = try repositoryProvider.openCheckout(at: srcroot)
101+
extraFiles = try getExtraFilesFor(package: graph.rootPackages[0], in: workingCheckout)
102+
}
103+
} else {
104+
extraDirs = []
94105
}
95106

96107
/// Generate the contents of project.xcodeproj (inside the .xcodeproj).
@@ -265,6 +276,7 @@ func findNonSourceFiles(path: AbsolutePath, recursively: Bool = false) throws ->
265276
return filesFromPath.filter({
266277
if !isFile($0) { return false }
267278
if $0.basename.hasPrefix(".") { return false }
279+
if $0.basename == "Package.resolved" { return false }
268280
if let `extension` = $0.extension, SupportedLanguageExtension.validExtensions.contains(`extension`) {
269281
return false
270282
}

Sources/Xcodeproj/pbxproj().swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -319,17 +319,6 @@ func xcodeProject(
319319
// Create a `Tests` group for the source targets in the root package.
320320
createSourceGroup(named: "Tests", for: testModules, in: project.mainGroup)
321321

322-
// Add "blue folders" for any other directories at the top level (note that
323-
// they are not guaranteed to be direct children of the root directory).
324-
for extraDir in extraDirs {
325-
project.mainGroup.addFileReference(path: extraDir.relative(to: sourceRootDir).asString, pathBase: .projectDir)
326-
}
327-
328-
for extraFile in extraFiles {
329-
let groupOfFile = srcPathsToGroups[extraFile.parentDirectory]
330-
groupOfFile?.addFileReference(path: extraFile.basename)
331-
}
332-
333322
// Determine the set of targets to generate in the project by excluding
334323
// any system targets.
335324
let targets = graph.reachableTargets.filter({ $0.type != .systemModule })
@@ -372,6 +361,17 @@ func xcodeProject(
372361
// the various targets; these references will be added to the link phases.
373362
let productsGroup = project.mainGroup.addGroup(path: "", pathBase: .buildDir, name: "Products")
374363

364+
// Add "blue folders" for any other directories at the top level (note that
365+
// they are not guaranteed to be direct children of the root directory).
366+
for extraDir in extraDirs {
367+
project.mainGroup.addFileReference(path: extraDir.relative(to: sourceRootDir).asString, pathBase: .projectDir)
368+
}
369+
370+
for extraFile in extraFiles {
371+
let groupOfFile = srcPathsToGroups[extraFile.parentDirectory]
372+
groupOfFile?.addFileReference(path: extraFile.basename)
373+
}
374+
375375
// Set the newly created `Products` group as the official products group of
376376
// the project.
377377
project.productGroup = productsGroup

Tests/XcodeprojTests/GenerateXcodeprojTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ class GenerateXcodeprojTests: XCTestCase {
231231
let project = try Xcodeproj.generate(projectName: projectName, xcodeprojPath: outpath, graph: graph, options: XcodeprojOptions(), diagnostics: diagnostics)
232232

233233
XCTAssertTrue(project.mainGroup.subitems.contains { $0.path == "a.txt" })
234+
235+
let projectWithoutExtraFiles = try Xcodeproj.generate(projectName: projectName, xcodeprojPath: outpath, graph: graph, options: XcodeprojOptions(addExtraFiles: false), diagnostics: diagnostics)
236+
XCTAssertFalse(projectWithoutExtraFiles.mainGroup.subitems.contains { $0.path == "a.txt" })
234237
}
235238
}
236239

0 commit comments

Comments
 (0)