getRepoInfo function

Future<RepoInfo> getRepoInfo(
  1. String repoPath
)

Returns repository information for the repository at repoPath.

Implementation

Future<RepoInfo> getRepoInfo(String repoPath) async {
  final name = p.basename(repoPath);
  String version = 'v.1.0.0';
  final pubspecFile = File(p.join(repoPath, 'pubspec.yaml'));
  if (await pubspecFile.exists()) {
    try {
      final content = await pubspecFile.readAsString();
      final pubspec = Pubspec.parse(content);
      if (pubspec.version != null) {
        version = 'v.${pubspec.version.toString()}';
      }
    } catch (e) {
      // Use default version.
    }
  }
  // Determine language.
  String language;
  final packageJson = File(p.join(repoPath, 'package.json'));
  final hasPubspec = await pubspecFile.exists();
  final hasPackageJson = await packageJson.exists();
  if (hasPubspec && hasPackageJson) {
    // Cross-language bridge: a repo carrying both a Dart and a Node manifest.
    language = 'dart+nodejs';
  } else if (hasPubspec) {
    language = 'dart';
  } else if (hasPackageJson) {
    language = 'nodejs';
  } else {
    final dir = Directory(repoPath);
    final files = dir.listSync(recursive: true).whereType<File>().toList();
    if (files.any((f) => f.path.endsWith('.py'))) {
      language = 'python';
    } else if (files.any((f) => f.path.endsWith('.java'))) {
      language = 'Java';
    } else if (files.any((f) => f.path.endsWith('.cpp'))) {
      language = 'c++';
    } else {
      language = 'dart';
    }
  }
  String organization = 'unknown';
  final gitConfig = File(p.join(repoPath, '.git', 'config'));
  if (await gitConfig.exists()) {
    try {
      final lines = await gitConfig.readAsLines();
      final urlLine = lines.firstWhere(
        (line) => line.trim().startsWith('url ='),
        orElse: () => '',
      );
      if (urlLine.isNotEmpty) {
        final parts = urlLine.split('=');
        if (parts.length >= 2) {
          final url = parts[1].trim();
          if (url.startsWith('git@')) {
            final match = RegExp(r'git@[^:]+:([^/]+)/').firstMatch(url);
            if (match != null) {
              organization = match.group(1)!;
            }
          } else {
            try {
              final uri = Uri.parse(url);
              if (uri.pathSegments.length >= 2) {
                organization = uri.pathSegments.first;
              }
            } catch (e) {
              // ignore
            }
          }
        }
      }
    } catch (e) {
      // ignore errors
    }
  }
  return RepoInfo(
    name: name,
    version: version,
    language: language,
    organization: organization,
  );
}