Using Version class.

This commit is contained in:
BaronGreenback 2020-06-22 11:57:46 +01:00
parent bf1bbbdd3e
commit a25a233b75
2 changed files with 35 additions and 97 deletions

View file

@ -1009,136 +1009,74 @@ namespace Emby.Server.Implementations
protected abstract void RestartInternal(); protected abstract void RestartInternal();
/// <summary> /// <summary>
/// Converts an string array to a number. /// Comparison function used in <<see cref="GetLatestDLLVersion" />.
/// Element 0 is the filename.
/// </summary> /// </summary>
/// <param name="version">Parts of the filename.</param> private static int VersionCompare(Tuple<Version, string, string> a, Tuple<Version, string, string> b)
/// <param name="foldername">Returns the folder name including any periods. eg. vs</param>
/// <returns>Long representing the version of the file.</returns>
private long StrToVersion(string[] version, out string foldername)
{
if (version.Length > 5)
{
Logger.LogError("Plugin version number too complex : {0}.", version[0]);
foldername = string.Join('.', version);
return 0;
}
foldername = string.Empty;
int start = 0;
do
{
foldername += "." + version[start];
start++;
}
while (start < version.Length && !int.TryParse(version[start], out _));
foldername = foldername.TrimStart('.');
if (start == version.Length)
{
// No valid version number.
return 0;
}
// Build version into a string. 1.2.3.4 => 001002003004, 2.1 -> 200100000000.
string res = string.Empty;
for (int x = start; x < version.Length; x++)
{
res += version[x].PadLeft(3, '0');
}
if (res.Length < 12)
{
res = res.PadRight(12, '0');
}
return long.Parse(res, CultureInfo.InvariantCulture);
}
private static int VersionCompare(Tuple<long, string, string> a, Tuple<long, string, string> b)
{ {
int compare = string.Compare(a.Item2, b.Item2, true, CultureInfo.InvariantCulture); int compare = string.Compare(a.Item2, b.Item2, true, CultureInfo.InvariantCulture);
if (compare == 0) if (compare == 0)
{ {
if (a.Item1 > b.Item1) return a.Item1.CompareTo(b.Item1);
{
return 1;
}
return -1;
} }
return compare; return compare;
} }
/// <summary> /// <summary>
/// Only loads the latest version of each assembly based upon the folder name. /// Only loads the latest version of each assembly based upon the folder name.
/// eg. MyAssembly 11.9.3.6 - will be ignored. /// eg. MyAssembly_11.9.3.6 - will be ignored.
/// MyAssembly 12.2.3.6 - will be loaded. /// MyAssembly_12.2.3.6 - will be loaded.
/// </summary> /// </summary>
/// <param name="path">Path to enumerate.</param> /// <param name="path">Path to enumerate.</param>
/// <param name="cleanup">Set to true, to try and clean up earlier versions.</param> /// <param name="cleanup">Set to true, to try and clean up earlier versions.</param>
/// <returns>IEnumerable{string} of filenames.</returns> /// <returns>IEnumerable{string} of filenames.</returns>
protected IEnumerable<string> GetLatestDLLVersion(string path, bool cleanup = false) protected IEnumerable<string> GetLatestDLLVersion(string path, bool cleanup = true)
{ {
var dllList = new List<string>(); var dllList = new List<string>();
var versions = new List<Tuple<long, string, string>>(); var versions = new List<Tuple<Version, string, string>>();
var directories = Directory.EnumerateDirectories(path, "*.*", SearchOption.TopDirectoryOnly);
var directories = Directory.EnumerateDirectories(path, "*.*", SearchOption.TopDirectoryOnly).ToList();
// Only add the latest version of the folder into the list.
foreach (var dir in directories) foreach (var dir in directories)
{ {
string[] parts = dir.Replace('_', '.').Split("."); int p = dir.LastIndexOf('_');
if (p != -1 && Version.TryParse(dir.Substring(p + 1), out Version ver))
if (parts.Length == 1)
{ {
dllList.AddRange(Directory.EnumerateFiles(dir, "*.dll", SearchOption.AllDirectories).ToList()); // Versioned folder.
versions.Add(Tuple.Create(ver, dir.Substring(0, p), dir));
} }
else else
{ {
long id = StrToVersion(parts, out string foldername); // Un-versioned folder.
// Add for version comparison later. versions.Add(Tuple.Create(new Version(), dir, dir));
versions.Add(Tuple.Create(id, foldername, dir));
} }
} }
if (versions.Count > 0) string lastName = string.Empty;
versions.Sort(VersionCompare);
// Traverse backwards through the list.
// The first item will be the latest version.
for (int x = versions.Count - 1; x >= 0; x--)
{ {
string lastName = string.Empty; if (!string.Equals(lastName, versions[x].Item2, StringComparison.OrdinalIgnoreCase))
versions.Sort(VersionCompare);
// Traverse backwards through the list.
// The first item will be the latest version.
for (int x = versions.Count - 1; x > 0; x--)
{ {
if (!string.Equals(lastName, versions[x].Item2, StringComparison.OrdinalIgnoreCase)) dllList.AddRange(Directory.EnumerateFiles(versions[x].Item3, "*.dll", SearchOption.AllDirectories));
{ lastName = versions[x].Item2;
dllList.AddRange(Directory.EnumerateFiles(versions[x].Item3, "*.dll", SearchOption.AllDirectories)); continue;
lastName = versions[x].Item2;
continue;
}
if (!string.IsNullOrEmpty(lastName) && cleanup)
{
// Attempt a cleanup of old folders.
try
{
Logger.LogDebug("Attempting to delete {0}", versions[x].Item3);
Directory.Delete(versions[x].Item3);
}
catch
{
// Ignore errors.
}
}
} }
if (!string.Equals(lastName, versions[0].Item2, StringComparison.OrdinalIgnoreCase)) if (!string.IsNullOrEmpty(lastName) && cleanup)
{ {
dllList.AddRange(Directory.EnumerateFiles(versions[0].Item3, "*.dll", SearchOption.AllDirectories)); // Attempt a cleanup of old folders.
try
{
Logger.LogDebug("Attempting to delete {0}", versions[x].Item3);
Directory.Delete(versions[x].Item3, true);
}
catch
{
// Ignore errors.
}
} }
} }

View file

@ -387,7 +387,7 @@ namespace Emby.Server.Implementations.Updates
} }
// Version folder as they cannot be overwritten in Windows. // Version folder as they cannot be overwritten in Windows.
targetDir += "." + package.Version.ToString(); targetDir += "_" + package.Version.ToString();
if (Directory.Exists(targetDir)) if (Directory.Exists(targetDir))
{ {