|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014-2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftParser |
| 14 | +import SwiftSyntax |
| 15 | + |
| 16 | +/// Describes a package dependency for refactoring purposes. This is a syntactic |
| 17 | +/// subset of the full package manifest's description of a package dependency. |
| 18 | +public enum PackageDependency: Sendable { |
| 19 | + case fileSystem(FileSystem) |
| 20 | + case sourceControl(SourceControl) |
| 21 | + case registry(Registry) |
| 22 | + |
| 23 | + public struct FileSystem: Sendable { |
| 24 | + public let identity: PackageIdentity |
| 25 | + public let nameForTargetDependencyResolutionOnly: String? |
| 26 | + public let path: AbsolutePath |
| 27 | + } |
| 28 | + |
| 29 | + public struct SourceControl: Sendable { |
| 30 | + public let identity: PackageIdentity |
| 31 | + public let location: Location |
| 32 | + public let requirement: Requirement |
| 33 | + |
| 34 | + public init(identity: PackageIdentity, location: Location, requirement: Requirement) { |
| 35 | + self.identity = identity |
| 36 | + self.location = location |
| 37 | + self.requirement = requirement |
| 38 | + } |
| 39 | + |
| 40 | + public enum Requirement: Sendable { |
| 41 | + case exact(SemanticVersion) |
| 42 | + case rangeFrom(SemanticVersion) |
| 43 | + case range(lowerBound: SemanticVersion, upperBound: SemanticVersion) |
| 44 | + case revision(String) |
| 45 | + case branch(String) |
| 46 | + } |
| 47 | + |
| 48 | + public enum Location: Sendable { |
| 49 | + case local(AbsolutePath) |
| 50 | + case remote(SourceControlURL) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public struct Registry: Sendable { |
| 55 | + public let identity: PackageIdentity |
| 56 | + public let requirement: Requirement |
| 57 | + |
| 58 | + /// The dependency requirement. |
| 59 | + public enum Requirement: Sendable { |
| 60 | + case exact(SemanticVersion) |
| 61 | + case rangeFrom(SemanticVersion) |
| 62 | + case range(lowerBound: SemanticVersion, upperBound: SemanticVersion) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +extension PackageDependency: ManifestSyntaxRepresentable { |
| 68 | + func asSyntax() -> ExprSyntax { |
| 69 | + switch self { |
| 70 | + case .fileSystem(let filesystem): filesystem.asSyntax() |
| 71 | + case .sourceControl(let sourceControl): sourceControl.asSyntax() |
| 72 | + case .registry(let registry): registry.asSyntax() |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +extension PackageDependency.FileSystem: ManifestSyntaxRepresentable { |
| 78 | + func asSyntax() -> ExprSyntax { |
| 79 | + ".package(path: \(literal: path.description))" |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +extension PackageDependency.SourceControl: ManifestSyntaxRepresentable { |
| 84 | + func asSyntax() -> ExprSyntax { |
| 85 | + // TODO: Not handling identity, nameForTargetDependencyResolutionOnly, |
| 86 | + // or productFilter yet. |
| 87 | + switch location { |
| 88 | + case .local: |
| 89 | + fatalError() |
| 90 | + case .remote(let url): |
| 91 | + ".package(url: \(literal: url.description), \(requirement.asSyntax()))" |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +extension PackageDependency.Registry: ManifestSyntaxRepresentable { |
| 97 | + func asSyntax() -> ExprSyntax { |
| 98 | + ".package(id: \(literal: identity.description), \(requirement.asSyntax()))" |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +extension PackageDependency.SourceControl.Requirement: ManifestSyntaxRepresentable { |
| 103 | + func asSyntax() -> LabeledExprSyntax { |
| 104 | + switch self { |
| 105 | + case .exact(let version): |
| 106 | + LabeledExprSyntax( |
| 107 | + label: "exact", |
| 108 | + expression: version.asSyntax() |
| 109 | + ) |
| 110 | + |
| 111 | + case .rangeFrom(let range): |
| 112 | + LabeledExprSyntax( |
| 113 | + label: "from", |
| 114 | + expression: range.asSyntax() |
| 115 | + ) |
| 116 | + |
| 117 | + case .range(let lowerBound, let upperBound): |
| 118 | + LabeledExprSyntax( |
| 119 | + expression: "\(lowerBound.asSyntax())..<\(upperBound.asSyntax())" as ExprSyntax |
| 120 | + ) |
| 121 | + |
| 122 | + case .revision(let revision): |
| 123 | + LabeledExprSyntax( |
| 124 | + label: "revision", |
| 125 | + expression: "\(literal: revision)" as ExprSyntax |
| 126 | + ) |
| 127 | + |
| 128 | + case .branch(let branch): |
| 129 | + LabeledExprSyntax( |
| 130 | + label: "branch", |
| 131 | + expression: "\(literal: branch)" as ExprSyntax |
| 132 | + ) |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +extension PackageDependency.Registry.Requirement: ManifestSyntaxRepresentable { |
| 138 | + func asSyntax() -> LabeledExprSyntax { |
| 139 | + switch self { |
| 140 | + case .exact(let version): |
| 141 | + LabeledExprSyntax( |
| 142 | + label: "exact", |
| 143 | + expression: version.asSyntax() |
| 144 | + ) |
| 145 | + |
| 146 | + case .rangeFrom(let range): |
| 147 | + LabeledExprSyntax( |
| 148 | + label: "from", |
| 149 | + expression: range.asSyntax() |
| 150 | + ) |
| 151 | + |
| 152 | + case .range(let lowerBound, let upperBound): |
| 153 | + LabeledExprSyntax( |
| 154 | + expression: "\(lowerBound.asSyntax())..<\(upperBound.asSyntax())" as ExprSyntax |
| 155 | + ) |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments