11
11
import TSCBasic
12
12
import TSCUtility
13
13
14
+ public class Product : Codable {
15
+
16
+ /// The name of the product.
17
+ public let name : String
18
+
19
+ /// The type of product to create.
20
+ public let type : ProductType
21
+
22
+ /// The list of targets to combine to form the product.
23
+ ///
24
+ /// This is never empty, and is only the targets which are required to be in
25
+ /// the product, but not necessarily their transitive dependencies.
26
+ @PolymorphicCodableArray
27
+ public var targets : [ Target ]
28
+
29
+ /// The path to linux main file.
30
+ public let linuxMain : AbsolutePath ?
31
+
32
+ /// The suffix for REPL product name.
33
+ public static let replProductSuffix : String = " __REPL "
34
+
35
+ public init ( name: String , type: ProductType , targets: [ Target ] , linuxMain: AbsolutePath ? = nil ) {
36
+ precondition ( !targets. isEmpty)
37
+ if type == . executable {
38
+ assert ( targets. filter ( { $0. type == . executable } ) . count == 1 ,
39
+ " Executable products should have exactly one executable target. " )
40
+ }
41
+ if linuxMain != nil {
42
+ assert ( type == . test, " Linux main should only be set on test products " )
43
+ }
44
+ self . name = name
45
+ self . type = type
46
+ self . _targets = . init( wrappedValue: targets)
47
+ self . linuxMain = linuxMain
48
+ }
49
+ }
50
+
14
51
/// The type of product.
15
- public enum ProductType : CustomStringConvertible , Equatable {
52
+ public enum ProductType : Equatable {
16
53
17
54
/// The type of library.
18
55
public enum LibraryType : String , Codable {
@@ -35,7 +72,17 @@ public enum ProductType: CustomStringConvertible, Equatable {
35
72
36
73
/// A test product.
37
74
case test
38
-
75
+ }
76
+
77
+ // MARK: - CustomStringConvertible
78
+
79
+ extension Product : CustomStringConvertible {
80
+ public var description : String {
81
+ return " <Product: \( name) > "
82
+ }
83
+ }
84
+
85
+ extension ProductType : CustomStringConvertible {
39
86
public var description : String {
40
87
switch self {
41
88
case . executable:
@@ -55,45 +102,40 @@ public enum ProductType: CustomStringConvertible, Equatable {
55
102
}
56
103
}
57
104
58
- public class Product : Codable {
59
-
60
- /// The name of the product.
61
- public let name : String
62
-
63
- /// The type of product to create.
64
- public let type : ProductType
65
-
66
- /// The list of targets to combine to form the product.
67
- ///
68
- /// This is never empty, and is only the targets which are required to be in
69
- /// the product, but not necessarily their transitive dependencies.
70
- @PolymorphicCodableArray
71
- public var targets : [ Target ]
72
-
73
- /// The path to linux main file.
74
- public let linuxMain : AbsolutePath ?
105
+ // MARK: - Codable
75
106
76
- /// The suffix for REPL product name.
77
- public static let replProductSuffix : String = " __REPL "
107
+ extension ProductType : Codable {
108
+ private enum CodingKeys : String , CodingKey {
109
+ case library, executable, test
110
+ }
78
111
79
- public init ( name: String , type: ProductType , targets: [ Target ] , linuxMain: AbsolutePath ? = nil ) {
80
- precondition ( !targets. isEmpty)
81
- if type == . executable {
82
- assert ( targets. filter ( { $0. type == . executable } ) . count == 1 ,
83
- " Executable products should have exactly one executable target. " )
84
- }
85
- if linuxMain != nil {
86
- assert ( type == . test, " Linux main should only be set on test products " )
112
+ public func encode( to encoder: Encoder ) throws {
113
+ var container = encoder. container ( keyedBy: CodingKeys . self)
114
+ switch self {
115
+ case let . library( a1) :
116
+ var unkeyedContainer = container. nestedUnkeyedContainer ( forKey: . library)
117
+ try unkeyedContainer. encode ( a1)
118
+ case . executable:
119
+ try container. encodeNil ( forKey: . executable)
120
+ case . test:
121
+ try container. encodeNil ( forKey: . test)
87
122
}
88
- self . name = name
89
- self . type = type
90
- self . _targets = . init( wrappedValue: targets)
91
- self . linuxMain = linuxMain
92
123
}
93
- }
94
124
95
- extension Product : CustomStringConvertible {
96
- public var description : String {
97
- return " <Product: \( name) > "
125
+ public init ( from decoder: Decoder ) throws {
126
+ let values = try decoder. container ( keyedBy: CodingKeys . self)
127
+ guard let key = values. allKeys. first ( where: values. contains) else {
128
+ throw DecodingError . dataCorrupted ( . init( codingPath: decoder. codingPath, debugDescription: " Did not find a matching key " ) )
129
+ }
130
+ switch key {
131
+ case . library:
132
+ var unkeyedValues = try values. nestedUnkeyedContainer ( forKey: key)
133
+ let a1 = try unkeyedValues. decode ( ProductType . LibraryType. self)
134
+ self = . library( a1)
135
+ case . test:
136
+ self = . test
137
+ case . executable:
138
+ self = . executable
139
+ }
98
140
}
99
141
}
0 commit comments