extractModuleVersion static method

Future<String?> extractModuleVersion(
  1. String jarPath
)

Extracts the version information from a JAR file

Attempts to determine the module version by examining:

  • Version information in the filename
  • META-INF/MANIFEST.MF for Implementation-Version, Bundle-Version, or Specification-Version

jarPath - Path to the JAR file Returns the module version if found, null otherwise

Implementation

static Future<String?> extractModuleVersion(String jarPath) async {
  try {
    final jarFileName = p.basenameWithoutExtension(jarPath);
    final fileNameParts = jarFileName.split('-');

    if (fileNameParts.length >= 2) {
      for (int i = 1; i < fileNameParts.length; i++) {
        final part = fileNameParts[i];
        if (RegExp(r'^\d+(\.\d+)*$').hasMatch(part)) {
          return part;
        }
      }
    }

    final tempDir = await Directory.systemTemp.createTemp('jar_version_');

    try {
      final manifestPath = p.join(tempDir.path, 'META-INF', 'MANIFEST.MF');

      final process = await Process.run('jar', [
        'xf',
        jarPath,
        'META-INF/MANIFEST.MF',
      ], workingDirectory: tempDir.path);

      if (process.exitCode == 0 && await File(manifestPath).exists()) {
        final manifestFile = File(manifestPath);
        final content = await manifestFile.readAsString();

        final versionMatch = RegExp(
          r'(Implementation-Version|Bundle-Version|Specification-Version): ([^\r\n]+)',
        ).firstMatch(content);

        if (versionMatch != null && versionMatch.groupCount >= 2) {
          return versionMatch.group(2)?.trim();
        }
      }
    } finally {
      await tempDir.delete(recursive: true);
    }
  } catch (e) {
    debugPrint('Error extracting module version: $e');
  }

  return null;
}