feature<*>: upgrade to .NET 5.0; use System.Text.Json instead of System.Xml; supports auto check update at startup

This commit is contained in:
leo 2020-12-01 10:31:46 +08:00
parent 1a46551a77
commit 4cde777b98
18 changed files with 300 additions and 80 deletions

View file

@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using System.Text.Json;
namespace SourceGit.Git {
@ -48,7 +48,7 @@ namespace SourceGit.Git {
private static readonly string SAVE_PATH = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"SourceGit",
"preference.xml");
"preference.json");
/// <summary>
/// Runtime singleton instance.
/// </summary>
@ -64,6 +64,17 @@ namespace SourceGit.Git {
}
#endregion
#region SETTING_GENERAL
/// <summary>
/// Use light color theme.
/// </summary>
public bool UseLightTheme { get; set; }
/// <summary>
/// Check for updates.
/// </summary>
public bool CheckUpdate { get; set; }
#endregion
#region SETTING_GIT
/// <summary>
/// Git executable file path.
@ -96,10 +107,6 @@ namespace SourceGit.Git {
/// </summary>
public double UIMainWindowHeight { get; set; }
/// <summary>
/// Use light color theme.
/// </summary>
public bool UIUseLightTheme { get; set; }
/// <summary>
/// Show/Hide tags' list view.
/// </summary>
public bool UIShowTags { get; set; } = true;
@ -144,17 +151,9 @@ namespace SourceGit.Git {
public static void Load() {
if (!File.Exists(SAVE_PATH)) {
instance = new Preference();
return;
} else {
instance = JsonSerializer.Deserialize<Preference>(File.ReadAllText(SAVE_PATH));
}
try {
var stream = new FileStream(SAVE_PATH, FileMode.Open);
var reader = new XmlSerializer(typeof(Preference));
instance = (Preference)reader.Deserialize(stream);
stream.Close();
} catch {
instance = new Preference();
}
}
/// <summary>
@ -166,11 +165,8 @@ namespace SourceGit.Git {
var dir = Path.GetDirectoryName(SAVE_PATH);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
var stream = new FileStream(SAVE_PATH, FileMode.Create);
var writer = new XmlSerializer(typeof(Preference));
writer.Serialize(stream, instance);
stream.Flush();
stream.Close();
var data = JsonSerializer.Serialize(instance, new JsonSerializerOptions() { WriteIndented = true });
File.WriteAllText(SAVE_PATH, data);
}
#endregion

View file

@ -3,9 +3,9 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Windows.Threading;
using System.Xml.Serialization;
namespace SourceGit.Git {
@ -15,14 +15,14 @@ namespace SourceGit.Git {
public class Repository {
#region HOOKS
[XmlIgnore] public Action<string> OnNavigateCommit = null;
[XmlIgnore] public Action OnWorkingCopyChanged = null;
[XmlIgnore] public Action OnTagChanged = null;
[XmlIgnore] public Action OnStashChanged = null;
[XmlIgnore] public Action OnBranchChanged = null;
[XmlIgnore] public Action OnCommitsChanged = null;
[XmlIgnore] public Action OnSubmoduleChanged = null;
[XmlIgnore] public Action OnClosing = null;
public Action<string> OnNavigateCommit = null;
public Action OnWorkingCopyChanged = null;
public Action OnTagChanged = null;
public Action OnStashChanged = null;
public Action OnBranchChanged = null;
public Action OnCommitsChanged = null;
public Action OnSubmoduleChanged = null;
public Action OnClosing = null;
#endregion
#region PROPERTIES_SAVED
@ -57,8 +57,8 @@ namespace SourceGit.Git {
#endregion
#region PROPERTIES_RUNTIME
[XmlIgnore] public Repository Parent = null;
[XmlIgnore] public string GitDir = null;
[JsonIgnore] public Repository Parent = null;
[JsonIgnore] public string GitDir = null;
private List<Remote> cachedRemotes = new List<Remote>();
private List<Branch> cachedBranches = new List<Branch>();

25
src/Git/Version.cs Normal file
View file

@ -0,0 +1,25 @@
using System;
using System.Text.Json.Serialization;
namespace SourceGit.Git {
/// <summary>
/// Version information.
/// </summary>
public class Version {
[JsonPropertyName("id")]
public ulong Id { get; set; }
[JsonPropertyName("tag_name")]
public string TagName { get; set; }
[JsonPropertyName("target_commitish")]
public string CommitSHA { get; set; }
[JsonPropertyName("prerelease")]
public bool PreRelease { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("body")]
public string Body { get; set; }
[JsonPropertyName("created_at")]
public DateTime CreatedAt { get; set; }
}
}