publish static method

Future<void> publish(
  1. Client cloudApiClient,
  2. FileUploaderFactory fileUploaderFactory, {
  3. required CommandLogger logger,
  4. required String baseCommand,
  5. required String projectId,
  6. required Directory appDirectory,
  7. required List<FlutterPlatform> defaultPlatforms,
  8. Set<FlutterPlatform>? platforms,
  9. required Map<FlutterPlatform, String> configuredAppIdentifiers,
  10. required String configFileName,
  11. bool awaitBuilds = true,
})

Publishes the Flutter app in appDirectory in project projectId as one build per platform to publish, all from one uploaded archive.

The archive holds only the code of the platforms to publish, together with the workspace packages the app depends on when it is resolved in a Dart workspace. The workspace in the archive only lists the included packages, and their pubspec files are stripped of dev dependencies.

When platforms is null, the app is published for the defaultPlatforms it has code for. The app identifier of each platform is sent with the request for the builds: the one in configuredAppIdentifiers, or else the one the app's project files declare. Messages about an app identifier name the project configuration file configFileName.

The command to follow the builds is shown, invoked through baseCommand. With awaitBuilds, the progress and logs of the builds are then shown until they end.

Throws FailureException if the app has no code for a platform to publish, if the app identifier of a platform cannot be read or is refused, if the builds cannot be requested, or if an awaited build does not succeed. Throws ErrorExitException if the archive cannot be zipped or uploaded. The builds are cancelled when the archive cannot be uploaded.

Implementation

static Future<void> publish(
  Client cloudApiClient,
  FileUploaderFactory fileUploaderFactory, {
  required CommandLogger logger,
  required String baseCommand,
  required String projectId,
  required Directory appDirectory,
  required List<FlutterPlatform> defaultPlatforms,
  Set<FlutterPlatform>? platforms,
  required Map<FlutterPlatform, String> configuredAppIdentifiers,
  required String configFileName,
  bool awaitBuilds = true,
}) async {
  logger.init('Publishing the app of Serverpod Cloud project "$projectId".');

  final requestedPlatforms =
      platforms ?? _presentDefaultPlatforms(appDirectory, defaultPlatforms);
  final publishPlatforms = FlutterPlatform.values
      .where(requestedPlatforms.contains)
      .toList();

  final preparer = TenantProject.prepareApp(
    appDirectory,
    platforms: publishPlatforms.toSet(),
  );
  final appIdentifiers = {
    for (final platform in publishPlatforms)
      platform: _appIdentifier(
        appDirectory,
        platform,
        configured: configuredAppIdentifiers[platform],
        configFileName: configFileName,
      ),
  };
  for (final MapEntry(key: platform, value: appIdentifier)
      in appIdentifiers.entries) {
    logger.info('${appIdentifierName(platform)}: $appIdentifier');
  }

  if (preparer.isWorkspace) {
    logger.list(
      preparer.includedPackagePaths,
      title: 'Including workspace packages',
      level: LogLevel.debug,
      newParagraph: true,
    );
  }

  final appZip = await DeployArchive.zip(
    logger: logger,
    projectFilePreparer: preparer,
    showFiles: false,
    subject: 'app',
  );

  final gitMetadata = await readGitMetadata(
    appDirectory.path,
    logger: logger,
  );

  final versionName = _versionName(preparer.projectVersion);
  final upload = await AppOperations.createBuilds(
    cloudApiClient,
    projectId: projectId,
    platforms: publishPlatforms,
    appIdentifiers: appIdentifiers,
    flutterAppDir: preparer.projectSubPath,
    archiveSize: appZip.length,
    versionName: versionName,
    gitMetadata: gitMetadata,
  );
  for (final change
      in upload.appIdentifierChanges ?? <AppIdentifierChange>[]) {
    logger.warning(_appIdentifierChangeWarning(change));
  }

  try {
    await DeployArchive.upload(
      logger: logger,
      fileUploaderFactory: fileUploaderFactory,
      uploadDescription: upload.uploadDescription,
      projectZip: appZip,
      subject: 'app',
    );
  } on Exception {
    await _cancelBuilds(
      cloudApiClient,
      logger: logger,
      projectId: projectId,
      builds: upload.builds,
    );
    rethrow;
  }

  final builds = [...upload.builds]
    ..sort((a, b) => a.platform.index.compareTo(b.platform.index));
  final platformNames = AppBuildStatusFormatter.platformNames(
    builds.map((build) => build.platform),
  );
  logger.info(
    'Publishing ${versionName ?? 'the app'} to $platformNames',
    newParagraph: true,
  );
  logger.info('Follow it: $baseCommand app show ${upload.uploadId}');
  if (!awaitBuilds) return;

  await AppBuildProgress.follow(
    cloudApiClient,
    logger: logger,
    projectId: projectId,
    builds: builds,
    versionName: versionName,
    showLogs: true,
  );
}