refactor: merge sourcegit.issuetracker.setting to sourcegit.settings.

This commit is contained in:
leo 2024-08-05 18:18:57 +08:00
parent f754b2c63a
commit dfd098e131
No known key found for this signature in database
15 changed files with 100 additions and 122 deletions

View file

@ -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 { }
}

View file

@ -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<IssueTrackerRule> Rules
{
get;
set;
} = new AvaloniaList<IssueTrackerRule>();
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;
}
}
}

View file

@ -76,6 +76,12 @@ namespace SourceGit.Models
set;
} = new AvaloniaList<string>();
public AvaloniaList<IssueTrackerRule> IssueTrackerRules
{
get;
set;
} = new AvaloniaList<IssueTrackerRule>();
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);
}
}
}

View file

@ -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<Models.IssueTrackerRule> IssueTrackerRules
{
get => _issueTrackerSetting;
get => _issueTrackerRules;
}
public CommitDetail(string repo, IssueTrackerRuleSetting issueTrackerSetting)
public CommitDetail(string repo, AvaloniaList<Models.IssueTrackerRule> 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<Models.IssueTrackerRule> _issueTrackerRules = null;
private int _activePageIndex = 0;
private Models.Commit _commit = null;
private string _fullMessage = string.Empty;

View file

@ -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<Models.IssueTrackerRule> issueTrackerRules)
{
_repo = repo;
_file = file;
_detailContext = new CommitDetail(repo, issueTrackerSetting);
_detailContext = new CommitDetail(repo, issueTrackerRules);
Task.Run(() =>
{

View file

@ -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;
}

View file

@ -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(() =>
{

View file

@ -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;

View file

@ -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<IssueTrackerRule> IssueTrackerRules
public AvaloniaList<Models.IssueTrackerRule> 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<string, string> _cached = null;
private string _httpProxy;
private IssueTrackerRule _selectedIssueTrackerRule = null;
private Models.IssueTrackerRule _selectedIssueTrackerRule = null;
}
}

View file

@ -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;
};

View file

@ -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"/>
</Grid>
</StackPanel>

View file

@ -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<ViewModels.IssueTrackerRuleSetting> IssueTrackerSettingProperty =
AvaloniaProperty.Register<CommitBaseInfo, ViewModels.IssueTrackerRuleSetting>(nameof(IssueTrackerSetting));
public static readonly StyledProperty<AvaloniaList<Models.IssueTrackerRule>> IssueTrackerRulesProperty =
AvaloniaProperty.Register<CommitBaseInfo, AvaloniaList<Models.IssueTrackerRule>>(nameof(IssueTrackerRules));
public ViewModels.IssueTrackerRuleSetting IssueTrackerSetting
public AvaloniaList<Models.IssueTrackerRule> IssueTrackerRules
{
get => GetValue(IssueTrackerSettingProperty);
set => SetValue(IssueTrackerSettingProperty, value);
get => GetValue(IssueTrackerRulesProperty);
set => SetValue(IssueTrackerRulesProperty, value);
}
public CommitBaseInfo()

View file

@ -21,7 +21,7 @@
<!-- Base Information -->
<v:CommitBaseInfo Content="{Binding Commit}"
Message="{Binding FullMessage}"
IssueTrackerSetting="{Binding IssueTrackerSetting}"/>
IssueTrackerRules="{Binding IssueTrackerRules}"/>
<!-- Line -->
<Rectangle Height=".65" Margin="8" Fill="{DynamicResource Brush.Border2}"/>

View file

@ -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<ViewModels.IssueTrackerRuleSetting> IssueTrackerSettingProperty =
AvaloniaProperty.Register<CommitMessagePresenter, ViewModels.IssueTrackerRuleSetting>(nameof(IssueTrackerSetting));
public static readonly StyledProperty<AvaloniaList<Models.IssueTrackerRule>> IssueTrackerRulesProperty =
AvaloniaProperty.Register<CommitMessagePresenter, AvaloniaList<Models.IssueTrackerRule>>(nameof(IssueTrackerRules));
public ViewModels.IssueTrackerRuleSetting IssueTrackerSetting
public AvaloniaList<Models.IssueTrackerRule> 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<ViewModels.IssueTrackerMatch>();
var matches = new List<Models.IssueTrackerMatch>();
foreach (var rule in rules)
rule.Matches(matches, message);

View file

@ -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 @@
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate DataType="vm:IssueTrackerRule">
<DataTemplate DataType="m:IssueTrackerRule">
<TextBlock Grid.Column="1" Text="{Binding Name}" Margin="8,0" TextTrimming="CharacterEllipsis"/>
</DataTemplate>
</ListBox.ItemTemplate>
@ -187,7 +188,7 @@
</ContentControl.Content>
<ContentControl.DataTemplates>
<DataTemplate DataType="vm:IssueTrackerRule">
<DataTemplate DataType="m:IssueTrackerRule">
<Grid Grid.Column="1" RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,Auto">
<TextBlock Grid.Row="0" Text="{DynamicResource Text.Configure.IssueTracker.RuleName}"/>
<TextBox Grid.Row="1" CornerRadius="3" Height="28" Text="{Binding Name, Mode=TwoWay}"/>