inspect method

Future<OkfBundleLoadResult> inspect(
  1. String rootPath
)

Inventories and parses the bundle rooted at rootPath.

Implementation

Future<OkfBundleLoadResult> inspect(String rootPath) async {
  final root = await _validatedRoot(rootPath);
  final entities = <_BundleEntry>[];

  await for (final entity in root.list(recursive: true, followLinks: false)) {
    final type = await FileSystemEntity.type(
      entity.path,
      followLinks: false,
    );
    if (type != FileSystemEntityType.file) {
      continue;
    }

    final relativePath = _relativePath(root.path, entity.path);
    entities.add(_BundleEntry(relativePath, File(entity.path)));
  }
  entities.sort(
    (left, right) => left.relativePath.compareTo(right.relativePath),
  );

  final documents = <String, OkfDocument>{};
  final indexes = <String, String>{};
  final logs = <String, String>{};
  final assets = <String>[];
  final issues = <OkfBundleLoadIssue>[];

  for (final entry in entities) {
    try {
      _validateBundlePath(entry.relativePath);
    } on FormatException catch (error) {
      issues.add(
        OkfBundleLoadIssue(
          code: 'invalid_path',
          message: error.message,
          path: entry.relativePath,
        ),
      );
      continue;
    }

    if (!entry.relativePath.endsWith('.md')) {
      assets.add(entry.relativePath);
      continue;
    }

    late final String source;
    try {
      source = utf8.decode(
        await entry.file.readAsBytes(),
        allowMalformed: false,
      );
    } on FormatException catch (error) {
      issues.add(
        OkfBundleLoadIssue(
          code: 'invalid_utf8',
          message: error.message,
          path: entry.relativePath,
        ),
      );
      continue;
    }

    final basename = p.posix.basename(entry.relativePath);
    if (basename == 'index.md') {
      indexes[entry.relativePath] = source;
      continue;
    }
    if (basename == 'log.md') {
      logs[entry.relativePath] = source;
      continue;
    }

    try {
      documents[entry.relativePath] = OkfDocument.parse(
        source,
        sourcePath: entry.relativePath,
      );
    } on OkfDocumentException catch (error) {
      issues.add(
        OkfBundleLoadIssue(
          code: 'invalid_document',
          message: error.message,
          path: entry.relativePath,
          line: error.line,
          column: error.column,
        ),
      );
    } on FormatException catch (error) {
      issues.add(
        OkfBundleLoadIssue(
          code: 'invalid_document',
          message: error.message,
          path: entry.relativePath,
        ),
      );
    }
  }

  final bundle = OkfBundle.fromDocuments(
    documents,
    indexes: indexes,
    logs: logs,
    assets: assets,
  );
  return OkfBundleLoadResult(
    rootPath: root.path,
    bundle: bundle,
    documents: documents,
    indexes: indexes,
    logs: logs,
    assets: assets,
    issues: issues,
  );
}