content: import 'dart:io';
  
  import 'package:collection/collection.dart';
  import 'package:path/path.dart';
  import 'package:http/http.dart';
  import 'package:html/parser.dart';
  import 'package:pub_api_client/pub_api_client.dart';
  import 'package:pubspec_parse/pubspec_parse.dart';
  
  void main() async {
    final client = PubClient();
  
    final pubspec = Pubspec.parse(File('pubspec.yaml').readAsStringSync());
  
    final allDeps = [
      ...pubspec.dependencies.entries,
      ...pubspec.devDependencies.entries,
    ];
  
    final dependencies = allDeps
        .where((d) => d.value is HostedDependency)
        .map((d) => d.key)
        .toSet();
    final packageInfo = await Future.wait(dependencies.map(client.packageInfo));
  
    final gitDepsList = List.castFrom<MapEntry<String, Dependency>,
        MapEntry<String, GitDependency>>(
      allDeps
          .where((d) => d.value is GitDependency)
          .map((d) => MapEntry(d.key, d.value as GitDependency))
          .toList(),
    );
  
    final gitDeps = gitDepsList.map(
      (d) {
        return MapEntry(
          d.key,
          join(
            d.value.url.toString().replaceAll('.git', ''),
            'raw',
            d.value.ref ?? 'main',
            d.value.path ?? '',
            'pubspec.yaml',
          ),
        );
      },
    ).toList();
  
    final gitPubspecs = await Future.wait(
      gitDeps.map(
        (d) {
          Pubspec parser(res) {
            try {
              return Pubspec.parse(res.body);
            } catch (e) {
              final document = parse(res.body);
              final pre = document.querySelector('pre');
              if (pre == null) rethrow;
              return Pubspec.parse(pre.text);
            }
          }
  
          return get(Uri.parse(d.value)).then(parser).catchError(
                (_) => get(Uri.parse(d.value.replaceFirst('/main', '/master')))
                    .then(parser),
              );
        },
      ),
    );
  
    print(
      packageInfo
          .map(
            (package) =>
                '<li>\n  <a href="${package.latestPubspec.homepage ?? package.url}">${package.name}</a> - ${package.description.replaceAll('\n', '')}\n</li>',
          )
          .join('\n'),
    );
    print(
      gitPubspecs.map(
        (package) {
          final packageUrl = package.homepage ??
              gitDepsList
                  .firstWhereOrNull((dep) => dep.key == package.name)
                  ?.value
                  .url
                  .toString();
          return '<li>\n  <a href="$packageUrl">${package.name}</a> - ${package.description?.replaceAll('\n', '')}\n</li>';
        },
      ).join('\n'),
    );
    exit(0);
  }
  