-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ExternalLinksParser rework #4425
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
GitCommands/ExternalLinks/ConfiguredLinkDefinitionsProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using GitCommands.Settings; | ||
|
||
namespace GitCommands.ExternalLinks | ||
{ | ||
/// <summary> | ||
/// Provides the ability to retrieves available persisted external link definitions. | ||
/// </summary> | ||
public interface IConfiguredLinkDefinitionsProvider | ||
{ | ||
/// <summary> | ||
/// Loads all persisted external link definitions across all setting layers. | ||
/// </summary> | ||
IList<ExternalLinkDefinition> Get(RepoDistSettings settings); | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves available persisted external link definitions. | ||
/// </summary> | ||
public sealed class ConfiguredLinkDefinitionsProvider : IConfiguredLinkDefinitionsProvider | ||
{ | ||
private readonly IExternalLinksLoader _externalLinksLoader; | ||
|
||
|
||
public ConfiguredLinkDefinitionsProvider(IExternalLinksLoader externalLinksLoader) | ||
{ | ||
_externalLinksLoader = externalLinksLoader; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Loads all persisted external link definitions across all setting layers. | ||
/// </summary> | ||
public IList<ExternalLinkDefinition> Get(RepoDistSettings settings) | ||
{ | ||
if (settings == null) | ||
{ | ||
throw new ArgumentNullException(nameof(settings)); | ||
} | ||
|
||
var cachedSettings = new RepoDistSettings(null, settings.SettingsCache); | ||
IEnumerable<ExternalLinkDefinition> effective = _externalLinksLoader.Load(cachedSettings); | ||
|
||
if (settings.LowerPriority != null) | ||
{ | ||
var lowerPriorityLoader = new ConfiguredLinkDefinitionsProvider(_externalLinksLoader); | ||
effective = effective.Union(lowerPriorityLoader.Get(settings.LowerPriority)); | ||
} | ||
|
||
return effective.ToList(); | ||
} | ||
} | ||
} |
8 changes: 3 additions & 5 deletions
8
GitCommands/GitExtLinks/GitExtLink.cs → GitCommands/ExternalLinks/ExternalLink.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Xml; | ||
using System.Xml.Serialization; | ||
using GitCommands.Settings; | ||
|
||
namespace GitCommands.ExternalLinks | ||
{ | ||
public interface IExternalLinksLoader | ||
{ | ||
/// <summary> | ||
/// Loads external link definitions from the settings. | ||
/// </summary> | ||
IList<ExternalLinkDefinition> Load(RepoDistSettings settings); | ||
|
||
/// <summary> | ||
/// Saves the provided external link definitions to the settings. | ||
/// </summary> | ||
void Save(RepoDistSettings settings, IList<ExternalLinkDefinition> definitions); | ||
} | ||
|
||
public sealed class ExternalLinksLoader : IExternalLinksLoader | ||
{ | ||
private const string SettingName = "RevisionLinkDefs"; | ||
|
||
|
||
/// <summary> | ||
/// Loads external link definitions from the settings. | ||
/// </summary> | ||
public IList<ExternalLinkDefinition> Load(RepoDistSettings settings) | ||
{ | ||
var cachedSettings = new RepoDistSettings(null, settings.SettingsCache); | ||
var xml = cachedSettings.GetString(SettingName, null); | ||
return LoadFromXmlString(xml); | ||
} | ||
|
||
/// <summary> | ||
/// Saves the provided external link definitions to the settings. | ||
/// </summary> | ||
public void Save(RepoDistSettings settings, IList<ExternalLinkDefinition> definitions) | ||
{ | ||
try | ||
{ | ||
string xml; | ||
if (definitions.Count == 0) | ||
{ | ||
xml = null; | ||
} | ||
else | ||
{ | ||
definitions.ForEach(linkDef => linkDef.RemoveEmptyFormats()); | ||
|
||
var sw = new StringWriter(); | ||
var serializer = new XmlSerializer(typeof(List<ExternalLinkDefinition>)); | ||
serializer.Serialize(sw, definitions); | ||
xml = sw.ToString(); | ||
} | ||
|
||
var cachedSettings = new RepoDistSettings(null, settings.SettingsCache); | ||
cachedSettings.SetString(SettingName, xml); | ||
} | ||
catch (Exception e) | ||
{ | ||
Debug.WriteLine(e); | ||
} | ||
} | ||
|
||
|
||
// TODO: refactor and outsource to the centralised SettingsSerialiser implementations. | ||
private static IList<ExternalLinkDefinition> LoadFromXmlString(string xmlString) | ||
{ | ||
if (string.IsNullOrWhiteSpace(xmlString)) | ||
{ | ||
return new List<ExternalLinkDefinition>(); | ||
} | ||
|
||
try | ||
{ | ||
var serializer = new XmlSerializer(typeof(List<ExternalLinkDefinition>)); | ||
using (var stringReader = new StringReader(xmlString)) | ||
{ | ||
using (var xmlReader = new XmlTextReader(stringReader)) | ||
{ | ||
return serializer.Deserialize(xmlReader) as List<ExternalLinkDefinition>; | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Debug.WriteLine(ex.Message); | ||
} | ||
|
||
return new List<ExternalLinkDefinition>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using GitCommands.Settings; | ||
|
||
namespace GitCommands.ExternalLinks | ||
{ | ||
// NB: this implementation is stateful | ||
public sealed class ExternalLinksManager | ||
{ | ||
private readonly RepoDistSettings _cachedSettings; | ||
private readonly ExternalLinksManager _lowerPriority; | ||
private readonly IExternalLinksLoader _externalLinksLoader = new ExternalLinksLoader(); | ||
private readonly IList<ExternalLinkDefinition> _definitions; | ||
|
||
|
||
public ExternalLinksManager(RepoDistSettings settings) | ||
{ | ||
_cachedSettings = new RepoDistSettings(null, settings.SettingsCache); | ||
_definitions = _externalLinksLoader.Load(_cachedSettings); | ||
|
||
if (settings.LowerPriority != null) | ||
{ | ||
_lowerPriority = new ExternalLinksManager(settings.LowerPriority); | ||
} | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Adds the provided definition at the lowest available level. | ||
/// </summary> | ||
/// <param name="definition">External link defintion.</param> | ||
public void Add(ExternalLinkDefinition definition) | ||
{ | ||
if (definition == null) | ||
{ | ||
throw new ArgumentNullException(nameof(definition)); | ||
} | ||
|
||
if (_lowerPriority == null || _lowerPriority.Contains(definition.Name)) | ||
{ | ||
if (!Contains(definition.Name)) | ||
{ | ||
_definitions.Add(definition); | ||
} | ||
// TODO: else notify the user? | ||
} | ||
else | ||
{ | ||
_lowerPriority.Add(definition); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Checks if a definition with the supplied name exists in any level of available settings. | ||
/// </summary> | ||
/// <param name="definitionName">The name of the defintion to find.</param> | ||
/// <returns><see langword="true"/> if a definition already exists; otherwise <see langword="false"/>.</returns> | ||
public bool Contains(string definitionName) | ||
{ | ||
return _definitions.Any(linkDef => linkDef.Name.Equals(definitionName)); | ||
} | ||
|
||
/// <summary> | ||
/// Loads all settings from all available levels. | ||
/// </summary> | ||
/// <returns>A collection of all available definitions.</returns> | ||
public IList<ExternalLinkDefinition> GetEffectiveSettings() | ||
{ | ||
var effective = _definitions.Union(_lowerPriority?.GetEffectiveSettings() ?? Enumerable.Empty<ExternalLinkDefinition>()).ToList(); | ||
return effective.ToList(); | ||
} | ||
|
||
/// <summary> | ||
/// Removes the supplied deifintion. | ||
/// </summary> | ||
/// <param name="definition">External link defintion.</param> | ||
public void Remove(ExternalLinkDefinition definition) | ||
{ | ||
if (!_definitions.Remove(definition)) | ||
{ | ||
_lowerPriority?.Remove(definition); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Saves the provided external link definitions to the settings. | ||
/// </summary> | ||
public void Save() | ||
{ | ||
_lowerPriority?.Save(); | ||
_externalLinksLoader.Save(_cachedSettings, _definitions); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
GitCommands/ExternalLinks/GitRevisionExternalLinksParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using GitCommands.Settings; | ||
|
||
namespace GitCommands.ExternalLinks | ||
{ | ||
public interface IGitRevisionExternalLinksParser | ||
{ | ||
IEnumerable<ExternalLink> Parse(GitRevision revision, RepoDistSettings settings); | ||
} | ||
|
||
public sealed class GitRevisionExternalLinksParser : IGitRevisionExternalLinksParser | ||
{ | ||
private readonly IConfiguredLinkDefinitionsProvider _effectiveLinkDefinitionsProvider; | ||
|
||
|
||
public GitRevisionExternalLinksParser(IConfiguredLinkDefinitionsProvider effectiveLinkDefinitionsProvider) | ||
{ | ||
_effectiveLinkDefinitionsProvider = effectiveLinkDefinitionsProvider; | ||
} | ||
|
||
|
||
public IEnumerable<ExternalLink> Parse(GitRevision revision, RepoDistSettings settings) | ||
{ | ||
var definitions = _effectiveLinkDefinitionsProvider.Get(settings); | ||
return definitions.Where(definition => definition.Enabled) | ||
.SelectMany(linkDef => linkDef.Parse(revision)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be OK to not notify