Skip to content

Commit a739982

Browse files
authored
build: Generate Swift types from Sentry-Swift.h (#4488)
1 parent 0194f50 commit a739982

File tree

7 files changed

+1880
-1918
lines changed

7 files changed

+1880
-1918
lines changed

scripts/generate-cocoa-bindings.ps1

Lines changed: 28 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Set-StrictMode -Version Latest
44
$ErrorActionPreference = 'Stop'
5+
$PSNativeCommandUseErrorActionPreference = $true
56

67
$RootPath = (Get-Item $PSScriptRoot).Parent.FullName
78
$CocoaSdkPath = "$RootPath/modules/sentry-cocoa"
@@ -103,8 +104,11 @@ if (!(Test-Path '/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/
103104
}
104105

105106
# Get iPhone SDK version
106-
$iPhoneSdkVersion = sharpie xcode -sdks | grep -o -m 1 'iphoneos\S*'
107+
$XcodePath = (xcode-select -p) -replace '/Contents/Developer$', ''
108+
$iPhoneSdkVersion = sharpie xcode -xcode $XcodePath -sdks | grep -o -m 1 'iphoneos\S*'
107109
Write-Output "iPhoneSdkVersion: $iPhoneSdkVersion"
110+
$iPhoneSdkPath = xcrun --show-sdk-path --sdk $iPhoneSdkVersion
111+
Write-Output "iPhoneSdkPath: $iPhoneSdkPath"
108112

109113
## Imports in the various header files are provided in the "new" style of:
110114
# `#import <Sentry/SomeHeader.h>`
@@ -141,173 +145,55 @@ else
141145
{
142146
Write-Host "File not found: $privateHeaderFile"
143147
}
148+
$swiftHeaderFile = "$HeadersPath/Sentry-Swift.h"
149+
if (Test-Path $swiftHeaderFile)
150+
{
151+
$content = Get-Content -Path $swiftHeaderFile -Raw
152+
# Replace module @imports with traditional #includes
153+
$content = $content -replace '(?m)^#if\s+(__has_feature\(objc_modules\))', '#if 1 // $1'
154+
$content = $content -replace '(?m)^@import\s+ObjectiveC;\s*\n', ''
155+
$content = $content -replace '(?m)^@import\s+(\w+);', '#include <$1/$1.h>'
156+
$content = $content -replace '(?m)^#import\s+"Sentry.h"\s*\n', ''
157+
158+
Set-Content -Path $swiftHeaderFile -Value $content
159+
Write-Host "Patched includes: $swiftHeaderFile"
160+
}
161+
else
162+
{
163+
Write-Host "File not found: $swiftHeaderFile"
164+
}
144165

145166
# Generate bindings
146167
Write-Output 'Generating bindings with Objective Sharpie.'
147168
sharpie bind -sdk $iPhoneSdkVersion `
148169
-scope "$CocoaSdkPath" `
149170
"$HeadersPath/Sentry.h" `
171+
"$HeadersPath/Sentry-Swift.h" `
150172
"$PrivateHeadersPath/PrivateSentrySDKOnly.h" `
151173
-o $BindingsPath `
152-
-c -Wno-objc-property-no-attribute
174+
-c -Wno-objc-property-no-attribute `
175+
-F"$iPhoneSdkPath/System/Library/SubFrameworks" # needed for UIUtilities.framework in Xcode 26+
153176

154177
# Ensure backup path exists
155178
if (!(Test-Path $BackupPath))
156179
{
157180
New-Item -ItemType Directory -Path $BackupPath | Out-Null
158181
}
159182

160-
# The following header will be added to patched files. The notice applies
161-
# to those files, not this script which generates the files.
162-
$Header = @"
163-
// -----------------------------------------------------------------------------
164-
// This file is auto-generated by Objective Sharpie and patched via the script
165-
// at /scripts/generate-cocoa-bindings.ps1. Do not edit this file directly.
166-
// If changes are required, update the script instead.
167-
// -----------------------------------------------------------------------------
168-
"@
183+
Push-Location $PSScriptRoot
169184

170185
################################################################################
171186
# Patch StructsAndEnums.cs
172187
################################################################################
173188
$File = 'StructsAndEnums.cs'
174189
Write-Output "Patching $BindingsPath/$File"
175190
Copy-Item "$BindingsPath/$File" -Destination "$BackupPath/$File"
176-
$Text = Get-Content "$BindingsPath/$File" -Raw
177-
178-
# Tabs to spaces
179-
$Text = $Text -replace '\t', ' '
180-
181-
# Trim extra newline at EOF
182-
$Text = $Text -replace '\n$', ''
183-
184-
# Insert namespace
185-
$Text = $Text -replace 'using .+;\n\n', "$&namespace Sentry.CocoaSdk;`n`n"
186-
187-
# Public to internal
188-
$Text = $Text -replace '\bpublic\b', 'internal'
189-
190-
# Remove static CFunctions class
191-
$Text = $Text -replace '(?ms)\nstatic class CFunctions.*?}\n', ''
192-
193-
# Add header and output file
194-
$Text = "$Header`n`n$Text"
195-
$Text | Out-File "$BindingsPath/$File"
191+
& dotnet run "$PSScriptRoot/patch-cocoa-bindings.cs" "$BindingsPath/$File" | ForEach-Object { Write-Host $_ }
196192

197193
################################################################################
198194
# Patch ApiDefinitions.cs
199195
################################################################################
200196
$File = 'ApiDefinitions.cs'
201197
Write-Output "Patching $BindingsPath/$File"
202198
Copy-Item "$BindingsPath/$File" -Destination "$BackupPath/$File"
203-
$Text = Get-Content "$BindingsPath/$File" -Raw
204-
205-
# Tabs to spaces
206-
$Text = $Text -replace '\t', ' '
207-
208-
# Trim extra newline at EOF
209-
$Text = $Text -replace '\n$', ''
210-
211-
# Insert namespace
212-
$Text = $Text -replace 'using .+;\n\n', "$&namespace Sentry.CocoaSdk;`n`n"
213-
214-
# Set Internal attributes on interfaces and delegates
215-
$Text = $Text -replace '(?m)^(partial interface|interface|delegate)\b', "[Internal]`n$&"
216-
217-
# Fix ISentrySerializable usage
218-
$Text = $Text -replace '\bISentrySerializable\b', 'SentrySerializable'
219-
220-
# Remove INSCopying due to https://github.com/xamarin/xamarin-macios/issues/17130
221-
$Text = $Text -replace ': INSCopying,', ':' -replace '\s?[:,] INSCopying', ''
222-
223-
# Remove iOS attributes like [iOS (13, 0)]
224-
$Text = $Text -replace '\[iOS \(13,\s?0\)\]\n?\s*', ''
225-
226-
# Remove Unavailable attributes like [Unavailable (PlatformName.iOSAppExtension)]
227-
$Text = $Text -replace '\[Unavailable \(PlatformName\.\w+\)\]\n?\s*', ''
228-
229-
# Fix delegate argument names
230-
$Text = $Text -replace '(NSError) arg\d', '$1 error'
231-
$Text = $Text -replace '(NSHttpUrlResponse) arg\d', '$1 response'
232-
$Text = $Text -replace '(SentryEvent) arg\d', '$1 @event'
233-
$Text = $Text -replace '(SentrySamplingContext) arg\d', '$1 samplingContext'
234-
$Text = $Text -replace '(SentryBreadcrumb) arg\d', '$1 breadcrumb'
235-
$Text = $Text -replace '(SentrySpan) arg\d', '$1 span'
236-
$Text = $Text -replace '(SentryAppStartMeasurement) arg\d', '$1 appStartMeasurement'
237-
$Text = $Text -replace '(SentryLog) arg\d', '$1 log'
238-
239-
# Adjust nullable return delegates (though broken until this is fixed: https://github.com/xamarin/xamarin-macios/issues/17109)
240-
$Text = $Text -replace 'delegate \w+ Sentry(BeforeBreadcrumb|BeforeSendEvent|TracesSampler)Callback', "[return: NullAllowed]`n$&"
241-
242-
# Adjust protocols (some are models)
243-
$Text = $Text -replace '(?ms)(@protocol.+?)/\*.+?\*/', '$1'
244-
$Text = $Text -replace '(?ms)@protocol (SentrySerializable|SentrySpan).+?\[Protocol\]', "`$&`n[Model]"
245-
246-
# Adjust SentrySpan base type
247-
$Text = $Text -replace 'interface SentrySpan\b', "[BaseType (typeof(NSObject))]`n`$&"
248-
249-
# Fix string constants
250-
$Text = $Text -replace '(?m)(.*\n){2}^\s{4}NSString k.+?\n\n?', ''
251-
$Text = $Text -replace '(?m)(.*\n){4}^partial interface Constants\n{\n}\n', ''
252-
$Text = $Text -replace '\[Verify \(ConstantsInterfaceAssociation\)\]\n', ''
253-
254-
# Remove SentryVersionNumber
255-
$Text = $Text -replace '.*SentryVersionNumber.*\n?', ''
256-
257-
# Remove SentryVersionString
258-
$Text = $Text -replace '.*SentryVersionString.*\n?', ''
259-
260-
# Remove duplicate attributes
261-
$s = 'partial interface Constants'
262-
$t = $Text -split $s, 2
263-
$t[1] = $t[1] -replace "\[Static\]\n\[Internal\]\n$s", $s
264-
$Text = $t -join $s
265-
266-
# Remove empty Constants block
267-
$Text = $Text -replace '\[Static\]\s*\[Internal\]\s*partial\s+interface\s+Constants\s\{[\s\n]*\}\n\n', ''
268-
269-
# Update MethodToProperty translations
270-
$Text = $Text -replace '(Export \("get\w+"\)\]\n)\s*\[Verify \(MethodToProperty\)\]\n(.+ \{ get; \})', '$1$2'
271-
$Text = $Text -replace '\[Verify \(MethodToProperty\)\]\n\s*(.+ (?:Hash|Value|DefaultIntegrations|AppStartMeasurementWithSpans|BaggageHttpHeader) \{ get; \})', '$1'
272-
$Text = $Text -replace '\[Verify \(MethodToProperty\)\]\n\s*(.+) \{ get; \}', '$1();'
273-
274-
# Allow weakly typed NSArray
275-
# We have some that accept either NSString or NSRegularExpression, which have no common type so they use NSObject
276-
$Text = $Text -replace '\s*\[Verify \(StronglyTypedNSArray\)\]\n', ''
277-
278-
# Fix broken multi-line comments
279-
$Text = $Text -replace '(DEPRECATED_MSG_ATTRIBUTE\()\n\s*', '$1'
280-
$Text = $Text -replace '(DEPRECATED_MSG_ATTRIBUTE\([^)]*?)"\s*\r?\n\s*"', '$1 '
281-
282-
# Remove default IsEqual implementation (already implemented by NSObject)
283-
$Text = $Text -replace '(?ms)\n?^ *// [^\n]*isEqual:.*?$.*?;\n', ''
284-
285-
# Replace obsolete platform availability attributes
286-
$Text = $Text -replace '([\[,] )MacCatalyst \(', '$1Introduced (PlatformName.MacCatalyst, '
287-
$Text = $Text -replace '([\[,] )Mac \(', '$1Introduced (PlatformName.MacOSX, '
288-
$Text = $Text -replace '([\[,] )iOS \(', '$1Introduced (PlatformName.iOS, '
289-
290-
# Make interface partial if we need to access private APIs. Other parts will be defined in PrivateApiDefinitions.cs
291-
$Text = $Text -replace '(?m)^interface SentryScope', 'partial $&'
292-
293-
$Text = $Text -replace '.*SentryEnvelope .*?[\s\S]*?\n\n', ''
294-
$Text = $Text -replace '.*typedef.*SentryOnAppStartMeasurementAvailable.*?[\s\S]*?\n\n', ''
295-
296-
$propertiesToRemove = @(
297-
'SentryAppStartMeasurement',
298-
'SentryOnAppStartMeasurementAvailable',
299-
'SentryMetricsAPI',
300-
'SentryExperimentalOptions',
301-
'description',
302-
'enableMetricKitRawPayload'
303-
)
304-
305-
foreach ($property in $propertiesToRemove)
306-
{
307-
$Text = $Text -replace "\n.*property.*$property.*?[\s\S]*?\}\n", ''
308-
}
309-
310-
311-
# Add header and output file
312-
$Text = "$Header`n`n$Text"
313-
$Text | Out-File "$BindingsPath/$File"
199+
& dotnet run "$PSScriptRoot/patch-cocoa-bindings.cs" "$BindingsPath/$File" | ForEach-Object { Write-Host $_ }

0 commit comments

Comments
 (0)