Skip to content

Commit a3d8002

Browse files
committed
Make Xcodeproj testable
The Xcodeproj module became much easier to test when I removed the dependency on Package from Xcodeproj, instead we pass packageName and srcroot.
1 parent 6008cef commit a3d8002

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed

Sources/Xcodeproj/generate().swift

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,33 @@ import POSIX
1616
Generates an xcodeproj at the specified path.
1717
- Returns: the path to the generated project
1818
*/
19-
public func generate(path path: String, package: Package, modules: [SwiftModule], products: [Product]) throws -> String {
19+
public func generate(dstdir dstdir: String, projectName: String, srcroot: String, modules: [SwiftModule], products: [Product]) throws -> String {
2020

21-
/// If a specific *.xcodeproj path is already passed in, use that.
22-
/// Otherwise treat the path as the desired enclosing folder for
23-
/// the .xcodeproj folder.
24-
let rootdir = path.hasSuffix(".xcodeproj") ? path : Path.join(path, "\(package.name).xcodeproj")
25-
try mkdir(rootdir)
26-
27-
let schemedir = try mkdir(rootdir, "xcshareddata/xcschemes")
21+
let xcodeprojName = "\(projectName).xcodeproj"
22+
let xcodeprojPath = try mkdir(dstdir, xcodeprojName)
23+
let schemesDirectory = try mkdir(xcodeprojPath, "xcshareddata/xcschemes")
24+
let schemeName = "\(projectName).xcscheme"
2825

2926
////// the pbxproj file describes the project and its targets
30-
try open(rootdir, "project.pbxproj") { fwrite in
31-
pbxproj(projectPath: path, package: package, modules: modules, products: products, printer: fwrite)
27+
try open(xcodeprojPath, "project.pbxproj") { fwrite in
28+
pbxproj(srcroot: srcroot, projectRoot: dstdir, modules: modules, products: products, printer: fwrite)
3229
}
3330

3431
////// the scheme acts like an aggregate target for all our targets
3532
/// it has all tests associated so CMD+U works
36-
try open(schemedir, "\(package.name).xcscheme") { fwrite in
37-
xcscheme(packageName: package.name, modules: modules, printer: fwrite)
33+
try open(schemesDirectory, schemeName) { fwrite in
34+
xcscheme(container: xcodeprojName, modules: modules, printer: fwrite)
3835
}
3936

4037
////// we generate this file to ensure our main scheme is listed
4138
/// before any inferred schemes Xcode may autocreate
42-
try open(schemedir, "xcschememanagement.plist") { fwrite in
39+
try open(schemesDirectory, "xcschememanagement.plist") { fwrite in
4340
fwrite("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
4441
fwrite("<plist version=\"1.0\">")
4542
fwrite("<dict>")
4643
fwrite(" <key>SchemeUserState</key>")
4744
fwrite(" <dict>")
48-
fwrite(" <key>\(package.name).xcscheme</key>")
45+
fwrite(" <key>\(schemeName)</key>")
4946
fwrite(" <dict></dict>")
5047
fwrite(" </dict>")
5148
fwrite(" <key>SuppressBuildableAutocreation</key>")
@@ -54,7 +51,7 @@ public func generate(path path: String, package: Package, modules: [SwiftModule]
5451
fwrite("</plist>")
5552
}
5653

57-
return rootdir
54+
return xcodeprojPath
5855
}
5956

6057

Sources/Xcodeproj/pbxproj().swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
import PackageType
1616
import Utility
1717

18-
public func pbxproj(projectPath projectPath: String, package: Package, modules: [SwiftModule], products _: [Product], printer print: (String) -> Void) {
19-
20-
let srcroot = projectPath
18+
public func pbxproj(srcroot srcroot: String, projectRoot: String, modules: [SwiftModule], products _: [Product], printer print: (String) -> Void) {
2119
let nontests = modules.filter{ !($0 is TestModule) }
2220
let tests = modules.filter{ $0 is TestModule }
2321

@@ -60,7 +58,7 @@ public func pbxproj(projectPath projectPath: String, package: Package, modules:
6058
print(" \(module.groupReference) = {")
6159
print(" isa = PBXGroup;")
6260
print(" name = \(module.name);")
63-
print(" path = '\(Path(module.sources.root).relative(to: srcroot))';")
61+
print(" path = '\(Path(module.sources.root).relative(to: projectRoot))';")
6462
print(" sourceTree = '<group>';")
6563
print(" children = (" + fileRefs(forModuleSources: module, srcroot: srcroot).map{$0.0}.joined(separator: ", ") + ");")
6664
print(" };")

Sources/Xcodeproj/xcscheme().swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import PackageType
1212

13-
func xcscheme(packageName packageName: String, modules: [SwiftModule], printer print: (String) -> Void) {
13+
func xcscheme(container container: String, modules: [SwiftModule], printer print: (String) -> Void) {
1414
print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
1515
print("<Scheme LastUpgradeVersion = \"9999\" version = \"1.3\">")
1616
print(" <BuildAction parallelizeBuildables = \"YES\" buildImplicitDependencies = \"YES\">")
@@ -26,7 +26,7 @@ func xcscheme(packageName packageName: String, modules: [SwiftModule], printer p
2626
print(" BlueprintIdentifier = \"\(module.blueprintIdentifier)\"")
2727
print(" BuildableName = \"\(module.buildableName)\"")
2828
print(" BlueprintName = \"\(module.blueprintName)\"")
29-
print(" ReferencedContainer = \"container:\(packageName).xcodeproj\">")
29+
print(" ReferencedContainer = \"container:\(container)\">")
3030
print(" </BuildableReference>")
3131
print(" </BuildActionEntry>")
3232
}
@@ -48,7 +48,7 @@ func xcscheme(packageName packageName: String, modules: [SwiftModule], printer p
4848
print(" BlueprintIdentifier = \"\(module.blueprintIdentifier)\"")
4949
print(" BuildableName = \"\(module.buildableName)\"")
5050
print(" BlueprintName = \"\(module.blueprintName)\"")
51-
print(" ReferencedContainer = \"container:\(packageName).xcodeproj\">")
51+
print(" ReferencedContainer = \"container:\(container)\">")
5252
print(" </BuildableReference>")
5353
print(" </TestableReference>")
5454
}

Sources/swift-build/main.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,27 @@ do {
9696
let (modules, products) = try transmute(packages, rootdir: dirs.root)
9797
let swiftModules = modules.flatMap{ $0 as? SwiftModule }
9898

99-
let path = try Xcodeproj.generate(path: outpath ?? dirs.root, package: packages.last!, modules: swiftModules, products: products)
100-
101-
print("generated:", path.prettied)
99+
let projectName: String
100+
let dstdir: String
101+
let packageName = packages.last!.name //FIXME coincidental dependency on order
102+
103+
switch outpath {
104+
case let outpath? where outpath.hasSuffix(".xcodeproj"):
105+
// if user specified path ending with .xcodeproj, generate that
106+
"\(packageName).xcodeproj"
107+
projectName = String(outpath.basename.characters.dropLast(10))
108+
dstdir = outpath.parentDirectory
109+
case let outpath?:
110+
dstdir = outpath
111+
projectName = packageName
112+
case _:
113+
dstdir = dirs.root
114+
projectName = packageName
115+
}
116+
117+
let outpath = try Xcodeproj.generate(dstdir: dstdir, projectName: projectName, srcroot: dirs.root, modules: swiftModules, products: products)
118+
119+
print("generated:", outpath.prettied)
102120
}
103121

104122
} catch {

0 commit comments

Comments
 (0)