run method

  1. @override
FutureOr? run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
FutureOr? run() async {
  if (help) {
    logger.info(argParser.usage);
    return null;
  }

  try {
    // Find pubspec.yaml in the root directory
    final pubspecFile = File('${root.path}/pubspec.yaml');
    if (!pubspecFile.existsSync()) {
      logger.err('pubspec.yaml not found in ${root.path}');
      return 1;
    }

    // Read current version from pubspec
    final pubspecContent = await pubspecFile.readAsString();
    final version = PubspecUtils.getVersion(pubspecContent);
    logger.info('Current version: ${version.toString()}');

    // Generate badge
    final badgeContent = await generateVersionBadge(version.toString());
    logger.info('Badge generated successfully');

    // Output to file if specified, otherwise to console
    if (out != null) {
      final outputFile = File(out!);
      await outputFile.writeAsString(badgeContent);
      logger.info('Badge written to: ${outputFile.path}');
    }

    return 0;
  } catch (e) {
    logger.err('Failed to generate badge: $e');
    return 1;
  }
}