1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
//===--- BuildArgs.swift --------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
struct BuildArgs {
let command: KnownCommand
private var topLevelArgs: [Command.Argument] = []
private var subOptArgs: [SubOptionArgs] = []
init(for command: KnownCommand, args: [Command.Argument] = []) {
self.command = command
self += args
}
}
extension BuildArgs {
struct SubOptionArgs {
var flag: Command.Flag
var args: BuildArgs
var command: KnownCommand {
args.command
}
}
}
extension BuildArgs {
typealias Element = Command.Argument
/// Whether both the arguments and sub-option arguments are empty.
var isEmpty: Bool {
topLevelArgs.isEmpty && subOptArgs.isEmpty
}
/// Whether the argument have a given flag.
func hasFlag(_ flag: Command.Flag) -> Bool {
topLevelArgs.contains(where: { $0.flag == flag })
}
/// Retrieve the flag in a given list of flags.
func lastFlag(in flags: [Command.Flag]) -> Command.Flag? {
for arg in topLevelArgs.reversed() {
guard let flag = arg.flag, flags.contains(flag) else { continue }
return flag
}
return nil
}
/// Retrieve the flag in a given list of flags.
func lastFlag(in flags: Command.Flag...) -> Command.Flag? {
lastFlag(in: flags)
}
/// Retrieve the last value for a given flag, unescaped.
func lastValue(for flag: Command.Flag) -> String? {
topLevelArgs.last(where: { $0.flag == flag })?.value
}
/// Retrieve the last printed value for a given flag, escaping as needed.
func lastPrintedValue(for flag: Command.Flag) -> String? {
lastValue(for: flag)?.escaped
}
/// Retrieve the printed values for a given flag, escaping as needed.
func printedValues(for flag: Command.Flag) -> [String] {
topLevelArgs.compactMap { $0.option(for: flag)?.value.escaped }
}
var printedArgs: [String] {
topLevelArgs.flatMap(\.printedArgs) + subOptArgs.flatMap { subArgs in
let printedFlag = subArgs.flag.printed
return subArgs.args.printedArgs.flatMap { [printedFlag, $0] }
}
}
var printed: String {
printedArgs.joined(separator: " ")
}
func hasSubOptions(for command: KnownCommand) -> Bool {
subOptArgs.contains(where: { $0.command == command })
}
/// Retrieve a set of sub-options for a given command.
func subOptions(for command: KnownCommand) -> BuildArgs {
hasSubOptions(for: command) ? self[subOptions: command] : .init(for: command)
}
subscript(subOptions command: KnownCommand) -> BuildArgs {
_read {
let index = subOptArgs.firstIndex(where: { $0.command == command })!
yield subOptArgs[index].args
}
_modify {
let index = subOptArgs.firstIndex(where: { $0.command == command })!
yield &subOptArgs[index].args
}
}
/// Apply a transform to the set of arguments. Note this doesn't include any
/// sub-options.
func map(_ transform: (Element) throws -> Element) rethrows -> Self {
var result = self
result.topLevelArgs = try topLevelArgs.map(transform)
return result
}
/// Apply a filter to the set of arguments. Note this doesn't include any
/// sub-options.
func filter(_ predicate: (Element) throws -> Bool) rethrows -> Self {
var result = self
result.topLevelArgs = try topLevelArgs.filter(predicate)
return result
}
/// Remove a set of flags from the arguments.
mutating func exclude(_ flags: [Command.Flag]) {
topLevelArgs.removeAll { arg in
guard let f = arg.flag else { return false }
return flags.contains(f)
}
}
/// Remove a set of flags from the arguments.
mutating func exclude(_ flags: Command.Flag...) {
exclude(flags)
}
/// Remove a set of flags from the arguments.
func excluding(_ flags: [Command.Flag]) -> Self {
var result = self
result.exclude(flags)
return result
}
/// Remove a set of flags from the arguments.
func excluding(_ flags: Command.Flag...) -> Self {
excluding(flags)
}
/// Take the last unescaped value for a given flag, removing all occurances
/// of the flag from the arguments.
mutating func takeLastValue(for flag: Command.Flag) -> String? {
guard let value = lastValue(for: flag) else { return nil }
exclude(flag)
return value
}
/// Take the last printed value for a given flag, escaping as needed, and
/// removing all occurances of the flag from the arguments
mutating func takePrintedLastValue(for flag: Command.Flag) -> String? {
guard let value = lastPrintedValue(for: flag) else { return nil }
exclude(flag)
return value
}
/// Take a set of printed values for a given flag, escaping as needed.
mutating func takePrintedValues(for flag: Command.Flag) -> [String] {
let result = topLevelArgs.compactMap { $0.option(for: flag)?.value.escaped }
exclude(flag)
return result
}
/// Take a flag, returning `true` if it was removed, `false` if it isn't
/// present.
mutating func takeFlag(_ flag: Command.Flag) -> Bool {
guard hasFlag(flag) else { return false }
exclude(flag)
return true
}
/// Takes a set of flags, returning `true` if the flags were removed, `false`
/// if they aren't present.
mutating func takeFlags(_ flags: Command.Flag...) -> Bool {
guard flags.contains(where: self.hasFlag) else { return false }
exclude(flags)
return true
}
/// Takes a set of related flags, returning the last one encountered, or `nil`
/// if no flags in the group are present.
mutating func takeFlagGroup(_ flags: Command.Flag...) -> Command.Flag? {
guard let value = lastFlag(in: flags) else { return nil }
exclude(flags)
return value
}
private mutating func appendSubOptArg(
_ value: String, for command: KnownCommand, flag: Command.Flag
) {
let idx = subOptArgs.firstIndex(where: { $0.command == command }) ?? {
subOptArgs.append(.init(flag: flag, args: .init(for: command)))
return subOptArgs.endIndex - 1
}()
subOptArgs[idx].args.append(value.escaped)
}
mutating func append(_ element: Element) {
if let flag = element.flag, let command = flag.subOptionCommand,
let value = element.value {
appendSubOptArg(value, for: command, flag: flag)
} else if let last = topLevelArgs.last, case .flag(let flag) = last,
case .value(let value) = element {
// If the last element is a flag, and this is a value, we may need to
// merge.
topLevelArgs.removeLast()
topLevelArgs += try! CommandParser.parseArguments(
"\(flag) \(value.escaped)", for: command
)
} else {
topLevelArgs.append(element)
}
}
mutating func append<S: Sequence>(contentsOf seq: S) where S.Element == Element {
for element in seq {
append(element)
}
}
static func += <S: Sequence> (lhs: inout Self, rhs: S) where S.Element == Element {
lhs.append(contentsOf: rhs)
}
mutating func append(_ input: String) {
self += try! CommandParser.parseArguments(input, for: command)
}
/// Apply a transform to the values of any options present. If
/// `includeSubOptions` is `true`, the transform will also be applied to any
/// sub-options present.
mutating func transformValues(
for flag: Command.Flag? = nil, includeSubOptions: Bool,
_ fn: (String) throws -> String
) rethrows {
topLevelArgs = try topLevelArgs.map { arg in
guard flag == nil || arg.flag == flag else { return arg }
return try arg.mapValue(fn)
}
if includeSubOptions {
for idx in subOptArgs.indices {
try subOptArgs[idx].args.transformValues(
for: flag, includeSubOptions: true, fn
)
}
}
}
struct PathSubstitution: Hashable {
var oldPath: AbsolutePath
var newPath: AnyPath
}
/// Apply a substitution to any paths present in the option values, returning
/// the substitutions made. If `includeSubOptions` is `true`, the substitution
/// will also be applied to any sub-options present.
mutating func substitutePaths<Path: PathProtocol>(
for flag: Command.Flag? = nil, includeSubOptions: Bool,
_ fn: (AbsolutePath) throws -> Path?
) rethrows -> [BuildArgs.PathSubstitution] {
var subs: [BuildArgs.PathSubstitution] = []
try transformValues(for: flag,
includeSubOptions: includeSubOptions) { value in
guard case .absolute(let path) = AnyPath(value),
let newPath = try fn(path) else { return value }
let subst = PathSubstitution(oldPath: path, newPath: AnyPath(newPath))
subs.append(subst)
return subst.newPath.rawPath
}
return subs
}
}
extension BuildArgs: CustomStringConvertible {
var description: String { printed }
}
|