generate<T> method

Implementation

Future<HybridGenerationReport> generate<T>(
  CollectionRouteSource<T> source,
) async {
  if (source.pageSize < 1 ||
      source.pageSize > 1000 ||
      source.concurrency < 1 ||
      source.concurrency > 32 ||
      source.maxRecords < 1 ||
      source.maxRecords > 1000000) {
    throw ArgumentError('Page size or concurrency is out of bounds.');
  }
  if (source.routeId.trim().isEmpty) {
    throw ArgumentError('routeId is required.');
  }
  if (':key'.allMatches(source.pathPattern).length != 1) {
    throw ArgumentError('pathPattern must contain exactly one :key.');
  }
  if (source.initialGenerationPolicy == InitialGenerationPolicy.selected &&
      source.includeInInitialGeneration == null) {
    throw ArgumentError(
      'selected generation requires includeInInitialGeneration.',
    );
  }
  _normalizePath(source.pathPattern.replaceAll(':key', 'placeholder'));
  final startedAt = _clock().toUtc();
  final version = await store.beginVersion();
  try {
    final deps = await store.loadVersionDependencies(version);
    final oldRoutePaths = deps.values
        .where((d) => d.routeId == source.routeId)
        .map((d) => d.path)
        .toSet();
    if (source.initialGenerationPolicy == InitialGenerationPolicy.none) {
      for (final path in oldRoutePaths) {
        await store.deleteVersion(version, path);
        deps.remove(path);
      }
      await store.saveVersionDependencies(version, deps);
      await _writeSitemapIfConfigured(version, deps);
      await store.publishVersion(version);
      return HybridGenerationReport(
        discovered: 0,
        generated: 0,
        pages: const [],
        deleted: oldRoutePaths.length,
        siteId: siteId,
        buildId: version,
        startedAt: startedAt,
        completedAt: _clock().toUtc(),
      );
    }
    final keys = <String>{}, pages = <String>[];
    final cursors = <String>{};
    var discovered = 0;
    var skipped = 0;
    String? cursor;
    do {
      if (source.isCancelled?.call() == true) {
        throw StateError('Collection generation was cancelled.');
      }
      final page = await source.discover(cursor, source.pageSize);
      if (page.records.length > source.pageSize) {
        throw StateError('Source exceeded requested page size.');
      }
      discovered += page.records.length;
      if (discovered > source.maxRecords) {
        throw StateError(
          'Collection exceeded the configured maxRecords limit.',
        );
      }
      for (var i = 0; i < page.records.length; i += source.concurrency) {
        await Future.wait(
          page.records.skip(i).take(source.concurrency).map((record) async {
            final key = source.keyOf(record);
            if (key.isEmpty ||
                key == '.' ||
                key == '..' ||
                key.contains('/') ||
                key.contains('\\') ||
                key.contains('\u0000')) {
              throw const FormatException('Invalid route key.');
            }
            if (!keys.add(key)) throw StateError('Duplicate route key.');
            if (source.initialGenerationPolicy ==
                    InitialGenerationPolicy.selected &&
                !source.includeInInitialGeneration!(record)) {
              skipped++;
              return;
            }
            final path = _normalizePath(
              source.pathPattern.replaceAll(':key', Uri.encodeComponent(key)),
            );
            if (deps[path] != null && deps[path]!.routeId != source.routeId) {
              throw StateError('Route collision at $path.');
            }
            final document = await source.render(record, path);
            _validateCompleteDocument(document.html);
            await store.writeVersion(version, path, document.html);
            deps[path] = RouteDependency(
              path: path,
              routeId: source.routeId,
              tags: {
                ...?source.tagsOf?.call(record),
                ...document.tags,
              }.toList()..sort(),
              generatedAt: _clock().toUtc(),
              sourceVersion:
                  document.sourceVersion ?? source.versionOf?.call(record),
              lastModified: document.lastModified,
              canonicalUrl: document.canonicalUrl ?? _defaultCanonical(path),
              indexable: document.indexable ?? true,
            );
            pages.add(path);
          }),
        );
      }
      source.onProgress?.call(discovered, pages.length, skipped);
      if (page.nextCursor != null && !cursors.add(page.nextCursor!)) {
        throw StateError('Pagination cursor cycle detected.');
      }
      cursor = page.nextCursor;
    } while (cursor != null);
    final obsolete = oldRoutePaths.difference(pages.toSet());
    for (final path in obsolete) {
      await store.deleteVersion(version, path);
      deps.remove(path);
    }
    await store.saveVersionDependencies(version, deps);
    await _writeSitemapIfConfigured(version, deps);
    await store.publishVersion(version);
    return HybridGenerationReport(
      discovered: discovered,
      generated: pages.length,
      pages: List.unmodifiable(pages),
      skipped: skipped,
      deleted: obsolete.length,
      siteId: siteId,
      buildId: version,
      startedAt: startedAt,
      completedAt: _clock().toUtc(),
    );
  } catch (_) {
    await store.discardVersion(version);
    rethrow;
  }
}