Skip to content

Commit 0b45b6d

Browse files
committed
[PackageLoading] Split out function which loads a specific manifest file.
- This should become private eventually, we want to define loading in terms of a complete package not just the manifest (in case in the future we expose other parts of the package to the manifest loading process).
1 parent bf4d43b commit 0b45b6d

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

Sources/Commands/SwiftPackageTool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public struct SwiftPackageTool: SwiftTool {
224224

225225
case .dumpPackage:
226226
let root = opts.inputPath ?? opts.path.root
227-
let manifest = try packageGraphLoader.manifestLoader.load(path: root, baseURL: root.asString, version: nil)
227+
let manifest = try packageGraphLoader.manifestLoader.loadFile(path: root, baseURL: root.asString, version: nil)
228228
let package = manifest.package
229229
let json = try jsonString(package: package)
230230
print(json)

Sources/Get/PackagesDirectory.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public final class PackagesDirectory {
6868
/// - Throws: Error.InvalidDependencyGraph
6969
public func loadManifests(ignoreDependencies: Bool = false) throws -> (rootManifest: Manifest, externalManifests: [Manifest]) {
7070
// Load the manifest for the root package.
71-
let manifest = try manifestLoader.load(path: rootPath, baseURL: rootPath.asString, version: nil)
71+
let manifest = try manifestLoader.load(packagePath: rootPath, baseURL: rootPath.asString, version: nil)
7272
if ignoreDependencies {
7373
return (manifest, [])
7474
}
@@ -114,7 +114,7 @@ extension PackagesDirectory: Fetcher {
114114
return nil
115115
}
116116

117-
return try manifestLoader.load(path: repo.path, baseURL: origin, version: version)
117+
return try manifestLoader.load(packagePath: repo.path, baseURL: origin, version: version)
118118
}
119119

120120
func find(url: String) throws -> Fetchable? {
@@ -126,7 +126,7 @@ extension PackagesDirectory: Fetcher {
126126

127127
func fetch(url: String) throws -> Fetchable {
128128
// Clone into a staging location, we will rename it once all versions are selected.
129-
let manifestParser = { try self.manifestLoader.load(path: $0, baseURL: $1, version: $2) }
129+
let manifestParser = { try self.manifestLoader.load(packagePath: $0, baseURL: $1, version: $2) }
130130
let basename = url.components(separatedBy: "/").last!
131131
let dstdir = packagesPath.appending(component: basename)
132132
if let repo = Git.Repo(path: dstdir), repo.origin == url {

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,34 @@ public final class ManifestLoader {
5050
self.resources = resources
5151
}
5252

53-
/// Create a manifest by loading from the given `path`.
53+
/// Load the manifest for the package at `path`.
5454
///
5555
/// - Parameters:
56-
/// - path: The path to the manifest file or directory containing `Package.swift`.
56+
/// - path: The root path of the package.
5757
/// - baseURL: The URL the manifest was loaded from.
5858
/// - version: The version the manifest is from, if known.
5959
/// - fileSystem: If given, the file system to load from (otherwise load from the local file system).
60-
public func load(path inputPath: AbsolutePath, baseURL: String, version: Version?, fileSystem: FileSystem? = nil) throws -> Manifest {
60+
public func load(packagePath: AbsolutePath, baseURL: String, version: Version?, fileSystem: FileSystem? = nil) throws -> Manifest {
61+
return try loadFile(path: packagePath.appending(component: Manifest.filename), baseURL: baseURL, version: version, fileSystem: fileSystem)
62+
}
63+
64+
/// Create a manifest by loading a specific manifest file from the given `path`.
65+
///
66+
/// - Parameters:
67+
/// - path: The path to the manifest file (or a package root).
68+
/// - baseURL: The URL the manifest was loaded from.
69+
/// - version: The version the manifest is from, if known.
70+
/// - fileSystem: If given, the file system to load from (otherwise load from the local file system).
71+
//
72+
// FIXME: We should stop exposing this publicly, from a public perspective
73+
// we should only ever load complete repositories.
74+
public func loadFile(path inputPath: AbsolutePath, baseURL: String, version: Version?, fileSystem: FileSystem? = nil) throws -> Manifest {
6175
// If we were given a file system, load via a temporary file.
6276
if let fileSystem = fileSystem {
6377
let tmpFile = try TemporaryFile()
6478
let contents = try fileSystem.readFileContents(inputPath)
6579
try localFileSystem.writeFileContents(tmpFile.path, bytes: contents)
66-
return try load(path: tmpFile.path, baseURL: baseURL, version: version)
80+
return try loadFile(path: tmpFile.path, baseURL: baseURL, version: version)
6781
}
6882

6983
guard baseURL.chuzzle() != nil else { fatalError() } //TODO

Tests/PackageLoadingTests/ManifestTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ManifestTests: XCTestCase {
5959
private func loadManifest(_ inputName: String, line: UInt = #line, body: (Manifest) -> Void) {
6060
do {
6161
let input = AbsolutePath(#file).parentDirectory.appending(component: "Inputs").appending(component: inputName)
62-
body(try ManifestLoader(resources: Resources()).load(path: input, baseURL: input.parentDirectory.asString, version: nil))
62+
body(try ManifestLoader(resources: Resources()).loadFile(path: input, baseURL: input.parentDirectory.asString, version: nil))
6363
} catch {
6464
XCTFail("Unexpected error: \(error)", file: #file, line: line)
6565
}
@@ -70,7 +70,7 @@ class ManifestTests: XCTestCase {
7070
let fs = InMemoryFileSystem()
7171
let manifestPath = AbsolutePath.root.appending(component: Manifest.filename)
7272
try fs.writeFileContents(manifestPath, bytes: contents)
73-
body(try ManifestLoader(resources: Resources()).load(path: manifestPath, baseURL: AbsolutePath.root.asString, version: nil, fileSystem: fs))
73+
body(try ManifestLoader(resources: Resources()).loadFile(path: manifestPath, baseURL: AbsolutePath.root.asString, version: nil, fileSystem: fs))
7474
} catch {
7575
XCTFail("Unexpected error: \(error)", file: #file, line: line)
7676
}
@@ -115,14 +115,14 @@ class ManifestTests: XCTestCase {
115115
}
116116

117117
func testNoManifest() {
118-
let foo = try? ManifestLoader(resources: Resources()).load(path: AbsolutePath("/non-existent-file"), baseURL: "/", version: nil)
118+
let foo = try? ManifestLoader(resources: Resources()).loadFile(path: AbsolutePath("/non-existent-file"), baseURL: "/", version: nil)
119119
XCTAssertNil(foo)
120120
}
121121

122122
func testInvalidTargetName() {
123123
fixture(name: "Miscellaneous/PackageWithInvalidTargets") { (prefix: AbsolutePath) in
124124
do {
125-
let manifest = try ManifestLoader(resources: Resources()).load(path: prefix.appending(component: "Package.swift"), baseURL: prefix.asString, version: nil)
125+
let manifest = try ManifestLoader(resources: Resources()).loadFile(path: prefix.appending(component: "Package.swift"), baseURL: prefix.asString, version: nil)
126126
_ = try PackageBuilder(manifest: manifest, path: prefix).construct(includingTestModules: false)
127127
} catch ModuleError.modulesNotFound(let moduleNames) {
128128
XCTAssertEqual(Set(moduleNames), Set(["Bake", "Fake"]))

0 commit comments

Comments
 (0)