release method

Future<void> release(
  1. String name, {
  2. required List<String> jobNameList,
  3. required List<String> skipJobNameList,
  4. required bool cleanBeforeBuild,
})
inherited

Release an application to a third party provider

This method releases an application to a third party provider.

The name parameter is the name of the release to be released. The jobNameList parameter is the list of job names to be released. The skipJobNameList parameter is the list of job names to be skipped. The cleanBeforeBuild parameter is a boolean that indicates whether to clean the build directory before building.

Implementation

Future<void> release(
  String name, {
  required List<String> jobNameList,
  required List<String> skipJobNameList,
  required bool cleanBeforeBuild,
}) async {
  final time = Stopwatch()..start();

  try {
    Directory outputDirectory = distributeOptions.outputDirectory;
    if (!outputDirectory.existsSync()) {
      outputDirectory.createSync(recursive: true);
    }

    List<Release> releases = distributeOptions.releases;

    if (name.isNotEmpty) {
      releases =
          distributeOptions.releases.where((e) => e.name == name).toList();
    }

    if (releases.isEmpty) {
      throw Exception('Missing/incomplete `distribute_options.yaml` file.');
    }

    for (Release release in releases) {
      List<ReleaseJob> filteredJobs = release.jobs.where((e) {
        if (jobNameList.isNotEmpty) {
          return jobNameList.contains(e.name);
        }
        if (skipJobNameList.isNotEmpty) {
          return !skipJobNameList.contains(e.name);
        }
        return true;
      }).toList();
      if (filteredJobs.isEmpty) {
        throw Exception('No available jobs found in ${release.name}.');
      }

      bool needCleanBeforeBuild = cleanBeforeBuild;

      for (ReleaseJob job in filteredJobs) {
        logger.info('');
        logger.info(
          '${'===>'.blue()} ${'Releasing'.white(bold: true)} $name:${job.name.green(bold: true)}',
        );

        Map<String, String> variables = {}
          ..addAll(globalVariables)
          ..addAll(release.variables ?? {})
          ..addAll(job.variables ?? {});

        List<MakeResult> makeResultList = await package(
          job.package.platform,
          [job.package.target],
          channel: job.package.channel,
          artifactName: distributeOptions.artifactName,
          cleanBeforeBuild: needCleanBeforeBuild,
          buildArguments: job.package.buildArgs ?? {},
          variables: variables,
        );
        // Clean only once
        needCleanBeforeBuild = false;

        if (job.publish != null || job.publishTo != null) {
          String? publishTarget = job.publishTo ?? job.publish?.target;
          MakeResult makeResult = makeResultList.first;
          FileSystemEntity artifact = makeResult.artifacts.first;
          await publish(
            artifact,
            [publishTarget!],
            publishArguments: job.publish?.args,
            variables: variables,
          );
        }
      }
    }

    time.stop();
    logger.info('');
    logger.info(
      'RELEASE SUCCESSFUL in ${time.elapsed.inSeconds}s'.green(bold: true),
    );
  } catch (error, stacktrace) {
    time.stop();
    logger.info('');
    logger.severe(
      [
        'RELEASE FAILED in ${time.elapsed.inSeconds}s'.red(bold: true),
        error.toString().red(),
        stacktrace,
      ].join('\n'),
    );
    rethrow;
  }
  return Future.value();
}