-
-
Notifications
You must be signed in to change notification settings - Fork 12
fix(ios): improve bundle identifier detection and replacement #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
smithemely
commented
Apr 22, 2025
- fix: handle both quoted and unquoted bundle identifiers
- feat: implement smarter base identifier detection logic
- fix: properly preserve extensions with multiple segments
…edGuy#86) * fix: handle both quoted and unquoted bundle identifiers * feat: implement smarter base identifier detection logic * fix: properly preserve extensions with multiple segments
I tested the updated implementation with various bundle identifier patterns in real projects, and it works correctly. The solution now handles:
This fix ensures bundle identifiers are consistently renamed throughout the project file while maintaining the structure of complex extension identifiers. It's more robust than the previous implementation. |
I'll test this once I get some free time. Although I was thinking of adding an additional property where users can provide the old package name, so no need for any complex logic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly looks good to be. Just few questions and changes.
|
||
// Extract all bundle identifiers, accounting for both quoted and unquoted formats | ||
final bundleIdRegex = RegExp( | ||
r'PRODUCT_BUNDLE_IDENTIFIER = "?([^";]+)"?;', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use only the allowed characters to match group. i.e. ``r'PRODUCT_BUNDLE_IDENTIFIER = "?([A-Za-z0-9.-_]+)"?;'`
// Extract all bundle identifiers, accounting for both quoted and unquoted formats | ||
final bundleIdRegex = RegExp( | ||
r'PRODUCT_BUNDLE_IDENTIFIER = "?([^";]+)"?;', | ||
multiLine: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not needed AFAIK
final bundleIdentifierMatches = bundleIdRegex | ||
.allMatches(iosProjectString) | ||
.map((m) => m.group(1)!) | ||
.toList(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to convert to list. Iterable
is fine and faster
for (final identifier in bundleIdentifierMatches) { | ||
bool isBaseForOthers = false; | ||
for (final other in bundleIdentifierMatches) { | ||
if (identifier != other && other.startsWith(identifier)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is one value is com.example.app
and other is com.example.application
?
// Special case for RunnerTests which might not be caught by the pattern above | ||
if (identifierReplacements.containsKey(baseIdentifier)) { | ||
newIosProjectString = newIosProjectString.replaceAll( | ||
RegExp('PRODUCT_BUNDLE_IDENTIFIER = "$baseIdentifier\\.RunnerTests";'), | ||
'PRODUCT_BUNDLE_IDENTIFIER = "$packageName.RunnerTests";', | ||
); | ||
|
||
newIosProjectString = newIosProjectString.replaceAll( | ||
RegExp('PRODUCT_BUNDLE_IDENTIFIER = $baseIdentifier\\.RunnerTests;'), | ||
'PRODUCT_BUNDLE_IDENTIFIER = $packageName.RunnerTests;', | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary? Looks redundant to me. Are there any special cases you faced that requires this?
// Create a map of all identifiers to their new values | ||
final identifierReplacements = <String, String>{}; | ||
|
||
for (final identifier in bundleIdentifierMatches) { | ||
if (identifier == baseIdentifier) { | ||
identifierReplacements[identifier] = packageName; | ||
} else if (identifier.startsWith(baseIdentifier)) { | ||
// This is an extension | ||
final extension = identifier.substring(baseIdentifier.length); | ||
identifierReplacements[identifier] = '$packageName$extension'; | ||
} else { | ||
// This is an unrelated identifier, skip it | ||
_logger.w('Skipping unrelated identifier: $identifier'); | ||
} | ||
} | ||
|
||
// Apply all replacements to the project file | ||
var newIosProjectString = iosProjectString; | ||
|
||
identifierReplacements.forEach((oldId, newId) { | ||
// Replace unquoted format | ||
newIosProjectString = newIosProjectString.replaceAll( | ||
'PRODUCT_BUNDLE_IDENTIFIER = $oldId;', | ||
'PRODUCT_BUNDLE_IDENTIFIER = $newId;', | ||
); | ||
|
||
// Replace quoted format | ||
newIosProjectString = newIosProjectString.replaceAll( | ||
'PRODUCT_BUNDLE_IDENTIFIER = "$oldId";', | ||
'PRODUCT_BUNDLE_IDENTIFIER = "$newId";', | ||
); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be merged in a single logic? Like:
newIosProjectString = iosProjectString.replaceAllMapped(
'PRODUCT_BUNDLE_IDENTIFIER = ("?)$baseIdentifier',
(match) {
final quote = match.group(1) ?? '';
return 'PRODUCT_BUNDLE_IDENTIFIER = $quote$packageName';
},
);