Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
main ▾

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

DeveloperServicesAssignAppGroupsOperation.swift78 lines · 2.6 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
11import XUtils
12
13public struct DeveloperServicesAssignAppGroupsOperation: DeveloperServicesOperation {
14
15 public let context: SigningContext
16 public let groupIDs: [DeveloperServicesAppGroup.GroupID]
17 public let appID: Components.Schemas.BundleId
18 public let xcodeAuthData: XcodeAuthData
19
20 private let client: DeveloperServicesClient
21
22 public init?(
23 context: SigningContext,
24 groupIDs: [DeveloperServicesAppGroup.GroupID],
25 appID: Components.Schemas.BundleId
26 ) {
27 self.context = context
28 self.groupIDs = groupIDs
29 self.appID = appID
30
31 guard case .xcode(let authData) = context.auth else { return nil }
32 self.xcodeAuthData = authData
33
34 self.client = DeveloperServicesClient(authData: authData)
35 }
36
37 private func getOrCreateAppGroup(
38 _ groupID: DeveloperServicesAppGroup.GroupID,
39 existingGroups: [String: DeveloperServicesAppGroup]
40 ) async throws -> DeveloperServicesAppGroup {
41 let sanitized = ProvisioningIdentifiers.sanitize(groupID: groupID)
42 if let existingGroup = existingGroups[sanitized] {
43 return existingGroup
44 }
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 return try await client.send(request)
54 }
55
56 public func perform() async throws -> [DeveloperServicesAppGroup.GroupID] {
57 let existing = try await client.send(DeveloperServicesListAppGroupsRequest(
58 platform: .iOS, teamID: xcodeAuthData.teamID
59 ))
60 let sanitized = existing.map { (ProvisioningIdentifiers.sanitize(groupID: $0.groupID), $0) }
61 let dict = Dictionary(sanitized, uniquingKeysWith: { $1 })
62 let groups = try await groupIDs.concurrentMap { groupID in
63 try await getOrCreateAppGroup(groupID, existingGroups: dict)
64 }
65
66 _ = try await client.send(
67 DeveloperServicesAssignAppGroupRequest(
68 platform: .iOS,
69 teamID: xcodeAuthData.teamID,
70 appIDID: appID.id,
71 groupIDs: groups.map(\.id)
72 )
73 )
74
75 return groups.map(\.groupID)
76 }
77
78}