Skip to content

Commit 2b52d96

Browse files
committed
Post-process all (Obj-C and Swift) bindings
1 parent eba48ba commit 2b52d96

File tree

7 files changed

+1497
-1626
lines changed

7 files changed

+1497
-1626
lines changed

scripts/generate-cocoa-bindings.ps1

Lines changed: 121 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,14 @@ else
159159
Write-Host "File not found: $swiftHeaderFile"
160160
}
161161

162-
# Generate Obj-C bindings
163-
Write-Output 'Generating Obj-C bindings with Objective Sharpie.'
164-
sharpie bind -sdk $iPhoneSdkVersion `
165-
-scope "$CocoaSdkPath" `
166-
"$HeadersPath/Sentry.h" `
167-
"$PrivateHeadersPath/PrivateSentrySDKOnly.h" `
168-
-o $BindingsPath `
169-
-c -Wno-objc-property-no-attribute
170-
171-
# Generate Swift bindings
172-
Write-Output 'Generating Swift bindings with Objective Sharpie.'
162+
# Generate bindings
163+
Write-Output 'Generating bindings with Objective Sharpie.'
173164
sharpie bind -sdk $iPhoneSdkVersion `
174165
-scope "$CocoaSdkPath" `
175166
"$HeadersPath/Sentry.h" `
176167
"$HeadersPath/Sentry-Swift.h" `
168+
"$PrivateHeadersPath/PrivateSentrySDKOnly.h" `
177169
-o $BindingsPath `
178-
-p Swift `
179170
-c -Wno-objc-property-no-attribute
180171

181172
# Ensure backup path exists
@@ -197,189 +188,167 @@ $Header = @"
197188
################################################################################
198189
# Patch StructsAndEnums.cs
199190
################################################################################
200-
function Patch-StructsAndEnums([string] $File)
201-
{
202-
Write-Output "Patching $BindingsPath/$File"
203-
Copy-Item "$BindingsPath/$File" -Destination "$BackupPath/$File"
204-
$Text = Get-Content "$BindingsPath/$File" -Raw
191+
$File = 'StructsAndEnums.cs'
192+
Write-Output "Patching $BindingsPath/$File"
193+
Copy-Item "$BindingsPath/$File" -Destination "$BackupPath/$File"
194+
$Text = Get-Content "$BindingsPath/$File" -Raw
205195

206-
# Tabs to spaces
207-
$Text = $Text -replace '\t', ' '
196+
# Tabs to spaces
197+
$Text = $Text -replace '\t', ' '
208198

209-
# Trim extra newline at EOF
210-
$Text = $Text -replace '\n$', ''
199+
# Trim extra newline at EOF
200+
$Text = $Text -replace '\n$', ''
211201

212-
# Insert namespace
213-
$Text = $Text -replace 'using .+;\n\n', "$&namespace Sentry.CocoaSdk;`n`n"
202+
# Insert namespace
203+
$Text = $Text -replace 'using .+;\n\n', "$&namespace Sentry.CocoaSdk;`n`n"
214204

215-
# Public to internal
216-
$Text = $Text -replace '\bpublic\b', 'internal'
205+
# Public to internal
206+
$Text = $Text -replace '\bpublic\b', 'internal'
217207

218-
# Remove static CFunctions class
219-
$Text = $Text -replace '(?ms)\nstatic class CFunctions.*?}\n', ''
220-
221-
# Add header and output file
222-
$Text = "$Header`n`n$Text"
223-
$Text | Out-File "$BindingsPath/$File"
224-
}
208+
# Remove static CFunctions class
209+
$Text = $Text -replace '(?ms)\nstatic class CFunctions.*?}\n', ''
225210

226-
Patch-StructsAndEnums 'StructsAndEnums.cs'
227-
Patch-StructsAndEnums 'SwiftStructsAndEnums.cs'
211+
# Add header and output file
212+
$Text = "$Header`n`n$Text"
213+
$Text | Out-File "$BindingsPath/$File"
228214

229215
################################################################################
230216
# Patch ApiDefinitions.cs
231217
################################################################################
232-
function Patch-ApiDefinitions([string] $File, [switch] $RemoveDelegates = $false)
233-
{
234-
Write-Output "Patching $BindingsPath/$File"
235-
Copy-Item "$BindingsPath/$File" -Destination "$BackupPath/$File"
236-
$Text = Get-Content "$BindingsPath/$File" -Raw
237-
238-
# Tabs to spaces
239-
$Text = $Text -replace '\t', ' '
240-
241-
# Trim extra newline at EOF
242-
$Text = $Text -replace '\n$', ''
218+
$File = 'ApiDefinitions.cs'
219+
Write-Output "Patching $BindingsPath/$File"
220+
Copy-Item "$BindingsPath/$File" -Destination "$BackupPath/$File"
221+
$Text = Get-Content "$BindingsPath/$File" -Raw
243222

244-
# Insert namespace
245-
$Text = $Text -replace 'using .+;\n\n', "$&namespace Sentry.CocoaSdk;`n`n"
223+
# Tabs to spaces
224+
$Text = $Text -replace '\t', ' '
246225

247-
# Fix broken multi-line comments
248-
$Text = $Text -replace '(DEPRECATED_MSG_ATTRIBUTE\()\n\s*', '$1'
249-
$Text = $Text -replace '(DEPRECATED_MSG_ATTRIBUTE\([^)]*?)"\s*\r?\n\s*"', '$1 '
226+
# Trim extra newline at EOF
227+
$Text = $Text -replace '\n$', ''
250228

251-
if ($RemoveDelegates)
252-
{
253-
# Remove delegate definitions
254-
$Text = $Text -replace '(?ms)// typedef[^\n]*\n(?:\[Internal\]\n)?delegate[^\{;]+;\n\n', ''
255-
}
229+
# Insert namespace
230+
$Text = $Text -replace 'using .+;\n\n', "$&namespace Sentry.CocoaSdk;`n`n"
256231

257-
# Set Internal attributes on interfaces and delegates
258-
$Text = $Text -replace '(?m)^(partial interface|interface|delegate)\b', "[Internal]`n$&"
232+
# Set Internal attributes on interfaces and delegates
233+
$Text = $Text -replace '(?m)^(partial interface|interface|delegate)\b', "[Internal]`n$&"
259234

260-
# Fix ISentrySerializable usage
261-
$Text = $Text -replace '\bISentrySerializable\b', 'SentrySerializable'
235+
# Fix ISentrySerializable usage
236+
$Text = $Text -replace '\bISentrySerializable\b', 'SentrySerializable'
262237

263-
# Remove INSCopying due to https://github.com/xamarin/xamarin-macios/issues/17130
264-
$Text = $Text -replace ': INSCopying,', ':' -replace '\s?[:,] INSCopying', ''
238+
# Remove INSCopying due to https://github.com/xamarin/xamarin-macios/issues/17130
239+
$Text = $Text -replace ': INSCopying,', ':' -replace '\s?[:,] INSCopying', ''
265240

266-
# Remove iOS attributes like [iOS (13, 0)]
267-
$Text = $Text -replace '\[iOS \(13,\s?0\)\]\n?\s*', ''
241+
# Remove iOS attributes like [iOS (13, 0)]
242+
$Text = $Text -replace '\[iOS \(13,\s?0\)\]\n?\s*', ''
268243

269-
# Remove Unavailable attributes like [Unavailable (PlatformName.iOSAppExtension)]
270-
$Text = $Text -replace '\[Unavailable \(PlatformName\.\w+\)\]\n?\s*', ''
244+
# Remove Unavailable attributes like [Unavailable (PlatformName.iOSAppExtension)]
245+
$Text = $Text -replace '\[Unavailable \(PlatformName\.\w+\)\]\n?\s*', ''
271246

272-
# Fix delegate argument names
273-
$Text = $Text -replace '(NSError) arg\d', '$1 error'
274-
$Text = $Text -replace '(NSHttpUrlResponse) arg\d', '$1 response'
275-
$Text = $Text -replace '(SentryEvent) arg\d', '$1 @event'
276-
$Text = $Text -replace '(SentrySamplingContext) arg\d', '$1 samplingContext'
277-
$Text = $Text -replace '(SentryBreadcrumb) arg\d', '$1 breadcrumb'
278-
$Text = $Text -replace '(SentrySpan) arg\d', '$1 span'
279-
$Text = $Text -replace '(SentryAppStartMeasurement) arg\d', '$1 appStartMeasurement'
280-
$Text = $Text -replace '(SentryLog) arg\d', '$1 log'
247+
# Fix delegate argument names
248+
$Text = $Text -replace '(NSError) arg\d', '$1 error'
249+
$Text = $Text -replace '(NSHttpUrlResponse) arg\d', '$1 response'
250+
$Text = $Text -replace '(SentryEvent) arg\d', '$1 @event'
251+
$Text = $Text -replace '(SentrySamplingContext) arg\d', '$1 samplingContext'
252+
$Text = $Text -replace '(SentryBreadcrumb) arg\d', '$1 breadcrumb'
253+
$Text = $Text -replace '(SentrySpan) arg\d', '$1 span'
254+
$Text = $Text -replace '(SentryAppStartMeasurement) arg\d', '$1 appStartMeasurement'
255+
$Text = $Text -replace '(SentryLog) arg\d', '$1 log'
281256

282-
# Adjust nullable return delegates (though broken until this is fixed: https://github.com/xamarin/xamarin-macios/issues/17109)
283-
$Text = $Text -replace 'delegate \w+ Sentry(BeforeBreadcrumb|BeforeSendEvent|TracesSampler)Callback', "[return: NullAllowed]`n$&"
257+
# Adjust nullable return delegates (though broken until this is fixed: https://github.com/xamarin/xamarin-macios/issues/17109)
258+
$Text = $Text -replace 'delegate \w+ Sentry(BeforeBreadcrumb|BeforeSendEvent|TracesSampler)Callback', "[return: NullAllowed]`n$&"
284259

285-
# Remove empty SentryRRWebEvent protocol
286-
$Text = $Text -replace '(?ms)\[Protocol\]\s*\[Internal\]\s*interface\s+SentryRRWebEvent\s*:\s*SentrySerializable[^\{]*\{[^\}]*\}', ''
260+
# Remove empty SentryRRWebEvent protocol
261+
$Text = $Text -replace '(?ms)\[Protocol\]\s*\[Internal\]\s*interface\s+SentryRRWebEvent\s*:\s*SentrySerializable[^\{]*\{[^\}]*\}', ''
287262

288-
# Adjust protocols (some are models)
289-
$Text = $Text -replace '(?ms)(@protocol.+?)/\*.+?\*/', '$1'
290-
$Text = $Text -replace '(?ms)@protocol (SentrySerializable|SentrySpan).+?\[Protocol\]', "`$&`n[Model]"
291-
$Text = $Text -replace '(?ms)@protocol (SentryRedactOptions|SentryCurrentDateProvider).+?\[Protocol \(Name = \"\w+\"\)\]', "`$&`n[Model]"
263+
# Adjust protocols (some are models)
264+
$Text = $Text -replace '(?ms)(@protocol.+?)/\*.+?\*/', '$1'
265+
$Text = $Text -replace '(?ms)@protocol (SentrySerializable|SentrySpan).+?\[Protocol\]', "`$&`n[Model]"
266+
$Text = $Text -replace '(?ms)@protocol (SentryRedactOptions|SentryCurrentDateProvider).+?\[Protocol \(Name = \"\w+\"\)\]', "`$&`n[Model]"
292267

293-
# Adjust base types
294-
$Text = $Text -replace 'interface (SentrySpan|SentryRedactOptions|SentryCurrentDateProvider)\b', "[BaseType (typeof(NSObject))]`n`$&"
268+
# Adjust base types
269+
$Text = $Text -replace 'interface (SentrySpan|SentryRedactOptions|SentryCurrentDateProvider)\b', "[BaseType (typeof(NSObject))]`n`$&"
295270

296-
# Fix string constants
297-
$Text = $Text -replace '(?m)(.*\n){2}^\s{4}NSString k.+?\n\n?', ''
298-
$Text = $Text -replace '(?m)(.*\n){4}^partial interface Constants\n{\n}\n', ''
299-
$Text = $Text -replace '\[Verify \(ConstantsInterfaceAssociation\)\]\n', ''
271+
# Fix string constants
272+
$Text = $Text -replace '(?m)(.*\n){2}^\s{4}NSString k.+?\n\n?', ''
273+
$Text = $Text -replace '(?m)(.*\n){4}^partial interface Constants\n{\n}\n', ''
274+
$Text = $Text -replace '\[Verify \(ConstantsInterfaceAssociation\)\]\n', ''
300275

301-
# Remove SentryVersionNumber
302-
$Text = $Text -replace '.*SentryVersionNumber.*\n?', ''
276+
# Remove SentryVersionNumber
277+
$Text = $Text -replace '.*SentryVersionNumber.*\n?', ''
303278

304-
# Remove SentryVersionString
305-
$Text = $Text -replace '.*SentryVersionString.*\n?', ''
279+
# Remove SentryVersionString
280+
$Text = $Text -replace '.*SentryVersionString.*\n?', ''
306281

307-
# Remove duplicate attributes
308-
$s = 'partial interface Constants'
309-
$t = $Text -split $s, 2
310-
$t[1] = $t[1] -replace "\[Static\]\n\[Internal\]\n$s", $s
311-
$Text = $t -join $s
282+
# Remove duplicate attributes
283+
$s = 'partial interface Constants'
284+
$t = $Text -split $s, 2
285+
$t[1] = $t[1] -replace "\[Static\]\n\[Internal\]\n$s", $s
286+
$Text = $t -join $s
312287

313-
# Remove empty Constants block
314-
$Text = $Text -replace '\[Static\]\s*\[Internal\]\s*partial\s+interface\s+Constants\s\{[\s\n]*\}\n\n', ''
288+
# Remove empty Constants block
289+
$Text = $Text -replace '\[Static\]\s*\[Internal\]\s*partial\s+interface\s+Constants\s\{[\s\n]*\}\n\n', ''
315290

316-
# Update MethodToProperty translations
317-
$Text = $Text -replace '(Export \("get\w+"\)\]\n)\s*\[Verify \(MethodToProperty\)\]\n(.+ \{ get; \})', '$1$2'
318-
$Text = $Text -replace '\[Verify \(MethodToProperty\)\]\n\s*(.+ (?:Hash|Value|DefaultIntegrations|AppStartMeasurementWithSpans|BaggageHttpHeader) \{ get; \})', '$1'
319-
$Text = $Text -replace '\[Verify \(MethodToProperty\)\]\n\s*(.+) \{ get; \}', '$1();'
291+
# Update MethodToProperty translations
292+
$Text = $Text -replace '(Export \("get\w+"\)\]\n)\s*\[Verify \(MethodToProperty\)\]\n(.+ \{ get; \})', '$1$2'
293+
$Text = $Text -replace '\[Verify \(MethodToProperty\)\]\n\s*(.+ (?:Hash|Value|DefaultIntegrations|AppStartMeasurementWithSpans|BaggageHttpHeader) \{ get; \})', '$1'
294+
$Text = $Text -replace '\[Verify \(MethodToProperty\)\]\n\s*(.+) \{ get; \}', '$1();'
320295

321-
# Allow weakly typed NSArray
322-
# We have some that accept either NSString or NSRegularExpression, which have no common type so they use NSObject
323-
$Text = $Text -replace '\s*\[Verify \(StronglyTypedNSArray\)\]\n', ''
296+
# Allow weakly typed NSArray
297+
# We have some that accept either NSString or NSRegularExpression, which have no common type so they use NSObject
298+
$Text = $Text -replace '\s*\[Verify \(StronglyTypedNSArray\)\]\n', ''
324299

325-
# Remove default IsEqual/CopyWithZone/Description implementations (already implemented by NSObject)
326-
$Text = $Text -replace '(?ms)\n?^ *// [^\n]*isEqual:.*?$.*?;\n', ''
327-
$Text = $Text -replace '(?ms)\n?^ *// [^\n]*copyWithZone:.*?$.*?;\n', ''
300+
# Fix broken multi-line comments
301+
$Text = $Text -replace '(DEPRECATED_MSG_ATTRIBUTE\()\n\s*', '$1'
302+
$Text = $Text -replace '(DEPRECATED_MSG_ATTRIBUTE\([^)]*?)"\s*\r?\n\s*"', '$1 '
328303

329-
# Replace obsolete platform availability attributes
330-
$Text = $Text -replace '([\[,] )MacCatalyst \(', '$1Introduced (PlatformName.MacCatalyst, '
331-
$Text = $Text -replace '([\[,] )Mac \(', '$1Introduced (PlatformName.MacOSX, '
332-
$Text = $Text -replace '([\[,] )iOS \(', '$1Introduced (PlatformName.iOS, '
304+
# Remove default IsEqual/CopyWithZone/Description implementations (already implemented by NSObject)
305+
$Text = $Text -replace '(?ms)\n?^ *// [^\n]*isEqual:.*?$.*?;\n', ''
306+
$Text = $Text -replace '(?ms)\n?^ *// [^\n]*copyWithZone:.*?$.*?;\n', ''
333307

334-
# Make interface partial if we need to access private APIs. Other parts will be defined in PrivateApiDefinitions.cs
335-
$Text = $Text -replace '(?m)^interface SentryScope', 'partial $&'
308+
# Replace obsolete platform availability attributes
309+
$Text = $Text -replace '([\[,] )MacCatalyst \(', '$1Introduced (PlatformName.MacCatalyst, '
310+
$Text = $Text -replace '([\[,] )Mac \(', '$1Introduced (PlatformName.MacOSX, '
311+
$Text = $Text -replace '([\[,] )iOS \(', '$1Introduced (PlatformName.iOS, '
336312

337-
$Text = $Text -replace '.*typedef.*SentryOnAppStartMeasurementAvailable.*?[\s\S]*?\n\n', ''
338-
$Text = $Text -replace 'NSDictionary<NSString, SentryStructuredLogAttribute>', 'NSDictionary<NSString, NSObject>'
313+
# Make interface partial if we need to access private APIs. Other parts will be defined in PrivateApiDefinitions.cs
314+
$Text = $Text -replace '(?m)^interface SentryScope', 'partial $&'
339315

340-
# Comment out interfaces
341-
$Text = $Text -replace 'interface (\w+) : (ISentryRedactOptions|ISentryRRWebEvent)', 'interface $1 //: $2'
316+
$Text = $Text -replace '.*typedef.*SentryOnAppStartMeasurementAvailable.*?[\s\S]*?\n\n', ''
317+
$Text = $Text -replace 'NSDictionary<NSString, SentryStructuredLogAttribute>', 'NSDictionary<NSString, NSObject>'
342318

343-
$propertiesToRemove = @(
344-
'SentryAppStartMeasurement',
345-
'SentryOnAppStartMeasurementAvailable',
346-
'SentryMetricsAPI',
347-
'SentryExperimentalOptions',
348-
'description',
349-
'enableMetricKitRawPayload'
350-
)
319+
# Comment out interfaces
320+
$Text = $Text -replace 'interface (\w+) : (ISentryRedactOptions|ISentryRRWebEvent)', 'interface $1 //: $2'
351321

352-
foreach ($property in $propertiesToRemove)
353-
{
354-
$Text = $Text -replace "\n.*property.*$property.*?[\s\S]*?\}\n", ''
355-
}
322+
$propertiesToRemove = @(
323+
'SentryAppStartMeasurement',
324+
'SentryOnAppStartMeasurementAvailable',
325+
'SentryMetricsAPI',
326+
'SentryExperimentalOptions',
327+
'description',
328+
'enableMetricKitRawPayload'
329+
)
356330

357-
358-
# Add header and output file
359-
$Text = "$Header`n`n$Text"
360-
$Text | Out-File "$BindingsPath/$File"
331+
foreach ($property in $propertiesToRemove)
332+
{
333+
$Text = $Text -replace "\n.*property.*$property.*?[\s\S]*?\}\n", ''
361334
}
362335

363-
Patch-ApiDefinitions 'ApiDefinitions.cs'
364-
Patch-ApiDefinitions 'SwiftApiDefinitions.cs' -RemoveDelegates
336+
337+
# Add header and output file
338+
$Text = "$Header`n`n$Text"
339+
$Text | Out-File "$BindingsPath/$File"
365340

366341
################################################################################
367-
# Post-process SwiftStructsAndEnums.cs and SwiftApiDefinitions.cs
342+
# Post-process ApiDefinitions.cs
368343
################################################################################
369344

370-
function PostProcess-SwiftBindings([string] $File)
345+
try
371346
{
372-
try
373-
{
374-
Write-Output "Post-processing $BindingsPath/$File"
375-
Push-Location $PSScriptRoot
376-
& dotnet run "post-process-swift-bindings.cs" "$BindingsPath/$File" | ForEach-Object { Write-Host $_ }
377-
}
378-
finally
379-
{
380-
Pop-Location
381-
}
347+
Write-Output "Post-processing $BindingsPath/$File"
348+
Push-Location $PSScriptRoot
349+
& dotnet run "post-process-cocoa-bindings.cs" "$BindingsPath/$File" | ForEach-Object { Write-Host $_ }
350+
}
351+
finally
352+
{
353+
Pop-Location
382354
}
383-
384-
PostProcess-SwiftBindings 'SwiftStructsAndEnums.cs'
385-
PostProcess-SwiftBindings 'SwiftApiDefinitions.cs'

0 commit comments

Comments
 (0)