Skip to content

Commit f179320

Browse files
committed
[PackageModel] Add modules and products to Package.
- Now that all of the individual package loading is centralized, coalesce it into a single method (not yet an initializer) within PackageLoading. This should be the sole place responsible for parsing all of the conventions and creating the model for an individual package.
1 parent 4e7192d commit f179320

File tree

5 files changed

+58
-35
lines changed

5 files changed

+58
-35
lines changed

Sources/PackageGraph/PackageGraphLoader.swift

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,25 @@ public struct PackageGraphLoader {
6767
let allManifests = externalManifests + [rootManifest]
6868

6969
// Create the packages and convert to modules.
70-
//
71-
// FIXME: This needs to be torn about, the module conversion should be
72-
// done on an individual package basis.
7370
var packages: [Package] = []
7471
var products: [Product] = []
7572
var map: [Package: [Module]] = [:]
7673
for (i, manifest) in allManifests.enumerated() {
77-
let package = Package(manifest: manifest)
7874
let isRootPackage = (i + 1) == allManifests.count
79-
packages.append(package)
8075

81-
let modules: [Module] = try package.modules()
76+
// Create a package from the manifest and sources.
77+
//
78+
// FIXME: We should always load the tests, but just change which
79+
// tests we build based on higher-level logic. This would make it
80+
// easier to allow testing of external package tests.
81+
let package = try Package.createUsingConventions(manifest: manifest, includingTestModules: isRootPackage)
82+
packages.append(package)
83+
84+
products += package.products
85+
map[package] = package.modules + package.testModules
8286

8387
// Diagnose empty non-root packages, which are something we allow as a special case.
84-
if modules.isEmpty {
88+
if package.modules.isEmpty {
8589
if isRootPackage {
8690
// Ignore and print warning if root package doesn't contain any sources.
8791
print("warning: root package '\(package)' does not contain any sources")
@@ -94,17 +98,6 @@ public struct PackageGraphLoader {
9498
throw PackageGraphError.noModules(package)
9599
}
96100
}
97-
98-
// TODO: Allow testing of external package tests.
99-
var testModules: [Module]
100-
if isRootPackage {
101-
testModules = try package.testModules(modules: modules)
102-
} else {
103-
testModules = []
104-
}
105-
106-
products += try package.products(modules, testModules: testModules)
107-
map[package] = modules + testModules
108101
}
109102

110103
// Load all of the package dependencies.

Sources/PackageLoading/PackageExtensions.swift

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ extension Package {
148148
}
149149

150150
/// Collects the modules which are defined by a package.
151-
//
152-
// FIXME: This should not be public.
153-
public func modules() throws -> [Module] {
151+
fileprivate func modules() throws -> [Module] {
154152
let moduleMapPath = path.appending("module.modulemap")
155153
if moduleMapPath.asString.isFile {
156154
let sources = Sources(paths: [moduleMapPath], root: path)
@@ -273,9 +271,7 @@ extension Package {
273271

274272
extension Package {
275273
/// Collects the products defined by a package.
276-
//
277-
// FIXME: This should not be public.
278-
public func products(_ modules: [Module], testModules: [Module]) throws -> [Product] {
274+
fileprivate func products(_ modules: [Module], testModules: [Module]) throws -> [Product] {
279275
var products = [Product]()
280276

281277
////// first auto-determine executables
@@ -374,8 +370,7 @@ extension Package {
374370
}
375371

376372
extension Package {
377-
// FIXME: This should not be public.
378-
public func testModules(modules: [Module]) throws -> [Module] {
373+
fileprivate func testModules(modules: [Module]) throws -> [Module] {
379374
let testsPath = self.path.appending("Tests")
380375

381376
// Don't try to walk Tests if it is in excludes.
@@ -436,3 +431,24 @@ extension Package {
436431
return testModules
437432
}
438433
}
434+
435+
extension Package {
436+
/// Load the package for the given manifest.
437+
///
438+
/// - Parameters:
439+
/// - includingTestModules: Whether the package's test modules should be loaded.
440+
//
441+
// FIXME: Rearrange this to be immutable and move to using an initializer if
442+
// it makes sense.
443+
public static func createUsingConventions(manifest: Manifest, includingTestModules: Bool) throws -> Package {
444+
let package = Package(manifest: manifest)
445+
package.modules = try package.modules()
446+
if includingTestModules {
447+
package.testModules = try package.testModules(modules: package.modules)
448+
} else {
449+
package.testModules = []
450+
}
451+
package.products = try package.products(package.modules, testModules: package.testModules)
452+
return package
453+
}
454+
}

Sources/PackageModel/Package.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public final class Package {
4545
public let manifest: Manifest
4646

4747
/// The name of the package.
48-
public let name: String
48+
public var name: String {
49+
return manifest.package.name
50+
}
4951

5052
/// The URL the package was loaded from.
5153
//
@@ -68,14 +70,30 @@ public final class Package {
6870
return manifest.version
6971
}
7072

73+
/// The modules contained in the package.
74+
//
75+
// FIXME: Move to an immutable model.
76+
public var modules: [Module] = []
77+
78+
/// The test modules contained in the package.
79+
//
80+
// FIXME: Move to an immutable model. Also, these should potentially just be
81+
// merged with the regular modules.
82+
public var testModules: [Module] = []
83+
84+
/// The products produced by the package.
85+
//
86+
// FIXME: Move to an immutable model. Also, these should potentially just be
87+
// merged with the regular modules.
88+
public var products: [Product] = []
89+
7190
/// The resolved dependencies of the package.
7291
///
7392
/// This value is only available once package loading is complete.
7493
public var dependencies: [Package] = []
7594

7695
public init(manifest: Manifest) {
7796
self.manifest = manifest
78-
self.name = manifest.package.name
7997
}
8098

8199
public enum Error: Swift.Error {

Tests/PackageLoading/ConventionTests.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ private func fixture(files: [RelativePath], body: @noescape (AbsolutePath) throw
9090
private func fixture(files: [RelativePath], file: StaticString = #file, line: UInt = #line, body: @noescape (PackageModel.Package, [Module]) throws -> ()) throws {
9191
fixture(files: files) { (prefix: AbsolutePath) in
9292
let manifest = Manifest(path: prefix.appending("Package.swift"), url: prefix.asString, package: Package(name: "name"), products: [], version: nil)
93-
let package = Package(manifest: manifest)
94-
let modules = try package.modules()
95-
try body(package, modules)
93+
let package = try Package.createUsingConventions(manifest: manifest, includingTestModules: false)
94+
try body(package, package.modules)
9695
}
9796
}

Tests/PackageLoading/ManifestTests.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ class ManifestTests: XCTestCase {
102102
fixture(name: "Miscellaneous/PackageWithInvalidTargets") { (prefix: AbsolutePath) in
103103
do {
104104
let manifest = try ManifestLoader(resources: Resources()).load(path: prefix.appending("Package.swift"), baseURL: prefix.asString, version: nil)
105-
let package = Package(manifest: manifest)
106-
107-
let _ = try package.modules()
108-
105+
_ = try Package.createUsingConventions(manifest: manifest, includingTestModules: false)
109106
} catch ModuleError.modulesNotFound(let moduleNames) {
110107
XCTAssertEqual(Set(moduleNames), Set(["Bake", "Fake"]))
111108
} catch {

0 commit comments

Comments
 (0)