zip static method

Future<List<int>> zip({
  1. required CommandLogger logger,
  2. required ProjectFilePreparer projectFilePreparer,
  3. int concurrency = ProjectZipper.defaultFileReadPoolSize,
  4. required bool showFiles,
  5. String subject = 'project',
})

Zips the files selected by projectFilePreparer, leaving out its excluded paths and filtering the pubspec.yaml and pubspec.lock files on the way. concurrency is the number of files processed at once.

Throws ErrorExitException if the project could not be zipped.

Implementation

static Future<List<int>> zip({
  required CommandLogger logger,
  required ProjectFilePreparer projectFilePreparer,
  int concurrency = ProjectZipper.defaultFileReadPoolSize,
  required bool showFiles,
  String subject = 'project',
}) async {
  late final List<int> projectZip;
  final isZipped = await logger.progress(
    'Zipping $subject',
    successMessage: 'Zipping successful.',
    padRight: StatusCommands.progressMessagePadLength,
    newParagraph: true,
    () async {
      try {
        projectZip = await ProjectZipper.zipProject(
          logger: logger,
          rootDirectory: projectFilePreparer.rootDirectory,
          beneath: projectFilePreparer.includedSubPaths,
          fileReadPoolSize: concurrency,
          showFiles: showFiles,
          excludeFile: projectFilePreparer.excludedSubPaths.isEmpty
              ? null
              : projectFilePreparer.isExcluded,
          fileContentModifier: (relativePath, contentReader) async {
            final pathSegments = p.split(relativePath);
            if (pathSegments.contains('.scloud')) return null;
            if (pathSegments.last == 'pubspec.yaml') {
              final content = await contentReader();
              return projectFilePreparer.filterPubspecYaml(content);
            }
            if (pathSegments.last == 'pubspec.lock') {
              final content = await contentReader();
              return projectFilePreparer.filterPubspecLock(content);
            }
            return null;
          },
        );
        return true;
      } on ProjectZipperExceptions catch (e) {
        switch (e) {
          case ProjectDirectoryDoesNotExistException():
            logger.error('Project directory does not exist: ${e.path}');
            break;
          case EmptyProjectException():
            logger.error(
              'No files to upload.',
              hint:
                  'Ensure that the correct project directory is selected (either through the --project-dir flag or the current directory) and check '
                  'that `.gitignore` and `.scloudignore` does not filter out all project files.',
            );
            break;
          case DirectorySymLinkException():
            logger.error(
              'Serverpod Cloud does not support directory symlinks: `${e.path}`',
            );
            break;
          case NonResolvingSymlinkException():
            logger.error(
              'Serverpod Cloud does not support non-resolving symlinks: `${e.path}` => `${e.target}`',
            );
            break;
          case NullZipException():
            logger.error(
              'Unknown error occurred while zipping $subject, please try again.',
            );
            break;
        }
        return false;
      }
    },
  );

  if (!isZipped) throw ErrorExitException('Failed to zip $subject.');
  return projectZip;
}