/
Dependency.cs
112 lines (98 loc) · 4.06 KB
/
Dependency.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright Bastian Eicher et al.
// Licensed under the GNU Lesser Public License
namespace ZeroInstall.Model;
#region Enumerations
/// <summary>
/// Describes how important a dependency is (i.e. whether ignoring it is an option).
/// </summary>
public enum Importance
{
/// <summary>A version of the <see cref="Dependency"/> must be selected.</summary>
[XmlEnum("essential")]
Essential,
/// <summary>No version of the <see cref="Dependency"/> is also an option, although selecting a version is preferable to not selecting one.</summary>
[XmlEnum("recommended")]
Recommended
}
#endregion
/// <summary>
/// A reference to an interface that is required as dependency.
/// </summary>
[Description("A reference to an interface that is required as dependency.")]
[Serializable, XmlRoot("requires", Namespace = Feed.XmlNamespace), XmlType("dependency", Namespace = Feed.XmlNamespace)]
[Equatable]
public partial class Dependency : Restriction, IInterfaceUriBindingContainer, ICloneable<Dependency>
{
/// <summary>
/// Controls how important this dependency is (i.e. whether ignoring it is an option).
/// </summary>
[Description("Controls how important this dependency is (i.e. whether ignoring it is an option).")]
[XmlAttribute("importance"), DefaultValue(typeof(Importance), "Essential")]
public Importance Importance { get; set; }
/// <summary>
/// This can be used to indicate that this dependency is only needed in some cases. Deprecated; use <see cref="Command"/>s instead.
/// </summary>
[Description("This can be used to indicate that this dependency is only needed in some cases; depcreated use <command>s instead.")]
[XmlAttribute("use"), DefaultValue("")]
public string? Use { get; set; }
/// <summary>
/// A list of <see cref="Binding"/>s for <see cref="Implementation"/>s to locate <see cref="Dependency"/>s.
/// </summary>
[Browsable(false)]
[XmlElement(typeof(GenericBinding)), XmlElement(typeof(EnvironmentBinding)), XmlElement(typeof(OverlayBinding)), XmlElement(typeof(ExecutableInVar)), XmlElement(typeof(ExecutableInPath))]
[OrderedEquality]
public List<Binding> Bindings { get; } = [];
/// <inheritdoc/>
public override bool IsApplicable(Requirements requirements)
{
#region Sanity checks
if (requirements == null) throw new ArgumentNullException(nameof(requirements));
#endregion
return string.IsNullOrEmpty(Use) && base.IsApplicable(requirements);
}
#region Normalize
protected override string XmlTagName => "requires";
/// <inheritdoc/>
public override void Normalize()
{
base.Normalize();
// Apply if-0install-version filter
Bindings.RemoveAll(FilterMismatch);
}
#endregion
#region Conversion
/// <summary>
/// Returns the dependency in the form "Interface (Use)". Not safe for parsing!
/// </summary>
public override string ToString()
=> string.Join(", ", new object?[]
{
InterfaceUri,
Use
}.Where(x => x is not 0)
.Select(x => x?.ToString())
.WhereNotNull());
#endregion
#region Clone
/// <summary>
/// Creates a deep copy of this <see cref="Dependency"/> instance.
/// </summary>
/// <returns>The new copy of the <see cref="Dependency"/>.</returns>
Dependency ICloneable<Dependency>.Clone() => new()
{
InterfaceUri = InterfaceUri,
OS = OS,
Versions = Versions,
Importance = Importance,
Use = Use,
Constraints = {Constraints.CloneElements()},
Distributions = {Distributions},
Bindings = {Bindings.CloneElements()}
};
/// <summary>
/// Creates a deep copy of this <see cref="Dependency"/> instance.
/// </summary>
/// <returns>The new copy of the <see cref="Dependency"/>.</returns>
public override Restriction Clone() => ((ICloneable<Dependency>)this).Clone();
#endregion
}