Skip to content

Commit 434dfec

Browse files
authored
Add excludeFromProject option for local packages (#1512)
1 parent b1e03f0 commit 434dfec

12 files changed

Lines changed: 79 additions & 31 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ XcodeGen.xcodeproj
99
xcodegen.zip
1010
xcodegen.artifactbundle.zip
1111
.vscode/launch.json
12+
DerivedData

Docs/ProjectSpec.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,7 @@ Swift packages are defined at a project level, and then linked to individual tar
12461246

12471247
- [x] **path**: **String** - the path to the package in local. The path must be directory with a `Package.swift`.
12481248
- [ ] **group** : **String**- Optional path that specifies the location where the package will live in your xcode project. Use `""` to specify the project root.
1249+
- [ ] **excludeFromProject** : **String**- Optional flag to exclude the package from the generated project (useful if the package is already added via xcworkspace and the project is not intended for standalone use), defaults to `false`
12491250

12501251
```yml
12511252
packages:
@@ -1260,6 +1261,7 @@ packages:
12601261
AppFeature:
12611262
path: ../Packages
12621263
group: Domains/AppFeature
1264+
excludeFromProject: false
12631265
```
12641266

12651267
## Project Reference

Sources/ProjectSpec/Project.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ extension Project {
202202
packages.merge(localPackages.reduce(into: [String: SwiftPackage]()) {
203203
// Project name will be obtained by resolved abstractpath's lastComponent for dealing with some path case, like "../"
204204
let packageName = (basePath + Path($1).normalize()).lastComponent
205-
$0[packageName] = .local(path: $1, group: nil)
205+
$0[packageName] = .local(path: $1, group: nil, excludeFromProject: false)
206206
}
207207
)
208208
}

Sources/ProjectSpec/SpecValidation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ extension Project {
5757
}
5858

5959
for (name, package) in packages {
60-
if case let .local(path, _) = package, !(basePath + Path(path).normalize()).exists {
60+
if case let .local(path, _, _) = package, !(basePath + Path(path).normalize()).exists {
6161
errors.append(.invalidLocalPackage(name))
6262
}
6363
}

Sources/ProjectSpec/SwiftPackage.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public enum SwiftPackage: Equatable {
1010
static let githubPrefix = "https://github.com/"
1111

1212
case remote(url: String, versionRequirement: VersionRequirement)
13-
case local(path: String, group: String?)
13+
case local(path: String, group: String?, excludeFromProject: Bool)
1414

1515
public var isLocal: Bool {
1616
if case .local = self {
@@ -23,10 +23,10 @@ public enum SwiftPackage: Equatable {
2323
extension SwiftPackage: JSONObjectConvertible {
2424

2525
public init(jsonDictionary: JSONDictionary) throws {
26-
if let path: String = jsonDictionary.json(atKeyPath: "path"), let customLocation: String = jsonDictionary.json(atKeyPath: "group") {
27-
self = .local(path: path, group: customLocation)
28-
} else if let path: String = jsonDictionary.json(atKeyPath: "path") {
29-
self = .local(path: path, group: nil)
26+
if let path: String = jsonDictionary.json(atKeyPath: "path") {
27+
let customLocation: String? = jsonDictionary.json(atKeyPath: "group")
28+
let excludeFromProject: Bool = jsonDictionary.json(atKeyPath: "excludeFromProject") ?? false
29+
self = .local(path: path, group: customLocation, excludeFromProject: excludeFromProject)
3030
} else {
3131
let versionRequirement: VersionRequirement = try VersionRequirement(jsonDictionary: jsonDictionary)
3232
try Self.validateVersion(versionRequirement: versionRequirement)
@@ -92,9 +92,10 @@ extension SwiftPackage: JSONEncodable {
9292
dictionary["revision"] = revision
9393
}
9494
return dictionary
95-
case let .local(path, group):
95+
case let .local(path, group, excludeFromProject):
9696
dictionary["path"] = path
9797
dictionary["group"] = group
98+
dictionary["excludeFromProject"] = excludeFromProject
9899
}
99100

100101
return dictionary

Sources/XcodeGenKit/PBXProjGenerator.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,14 @@ public class PBXProjGenerator {
169169
let packageReference = XCRemoteSwiftPackageReference(repositoryURL: url, versionRequirement: versionRequirement)
170170
packageReferences[name] = packageReference
171171
addObject(packageReference)
172-
case let .local(path, group):
172+
case let .local(path, group, excludeFromProject):
173173
let packageReference = XCLocalSwiftPackageReference(relativePath: path)
174174
localPackageReferences[name] = packageReference
175-
addObject(packageReference)
176-
177-
try sourceGenerator.createLocalPackage(path: Path(path), group: group.map { Path($0) })
175+
176+
if !excludeFromProject {
177+
addObject(packageReference)
178+
try sourceGenerator.createLocalPackage(path: Path(path), group: group.map { Path($0) })
179+
}
178180
}
179181
}
180182

Sources/XcodeGenKit/SchemeGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public class SchemeGenerator {
173173
switch target.location {
174174
case .package(let packageName):
175175
guard let package = self.project.getPackage(packageName),
176-
case let .local(path, _) = package else {
176+
case let .local(path, _, _) = package else {
177177
throw SchemeGenerationError.missingPackage(packageName)
178178
}
179179
return XCScheme.BuildableReference(

Tests/ProjectSpecTests/ProjectSpecTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class ProjectSpecTests: XCTestCase {
137137
project.settings = invalidSettings
138138
project.configFiles = ["invalidConfig": "invalidConfigFile"]
139139
project.fileGroups = ["invalidFileGroup"]
140-
project.packages = ["invalidLocalPackage": .local(path: "invalidLocalPackage", group: nil)]
140+
project.packages = ["invalidLocalPackage": .local(path: "invalidLocalPackage", group: nil, excludeFromProject: false)]
141141
project.settingGroups = ["settingGroup1": Settings(
142142
configSettings: ["invalidSettingGroupConfig": [:]],
143143
groups: ["invalidSettingGroupSettingGroup"]

Tests/ProjectSpecTests/SpecLoadingTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,10 +1489,10 @@ class SpecLoadingTests: XCTestCase {
14891489
"package6": .remote(url: "package.git", versionRequirement: .range(from: "1.2.0", to: "1.2.5")),
14901490
"package7": .remote(url: "package.git", versionRequirement: .exact("1.2.2")),
14911491
"package8": .remote(url: "package.git", versionRequirement: .upToNextMajorVersion("4.0.0-beta.5")),
1492-
"package9": .local(path: "package/package", group: nil),
1492+
"package9": .local(path: "package/package", group: nil, excludeFromProject: false),
14931493
"package10": .remote(url: "https://github.com/yonaskolb/XcodeGen", versionRequirement: .exact("1.2.2")),
1494-
"XcodeGen": .local(path: "../XcodeGen", group: nil),
1495-
"package11": .local(path: "../XcodeGen", group: "Packages/Feature"),
1494+
"XcodeGen": .local(path: "../XcodeGen", group: nil, excludeFromProject: false),
1495+
"package11": .local(path: "../XcodeGen", group: "Packages/Feature", excludeFromProject: false),
14961496
], options: .init(localPackagesGroup: "MyPackages"))
14971497

14981498
let dictionary: [String: Any] = [
@@ -1521,8 +1521,8 @@ class SpecLoadingTests: XCTestCase {
15211521

15221522
$0.it("parses old local package format") {
15231523
let project = Project(name: "spm", packages: [
1524-
"XcodeGen": .local(path: "../XcodeGen", group: nil),
1525-
"Yams": .local(path: "Yams", group: nil),
1524+
"XcodeGen": .local(path: "../XcodeGen", group: nil, excludeFromProject: false),
1525+
"Yams": .local(path: "Yams", group: nil, excludeFromProject: false),
15261526
], options: .init(localPackagesGroup: "MyPackages"))
15271527

15281528
let dictionary: [String: Any] = [

Tests/XcodeGenKitTests/PBXProjGeneratorTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,9 @@ class PBXProjGeneratorTests: XCTestCase {
325325
name: "Test",
326326
targets: [target],
327327
packages: [
328-
"Common": .local(path: "Packages/Common", group: nil),
329-
"FeatureA": .local(path: "Packages/FeatureA", group: nil),
330-
"FeatureB": .local(path: "Packages/FeatureB", group: nil),
328+
"Common": .local(path: "Packages/Common", group: nil, excludeFromProject: false),
329+
"FeatureA": .local(path: "Packages/FeatureA", group: nil, excludeFromProject: false),
330+
"FeatureB": .local(path: "Packages/FeatureB", group: nil, excludeFromProject: false),
331331
],
332332
options: options
333333
)

0 commit comments

Comments
 (0)