| 1 |
using System; |
| 2 |
using System.Collections.Generic; |
| 3 |
using System.IO; |
| 4 |
using System.Linq; |
| 5 |
using System.Xml; |
| 6 |
using System.Xml.Linq; |
| 7 |
|
| 8 |
using Mono.Cecil; |
| 9 |
|
| 10 |
namespace Mono.Documentation.Updater.Frameworks |
| 11 |
{ |
| 12 |
|
| 13 |
public class FrameworkIndex |
| 14 |
{ |
| 15 |
List<FrameworkEntry> frameworks = new List<FrameworkEntry> (); |
| 16 |
IList<FrameworkEntry> cachedFrameworks; |
| 17 |
|
| 18 |
string path; |
| 19 |
|
| 20 |
public FrameworkIndex (string pathToFrameworks, int fxCount, IList<FrameworkEntry> cachedfx) |
| 21 |
{ |
| 22 |
path = pathToFrameworks; |
| 23 |
FrameworksCount = fxCount; |
| 24 |
cachedFrameworks = cachedfx ?? frameworks; |
| 25 |
} |
| 26 |
|
| 27 |
public int FrameworksCount { |
| 28 |
get; private set; |
| 29 |
} |
| 30 |
|
| 31 |
public IList<FrameworkEntry> Frameworks { |
| 32 |
get { |
| 33 |
return this.frameworks; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public FrameworkEntry StartProcessingAssembly (AssemblySet set, AssemblyDefinition assembly, IEnumerable<DocumentationImporter> importers, string Id, string Version) |
| 38 |
{ |
| 39 |
if (string.IsNullOrWhiteSpace (this.path)) |
| 40 |
{ |
| 41 |
set.Framework = FrameworkEntry.Empty; |
| 42 |
return FrameworkEntry.Empty; |
| 43 |
} |
| 44 |
|
| 45 |
string assemblyPath = assembly.MainModule.FileName; |
| 46 |
string shortPath = GetFrameworkNameFromPath (this.path, assemblyPath); |
| 47 |
|
| 48 |
var entry = frameworks.FirstOrDefault (f => f.Name.Equals (shortPath)); |
| 49 |
if (entry == null) |
| 50 |
{ |
| 51 |
entry = new FrameworkEntry (frameworks, FrameworksCount, cachedFrameworks) { Name = shortPath, Importers = importers, Id = Id, Version = Version }; |
| 52 |
frameworks.Add (entry); |
| 53 |
} |
| 54 |
|
| 55 |
set.Framework = entry; |
| 56 |
entry.AddProcessedAssembly (assembly); |
| 57 |
|
| 58 |
return entry; |
| 59 |
} |
| 60 |
|
| 61 |
public static string GetFrameworkNameFromPath (string rootpath, string assemblyPath) |
| 62 |
{ |
| 63 |
char otherSepChar = '/'; |
| 64 |
if (Path.DirectorySeparatorChar == '/') |
| 65 |
otherSepChar = '\\'; |
| 66 |
|
| 67 |
if (rootpath.Contains(otherSepChar)) |
| 68 |
rootpath = rootpath.Replace(otherSepChar, Path.DirectorySeparatorChar); |
| 69 |
|
| 70 |
if (assemblyPath.Contains(otherSepChar)) |
| 71 |
assemblyPath = assemblyPath.Replace(otherSepChar, Path.DirectorySeparatorChar); |
| 72 |
|
| 73 |
var frameworksDirectory = rootpath.EndsWith ("frameworks.xml", StringComparison.OrdinalIgnoreCase) |
| 74 |
? Path.GetDirectoryName (rootpath) : rootpath; |
| 75 |
string relativePath = assemblyPath.Replace (frameworksDirectory, string.Empty); |
| 76 |
string shortPath = Path.GetDirectoryName (relativePath); |
| 77 |
if (shortPath.StartsWith (Path.DirectorySeparatorChar.ToString (), StringComparison.InvariantCultureIgnoreCase)) |
| 78 |
shortPath = shortPath.Substring (1, shortPath.Length - 1); |
| 79 |
return shortPath; |
| 80 |
} |
| 81 |
|
| 82 |
/// <summary>Writes the framework indices to disk.</summary> |
| 83 |
/// <param name="path">The folder where one file for every FrameworkEntry will be written.</param> |
| 84 |
public void WriteToDisk (string path) |
| 85 |
{ |
| 86 |
if (string.IsNullOrWhiteSpace (this.path)) |
| 87 |
return; |
| 88 |
|
| 89 |
string outputPath = Path.Combine (path, Consts.FrameworksIndex); |
| 90 |
|
| 91 |
if (!Directory.Exists (outputPath)) |
| 92 |
Directory.CreateDirectory (outputPath); |
| 93 |
|
| 94 |
foreach (var fx in this.frameworks) |
| 95 |
{ |
| 96 |
XElement frameworkElement = new XElement("Framework", new XAttribute("Name", fx.Name)); |
| 97 |
XDocument doc = new XDocument( |
| 98 |
frameworkElement |
| 99 |
); |
| 100 |
if (fx.Version!=null && fx.Id!= null) |
| 101 |
{ |
| 102 |
frameworkElement.Add(new XElement("package", new XAttribute("Id", fx.Id), |
| 103 |
new XAttribute("Version", fx.Version) |
| 104 |
)); |
| 105 |
} |
| 106 |
if (fx.AssemblyNames.Any()) |
| 107 |
{ |
| 108 |
frameworkElement.Add ( |
| 109 |
new XElement ( |
| 110 |
"Assemblies", |
| 111 |
fx.AssemblyNames.Distinct().Select(an => |
| 112 |
new XElement("Assembly", |
| 113 |
new XAttribute("Name", an.Item1), |
| 114 |
new XAttribute("Version", an.Item2) |
| 115 |
)) |
| 116 |
)); |
| 117 |
} |
| 118 |
|
| 119 |
frameworkElement.Add(fx.Types.GroupBy(t => t.Namespace) |
| 120 |
.Select(g => new XElement("Namespace", |
| 121 |
new XAttribute("Name", g.Key), |
| 122 |
g.Select(t => new XElement("Type", |
| 123 |
new XAttribute("Name", t.Name), |
| 124 |
new XAttribute("Id", t.Id), |
| 125 |
t.Members.Select(m => |
| 126 |
new XElement("Member", |
| 127 |
new XAttribute("Id", m)))))))); |
| 128 |
// now save the document |
| 129 |
string filePath = Path.Combine (outputPath, fx.Name + ".xml"); |
| 130 |
|
| 131 |
MdocFile.DeleteFile (filePath); |
| 132 |
|
| 133 |
var settings = new XmlWriterSettings { Indent = true }; |
| 134 |
using (var writer = XmlWriter.Create (filePath, settings)) { |
| 135 |
doc.WriteTo (writer); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |