publish method

  1. @override
Future<void> publish()
override

Implementation

@override
Future<void> publish() async {
  _logger.info('Install dependencies...');
  if (!await isInstalled('fastlane')) {
    await ensureInstalled('ruby');
    await ensureInstalled('ruby-dev');
    await ensureInstalled(
      'fastlane',
      installCommands: ['sudo', 'gem', 'install'],
    );
  }

  await ensureInstalled(
    'bundler',
    installCommands: ['sudo', 'gem', 'install'],
  );

  // Save Google play store credentials file
  final fastlaneSecretsJsonFile =
      File('$_androidDirectory/$_fastlaneSecretsJsonFile');
  await fastlaneSecretsJsonFile
      .writeAsBytes(base64.decode(fastlaneSecretsJsonBase64));

  await installFastlanePlugin('get_application_id_flavor',
      workingDirectory: _androidDirectory);

  final packageName = await runFastlaneProcess(
    [
      'run',
      'get_application_id_flavor',
      if (platformBuild.flutterBuild.flavor != null)
        'flavor:${platformBuild.flutterBuild.flavor}',
    ],
    printCall: true,
    workingDirectory: _androidDirectory,
  );
  if (packageName == null) throw Exception('Application Id not found');

  final fastlaneAppfile = '''
json_key_file("${fastlaneSecretsJsonFile.absolute.path}")
package_name("$packageName")
  ''';
  await File('$_fastlaneDirectory/Appfile').writeAsString(fastlaneAppfile);

  // Check if play store credentials are valid
  await runProcess(
    'fastlane',
    [
      'run',
      'validate_play_store_json_key',
      // 'json_key:${fastlaneSecretsJsonFile.absolute.path}',
    ],
    workingDirectory: _androidDirectory,
    runInShell: true,
  );

  final track = switch (flutterPublish.stage) {
    PublishStage.production => 'production',
    PublishStage.beta => 'beta',
    PublishStage.alpha => 'alpha',
    _ => 'internal',
  };

  if (platformBuild.flutterBuild.buildVersion.build.isEmpty) {
    var versionCode = await _getLastVersionCodeFromGooglePlay(track);
    if (versionCode != null) {
      // Increase versionCode by 1, if available:
      versionCode++;
      _logger.info(
        'Use "$versionCode" as next version code (fetched from Google Play).',
      );

      platformBuild.flutterBuild.buildVersion =
          platformBuild.flutterBuild.buildVersion.copyWith(
        build: versionCode.toString(),
      );
    }
  }

  _logger.info('Build application...');

  final outputPath = await platformBuild.build();
  _logger.info('Build artifact path: $outputPath');
  final outputFile = File(outputPath);

  if (flutterPublish.isDryRun) {
    _logger.info('Did NOT publish: Remove `--dry-run` flag for publishing.');
  } else {
    _logger.info('Publish...');
    await runProcess(
      'fastlane',
      [
        'supply',
        '--aab',
        outputFile.absolute.path,
        '--track',
        track,
        '--release_status',
        releaseStatus.name,
      ],
      workingDirectory: _androidDirectory,
      printCall: true,
      runInShell: true,
    );
  }
}