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