Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 49 additions & 23 deletions lib/platforms/ios.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,34 +107,60 @@ void _setIOSPackageName(dynamic packageName) {
}

final iosProjectString = iosProjectFile.readAsStringSync();
final newBundleIDIOSProjectString = iosProjectString
// Replaces old bundle id from
// `PRODUCT_BUNDLE_IDENTIFIER = {{BUNDLE_ID}};`
.replaceAll(
RegExp(
r'PRODUCT_BUNDLE_IDENTIFIER = ([A-Za-z0-9.-_]+)(?<!\.RunnerTests);',
),
'PRODUCT_BUNDLE_IDENTIFIER = $packageName;',
)
// Replaces old bundle id from
// `PRODUCT_BUNDLE_IDENTIFIER = {{BUNDLE_ID}}.RunnerTests;`
.replaceAll(
RegExp('PRODUCT_BUNDLE_IDENTIFIER = (.*?).RunnerTests;'),
'PRODUCT_BUNDLE_IDENTIFIER = $packageName.RunnerTests;',
)
// Removes old bundle id from
// `PRODUCT_BUNDLE_IDENTIFIER = "{{BUNDLE_ID}}.{{EXTENSION_NAME}}";`
.replaceAllMapped(

// Extract all bundle identifiers, using only allowed characters
final bundleIdRegex = RegExp(
r'PRODUCT_BUNDLE_IDENTIFIER = "?([A-Za-z0-9._-]+)"?;',
);

final bundleIdentifierMatches = bundleIdRegex
.allMatches(iosProjectString)
.map((m) => m.group(1)!)
.toSet();

if (bundleIdentifierMatches.isEmpty) {
throw Exception('No bundle identifiers found in project file');
}

// Find the base identifier by counting extensions
String? baseIdentifier;
int maxExtensions = 0;

for (final identifier in bundleIdentifierMatches) {
int extensionCount = 0;

for (final other in bundleIdentifierMatches) {
// Check if 'other' extends 'identifier' with a dot separator
if (identifier != other && other.startsWith('$identifier.')) {
extensionCount++;
}
}

// Use the identifier that has the most extensions
if (extensionCount > maxExtensions) {
maxExtensions = extensionCount;
baseIdentifier = identifier;
}
}

// If no base identifier found, use the shortest unique identifier
baseIdentifier ??=
bundleIdentifierMatches.reduce((a, b) => a.length <= b.length ? a : b);

// Replace all occurrences
final newIosProjectString = iosProjectString.replaceAllMapped(
RegExp(
r'PRODUCT_BUNDLE_IDENTIFIER = "([A-Za-z0-9.-_]+)\.([A-Za-z0-9.-_]+)";',
),
'PRODUCT_BUNDLE_IDENTIFIER = ("?)${RegExp.escape(baseIdentifier)}(\\.[A-Za-z0-9._-]+)?("?);'),
(match) {
final extensionName = match.group(2);
return 'PRODUCT_BUNDLE_IDENTIFIER = "$packageName.$extensionName";';
final openQuote = match.group(1) ?? '';
final extension = match.group(2) ?? '';
final closeQuote = match.group(3) ?? '';

return 'PRODUCT_BUNDLE_IDENTIFIER = $openQuote$packageName$extension$closeQuote;';
},
);

iosProjectFile.writeAsStringSync(newBundleIDIOSProjectString);
iosProjectFile.writeAsStringSync(newIosProjectString);

_logger.i('iOS bundle identifier set to: `$packageName` (project.pbxproj)');
} on _PackageRenameException catch (e) {
Expand Down