Sic08/SicSetupMaker/ReleaseNoteSupport/Notes.cs

106 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace SicSetupMaker.ReleaseNoteSupport
{
public class Notes
{
#region Constructors
public Notes()
{
}
public Notes(string features, string bugsFixed)
{
Features = FromText(features);
BugFixed = FromText(bugsFixed);
RawJson = ToJsonString();
}
public Notes(IEnumerable<string> features, IEnumerable<string> bugFixed)
{
Features = features;
BugFixed = bugFixed;
RawJson = ToJsonString();
}
public Notes(string jsonString)
{
RawJson = jsonString;
dynamic jNote = JsonConvert.DeserializeObject(RawJson);
Features = jNote?.feats ?? Array.Empty<string>();
BugFixed = jNote?.@fixed ?? Array.Empty<string>();
}
#endregion
#region Properties
[JsonIgnore]
public string RawJson { get; private set; }
[JsonProperty("feat")]
public IEnumerable<string> Features { get; set; }
[JsonProperty("fixed")]
public IEnumerable<string> BugFixed { get; set;}
#endregion
#region Methods
private IEnumerable<string> FromText(string content)
{
if (string.IsNullOrEmpty(content))
return Array.Empty<string>();
var list = new List<string>();
using (var reader = new StringReader(content))
{
while (reader.ReadLine() is { } item)
{
if(!string.IsNullOrEmpty(item))
list.Add(item);
}
}
return [.. list];
}
private string ToJsonString()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
public string ToMarkDown()
{
var sb = new StringBuilder();
sb.AppendLine("- Bug修复");
if (BugFixed.Any())
BugFixed.ToList().ForEach(x => sb.AppendLine($"\t- {x}"));
else
sb.AppendLine($"\t- 无");
sb.AppendLine("- 新特性");
if (Features.Any())
Features.ToList().ForEach(x => sb.AppendLine($"\t- {x}"));
else
sb.AppendLine($"\t- 无");
return sb.ToString();
}
public override string ToString()
{
return $"{Features} features, {BugFixed} bugs fixed";
}
#endregion
}
}