-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathProductDescription.swift
More file actions
116 lines (95 loc) · 2.86 KB
/
ProductDescription.swift
File metadata and controls
116 lines (95 loc) · 2.86 KB
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2025 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
//
//===----------------------------------------------------------------------===//
import SwiftSyntax
import SwiftSyntaxBuilder
/// Syntactic wrapper type that describes a product for refactoring
/// purposes but does not interpret its contents.
@_spi(PackageRefactor)
public struct ProductDescription {
/// The name of the product.
public let name: String
/// The targets in the product.
public let targets: [String]
/// The type of product.
public let type: ProductType
public enum ProductType {
/// The type of library.
public enum LibraryType: String, Codable, Sendable {
/// Static library.
case `static`
/// Dynamic library.
case `dynamic`
/// The type of library is unspecified and should be decided by package manager.
case automatic
}
/// A library product.
case library(LibraryType)
/// An executable product.
case executable
/// An executable code snippet.
case snippet
/// An plugin product.
case plugin
/// A test product.
case test
/// A macro product.
case `macro`
}
public init(
name: String,
type: ProductType,
targets: [String]
) {
self.name = name
self.type = type
self.targets = targets
}
}
extension ProductDescription: ManifestSyntaxRepresentable {
/// The function name in the package manifest.
///
/// Some of these are actually invalid, but it's up to the caller
/// to check the precondition.
private var functionName: String {
switch type {
case .executable: return "executable"
case .library: return "library"
case .macro: return "macro"
case .plugin: return "plugin"
case .snippet: return "snippet"
case .test: return "test"
}
}
func asSyntax() -> ExprSyntax {
var arguments: [LabeledExprSyntax] = []
arguments.append(label: "name", stringLiteral: name)
// Libraries have a type.
if case .library(let libraryType) = type {
switch libraryType {
case .automatic:
break
case .dynamic, .static:
arguments.append(
label: "type",
expression: ".\(raw: libraryType.rawValue)"
)
}
}
arguments.appendIfNonEmpty(
label: "targets",
arrayLiteral: targets
)
let separateParen: String = arguments.count > 1 ? "\n" : ""
let argumentsSyntax = LabeledExprListSyntax(arguments)
return ".\(raw: functionName)(\(argumentsSyntax)\(raw: separateParen))"
}
}