Files
xtool/Sources/XUtils/Stdlib+Utils.swift
David Reinecke bca6f59848 ds: fix assigning multiple app groups (#403)
## The bug

`DeveloperServicesAssignAppGroupsOperation` sends one
`assignApplicationGroupToAppId` request per app group, concurrently.
That endpoint replaces the App ID's groups with the ones in the request,
so an app whose entitlements list two or more groups ends up with only
one of them on the App ID: whichever request Apple handles last. The
provisioning profile then carries one group while the signed binary
claims two, and the device refuses the install with
`ApplicationVerificationFailed`.

Setting the groups by hand in the developer portal does not help,
because the next `xtool dev build --sign` sends the per-group requests
again.

## The fix

- `DeveloperServicesAssignAppGroupRequest` takes `groupIDs:
[DeveloperServicesAppGroup.ID]` and sends `applicationGroups` as an
array. The endpoint accepts an array; it was being given a single string
before.
- `DeveloperServicesAssignAppGroupsOperation.perform()` still finds or
creates each group concurrently, then sends a single assign request
carrying every group ID, and returns the group IDs in the order the
entitlements listed them, so the rewritten entitlement is stable between
builds.

The only caller, `DeveloperServicesAddAppOperation`, is unchanged.

## Related

#324 would report the resulting mismatch between the entitlements and
the profile as a clear error at signing time; this change removes the
cause, so an app with several groups gets a profile that grants all of
them.

## Checked

Built on Linux with the Swift 6.4 toolchain and used to sign an iOS app
whose main target has two app groups and a Share extension. The app's
`embedded.mobileprovision` now carries both groups and the device
installs the app. The same app signed with the unpatched build got one
group in the profile and `ApplicationVerificationFailed` on install.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01UKzxgxaLwK7Ap5LMwj9MxH


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Apps can now be assigned multiple app groups in a single operation.
The assignment replaces the app’s existing group associations with the
selected groups, so any groups not included in the new assignment are no
longer associated with the app.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: David Reinecke <david.reinecke@gmail.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Kabir Oberai <oberai.kabir@gmail.com>
2026-10-30 05:38:25 +00:00

67 lines
2.7 KiB
Swift

extension Sequence where Element: Sendable {
/// Like `map`, but runs transformations concurrently.
///
/// - Parameter maxConcurrency: The maximum number of transforms to run concurrently.
/// If `nil`, we choose a reasonable default value. Use `.max` to run all at once.
///
/// - Parameter transform: The transformation to apply to each element.
///
/// - Returns: Transformed elements in the same order as the original sequence.
public nonisolated(nonsending) func concurrentMap<T: Sendable, E: Error>(
maxConcurrency: Int? = nil,
_ transform: @escaping @Sendable @isolated(any) (Element) async throws(E) -> T,
) async throws(E) -> [T] {
try await _concurrentMap(
maxConcurrency: maxConcurrency ?? 10,
transform,
willWaitForNext: nil,
)
}
nonisolated(nonsending) func _concurrentMap<T: Sendable, E: Error>(
maxConcurrency: Int,
_ transform: @escaping @Sendable @isolated(any) (Element) async throws(E) -> T,
willWaitForNext: (() -> Void)?, // hook for testing
) async throws(E) -> [T] {
precondition(maxConcurrency > 0, "maxConcurrency must be > 0")
return try await withTaskGroup(of: (Int, Result<T, E>).self) { group -> Result<[T], E> in
var results: [T?] = []
results.reserveCapacity(self.underestimatedCount)
var iterator = makeIterator()
var exhausted = false
var inFlight = 0
while true {
while !exhausted && inFlight < maxConcurrency {
guard let element = iterator.next() else {
exhausted = true
break
}
let index = results.count
results.append(nil)
group.addTask {
let result = await Result { () async throws(E) in
try await transform(element)
}
return (index, result)
}
inFlight += 1
}
willWaitForNext?()
guard let (index, result) = await group.next() else { break }
inFlight -= 1
switch result {
case .success(let value):
results[index] = value
case .failure(let error):
group.cancelAll()
return .failure(error)
}
}
// we can't use `as! [T]` because it breaks nested optionals (Foo??)
return .success(results.map { $0! })
}
.get()
}
}