79 lines
2.4 KiB
C#
79 lines
2.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
namespace Katteker
|
|
{
|
|
public class Releases : IEnumerable<ReleaseEntry>
|
|
{
|
|
private readonly string _filePath;
|
|
|
|
private SortedList<SemanticVersion, ReleaseEntry> ReleaseEntries { get; }
|
|
|
|
private Releases()
|
|
{
|
|
ReleaseEntries = new SortedList<SemanticVersion, ReleaseEntry>();
|
|
}
|
|
|
|
public Releases(string path) : this()
|
|
{
|
|
_filePath = Path.Combine(path, Constants.RELEASE);
|
|
if (!File.Exists(_filePath)) return;
|
|
AddRange(File.ReadAllLines(_filePath));
|
|
}
|
|
|
|
public Releases(string[] content) : this()
|
|
{
|
|
AddRange(content);
|
|
}
|
|
|
|
private void AddRange(IEnumerable<string> content)
|
|
{
|
|
foreach (var line in content)
|
|
{
|
|
var entry = new ReleaseEntry(line);
|
|
ReleaseEntries.Add(entry.Version, entry);
|
|
}
|
|
}
|
|
|
|
public void WriteReleaseFile()
|
|
{
|
|
File.WriteAllLines(_filePath, ReleaseEntries.Select(x => x.Value.EntryAsString));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds new entry to file.
|
|
/// </summary>
|
|
/// <param name="setupFilePath">Path to setup-file</param>
|
|
/// <param name="version">Version of setup-file</param>
|
|
/// <returns>Returns true if newest version.</returns>
|
|
public ReleaseEntry Add(string setupFilePath, SemanticVersion version)
|
|
{
|
|
var sha1 = Utility.ComputeFileHash(setupFilePath);
|
|
var setupFile = new FileInfo(setupFilePath);
|
|
var entry = new ReleaseEntry(setupFile.Name, version, setupFile.Length, false, sha1);
|
|
|
|
if (!ReleaseEntries.ContainsValue(entry))
|
|
{
|
|
if (ReleaseEntries.ContainsKey(version))
|
|
{
|
|
ReleaseEntries.Remove(version);
|
|
}
|
|
ReleaseEntries.Add(version, entry);
|
|
}
|
|
return entry;
|
|
}
|
|
|
|
public bool IsLatestEntry(ReleaseEntry entry) => entry.Equals(ReleaseEntries.LastOrDefault().Value);
|
|
public IEnumerator<ReleaseEntry> GetEnumerator()
|
|
{
|
|
return ReleaseEntries.Values.GetEnumerator();
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return GetEnumerator();
|
|
}
|
|
}
|
|
} |