Katteker/Katteker/ReleaseEntry.cs
Holger Boerchers d383f0ef1c Initial commit
2018-03-21 20:07:40 +01:00

92 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Katteker
{
public class ReleaseEntry : IComparable<ReleaseEntry>, IComparable
{
public const string FilenameRegex = @"(^.*)-((?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(?:\+[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?).*-(full|delta)";
public ReleaseEntry(string filename, SemanticVersion version, long fileSize, bool isDelta, string sha1)
{
Filename = filename;
Version = version;
Filesize = fileSize;
IsDelta = isDelta;
SHA1 = sha1;
}
public ReleaseEntry(string line)
{
var elements = line?.Split(' ');
if (elements?.Length == 3)
{
SHA1 = elements[0];
Filename = elements[1];
Filesize = long.Parse(elements[2]);
var fileSegments = Regex.Match(Filename, FilenameRegex);
if (fileSegments.Groups.Count < 3) throw new ArgumentOutOfRangeException("Filename is not compilant.");
ApplicationName = fileSegments.Groups[1].Value;
Version = SemanticVersion.Parse(fileSegments.Groups[2].Value);
IsDelta = fileSegments.Groups[3].Value != "full";
}
}
public ReleaseEntry(string applicationName, SemanticVersion version)
{
ApplicationName = applicationName;
Version = version;
}
public SemanticVersion Version { get; }
public string SHA1 { get; }
public string Filename { get; }
public long Filesize { get; }
public bool IsDelta { get; }
public string ApplicationName { get; }
public string EntryAsString => $"{SHA1} {Filename} {Filesize}";
public override string ToString() => $"{ApplicationName} {Version}";
public int CompareTo(object obj) => CompareTo(obj as ReleaseEntry);
public int CompareTo(ReleaseEntry other)
{
if (ReferenceEquals(this, other)) return 0;
if (ReferenceEquals(null, other)) return 1;
var versionComparison = Comparer<SemanticVersion>.Default.Compare(Version, other.Version);
if (versionComparison != 0) return versionComparison;
return string.Compare(Filename, other.Filename, StringComparison.Ordinal);
}
public override bool Equals(object obj) => Equals(obj as ReleaseEntry);
protected bool Equals(ReleaseEntry other)
{
return Equals(Version, other.Version) && string.Equals(SHA1, other.SHA1) && string.Equals(Filename, other.Filename) && Filesize == other.Filesize && IsDelta == other.IsDelta;
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (Version != null ? Version.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (SHA1 != null ? SHA1.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Filename != null ? Filename.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ Filesize.GetHashCode();
hashCode = (hashCode * 397) ^ IsDelta.GetHashCode();
return hashCode;
}
}
public static bool operator ==(ReleaseEntry left, ReleaseEntry right)
{
return Equals(left, right);
}
public static bool operator !=(ReleaseEntry left, ReleaseEntry right)
{
return !Equals(left, right);
}
}
}