|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014-2020 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 SwiftSyntax |
| 14 | + |
| 15 | +/// Syntactic wrapper type that describes a product for refactoring |
| 16 | +/// purposes but does not interpret its contents. |
| 17 | +public struct ProductDescription { |
| 18 | + /// The name of the product. |
| 19 | + public let name: String |
| 20 | + |
| 21 | + /// The targets in the product. |
| 22 | + public let targets: [String] |
| 23 | + |
| 24 | + /// The type of product. |
| 25 | + public let type: ProductType |
| 26 | + |
| 27 | + public init( |
| 28 | + name: String, |
| 29 | + type: ProductType, |
| 30 | + targets: [String] |
| 31 | + ) { |
| 32 | + self.name = name |
| 33 | + self.type = type |
| 34 | + self.targets = targets |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +extension ProductDescription: ManifestSyntaxRepresentable { |
| 39 | + /// The function name in the package manifest. |
| 40 | + /// |
| 41 | + /// Some of these are actually invalid, but it's up to the caller |
| 42 | + /// to check the precondition. |
| 43 | + private var functionName: String { |
| 44 | + switch type { |
| 45 | + case .executable: "executable" |
| 46 | + case .library(_): "library" |
| 47 | + case .macro: "macro" |
| 48 | + case .plugin: "plugin" |
| 49 | + case .snippet: "snippet" |
| 50 | + case .test: "test" |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + func asSyntax() -> ExprSyntax { |
| 55 | + var arguments: [LabeledExprSyntax] = [] |
| 56 | + arguments.append(label: "name", stringLiteral: name) |
| 57 | + |
| 58 | + // Libraries have a type. |
| 59 | + if case .library(let libraryType) = type { |
| 60 | + switch libraryType { |
| 61 | + case .automatic: |
| 62 | + break |
| 63 | + |
| 64 | + case .dynamic, .static: |
| 65 | + arguments.append( |
| 66 | + label: "type", |
| 67 | + expression: ".\(raw: libraryType.rawValue)" |
| 68 | + ) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + arguments.appendIfNonEmpty( |
| 73 | + label: "targets", |
| 74 | + arrayLiteral: targets |
| 75 | + ) |
| 76 | + |
| 77 | + let separateParen: String = arguments.count > 1 ? "\n" : "" |
| 78 | + let argumentsSyntax = LabeledExprListSyntax(arguments) |
| 79 | + return ".\(raw: functionName)(\(argumentsSyntax)\(raw: separateParen))" |
| 80 | + } |
| 81 | +} |
0 commit comments