doRun method

  1. @override
Future<int> doRun()
override

Implementation

@override
Future<int> doRun() async {
  final args = argResults!;
  final isDemo = args[_argDemo] as bool;

  final gitConfig = config.git;
  if (!isDemo) {
    fs = const IOFileSystem();
    git = GitCommands(GitClient(), gitConfig);
  } else {
    printInfo("Demonstration mode");
    fs = DemoFileSystem();
    git = GitCommands(DemoGit(verbose: isVerbose), gitConfig);
  }

  final skipL10n = args[_argSkipL10n] as bool? ?? false;
  final isLocalRelease = args[_argLocal] as bool? ?? false;

  final ciConfig = config.ci;
  if (!ciConfig.enabled && !isLocalRelease) {
    return error(1,
        message: 'You can only use local release if CI is disabled. '
            'See --$_argLocal and section ci in alex config section.');
  }

  git.ensureCleanAndCheckoutDevelop();

  final spec = await Spec.pub(fs);
  final version = spec.version;
  final vs = version.short;

  if (int.tryParse(version.build) == null) {
    return error(1,
        message: 'Invalid version "$vs": '
            'you should define build number (after +).');
  }

  if (skipL10n) {
    if (args.wasParsed(_argLocale)) {
      return error(1,
          message:
              "You can't pass --$_argSkipL10n and --$_argLocale at the same time");
    }
  } else {
    final baseLocale = args[_argLocale] as String? ?? _defaultLocale;
    final processLocResult = await _processLocalization(baseLocale);
    if (processLocResult != 0) {
      return processLocResult;
    }

    // Commit translations.
    _commit("Generated translations.");
  }

  final scriptPaths = config.scripts?.preReleaseScriptsPaths;

  if (scriptPaths != null && scriptPaths.isNotEmpty) {
    printInfo('Running pre release scripts.');
    for (final path in scriptPaths) {
      final res = await flutter.runPubOrFail(
        '${config.rootPath}/$path',
        const [],
      );
      if (res.exitCode == 0) {
        printInfo('Pre release script $path run - OK');
      } else {
        // TODO: Clean current changes for git.
        return error(res.exitCode, message: '${res.stderr}');
      }
      _commit('Pre release scripts run.');
    }
  } else {
    printInfo('There are no pre release scripts to run.');
  }

  printInfo('Start new release <v$vs>');

  git.gitflowReleaseStart(vs);

  printInfo('Upgrading CHANGELOG.md...');

  final changeLog = await upgradeChangeLog(version) ?? '';

  printInfo("Change log: \n$changeLog");

  final summary = StringBuffer();
  if (ciConfig.enabled) {
    final Map<String, String> prompt;
    final chatGptApiKey = await settings.openAIApiKey;
    if (chatGptApiKey != null && chatGptApiKey.isNotEmpty) {
      printInfo('Trying to generate release notes prompt...');
      prompt = await _getReleaseNotesPrompt(chatGptApiKey, changeLog);
    } else {
      prompt = {};
    }

    printInfo('Waiting for release notes...');
    final releaseNotes = await getReleaseNotes(version, changeLog, prompt);
    summary
      ..writeln()
      ..writeln('# Release Notes')
      ..writeln()
      ..writeln(releaseNotes);
  }

  summary
    ..writeln()
    ..writeln('# Changelog')
    ..writeln()
    ..writeln(changeLog);

  if (isLocalRelease) {
    final entryPoint = args[_argEntryPoint] as String?;
    final platforms =
        _BuildPlatform.parseArgs(args[_argBuildPlatforms] as String);

    printVerbose('Platforms: ${platforms.asDesc()}');

    for (final platform in platforms) {
      final localBuildResult = await _localBuild(entryPoint, platform);
      if (localBuildResult != 0) return localBuildResult;
    }
  }

  printInfo("Finishing release...");

  // committing changes
  _commit("Changelog and release notes");

  // finishing release
  git.gitflowReleaseFinish(vs);

  final branchDevelop = git.branchDevelop;
  if (git.getCurrentBranch() != branchDevelop) {
    git.checkout(branchDevelop);
  }

  // increment version
  incrementVersion(spec, version);

  _commit("Version increment");

  git.push(branchDevelop);
  git.push(git.branchMaster);

  printInfo('Release successfully completed');
  printInfo('');
  printInfo(
      'Release summary, copypaste it in the comment for release issue:');
  printInfo('');
  printInfo(summary.toString());

  return 0;
}