Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
appstore-distribution ▾

xtool.git / Sources / XKit / DeveloperServices / App Groups / DeveloperServicesAssignAppGroupsOperation.swift

DeveloperServicesAssignAppGroupsOperation.swift84 lines · 2.9 KiBRaw
1//
2// DeveloperServicesAssignAppGroupsOperation.swift
3// XKit
4//
5// Created by Kabir Oberai on 14/10/19.
6// Copyright © 2019 Kabir Oberai. All rights reserved.
7//
8
9import Foundation
10import DeveloperAPI
11
12public struct DeveloperServicesAssignAppGroupsOperation: DeveloperServicesOperation {
13
14 public let context: SigningContext
15 public let groupIDs: [DeveloperServicesAppGroup.GroupID]
16 public let appID: Components.Schemas.BundleId
17 public let xcodeAuthData: XcodeAuthData
18
19 private let client: DeveloperServicesClient
20
21 public init?(
22 context: SigningContext,
23 groupIDs: [DeveloperServicesAppGroup.GroupID],
24 appID: Components.Schemas.BundleId
25 ) {
26 self.context = context
27 self.groupIDs = groupIDs
28 self.appID = appID
29
30 guard case .xcode(let authData) = context.auth else { return nil }
31 self.xcodeAuthData = authData
32
33 self.client = DeveloperServicesClient(authData: authData)
34 }
35
36 private func upsertAppGroup(
37 _ groupID: DeveloperServicesAppGroup.GroupID,
38 existingGroups: [String: DeveloperServicesAppGroup]
39 ) async throws -> DeveloperServicesAppGroup.GroupID {
40 let sanitized = ProvisioningIdentifiers.sanitize(groupID: groupID)
41 let group: DeveloperServicesAppGroup
42 if let existingGroup = existingGroups[sanitized] {
43 group = existingGroup
44 } else {
45 let groupID = ProvisioningIdentifiers.groupID(fromSanitized: sanitized, context: context)
46 let name = ProvisioningIdentifiers.groupName(fromSanitized: sanitized)
47 let request = DeveloperServicesAddAppGroupRequest(
48 platform: .iOS,
49 teamID: xcodeAuthData.teamID,
50 name: name,
51 groupID: groupID
52 )
53 group = try await client.send(request)
54 }
55
56 _ = try await client.send(
57 DeveloperServicesAssignAppGroupRequest(
58 platform: .iOS,
59 teamID: xcodeAuthData.teamID,
60 appIDID: appID.id,
61 groupID: group.id
62 )
63 )
64
65 return group.groupID
66 }
67
68 public func perform() async throws -> [DeveloperServicesAppGroup.GroupID] {
69 let existing = try await client.send(DeveloperServicesListAppGroupsRequest(
70 platform: .iOS, teamID: xcodeAuthData.teamID
71 ))
72 let sanitized = existing.map { (ProvisioningIdentifiers.sanitize(groupID: $0.groupID), $0) }
73 let dict = Dictionary(sanitized, uniquingKeysWith: { $1 })
74 return try await withThrowingTaskGroup(of: DeveloperServicesAppGroup.GroupID.self) { group in
75 for groupID in groupIDs {
76 group.addTask {
77 try await upsertAppGroup(groupID, existingGroups: dict)
78 }
79 }
80 return try await group.reduce(into: []) { $0.append($1) }
81 }
82 }
83
84}