configureSigning method

Future<void> configureSigning({
  1. required String gitUrl,
  2. bool isAdhoc = false,
})

Implementation

Future<void> configureSigning({
  required final String gitUrl,
  final bool isAdhoc = false,
}) async {
  final bundleId = File('ios/Runner.xcodeproj/project.pbxproj')
      .readAsStringSync()
      .split('\n')
      .firstWhere(
        (final line) => line.contains('PRODUCT_BUNDLE_IDENTIFIER'),
      )
      .split(' = ')[1]
      .replaceAll(';', '')
      .trim();
  final projectFile = File('ios/Runner.xcodeproj/project.pbxproj');
  projectFile.writeAsStringSync(
    projectFile
        .readAsStringSync()
        .replaceAll(
          'CODE_SIGN_IDENTITY = "Apple Development";',
          'CODE_SIGN_IDENTITY = "iPhone Distribution";',
        )
        .replaceAll(
          'CODE_SIGN_STYLE = Automatic;',
          'CODE_SIGN_STYLE = Manual;',
        )
        .replaceAll(
          'PROVISIONING_PROFILE_SPECIFIER = "";',
          'PROVISIONING_PROFILE_SPECIFIER = "match '
              '${isAdhoc ? 'AdHoc' : 'AppStore'} '
              '$bundleId";',
        ),
  );

  final apiKey = '{"key_id": "$_keyId", '
      '"issuer_id": "$_issuerId", '
      '"key": "${_privateKey.replaceAll('\n', r'\n')}", '
      '"in_house": false }';
  final fastlaneMatchProcess = await Process.start(
    'fastlane',
    [
      'match',
      if (isAdhoc) 'adhoc' else 'appstore',
      '--app_identifier',
      bundleId,
      '--git_url',
      gitUrl,
      '--api_key',
      apiKey,
      '--force_for_new_devices',
      'true',
    ],
    mode: ProcessStartMode.inheritStdio,
    environment: Platform.environment,
  );

  final exitCode = await fastlaneMatchProcess.exitCode;
  if (exitCode != 0) {
    throw Exception(
      'Error configuring signing for iOS app. ${fastlaneMatchProcess.stderr}',
    );
  }
}