|
8 | 8 | See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
9 | 9 | */ |
10 | 10 |
|
11 | | -/// Defines a product in the package. |
12 | | -public enum Product { |
| 11 | +public struct Product { |
| 12 | + public let name: String |
| 13 | + public let type: ProductType |
| 14 | + public let modules: [String] |
13 | 15 |
|
14 | | - /// An exectuable product. |
15 | | - public struct ExecutableProduct { |
16 | | - |
17 | | - /// The name of the executable product. |
18 | | - public let name: String |
19 | | - |
20 | | - /// The names of targets in the product. |
21 | | - public let targets: [String] |
22 | | - } |
23 | | - |
24 | | - /// A library product. |
25 | | - public struct LibraryProduct { |
26 | | - |
27 | | - /// The type of library products. |
28 | | - public enum LibraryType: String { |
29 | | - case `static` |
30 | | - case `dynamic` |
31 | | - } |
32 | | - |
33 | | - /// The name of the library product. |
34 | | - public let name: String |
35 | | - |
36 | | - /// The type of the library. |
37 | | - /// |
38 | | - /// If the type is unspecified, package manager will automatically choose a type. |
39 | | - public let type: LibraryType? |
40 | | - |
41 | | - /// The names of targets in the product. |
42 | | - public let targets: [String] |
| 16 | + public init(name: String, type: ProductType, modules: String...) { |
| 17 | + self.init(name: name, type: type, modules: modules) |
43 | 18 | } |
44 | 19 |
|
45 | | - /// Executable product. |
46 | | - case exe(ExecutableProduct) |
47 | | - |
48 | | - /// Library product. |
49 | | - case lib(LibraryProduct) |
50 | | - |
51 | | - /// Create an executable product. |
52 | | - public static func Executable(name: String, targets: [String]) -> Product { |
53 | | - return .exe(ExecutableProduct(name: name, targets: targets)) |
54 | | - } |
55 | | - |
56 | | - /// Create a library product. |
57 | | - public static func Library(name: String, type: LibraryProduct.LibraryType? = nil, targets: [String]) -> Product { |
58 | | - return .lib(LibraryProduct(name: name, type: type, targets: targets)) |
| 20 | + public init(name: String, type: ProductType, modules: [String]) { |
| 21 | + self.name = name |
| 22 | + self.type = type |
| 23 | + self.modules = modules |
59 | 24 | } |
| 25 | +} |
60 | 26 |
|
61 | | - /// Name of the product. |
62 | | - public var name: String { |
63 | | - switch self { |
64 | | - case .exe(let p): return p.name |
65 | | - case .lib(let p): return p.name |
66 | | - } |
67 | | - } |
| 27 | +public enum LibraryType { |
| 28 | + case Static |
| 29 | + case Dynamic |
68 | 30 | } |
69 | 31 |
|
70 | | -extension Product.ExecutableProduct { |
71 | | - func toJSON() -> [String: JSON] { |
72 | | - return [ |
73 | | - "name": .string(name), |
74 | | - "targets": .array(targets.map(JSON.string)), |
75 | | - ] |
76 | | - } |
| 32 | +public enum ProductType { |
| 33 | + case Test |
| 34 | + case Executable |
| 35 | + case Library(LibraryType) |
77 | 36 | } |
78 | 37 |
|
79 | | -extension Product.LibraryProduct { |
80 | | - func toJSON() -> [String: JSON] { |
81 | | - return [ |
82 | | - "name": .string(name), |
83 | | - "type": type.map{ JSON.string($0.rawValue) } ?? .null, |
84 | | - "targets": .array(targets.map(JSON.string)), |
85 | | - ] |
| 38 | +extension ProductType: CustomStringConvertible { |
| 39 | + public var description: String { |
| 40 | + switch self { |
| 41 | + case .Test: |
| 42 | + return "test" |
| 43 | + case .Executable: |
| 44 | + return "exe" |
| 45 | + case .Library(.Static): |
| 46 | + return "a" |
| 47 | + case .Library(.Dynamic): |
| 48 | + return "dylib" |
| 49 | + } |
86 | 50 | } |
87 | 51 | } |
88 | 52 |
|
89 | 53 | extension Product { |
90 | 54 | func toJSON() -> JSON { |
91 | | - switch self { |
92 | | - case .exe(let product): |
93 | | - var dict = product.toJSON() |
94 | | - dict["product_type"] = .string("exe") |
95 | | - return .dictionary(dict) |
96 | | - case .lib(let product): |
97 | | - var dict = product.toJSON() |
98 | | - dict["product_type"] = .string("lib") |
99 | | - return .dictionary(dict) |
100 | | - } |
| 55 | + var dict: [String: JSON] = [:] |
| 56 | + dict["name"] = .string(name) |
| 57 | + dict["type"] = .string(type.description) |
| 58 | + dict["modules"] = .array(modules.map(JSON.string)) |
| 59 | + return .dictionary(dict) |
101 | 60 | } |
102 | 61 | } |
103 | 62 |
|
104 | | -extension Product.ExecutableProduct: Equatable { |
105 | | - public static func ==(lhs: Product.ExecutableProduct, rhs: Product.ExecutableProduct) -> Bool { |
106 | | - return lhs.name == rhs.name && |
107 | | - lhs.targets == rhs.targets |
| 63 | +extension ProductType: Equatable { |
| 64 | + public static func ==(lhs: ProductType, rhs: ProductType) -> Bool { |
| 65 | + switch (lhs, rhs) { |
| 66 | + case (.Executable, .Executable): |
| 67 | + return true |
| 68 | + case (.Executable, _): |
| 69 | + return false |
| 70 | + case (.Test, .Test): |
| 71 | + return true |
| 72 | + case (.Test, _): |
| 73 | + return false |
| 74 | + case (.Library(let lhsType), .Library(let rhsType)): |
| 75 | + return lhsType == rhsType |
| 76 | + case (.Library, _): |
| 77 | + return false |
| 78 | + } |
108 | 79 | } |
109 | 80 | } |
110 | 81 |
|
111 | | -extension Product.LibraryProduct: Equatable { |
112 | | - public static func ==(lhs: Product.LibraryProduct, rhs: Product.LibraryProduct) -> Bool { |
| 82 | +extension Product: Equatable { |
| 83 | + public static func ==(lhs: PackageDescription.Product, rhs: PackageDescription.Product) -> Bool { |
113 | 84 | return lhs.name == rhs.name && |
114 | 85 | lhs.type == rhs.type && |
115 | | - lhs.targets == rhs.targets |
| 86 | + lhs.modules == rhs.modules |
116 | 87 | } |
117 | 88 | } |
118 | 89 |
|
119 | | -extension Product: Equatable { |
120 | | - public static func ==(lhs: Product, rhs: Product) -> Bool { |
121 | | - switch (lhs, rhs) { |
122 | | - case (.exe(let a), .exe(let b)): |
123 | | - return a == b |
124 | | - case (.exe, _): |
125 | | - return false |
126 | | - case (.lib(let a), .lib(let b)): |
127 | | - return a == b |
128 | | - case (.lib, _): |
129 | | - return false |
130 | | - } |
131 | | - } |
132 | | -} |
| 90 | +public var products = [Product]() |
0 commit comments