| 1 |
using System; |
| 2 |
using System.Collections.Generic; |
| 3 |
using System.Linq; |
| 4 |
using Mono.Cecil; |
| 5 |
using Mono.Cecil.Rocks; |
| 6 |
|
| 7 |
namespace Mono.Documentation.Updater.Frameworks |
| 8 |
{ |
| 9 |
public class FrameworkEntry |
| 10 |
{ |
| 11 |
SortedSet<FrameworkTypeEntry> types = new SortedSet<FrameworkTypeEntry> (); |
| 12 |
|
| 13 |
IList<FrameworkEntry> allcachedframeworks; |
| 14 |
IList<FrameworkEntry> allframeworks; |
| 15 |
ISet<AssemblySet> allAssemblies = new SortedSet<AssemblySet> (); |
| 16 |
|
| 17 |
public int Index = 0; |
| 18 |
int _fxCount; |
| 19 |
public int FrameworksCount { |
| 20 |
get => _fxCount < 1 ? allframeworks.Count : _fxCount; |
| 21 |
} |
| 22 |
|
| 23 |
public FrameworkEntry (IList<FrameworkEntry> frameworks, IList<FrameworkEntry> cachedfx) : this(frameworks, -1, cachedfx) {} |
| 24 |
|
| 25 |
public FrameworkEntry (IList<FrameworkEntry> frameworks, int fxCount, IList<FrameworkEntry> cachedFx) |
| 26 |
{ |
| 27 |
allframeworks = frameworks; |
| 28 |
if (allframeworks == null) |
| 29 |
{ |
| 30 |
allframeworks = new List<FrameworkEntry> (1); |
| 31 |
allframeworks.Add (this); |
| 32 |
Index = 0; |
| 33 |
allcachedframeworks = allframeworks; |
| 34 |
} |
| 35 |
else |
| 36 |
{ |
| 37 |
Index = allframeworks.Count; |
| 38 |
|
| 39 |
allcachedframeworks = cachedFx; |
| 40 |
} |
| 41 |
_fxCount = fxCount; |
| 42 |
} |
| 43 |
|
| 44 |
public string Name { get; set; } |
| 45 |
public string Version { get; set; } |
| 46 |
public string Id { get; set; } |
| 47 |
|
| 48 |
/// <summary>Gets a value indicating whether this <see cref="T:Mono.Documentation.Updater.Frameworks.FrameworkEntry"/> is last framework being processed.</summary> |
| 49 |
public bool IsLastFramework { |
| 50 |
get => Index == FrameworksCount - 1; |
| 51 |
} |
| 52 |
|
| 53 |
/// <param name="assemblyName">should be the assembly name (without version and file extension)</param> |
| 54 |
public bool IsLastFrameworkForAssembly(string assemblyName) |
| 55 |
{ |
| 56 |
if (this == Empty) return true; |
| 57 |
|
| 58 |
var retval = this.allcachedframeworks |
| 59 |
.Where(f => f.AllProcessedAssemblies |
| 60 |
.Any(ass => ass.Assemblies |
| 61 |
.Any(a => a.Name.Name.Equals(assemblyName, StringComparison.OrdinalIgnoreCase)))) |
| 62 |
.ToArray(); |
| 63 |
|
| 64 |
if (!retval.Any ()) return false; |
| 65 |
|
| 66 |
var lastListed = retval.Last (); |
| 67 |
return lastListed.Name == this.Name; |
| 68 |
} |
| 69 |
|
| 70 |
public bool IsLastFrameworkForType(FrameworkTypeEntry typeEntry) |
| 71 |
{ |
| 72 |
if (this == Empty) return true; |
| 73 |
|
| 74 |
var fxlist = this.allcachedframeworks.Where (f => f.FindTypeEntry (typeEntry) != null).ToArray(); |
| 75 |
|
| 76 |
if (!fxlist.Any ()) return false; |
| 77 |
|
| 78 |
var lastListed = fxlist.Last (); |
| 79 |
return lastListed.Name == this.Name; |
| 80 |
} |
| 81 |
|
| 82 |
public string AllFrameworksWithAssembly(string assemblyName) |
| 83 |
{ |
| 84 |
if (this == Empty) return this.Name; |
| 85 |
|
| 86 |
var fxlist = this.allcachedframeworks.Where (f => f.allAssemblies.Any (ass => ass.Assemblies.Any (a => a.Name.Name.Equals (assemblyName, StringComparison.OrdinalIgnoreCase)))); |
| 87 |
return string.Join (";", fxlist.Select (f => f.Name).ToArray ()); |
| 88 |
} |
| 89 |
|
| 90 |
public string AllFrameworksWithType(FrameworkTypeEntry typeEntry) |
| 91 |
{ |
| 92 |
if (this == Empty) return this.Name; |
| 93 |
|
| 94 |
var fxlist = this.allcachedframeworks.Where (f => f.FindTypeEntry (typeEntry) != null); |
| 95 |
return string.Join (";", fxlist.Select (f => f.Name).ToArray ()); |
| 96 |
} |
| 97 |
|
| 98 |
string _allFxString = ""; |
| 99 |
public string AllFrameworksString { |
| 100 |
get |
| 101 |
{ |
| 102 |
Lazy<string> fxString = new Lazy<string>(() => string.Join (";", allcachedframeworks.Select (f => f.Name).ToArray ())); |
| 103 |
|
| 104 |
if (!this.IsLastFramework) return fxString.Value; |
| 105 |
if (string.IsNullOrWhiteSpace(_allFxString)) |
| 106 |
{ |
| 107 |
_allFxString = fxString.Value; |
| 108 |
} |
| 109 |
return _allFxString; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public readonly List<Tuple<string,string>> AssemblyNames = new List<Tuple<string, string>> (); |
| 114 |
|
| 115 |
public void AddProcessedAssembly (AssemblyDefinition assembly) |
| 116 |
{ |
| 117 |
AssemblyNames.Add (new Tuple<string, string>(assembly.Name.Name, assembly.Name.Version.ToString())); |
| 118 |
} |
| 119 |
|
| 120 |
public IEnumerable<FrameworkEntry> PreviousFrameworks { |
| 121 |
get => allframeworks.Where (f => f.Index < this.Index); |
| 122 |
} |
| 123 |
|
| 124 |
public ISet<AssemblySet> AllProcessedAssemblies { get => allAssemblies; } |
| 125 |
|
| 126 |
public void AddAssemblySet (AssemblySet assemblySet) |
| 127 |
{ |
| 128 |
allAssemblies.Add (assemblySet); |
| 129 |
} |
| 130 |
|
| 131 |
/// <summary>Gets a value indicating whether this <see cref="T:Mono.Documentation.Updater.Frameworks.FrameworkEntry"/> is first framework being processed.</summary> |
| 132 |
public bool IsFirstFramework { |
| 133 |
get => this.Index == 0; |
| 134 |
} |
| 135 |
|
| 136 |
public bool IsFirstFrameworkForType(FrameworkTypeEntry typeEntry) |
| 137 |
{ |
| 138 |
if (this == Empty) return true; |
| 139 |
|
| 140 |
var firstFx = this.allcachedframeworks.FirstOrDefault(f => f.FindTypeEntry(typeEntry) != null); |
| 141 |
|
| 142 |
return firstFx == null || firstFx.Name == this.Name; |
| 143 |
} |
| 144 |
|
| 145 |
/// <summary>Only Use in Unit Tests</summary> |
| 146 |
public string Replace=""; |
| 147 |
|
| 148 |
/// <summary>Only Use in Unit Tests</summary> |
| 149 |
public string With =""; |
| 150 |
|
| 151 |
public IEnumerable<DocumentationImporter> Importers { get; set; } |
| 152 |
|
| 153 |
public ISet<FrameworkTypeEntry> Types { get { return this.types; } } |
| 154 |
Dictionary<string, FrameworkTypeEntry> typeMap = new Dictionary<string, FrameworkTypeEntry> (); |
| 155 |
|
| 156 |
public FrameworkTypeEntry FindTypeEntry (FrameworkTypeEntry type) |
| 157 |
{ |
| 158 |
return FindTypeEntry (Str(type.Name)); |
| 159 |
} |
| 160 |
|
| 161 |
/// <param name="name">The value from <see cref="FrameworkTypeEntry.Name"/>.</param> |
| 162 |
public FrameworkTypeEntry FindTypeEntry (string name) { |
| 163 |
FrameworkTypeEntry entry; |
| 164 |
typeMap.TryGetValue (Str(name), out entry); |
| 165 |
return entry; |
| 166 |
} |
| 167 |
|
| 168 |
public IEnumerable<FrameworkEntry> Frameworks { get { return this.allframeworks; } } |
| 169 |
|
| 170 |
public static readonly FrameworkEntry Empty = new EmptyFrameworkEntry () { Name = "Empty" }; |
| 171 |
|
| 172 |
public virtual FrameworkTypeEntry ProcessType (TypeDefinition type) |
| 173 |
{ |
| 174 |
FrameworkTypeEntry entry; |
| 175 |
|
| 176 |
if (!typeMap.TryGetValue (Str(type.FullName), out entry)) |
| 177 |
{ |
| 178 |
var docid = DocCommentId.GetDocCommentId (type); |
| 179 |
string nstouse = GetNamespace (type); |
| 180 |
entry = new FrameworkTypeEntry (this) { Id = Str (docid), Name = Str (type.FullName), Namespace = nstouse }; |
| 181 |
types.Add (entry); |
| 182 |
|
| 183 |
typeMap.Add (Str (entry.Name), entry); |
| 184 |
} |
| 185 |
return entry; |
| 186 |
} |
| 187 |
|
| 188 |
private string GetNamespace (TypeDefinition type) |
| 189 |
{ |
| 190 |
var nstouse = Str (type.Namespace); |
| 191 |
if (string.IsNullOrWhiteSpace (nstouse) && type.DeclaringType != null) |
| 192 |
{ |
| 193 |
return GetNamespace(type.DeclaringType); |
| 194 |
} |
| 195 |
|
| 196 |
return nstouse; |
| 197 |
} |
| 198 |
|
| 199 |
string Str(string value) { |
| 200 |
if (!string.IsNullOrWhiteSpace (Replace)) |
| 201 |
return value.Replace (Replace, With); |
| 202 |
return value; |
| 203 |
} |
| 204 |
|
| 205 |
public override string ToString () => Str(this.Name); |
| 206 |
|
| 207 |
class EmptyFrameworkEntry : FrameworkEntry |
| 208 |
{ |
| 209 |
public EmptyFrameworkEntry () : base (null, 1, null) { } |
| 210 |
public override FrameworkTypeEntry ProcessType (TypeDefinition type) { return FrameworkTypeEntry.Empty; } |
| 211 |
|
| 212 |
|
| 213 |
} |
| 214 |
} |
| 215 |
} |