extractModuleVersion static method
Extracts the version information from a JAR file
Attempts to determine the module version by examining:
- META-INF/MANIFEST.MF for Implementation-Version or Bundle-Version
- Falls back to extracting version information from the filename
jarPath The path to the JAR file
Returns the module version if found, null otherwise
Implementation
static Future<String?> extractModuleVersion(String jarPath) async {
try {
final jarFile = File(jarPath);
if (!await jarFile.exists()) {
return null;
}
final bytes = await jarFile.readAsBytes();
final archive = ZipDecoder().decodeBytes(bytes);
final manifestFile = archive.files.firstWhere(
(file) => file.name.toUpperCase() == 'META-INF/MANIFEST.MF',
orElse: () => ArchiveFile('', 0, []),
);
if (manifestFile.size > 0) {
final content = String.fromCharCodes(manifestFile.content as List<int>);
final lines = content.split('\n');
for (final line in lines) {
if (line.startsWith('Implementation-Version:')) {
return line.substring('Implementation-Version:'.length).trim();
} else if (line.startsWith('Bundle-Version:')) {
return line.substring('Bundle-Version:'.length).trim();
}
}
}
final fileName = p.basename(jarPath);
final versionPattern = RegExp(r'-(\d+(\.\d+)*(-[a-zA-Z0-9]+)?)\.jar$');
final match = versionPattern.firstMatch(fileName);
if (match != null) {
return match.group(1);
}
return null;
} catch (e) {
debugPrint('Error extracting module version from $jarPath: $e');
return null;
}
}