replaceBundleIdentifier method

Future<void> replaceBundleIdentifier({
  1. required String filePath,
  2. required String newIdentifier,
})

Replaces the bundle identifier in the given file.

Implementation

Future<void> replaceBundleIdentifier({
  required String filePath,
  required String newIdentifier,
}) async {
  final content = await File(filePath).readAsString();

  // 改进后的正则表达式
  final pattern = RegExp(
    r'(PRODUCT_BUNDLE_IDENTIFIER = )([\w.]+?)(\.RunnerTests)?;',
    dotAll: true,
  );

  final newContent = content.replaceAllMapped(pattern, (match) {
    final prefix = match.group(1)!; // "PRODUCT_BUNDLE_IDENTIFIER = "
    final suffix = match.group(3); // 可能存在的 ".RunnerTests"
    return '$prefix$newIdentifier${suffix ?? ""};'; // 保留后缀
  });

  await File(filePath).writeAsString(newContent);
}