3using System.Text.RegularExpressions;
14 internal static class JarFileUtil
45 public string Name {
get;
set; }
50 public ModuleVersion
Version {
get;
set; }
62 public static ModuleInfo GetModuleInfo(
string path)
65 throw new ArgumentNullException(nameof(path));
66 if (path.EndsWith(
".jar", StringComparison.OrdinalIgnoreCase) ==
false)
67 throw new ArgumentException(
"Path must refer to a JAR file.", nameof(path));
68 if (File.Exists(path) ==
false)
69 throw new FileNotFoundException(
"Cannot find JAR file.", path);
71 using var jar =
new JarFile(path, JarFile.RUNTIME_VERSION);
72 var info = jar.GetModuleInfo() ??
new ModuleInfo();
73 if (info.Name is
null || info.Version.IsValid ==
false)
75 var fromFileName = GetModuleNameAndVersionFromFileName(path);
76 if (info.Name is
null && fromFileName.Name is not
null)
77 info.Name = fromFileName.Name;
78 if (info.Version.IsValid ==
false && fromFileName.Version.IsValid)
79 info.Version = fromFileName.Version;
90 static ModuleInfo GetModuleNameAndVersionFromFileName(
string name)
92 var info =
new ModuleInfo();
95 name = Path.GetFileNameWithoutExtension(name);
96 var span = (Span<char>)stackalloc
char[name.Length];
97 name.AsSpan().CopyTo(span);
100 if (Regex.Match(name,
@"-(\d+(\.|$))") is Match m)
102 info.Version = TryParseVersion(span.Slice(m.Index + 1));
103 span = span.Slice(0, m.Index);
107 for (var i = 0; i < span.Length; i++)
108 if (
char.IsLetterOrDigit(span[i]) ==
false)
112 name = span.ToString();
113 while (name.IndexOf(
"..") is
int i && i != -1)
114 name = name.Remove(i, 1);
117 info.Name = name.Trim(
'.');
127 static ModuleVersion TryParseVersion(ReadOnlySpan<char> span)
129 return ModuleVersion.TryParse(span, out var v) ? v :
default;
global::java.lang.invoke.LambdaForm.Name Name
ModuleInfo(string name, ModuleVersion version)
Initializes a new instance.
ModuleInfo()
Initializes a new instance.
ModuleVersion Version
Version of the module.