From dfd098e13106cefe468993dd1c751361e5790e2a Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 5 Aug 2024 18:18:57 +0800 Subject: [PATCH] refactor: merge sourcegit.issuetracker.setting to sourcegit.settings. --- src/App.JsonCodeGen.cs | 1 - .../IssueTrackerRule.cs | 54 ++----------------- src/Models/RepositorySettings.cs | 51 ++++++++++++++++++ src/ViewModels/CommitDetail.cs | 15 +++--- src/ViewModels/FileHistories.cs | 6 +-- src/ViewModels/Histories.cs | 4 +- src/ViewModels/InteractiveRebase.cs | 2 +- src/ViewModels/Repository.cs | 27 ---------- src/ViewModels/RepositoryConfigure.cs | 23 ++++---- src/ViewModels/WorkingCopy.cs | 2 +- src/Views/CommitBaseInfo.axaml | 2 +- src/Views/CommitBaseInfo.axaml.cs | 11 ++-- src/Views/CommitDetail.axaml | 2 +- src/Views/CommitMessagePresenter.cs | 17 +++--- src/Views/RepositoryConfigure.axaml | 5 +- 15 files changed, 100 insertions(+), 122 deletions(-) rename src/{ViewModels => Models}/IssueTrackerRule.cs (67%) diff --git a/src/App.JsonCodeGen.cs b/src/App.JsonCodeGen.cs index 228928a7..f6e3cd88 100644 --- a/src/App.JsonCodeGen.cs +++ b/src/App.JsonCodeGen.cs @@ -64,6 +64,5 @@ namespace SourceGit [JsonSerializable(typeof(Models.Version))] [JsonSerializable(typeof(Models.RepositorySettings))] [JsonSerializable(typeof(ViewModels.Preference))] - [JsonSerializable(typeof(ViewModels.IssueTrackerRuleSetting))] internal partial class JsonCodeGen : JsonSerializerContext { } } diff --git a/src/ViewModels/IssueTrackerRule.cs b/src/Models/IssueTrackerRule.cs similarity index 67% rename from src/ViewModels/IssueTrackerRule.cs rename to src/Models/IssueTrackerRule.cs index 3056b984..a4a9a3d1 100644 --- a/src/ViewModels/IssueTrackerRule.cs +++ b/src/Models/IssueTrackerRule.cs @@ -5,7 +5,7 @@ using Avalonia.Collections; using CommunityToolkit.Mvvm.ComponentModel; -namespace SourceGit.ViewModels +namespace SourceGit.Models { public class IssueTrackerMatch { @@ -72,7 +72,7 @@ namespace SourceGit.ViewModels return; var matches = _regex.Matches(message); - for (int i = 0; i < matches.Count; i++) + for (var i = 0; i < matches.Count; i++) { var match = matches[i]; if (!match.Success) @@ -97,7 +97,7 @@ namespace SourceGit.ViewModels range.Start = start; range.Length = len; range.URL = _urlTemplate; - for (int j = 1; j < match.Groups.Count; j++) + for (var j = 1; j < match.Groups.Count; j++) { var group = match.Groups[j]; if (group.Success) @@ -113,52 +113,4 @@ namespace SourceGit.ViewModels private string _urlTemplate; private Regex _regex = null; } - - public class IssueTrackerRuleSetting - { - public AvaloniaList Rules - { - get; - set; - } = new AvaloniaList(); - - public IssueTrackerRule Add() - { - var rule = new IssueTrackerRule() - { - Name = "New Issue Tracker", - RegexString = "#(\\d+)", - URLTemplate = "https://xxx/$1", - }; - - Rules.Add(rule); - return rule; - } - - public IssueTrackerRule AddGithub(string repoURL) - { - var rule = new IssueTrackerRule() - { - Name = "Github ISSUE", - RegexString = "#(\\d+)", - URLTemplate = string.IsNullOrEmpty(repoURL) ? "https://github.com/username/repository/issues/$1" : $"{repoURL}/issues/$1", - }; - - Rules.Add(rule); - return rule; - } - - public IssueTrackerRule AddJira() - { - var rule = new IssueTrackerRule() - { - Name = "Jira Tracker", - RegexString = "PROJ-(\\d+)", - URLTemplate = "https://jira.yourcompany.com/browse/PROJ-$1", - }; - - Rules.Add(rule); - return rule; - } - } } diff --git a/src/Models/RepositorySettings.cs b/src/Models/RepositorySettings.cs index 514a0d59..bf15c4f4 100644 --- a/src/Models/RepositorySettings.cs +++ b/src/Models/RepositorySettings.cs @@ -76,6 +76,12 @@ namespace SourceGit.Models set; } = new AvaloniaList(); + public AvaloniaList IssueTrackerRules + { + get; + set; + } = new AvaloniaList(); + public void PushCommitMessage(string message) { var existIdx = CommitMessages.IndexOf(message); @@ -93,5 +99,50 @@ namespace SourceGit.Models CommitMessages.Insert(0, message); } + + public IssueTrackerRule AddNewIssueTracker() + { + var rule = new IssueTrackerRule() + { + Name = "New Issue Tracker", + RegexString = "#(\\d+)", + URLTemplate = "https://xxx/$1", + }; + + IssueTrackerRules.Add(rule); + return rule; + } + + public IssueTrackerRule AddGithubIssueTracker(string repoURL) + { + var rule = new IssueTrackerRule() + { + Name = "Github ISSUE", + RegexString = "#(\\d+)", + URLTemplate = string.IsNullOrEmpty(repoURL) ? "https://github.com/username/repository/issues/$1" : $"{repoURL}/issues/$1", + }; + + IssueTrackerRules.Add(rule); + return rule; + } + + public IssueTrackerRule AddJiraIssueTracker() + { + var rule = new IssueTrackerRule() + { + Name = "Jira Tracker", + RegexString = "PROJ-(\\d+)", + URLTemplate = "https://jira.yourcompany.com/browse/PROJ-$1", + }; + + IssueTrackerRules.Add(rule); + return rule; + } + + public void RemoveIssueTracker(IssueTrackerRule rule) + { + if (rule != null) + IssueTrackerRules.Remove(rule); + } } } diff --git a/src/ViewModels/CommitDetail.cs b/src/ViewModels/CommitDetail.cs index 667ed657..f01e333f 100644 --- a/src/ViewModels/CommitDetail.cs +++ b/src/ViewModels/CommitDetail.cs @@ -4,6 +4,7 @@ using System.IO; using System.Text.RegularExpressions; using System.Threading.Tasks; +using Avalonia.Collections; using Avalonia.Controls; using Avalonia.Media.Imaging; using Avalonia.Platform.Storage; @@ -88,15 +89,15 @@ namespace SourceGit.ViewModels set => SetProperty(ref _viewRevisionFileContent, value); } - public IssueTrackerRuleSetting IssueTrackerSetting + public AvaloniaList IssueTrackerRules { - get => _issueTrackerSetting; + get => _issueTrackerRules; } - public CommitDetail(string repo, IssueTrackerRuleSetting issueTrackerSetting) + public CommitDetail(string repo, AvaloniaList issueTrackerRules) { _repo = repo; - _issueTrackerSetting = issueTrackerSetting; + _issueTrackerRules = issueTrackerRules; } public void Cleanup() @@ -248,7 +249,7 @@ namespace SourceGit.ViewModels history.Icon = App.CreateMenuIcon("Icons.Histories"); history.Click += (_, ev) => { - var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, change.Path, _issueTrackerSetting) }; + var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, change.Path, _issueTrackerRules) }; window.Show(); ev.Handled = true; }; @@ -311,7 +312,7 @@ namespace SourceGit.ViewModels history.Icon = App.CreateMenuIcon("Icons.Histories"); history.Click += (_, ev) => { - var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, file.Path, _issueTrackerSetting) }; + var window = new Views.FileHistories() { DataContext = new FileHistories(_repo, file.Path, _issueTrackerRules) }; window.Show(); ev.Handled = true; }; @@ -463,7 +464,7 @@ namespace SourceGit.ViewModels }; private string _repo; - private IssueTrackerRuleSetting _issueTrackerSetting = null; + private AvaloniaList _issueTrackerRules = null; private int _activePageIndex = 0; private Models.Commit _commit = null; private string _fullMessage = string.Empty; diff --git a/src/ViewModels/FileHistories.cs b/src/ViewModels/FileHistories.cs index 82b0b11a..79696a7e 100644 --- a/src/ViewModels/FileHistories.cs +++ b/src/ViewModels/FileHistories.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; using System.Threading.Tasks; - +using Avalonia.Collections; using Avalonia.Threading; using CommunityToolkit.Mvvm.ComponentModel; @@ -54,11 +54,11 @@ namespace SourceGit.ViewModels set => SetProperty(ref _detailContext, value); } - public FileHistories(string repo, string file, IssueTrackerRuleSetting issueTrackerSetting) + public FileHistories(string repo, string file, AvaloniaList issueTrackerRules) { _repo = repo; _file = file; - _detailContext = new CommitDetail(repo, issueTrackerSetting); + _detailContext = new CommitDetail(repo, issueTrackerRules); Task.Run(() => { diff --git a/src/ViewModels/Histories.cs b/src/ViewModels/Histories.cs index da18f815..25500a4c 100644 --- a/src/ViewModels/Histories.cs +++ b/src/ViewModels/Histories.cs @@ -94,7 +94,7 @@ namespace SourceGit.ViewModels } else { - var commitDetail = new CommitDetail(_repo.FullPath, _repo.IssueTrackerSetting); + var commitDetail = new CommitDetail(_repo.FullPath, _repo.Settings.IssueTrackerRules); commitDetail.Commit = commit; DetailContext = commitDetail; } @@ -122,7 +122,7 @@ namespace SourceGit.ViewModels } else { - var commitDetail = new CommitDetail(_repo.FullPath, _repo.IssueTrackerSetting); + var commitDetail = new CommitDetail(_repo.FullPath, _repo.Settings.IssueTrackerRules); commitDetail.Commit = commit; DetailContext = commitDetail; } diff --git a/src/ViewModels/InteractiveRebase.cs b/src/ViewModels/InteractiveRebase.cs index aaeab204..32417e01 100644 --- a/src/ViewModels/InteractiveRebase.cs +++ b/src/ViewModels/InteractiveRebase.cs @@ -114,7 +114,7 @@ namespace SourceGit.ViewModels Current = current; On = on; IsLoading = true; - DetailContext = new CommitDetail(repoPath, repo.IssueTrackerSetting); + DetailContext = new CommitDetail(repoPath, repo.Settings.IssueTrackerRules); Task.Run(() => { diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index f37f59f0..3ee8edd8 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -44,11 +44,6 @@ namespace SourceGit.ViewModels get => _settings; } - public IssueTrackerRuleSetting IssueTrackerSetting - { - get => _issueTrackerSetting; - } - public int SelectedViewIndex { get => _selectedViewIndex; @@ -324,23 +319,6 @@ namespace SourceGit.ViewModels _settings = new Models.RepositorySettings(); } - var issueTrackerSettingsFile = Path.Combine(_gitDir, "sourcegit.issuetracker.settings"); - if (File.Exists(issueTrackerSettingsFile)) - { - try - { - _issueTrackerSetting = JsonSerializer.Deserialize(File.ReadAllText(issueTrackerSettingsFile), JsonCodeGen.Default.IssueTrackerRuleSetting); - } - catch - { - _issueTrackerSetting = new IssueTrackerRuleSetting(); - } - } - else - { - _issueTrackerSetting = new IssueTrackerRuleSetting(); - } - _watcher = new Models.Watcher(this); _histories = new Histories(this); _workingCopy = new WorkingCopy(this); @@ -361,10 +339,6 @@ namespace SourceGit.ViewModels File.WriteAllText(Path.Combine(_gitDir, "sourcegit.settings"), settingsSerialized); _settings = null; - var issueTrackerSerialized = JsonSerializer.Serialize(_issueTrackerSetting, JsonCodeGen.Default.IssueTrackerRuleSetting); - File.WriteAllText(Path.Combine(_gitDir, "sourcegit.issuetracker.settings"), issueTrackerSerialized); - _issueTrackerSetting = null; - _watcher.Dispose(); _histories.Cleanup(); _workingCopy.Cleanup(); @@ -1986,7 +1960,6 @@ namespace SourceGit.ViewModels private string _fullpath = string.Empty; private string _gitDir = string.Empty; private Models.RepositorySettings _settings = null; - private IssueTrackerRuleSetting _issueTrackerSetting = null; private Models.Watcher _watcher = null; private Histories _histories = null; diff --git a/src/ViewModels/RepositoryConfigure.cs b/src/ViewModels/RepositoryConfigure.cs index 4ce73c61..f252a075 100644 --- a/src/ViewModels/RepositoryConfigure.cs +++ b/src/ViewModels/RepositoryConfigure.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using Avalonia.Collections; using CommunityToolkit.Mvvm.ComponentModel; @@ -43,12 +42,12 @@ namespace SourceGit.ViewModels set => SetProperty(ref _httpProxy, value); } - public AvaloniaList IssueTrackerRules + public AvaloniaList IssueTrackerRules { - get => _repo.IssueTrackerSetting.Rules; + get => _repo.Settings.IssueTrackerRules; } - public IssueTrackerRule SelectedIssueTrackerRule + public Models.IssueTrackerRule SelectedIssueTrackerRule { get => _selectedIssueTrackerRule; set => SetProperty(ref _selectedIssueTrackerRule, value); @@ -86,29 +85,29 @@ namespace SourceGit.ViewModels { if (remote.TryGetVisitURL(out string url)) { - SelectedIssueTrackerRule = _repo.IssueTrackerSetting.AddGithub(url); + SelectedIssueTrackerRule = _repo.Settings.AddGithubIssueTracker(url); return; } } } - SelectedIssueTrackerRule = _repo.IssueTrackerSetting.AddGithub(null); + SelectedIssueTrackerRule = _repo.Settings.AddGithubIssueTracker(null); } public void AddSampleJiraIssueTracker() { - SelectedIssueTrackerRule = _repo.IssueTrackerSetting.AddJira(); + SelectedIssueTrackerRule = _repo.Settings.AddJiraIssueTracker(); } public void NewIssueTracker() { - SelectedIssueTrackerRule = _repo.IssueTrackerSetting.Add(); + SelectedIssueTrackerRule = _repo.Settings.AddNewIssueTracker(); } public void RemoveSelectedIssueTracker() { - if (_selectedIssueTrackerRule != null) - _repo.IssueTrackerSetting.Rules.Remove(_selectedIssueTrackerRule); + _repo.Settings.RemoveIssueTracker(_selectedIssueTrackerRule); + SelectedIssueTrackerRule = null; } public void Save() @@ -142,6 +141,6 @@ namespace SourceGit.ViewModels private readonly Repository _repo = null; private readonly Dictionary _cached = null; private string _httpProxy; - private IssueTrackerRule _selectedIssueTrackerRule = null; + private Models.IssueTrackerRule _selectedIssueTrackerRule = null; } } diff --git a/src/ViewModels/WorkingCopy.cs b/src/ViewModels/WorkingCopy.cs index f334c599..8f352fbe 100644 --- a/src/ViewModels/WorkingCopy.cs +++ b/src/ViewModels/WorkingCopy.cs @@ -541,7 +541,7 @@ namespace SourceGit.ViewModels history.Icon = App.CreateMenuIcon("Icons.Histories"); history.Click += (_, e) => { - var window = new Views.FileHistories() { DataContext = new FileHistories(_repo.FullPath, change.Path, _repo.IssueTrackerSetting) }; + var window = new Views.FileHistories() { DataContext = new FileHistories(_repo.FullPath, change.Path, _repo.Settings.IssueTrackerRules) }; window.Show(); e.Handled = true; }; diff --git a/src/Views/CommitBaseInfo.axaml b/src/Views/CommitBaseInfo.axaml index cc8becd7..d2c57810 100644 --- a/src/Views/CommitBaseInfo.axaml +++ b/src/Views/CommitBaseInfo.axaml @@ -96,7 +96,7 @@ Margin="12,5,8,0" Classes="primary" Message="{Binding #ThisControl.Message}" - IssueTrackerSetting="{Binding #ThisControl.IssueTrackerSetting}" + IssueTrackerRules="{Binding #ThisControl.IssueTrackerRules}" TextWrapping="Wrap"/> diff --git a/src/Views/CommitBaseInfo.axaml.cs b/src/Views/CommitBaseInfo.axaml.cs index eca16ada..86451dfe 100644 --- a/src/Views/CommitBaseInfo.axaml.cs +++ b/src/Views/CommitBaseInfo.axaml.cs @@ -1,4 +1,5 @@ using Avalonia; +using Avalonia.Collections; using Avalonia.Controls; using Avalonia.Input; @@ -24,13 +25,13 @@ namespace SourceGit.Views set => SetValue(MessageProperty, value); } - public static readonly StyledProperty IssueTrackerSettingProperty = - AvaloniaProperty.Register(nameof(IssueTrackerSetting)); + public static readonly StyledProperty> IssueTrackerRulesProperty = + AvaloniaProperty.Register>(nameof(IssueTrackerRules)); - public ViewModels.IssueTrackerRuleSetting IssueTrackerSetting + public AvaloniaList IssueTrackerRules { - get => GetValue(IssueTrackerSettingProperty); - set => SetValue(IssueTrackerSettingProperty, value); + get => GetValue(IssueTrackerRulesProperty); + set => SetValue(IssueTrackerRulesProperty, value); } public CommitBaseInfo() diff --git a/src/Views/CommitDetail.axaml b/src/Views/CommitDetail.axaml index 9b54f8ba..af733f49 100644 --- a/src/Views/CommitDetail.axaml +++ b/src/Views/CommitDetail.axaml @@ -21,7 +21,7 @@ + IssueTrackerRules="{Binding IssueTrackerRules}"/> diff --git a/src/Views/CommitMessagePresenter.cs b/src/Views/CommitMessagePresenter.cs index 593de4ce..116442d9 100644 --- a/src/Views/CommitMessagePresenter.cs +++ b/src/Views/CommitMessagePresenter.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using Avalonia; +using Avalonia.Collections; using Avalonia.Controls; using Avalonia.Controls.Documents; using Avalonia.Input; @@ -19,13 +20,13 @@ namespace SourceGit.Views set => SetValue(MessageProperty, value); } - public static readonly StyledProperty IssueTrackerSettingProperty = - AvaloniaProperty.Register(nameof(IssueTrackerSetting)); + public static readonly StyledProperty> IssueTrackerRulesProperty = + AvaloniaProperty.Register>(nameof(IssueTrackerRules)); - public ViewModels.IssueTrackerRuleSetting IssueTrackerSetting + public AvaloniaList IssueTrackerRules { - get => GetValue(IssueTrackerSettingProperty); - set => SetValue(IssueTrackerSettingProperty, value); + get => GetValue(IssueTrackerRulesProperty); + set => SetValue(IssueTrackerRulesProperty, value); } protected override Type StyleKeyOverride => typeof(SelectableTextBlock); @@ -34,7 +35,7 @@ namespace SourceGit.Views { base.OnPropertyChanged(change); - if (change.Property == MessageProperty || change.Property == IssueTrackerSettingProperty) + if (change.Property == MessageProperty || change.Property == IssueTrackerRulesProperty) { Inlines.Clear(); @@ -42,14 +43,14 @@ namespace SourceGit.Views if (string.IsNullOrEmpty(message)) return; - var rules = IssueTrackerSetting?.Rules; + var rules = IssueTrackerRules; if (rules == null || rules.Count == 0) { Inlines.Add(new Run(message)); return; } - var matches = new List(); + var matches = new List(); foreach (var rule in rules) rule.Matches(matches, message); diff --git a/src/Views/RepositoryConfigure.axaml b/src/Views/RepositoryConfigure.axaml index 42e60b7c..6ffe66d1 100644 --- a/src/Views/RepositoryConfigure.axaml +++ b/src/Views/RepositoryConfigure.axaml @@ -2,6 +2,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:m="using:SourceGit.Models" xmlns:vm="using:SourceGit.ViewModels" xmlns:v="using:SourceGit.Views" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" @@ -141,7 +142,7 @@ - + @@ -187,7 +188,7 @@ - +