Skip to content

Commit adbdf89

Browse files
authored
Allow for workspaces containing a package (#57)
1 parent 3149624 commit adbdf89

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

Sources/DependencyCalculator/DependencyGraph.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,33 @@ extension PBXNativeTarget {
6666
}
6767

6868
extension WorkspaceInfo {
69+
/// Checks if the root Package.swift should be evaluated. It depends if there is a project file already. Project file is considered a higher level definition and should be parsed first.
70+
private static func shouldIncludeRootPackage(at path: Path) throws -> Bool {
71+
switch path.extension {
72+
case "xcodeproj":
73+
return false
74+
case "xcworkspace":
75+
let workspace = try XCWorkspace(path: path)
76+
let projects = try workspace.allProjects(basePath: path.parent())
77+
78+
if projects.contains(where: { (_, path) in
79+
path.contains("xcodeproj")
80+
}) {
81+
return false
82+
}
83+
else {
84+
return true
85+
}
86+
default:
87+
return true
88+
}
89+
}
90+
6991
public static func parseWorkspace(at path: Path,
7092
config: WorkspaceInfo.AdditionalConfig? = nil,
7193
exclude: [String]) throws -> WorkspaceInfo
7294
{
73-
let includeRootPackage = !Set(["xcworkspace", "xcodeproj"]).contains(path.extension)
74-
95+
let includeRootPackage = try shouldIncludeRootPackage(at: path)
7596
var (packageWorkspaceInfo, packages) = try parsePackages(in: path, includeRootPackage: includeRootPackage, exclude: exclude)
7697

7798
var resultDependencies = packageWorkspaceInfo.dependencyStructure
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// swift-tools-version: 6.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "APackage",
8+
platforms: [.iOS(.v15)],
9+
products: [
10+
// Products define the executables and libraries a package produces, making them visible to other packages.
11+
.library(
12+
name: "APackage",
13+
targets: ["APackage"]),
14+
],
15+
dependencies: [],
16+
targets: [
17+
// Targets are the basic building blocks of a package, defining a module or a test suite.
18+
// Targets can depend on other targets in this package and products from dependencies.
19+
.target(
20+
name: "APackage"),
21+
.testTarget(
22+
name: "APackageTests",
23+
dependencies: ["APackage"]
24+
),
25+
]
26+
)

Tests/DependencyCalculatorTests/ExamplePackages/PackageAndWorkspace/Workspace.xcworkspace/contents.xcworkspacedata

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/DependencyCalculatorTests/PackageMetadataTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,36 @@ final class PackageMetadataTests: XCTestCase {
8080
basePath + "Sources" + "SelectiveTestingCore"
8181
]))
8282
}
83+
84+
func testPackageAndWorkspace() async throws {
85+
// given
86+
guard let exampleInBundle = Bundle.module.path(forResource: "ExamplePackages", ofType: "") else {
87+
fatalError("Missing ExamplePackages in TestBundle")
88+
}
89+
// when
90+
let basePath = Path(exampleInBundle) + "PackageAndWorkspace"
91+
let metadata = try PackageTargetMetadata.parse(at: basePath)
92+
93+
// then
94+
XCTAssertEqual(metadata.count, 2)
95+
let first = metadata[0]
96+
XCTAssertEqual(first.name, "APackage")
97+
XCTAssertEqual(first.path, basePath)
98+
XCTAssertEqual(first.dependsOn, Set([]))
99+
XCTAssertEqual(first.affectedBy, Set([
100+
basePath + "Package.swift",
101+
basePath + "Package.resolved",
102+
basePath + "Sources" + "APackage"
103+
]))
104+
105+
let second = metadata[1]
106+
XCTAssertEqual(second.name, "APackageTests")
107+
XCTAssertEqual(second.path, basePath)
108+
XCTAssertEqual(second.dependsOn.count, 1)
109+
XCTAssertEqual(second.affectedBy, Set([
110+
basePath + "Package.swift",
111+
basePath + "Package.resolved",
112+
basePath + "Tests" + "APackageTests"
113+
]))
114+
}
83115
}

0 commit comments

Comments
 (0)