| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="NamingConventionHelpers.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | |
| | | 9 | | namespace MyNet.UI.Locators.Conventions; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides helper methods for naming conventions used in view and view model resolution, such as removing common suffi |
| | | 13 | | /// </summary> |
| | | 14 | | internal static class NamingConventionHelpers |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Gets the list of recognized view suffixes used for type resolution. |
| | | 18 | | /// </summary> |
| | 3 | 19 | | internal static readonly string[] ViewSuffixes = |
| | 3 | 20 | | [ |
| | 3 | 21 | | "View", "Control", "UserControl", "Page", "Activity", "Window", "Fragment" |
| | 3 | 22 | | ]; |
| | | 23 | | |
| | 3 | 24 | | private static readonly string[] ViewModelSuffixes = |
| | 3 | 25 | | [ |
| | 3 | 26 | | "ViewModel" |
| | 3 | 27 | | ]; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Removes any of the specified suffixes from the end of the given name, if present. Comparison is case-insensitive |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="name">The name from which to remove the suffix.</param> |
| | | 33 | | /// <param name="suffixes">An array of suffixes to remove.</param> |
| | | 34 | | /// <returns>The name without the suffix if a match was found; otherwise, the original name.</returns> |
| | | 35 | | public static string RemoveSuffix(string name, string[] suffixes) |
| | | 36 | | { |
| | 318 | 37 | | foreach (var suffix in suffixes) |
| | | 38 | | { |
| | 123 | 39 | | if (name.EndsWith(suffix, StringComparison.OrdinalIgnoreCase)) |
| | 54 | 40 | | return name[..^suffix.Length]; |
| | | 41 | | } |
| | | 42 | | |
| | 9 | 43 | | return name; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Removes common view model suffixes from the given type name to derive the base name for view resolution. This me |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="type">The type from which to derive the base name.</param> |
| | | 50 | | /// <returns>The base name without the view model suffix.</returns> |
| | 24 | 51 | | public static string GetBaseNameFromViewModel(Type type) => RemoveSuffix(type.Name, ViewModelSuffixes); |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Removes common view suffixes from the given type name to derive the base name for view model resolution. This me |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="type">The type from which to derive the base name.</param> |
| | | 57 | | /// <returns>The base name without the view suffix.</returns> |
| | 39 | 58 | | public static string GetBaseNameFromView(Type type) => RemoveSuffix(type.Name, ViewSuffixes); |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Determines whether the specified type is a view type based on its name ending with one of the recognized view su |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="type">The type to check.</param> |
| | | 64 | | /// <returns><see langword="true"/> if the type is a view type; otherwise, <see langword="false"/>.</returns> |
| | 24 | 65 | | public static bool IsViewType(Type type) => GetBaseNameFromView(type) != type.Name; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Determines whether the specified type is a view model type (name ends with <c>ViewModel</c>). |
| | | 69 | | /// </summary> |
| | | 70 | | public static bool IsViewModelType(Type type) => |
| | 48 | 71 | | type.Name.EndsWith("ViewModel", StringComparison.Ordinal); |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Loads a type from the same assembly as <paramref name="source"/> using its fully qualified name. |
| | | 75 | | /// </summary> |
| | | 76 | | public static Type? GetAssemblyType(Type source, string fullTypeName) => |
| | 42 | 77 | | source.Assembly.GetType(fullTypeName); |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Replaces exact namespace segments (dot-separated) with a new value. Only whole segments are matched. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="ns">The original namespace.</param> |
| | | 83 | | /// <param name="from">The segment to replace.</param> |
| | | 84 | | /// <param name="to">The new segment.</param> |
| | | 85 | | /// <returns>The modified namespace, or the original when no segment matches.</returns> |
| | | 86 | | public static string? ReplaceNamespaceSegment(string? ns, string from, string to) |
| | | 87 | | { |
| | 39 | 88 | | if (string.IsNullOrEmpty(ns)) |
| | 3 | 89 | | return ns; |
| | | 90 | | |
| | 36 | 91 | | var segments = ns.Split('.'); |
| | 36 | 92 | | var replaced = false; |
| | | 93 | | |
| | 348 | 94 | | for (var i = 0; i < segments.Length; i++) |
| | | 95 | | { |
| | 138 | 96 | | if (!segments[i].Equals(from, StringComparison.Ordinal)) |
| | | 97 | | continue; |
| | | 98 | | |
| | 36 | 99 | | segments[i] = to; |
| | 36 | 100 | | replaced = true; |
| | | 101 | | } |
| | | 102 | | |
| | 36 | 103 | | return replaced ? string.Join('.', segments) : ns; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Gets the parent namespace of the given namespace. |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="ns">The namespace.</param> |
| | | 110 | | /// <returns>The parent namespace if it exists; otherwise, the original namespace.</returns> |
| | | 111 | | public static string? GetParentNamespace(string? ns) |
| | | 112 | | { |
| | 6 | 113 | | if (string.IsNullOrWhiteSpace(ns)) |
| | 0 | 114 | | return ns; |
| | | 115 | | |
| | 6 | 116 | | var lastDot = ns.LastIndexOf('.'); |
| | 6 | 117 | | return lastDot > 0 ? ns[..lastDot] : ns; |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |