|
4 | 4 | import PackageDescription |
5 | 5 | import Foundation |
6 | 6 |
|
7 | | -/// The generator supports filtering the OpenAPI document prior to generation, |
| 7 | +let package = Package( |
| 8 | + name: "GitHubRestAPISwiftOpenAPI", |
| 9 | + platforms: [.macOS(.v10_15)], |
| 10 | + products: GitHubRestAPIOpenAPITag.allCases.map(\.library), |
| 11 | + dependencies: [ |
| 12 | + .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"), |
| 13 | + .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"), |
| 14 | + ], |
| 15 | + targets: GitHubRestAPIOpenAPITag.allCases.map(\.target) |
| 16 | + + GitHubRestAPIOpenAPITag.allCases.compactMap(\.testTarget) |
| 17 | +) |
| 18 | + |
| 19 | +/// The generator supports filtering the OpenAPI document prior to generation, |
8 | 20 | /// which can be useful when generating client code for a subset of a large API, |
9 | 21 | /// or splitting an implementation of a server across multiple modules. |
10 | 22 | enum GitHubRestAPIOpenAPITag: String, CaseIterable { |
@@ -46,56 +58,82 @@ enum GitHubRestAPIOpenAPITag: String, CaseIterable { |
46 | 58 | case classroom |
47 | 59 | case desktop |
48 | 60 |
|
49 | | - var targetName: String { |
| 61 | + var library: PackageDescription.Product { |
| 62 | + let targetName = targetName |
| 63 | + if isBuildingCode { |
| 64 | + return .library( |
| 65 | + name: targetName, |
| 66 | + targets: [targetName] |
| 67 | + ) |
| 68 | + } else { |
| 69 | + return .library( |
| 70 | + name: targetName, |
| 71 | + targets: [targetName, Self.dependenciesTarget.name] |
| 72 | + ) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private var targetName: String { |
50 | 77 | let name = rawValue.replacingOccurrences(of: "-", with: "_").capitalized |
51 | 78 | return "GitHubRestAPI\(name)" |
52 | 79 | } |
53 | 80 |
|
54 | 81 | var target: PackageDescription.Target { |
55 | 82 | let targetName = targetName |
56 | | - return .target( |
57 | | - name: targetName, |
58 | | - dependencies: [ |
59 | | - .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), |
60 | | - .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), |
61 | | - ], |
62 | | - path: "Sources/\(rawValue)", |
63 | | - exclude: [ |
64 | | - "openapi-generator-config.yml", |
65 | | - "openapi.yml", |
66 | | - ] |
67 | | - ) |
68 | | - } |
69 | | - |
70 | | - var library: PackageDescription.Product { |
71 | | - let targetName = targetName |
72 | | - return .library( |
73 | | - name: targetName, |
74 | | - targets: [targetName] |
75 | | - ) |
| 83 | + if isBuildingCode { |
| 84 | + return .target( |
| 85 | + name: targetName, |
| 86 | + dependencies: [ |
| 87 | + .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), |
| 88 | + .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), |
| 89 | + ], |
| 90 | + path: "Sources/\(rawValue)", |
| 91 | + exclude: [ |
| 92 | + "openapi-generator-config.yml", |
| 93 | + "openapi.yml", |
| 94 | + ] |
| 95 | + ) |
| 96 | + } else { |
| 97 | + return .binaryTarget( |
| 98 | + name: targetName, |
| 99 | + path: "XCFrameworks/\(targetName).zip") |
| 100 | + } |
76 | 101 | } |
77 | 102 |
|
78 | | - var testTarget: PackageDescription.Target { |
| 103 | + var testTarget: PackageDescription.Target? { |
| 104 | + guard self == .users else { return nil } |
| 105 | + let dependencies: [Target.Dependency] = [ |
| 106 | + .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), |
| 107 | + .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), |
| 108 | + ] |
79 | 109 | let targetName = targetName |
80 | 110 | return .testTarget( |
81 | 111 | name: "UserTests", |
82 | | - dependencies: [.target(name: targetName)] |
| 112 | + dependencies: dependencies + [ |
| 113 | + .target(name: targetName) |
| 114 | + ] |
83 | 115 | ) |
84 | 116 | } |
| 117 | + |
| 118 | + static var dependenciesTarget: PackageDescription.Target = .target( |
| 119 | + name: "DependenciesTarget", |
| 120 | + dependencies: [ |
| 121 | + .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), |
| 122 | + .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), |
| 123 | + ], |
| 124 | + path: "XCFrameworks/DependenciesTarget" |
| 125 | + ) |
| 126 | + |
85 | 127 | } |
86 | 128 |
|
87 | | -let package = Package( |
88 | | - name: "GitHubRestAPISwiftOpenAPI", |
89 | | - platforms: [.macOS(.v10_15)], |
90 | | - products: GitHubRestAPIOpenAPITag.allCases.map(\.library), |
91 | | - dependencies: [ |
92 | | - .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"), |
93 | | - .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"), |
94 | | - ], |
95 | | - targets: GitHubRestAPIOpenAPITag.allCases.map(\.target) + [ |
96 | | - GitHubRestAPIOpenAPITag.users.testTarget, |
| 129 | +let isBuildingCode = ProcessInfo.processInfo.environment["BUILD_CODE"] == "true" |
| 130 | + |
| 131 | +// dependencies is needed for package users |
| 132 | +if !isBuildingCode { |
| 133 | + package.targets += [ |
| 134 | + GitHubRestAPIOpenAPITag.dependenciesTarget |
97 | 135 | ] |
98 | | -) |
| 136 | +} |
99 | 137 |
|
100 | 138 | let isBuildingDocC = ProcessInfo.processInfo.environment["GITHUB_PAGES"] == "true" |
101 | 139 |
|
|
0 commit comments