|
| 1 | +public import SPM_Standard |
| 2 | + |
| 3 | +extension Package.Manifest { |
| 4 | + /// A located `.package(...)` clause within manifest source: the exact text |
| 5 | + /// it spans and the UTF-8 byte range it occupies. |
| 6 | + /// |
| 7 | + /// Located by paren-matching over the **raw** manifest bytes, not by |
| 8 | + /// re-serialising an evaluated manifest. The reversal a redirection owes is |
| 9 | + /// *byte-identical* — the declared clause must be restored verbatim — so it |
| 10 | + /// has to be captured verbatim, and only the original bytes are verbatim. |
| 11 | + /// String-literal contents are skipped during the scan, so a parenthesis |
| 12 | + /// inside a quoted URL or path cannot close the clause early. |
| 13 | + /// |
| 14 | + /// The range is expressed in bytes rather than `String.Index` because every |
| 15 | + /// delimiter this engine keys on — `.package(`, `(`, `)`, `"`, `\` — is |
| 16 | + /// ASCII, so byte boundaries at those points never fall inside a multi-byte |
| 17 | + /// UTF-8 scalar and slicing on them is exact. Working over `String.Index` |
| 18 | + /// would pull in Foundation's `range(of:)`, which the layer forbids. |
| 19 | + public struct Clause: Swift.Sendable, Swift.Equatable { |
| 20 | + /// The exact source text of the clause, from `.package(` to its |
| 21 | + /// matching `)`, inclusive. |
| 22 | + public let text: Swift.String |
| 23 | + |
| 24 | + /// The half-open UTF-8 byte range the clause occupies in its source. |
| 25 | + public let range: Swift.Range<Swift.Int> |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +extension Package.Manifest.Clause { |
| 30 | + /// Every `.package(...)` clause in `source`, in source order. |
| 31 | + public static func all(in source: Swift.String) -> [Self] { |
| 32 | + let bytes = [Swift.UInt8](source.utf8) |
| 33 | + let marker = [Swift.UInt8](".package(".utf8) |
| 34 | + |
| 35 | + var clauses: [Self] = [] |
| 36 | + var index = 0 |
| 37 | + while let start = firstIndex(of: marker, in: bytes, from: index) { |
| 38 | + let openParen = start + marker.count |
| 39 | + guard let close = closingParen(in: bytes, after: openParen) else { |
| 40 | + // Unbalanced from here on; nothing further is a well-formed |
| 41 | + // clause, so stop rather than ressynchronising on a guess. |
| 42 | + break |
| 43 | + } |
| 44 | + let range = start..<(close + 1) |
| 45 | + clauses.append( |
| 46 | + .init( |
| 47 | + text: Swift.String(decoding: bytes[range], as: Swift.UTF8.self), |
| 48 | + range: range |
| 49 | + ) |
| 50 | + ) |
| 51 | + index = close + 1 |
| 52 | + } |
| 53 | + return clauses |
| 54 | + } |
| 55 | + |
| 56 | + /// The url-form clause whose declared URL has package identity `identity`, |
| 57 | + /// or `nil` when the source declares no such dependency by URL. |
| 58 | + /// |
| 59 | + /// Matched on derived identity, not on a literal URL comparison, so a |
| 60 | + /// declaration that omits a trailing `.git` or differs in case still |
| 61 | + /// matches — identity is exactly what SwiftPM keys a dependency on. |
| 62 | + public static func url( |
| 63 | + identity: Swift.String, |
| 64 | + in source: Swift.String |
| 65 | + ) -> Self? { |
| 66 | + all(in: source).first { clause in |
| 67 | + guard let url = clause.declaredURL else { return false } |
| 68 | + return Self.identity(ofURL: url) == identity |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// The declared URL of a url-form clause (`.package(url: "…", …)`), or |
| 73 | + /// `nil` when the clause is not url-form. |
| 74 | + public var declaredURL: Swift.String? { |
| 75 | + Self.declaredURL(in: text) |
| 76 | + } |
| 77 | + |
| 78 | + /// The declared path of a path-form clause (`.package(path: "…")`), or |
| 79 | + /// `nil` when the clause is not path-form. |
| 80 | + public var declaredPath: Swift.String? { |
| 81 | + Self.declaredPath(in: text) |
| 82 | + } |
| 83 | + |
| 84 | + /// The declared URL of a standalone url-form clause text. |
| 85 | + /// |
| 86 | + /// The standalone form lets a persisted clause be re-parsed without |
| 87 | + /// re-locating it in a manifest — a restore knows the exact clause text it |
| 88 | + /// stored and needs the identity it carries, not a position. |
| 89 | + public static func declaredURL(in text: Swift.String) -> Swift.String? { |
| 90 | + quoted(after: "url:", in: text) |
| 91 | + } |
| 92 | + |
| 93 | + /// The declared path of a standalone path-form clause text. |
| 94 | + public static func declaredPath(in text: Swift.String) -> Swift.String? { |
| 95 | + quoted(after: "path:", in: text) |
| 96 | + } |
| 97 | + |
| 98 | + /// `source` with this clause's byte span replaced by `replacement`. |
| 99 | + /// |
| 100 | + /// The clause carries a byte range into the exact `source` it was located |
| 101 | + /// in; applying it to any other text is a programmer error the caller |
| 102 | + /// prevents by re-locating against the text it edits. |
| 103 | + public func replacing( |
| 104 | + with replacement: Swift.String, |
| 105 | + in source: Swift.String |
| 106 | + ) -> Swift.String { |
| 107 | + let bytes = [Swift.UInt8](source.utf8) |
| 108 | + var result = [Swift.UInt8]() |
| 109 | + result.reserveCapacity(bytes.count) |
| 110 | + result.append(contentsOf: bytes[bytes.startIndex..<range.lowerBound]) |
| 111 | + result.append(contentsOf: replacement.utf8) |
| 112 | + result.append(contentsOf: bytes[range.upperBound..<bytes.endIndex]) |
| 113 | + return Swift.String(decoding: result, as: Swift.UTF8.self) |
| 114 | + } |
| 115 | + |
| 116 | + /// SwiftPM's package identity for a source-control URL: the last path |
| 117 | + /// component, with a trailing slash and a trailing `.git` removed, folded |
| 118 | + /// to lower case. This mirrors how SwiftPM derives identity from a git URL. |
| 119 | + public static func identity(ofURL url: Swift.String) -> Swift.String { |
| 120 | + var value = url[...] |
| 121 | + while value.last == "/" { value = value.dropLast() } |
| 122 | + if value.hasSuffix(".git") { value = value.dropLast(4) } |
| 123 | + let component = value.split(separator: "/").last.map(Swift.String.init) ?? Swift.String(value) |
| 124 | + return component.lowercased() |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +extension Package.Manifest.Clause { |
| 129 | + /// The first quoted string literal that follows `label` in the clause text, |
| 130 | + /// or `nil` when the label or a following literal is absent. |
| 131 | + /// |
| 132 | + /// Escaped quotes are not interpreted: a URL or filesystem path carrying a |
| 133 | + /// literal `"` is pathological here, and treating the first inner quote as |
| 134 | + /// the terminator fails loudly rather than smuggling a wrong value through. |
| 135 | + private static func quoted(after label: Swift.String, in text: Swift.String) -> Swift.String? { |
| 136 | + let bytes = [Swift.UInt8](text.utf8) |
| 137 | + let needle = [Swift.UInt8](label.utf8) |
| 138 | + let located = firstIndex(of: needle, in: bytes, from: 0) |
| 139 | + guard let labelStart = located else { return nil } |
| 140 | + |
| 141 | + let quote = Swift.UInt8(ascii: "\"" as Swift.Unicode.Scalar) |
| 142 | + var index = labelStart + needle.count |
| 143 | + while index < bytes.count, bytes[index] != quote { index += 1 } |
| 144 | + guard index < bytes.count else { return nil } |
| 145 | + let open = index + 1 |
| 146 | + var close = open |
| 147 | + while close < bytes.count, bytes[close] != quote { close += 1 } |
| 148 | + guard close < bytes.count else { return nil } |
| 149 | + return Swift.String(decoding: bytes[open..<close], as: Swift.UTF8.self) |
| 150 | + } |
| 151 | + |
| 152 | + /// The index of the `)` that closes the `.package(` whose `(` sits at |
| 153 | + /// `start - 1`. Depth-counts parentheses and skips string-literal contents |
| 154 | + /// so a paren inside a quoted URL or path cannot close the clause early. |
| 155 | + /// `nil` when the parentheses never balance. |
| 156 | + private static func closingParen(in bytes: [Swift.UInt8], after start: Swift.Int) -> Swift.Int? { |
| 157 | + let quote = Swift.UInt8(ascii: "\"" as Swift.Unicode.Scalar) |
| 158 | + let backslash = Swift.UInt8(ascii: "\\" as Swift.Unicode.Scalar) |
| 159 | + let open = Swift.UInt8(ascii: "(" as Swift.Unicode.Scalar) |
| 160 | + let close = Swift.UInt8(ascii: ")" as Swift.Unicode.Scalar) |
| 161 | + |
| 162 | + var depth = 1 |
| 163 | + var index = start |
| 164 | + var insideString = false |
| 165 | + var escaped = false |
| 166 | + while index < bytes.count { |
| 167 | + let byte = bytes[index] |
| 168 | + if insideString { |
| 169 | + if escaped { |
| 170 | + escaped = false |
| 171 | + } else if byte == backslash { |
| 172 | + escaped = true |
| 173 | + } else if byte == quote { |
| 174 | + insideString = false |
| 175 | + } |
| 176 | + } else { |
| 177 | + switch byte { |
| 178 | + case quote: insideString = true |
| 179 | + |
| 180 | + case open: depth += 1 |
| 181 | + |
| 182 | + case close: |
| 183 | + depth -= 1 |
| 184 | + if depth == 0 { return index } |
| 185 | + |
| 186 | + default: break |
| 187 | + } |
| 188 | + } |
| 189 | + index += 1 |
| 190 | + } |
| 191 | + return nil |
| 192 | + } |
| 193 | + |
| 194 | + /// The index in `haystack` at or after `from` where `needle` first occurs, |
| 195 | + /// or `nil`. A plain forward scan; manifests are small and this runs a |
| 196 | + /// handful of times per operation. |
| 197 | + private static func firstIndex( |
| 198 | + of needle: [Swift.UInt8], |
| 199 | + in haystack: [Swift.UInt8], |
| 200 | + from: Swift.Int |
| 201 | + ) -> Swift.Int? { |
| 202 | + guard !needle.isEmpty, haystack.count >= needle.count else { return nil } |
| 203 | + var start = from |
| 204 | + let last = haystack.count - needle.count |
| 205 | + while start <= last { |
| 206 | + var offset = 0 |
| 207 | + while offset < needle.count, haystack[start + offset] == needle[offset] { |
| 208 | + offset += 1 |
| 209 | + } |
| 210 | + if offset == needle.count { return start } |
| 211 | + start += 1 |
| 212 | + } |
| 213 | + return nil |
| 214 | + } |
| 215 | +} |
0 commit comments