Skip to content

Commit c6d49ef

Browse files
committed
Reorganize ProductType declaration
Move ProductType Codable conformance closer to declaration
1 parent a5ad6f7 commit c6d49ef

File tree

2 files changed

+79
-73
lines changed

2 files changed

+79
-73
lines changed

Sources/PackageModel/PackageModel+Codable.swift

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,6 @@
1111
import Foundation
1212
import TSCBasic
1313

14-
extension ProductType: Codable {
15-
private enum CodingKeys: String, CodingKey {
16-
case library, executable, test
17-
}
18-
19-
public func encode(to encoder: Encoder) throws {
20-
var container = encoder.container(keyedBy: CodingKeys.self)
21-
switch self {
22-
case let .library(a1):
23-
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .library)
24-
try unkeyedContainer.encode(a1)
25-
case .executable:
26-
try container.encodeNil(forKey: .executable)
27-
case .test:
28-
try container.encodeNil(forKey: .test)
29-
}
30-
}
31-
32-
public init(from decoder: Decoder) throws {
33-
let values = try decoder.container(keyedBy: CodingKeys.self)
34-
guard let key = values.allKeys.first(where: values.contains) else {
35-
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Did not find a matching key"))
36-
}
37-
switch key {
38-
case .library:
39-
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
40-
let a1 = try unkeyedValues.decode(ProductType.LibraryType.self)
41-
self = .library(a1)
42-
case .test:
43-
self = .test
44-
case .executable:
45-
self = .executable
46-
}
47-
}
48-
}
49-
5014
extension SystemPackageProviderDescription: Codable {
5115
private enum CodingKeys: String, CodingKey {
5216
case brew, apt, yum

Sources/PackageModel/Product.swift

Lines changed: 79 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,45 @@
1111
import TSCBasic
1212
import TSCUtility
1313

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+
1451
/// The type of product.
15-
public enum ProductType: CustomStringConvertible, Equatable {
52+
public enum ProductType: Equatable {
1653

1754
/// The type of library.
1855
public enum LibraryType: String, Codable {
@@ -35,7 +72,17 @@ public enum ProductType: CustomStringConvertible, Equatable {
3572

3673
/// A test product.
3774
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 {
3986
public var description: String {
4087
switch self {
4188
case .executable:
@@ -55,45 +102,40 @@ public enum ProductType: CustomStringConvertible, Equatable {
55102
}
56103
}
57104

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
75106

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+
}
78111

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)
87122
}
88-
self.name = name
89-
self.type = type
90-
self._targets = .init(wrappedValue: targets)
91-
self.linuxMain = linuxMain
92123
}
93-
}
94124

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+
}
98140
}
99141
}

0 commit comments

Comments
 (0)