|
| 1 | +using System.Text.Json; |
| 2 | +using System.Text.Json.Serialization; |
| 3 | + |
| 4 | +namespace XafLogicExplainer.Core.Catalog; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// What the XAF framework itself provides: its attributes, controllers, model interfaces and modules. |
| 8 | +/// </summary> |
| 9 | +/// <remarks> |
| 10 | +/// Extraction reads an application's source without knowing anything about the framework it is |
| 11 | +/// written against. That makes one question unanswerable: is <c>DeleteObjectsViewController</c> |
| 12 | +/// something this team wrote, or something DevExpress ships? Without an answer, generated |
| 13 | +/// documentation presents framework behavior and the application's own logic as though they were |
| 14 | +/// the same thing — and an agent asked to change the former will happily try. |
| 15 | +/// <para> |
| 16 | +/// The catalog supplies that ground truth. It is generated locally by a licensee from their own |
| 17 | +/// DevExpress installation (see <c>xaflogic catalog build</c>) and written outside the repository. |
| 18 | +/// <strong>Nothing derived from DevExpress is distributed with this project.</strong> |
| 19 | +/// </para> |
| 20 | +/// <para> |
| 21 | +/// It is entirely optional. With no catalog present, extraction behaves exactly as it did before |
| 22 | +/// one existed. |
| 23 | +/// </para> |
| 24 | +/// </remarks> |
| 25 | +public sealed class XafCatalog |
| 26 | +{ |
| 27 | + /// <summary>Version of the DevExpress installation this was generated from, e.g. "26.1".</summary> |
| 28 | + public string DevExpressVersion { get; init; } = string.Empty; |
| 29 | + |
| 30 | + /// <summary>When it was generated, in round-trip format.</summary> |
| 31 | + public string GeneratedAt { get; init; } = string.Empty; |
| 32 | + |
| 33 | + /// <summary>Assemblies that were read.</summary> |
| 34 | + public List<string> Assemblies { get; init; } = []; |
| 35 | + |
| 36 | + /// <summary>XAF attributes, keyed by simple type name.</summary> |
| 37 | + public Dictionary<string, XafCatalogType> Attributes { get; init; } = []; |
| 38 | + |
| 39 | + /// <summary>Framework controllers, keyed by simple type name.</summary> |
| 40 | + public Dictionary<string, XafCatalogType> Controllers { get; init; } = []; |
| 41 | + |
| 42 | + /// <summary>Application Model interfaces, keyed by simple type name.</summary> |
| 43 | + public Dictionary<string, XafCatalogType> ModelInterfaces { get; init; } = []; |
| 44 | + |
| 45 | + /// <summary>Framework modules, keyed by simple type name.</summary> |
| 46 | + public Dictionary<string, XafCatalogType> Modules { get; init; } = []; |
| 47 | + |
| 48 | + /// <summary>Total number of framework types recorded.</summary> |
| 49 | + [JsonIgnore] |
| 50 | + public int TypeCount => |
| 51 | + Attributes.Count + Controllers.Count + ModelInterfaces.Count + Modules.Count; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Looks up an attribute by the name as written in source, with or without the suffix. |
| 55 | + /// </summary> |
| 56 | + /// <remarks> |
| 57 | + /// C# lets <c>[Description]</c> stand for <c>DescriptionAttribute</c>, and source is written |
| 58 | + /// both ways, so both must resolve. |
| 59 | + /// </remarks> |
| 60 | + /// <param name="attributeName">Name as it appears in the attribute list.</param> |
| 61 | + public XafCatalogType? FindAttribute(string? attributeName) |
| 62 | + { |
| 63 | + if (string.IsNullOrWhiteSpace(attributeName)) |
| 64 | + return null; |
| 65 | + |
| 66 | + var name = attributeName.Trim(); |
| 67 | + |
| 68 | + if (Attributes.TryGetValue(name, out var exact)) |
| 69 | + return exact; |
| 70 | + |
| 71 | + return Attributes.TryGetValue(name + "Attribute", out var suffixed) ? suffixed : null; |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Looks up a controller by name, ignoring any generic arguments. |
| 76 | + /// </summary> |
| 77 | + /// <param name="typeName">A base type as written in source, e.g. <c>ViewController<DetailView></c>.</param> |
| 78 | + public XafCatalogType? FindController(string? typeName) |
| 79 | + { |
| 80 | + if (string.IsNullOrWhiteSpace(typeName)) |
| 81 | + return null; |
| 82 | + |
| 83 | + var name = typeName.Trim(); |
| 84 | + |
| 85 | + var generic = name.IndexOf('<'); |
| 86 | + if (generic > 0) |
| 87 | + name = name[..generic]; |
| 88 | + |
| 89 | + var lastDot = name.LastIndexOf('.'); |
| 90 | + if (lastDot >= 0) |
| 91 | + name = name[(lastDot + 1)..]; |
| 92 | + |
| 93 | + return Controllers.TryGetValue(name, out var controller) ? controller : null; |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary>Serialization settings shared by the generator and the loader.</summary> |
| 97 | + public static JsonSerializerOptions JsonOptions { get; } = new() |
| 98 | + { |
| 99 | + WriteIndented = true, |
| 100 | + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| 101 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| 102 | + PropertyNameCaseInsensitive = true, |
| 103 | + }; |
| 104 | + |
| 105 | + /// <summary>Serializes the catalog.</summary> |
| 106 | + public string ToJson() => JsonSerializer.Serialize(this, JsonOptions); |
| 107 | + |
| 108 | + /// <summary>Deserializes a catalog, returning null when the text is not one.</summary> |
| 109 | + /// <param name="json">Catalog JSON.</param> |
| 110 | + public static XafCatalog? FromJson(string json) |
| 111 | + { |
| 112 | + try |
| 113 | + { |
| 114 | + return JsonSerializer.Deserialize<XafCatalog>(json, JsonOptions); |
| 115 | + } |
| 116 | + catch (JsonException) |
| 117 | + { |
| 118 | + return null; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/// <summary> |
| 124 | +/// One type the XAF framework provides. |
| 125 | +/// </summary> |
| 126 | +public sealed class XafCatalogType |
| 127 | +{ |
| 128 | + /// <summary>Simple type name, e.g. <c>DeleteObjectsViewController</c>.</summary> |
| 129 | + public string Name { get; init; } = string.Empty; |
| 130 | + |
| 131 | + /// <summary>Namespace it lives in.</summary> |
| 132 | + public string Namespace { get; init; } = string.Empty; |
| 133 | + |
| 134 | + /// <summary>Assembly it ships in, without the version suffix.</summary> |
| 135 | + public string Assembly { get; init; } = string.Empty; |
| 136 | + |
| 137 | + /// <summary>Immediate base type, for controllers.</summary> |
| 138 | + public string? BaseType { get; init; } |
| 139 | + |
| 140 | + /// <summary>First sentence of the official documentation, when the XML docs supplied one.</summary> |
| 141 | + public string? Summary { get; init; } |
| 142 | + |
| 143 | + /// <summary>Official documentation URL, when the XML docs linked one.</summary> |
| 144 | + public string? DocumentationUrl { get; init; } |
| 145 | + |
| 146 | + /// <summary>Whether the type is abstract, which for a controller means it is meant to be derived from.</summary> |
| 147 | + public bool IsAbstract { get; init; } |
| 148 | + |
| 149 | + /// <summary>Full name, for display.</summary> |
| 150 | + [JsonIgnore] |
| 151 | + public string FullName => string.IsNullOrEmpty(Namespace) ? Name : $"{Namespace}.{Name}"; |
| 152 | +} |
0 commit comments