| | | 1 | | using System.Reflection; |
| | | 2 | | using System.Text; |
| | | 3 | | |
| | | 4 | | namespace ClutterStock.Api.Extensions; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Reads version info baked in at publish (<c>InformationalVersion</c>, <c>SourceRevisionId</c>). |
| | | 8 | | /// </summary> |
| | | 9 | | internal static class ApiBuildMetadata |
| | | 10 | | { |
| | | 11 | | public static string BuildOpenApiDescription(string introduction) |
| | | 12 | | { |
| | 3 | 13 | | var asm = Assembly.GetExecutingAssembly(); |
| | 3 | 14 | | var informational = |
| | 3 | 15 | | asm.GetCustomAttribute<AssemblyInformationalVersionAttribute>() |
| | 3 | 16 | | ?.InformationalVersion; |
| | | 17 | | |
| | 3 | 18 | | var (semver, commitFromInfo) = ParseInformationalVersion(informational); |
| | 3 | 19 | | var commit = !string.IsNullOrWhiteSpace(commitFromInfo) |
| | 3 | 20 | | ? commitFromInfo |
| | 3 | 21 | | : GetSourceRevisionId(asm); |
| | | 22 | | |
| | 3 | 23 | | var sb = new StringBuilder(); |
| | 3 | 24 | | sb.AppendLine(introduction.TrimEnd()); |
| | 3 | 25 | | sb.AppendLine(); |
| | 3 | 26 | | sb.AppendLine("---"); |
| | 3 | 27 | | sb.AppendLine(); |
| | | 28 | | |
| | 3 | 29 | | if (!string.IsNullOrWhiteSpace(semver)) |
| | | 30 | | { |
| | 3 | 31 | | sb.Append("**Version:** `"); |
| | 3 | 32 | | sb.Append(semver); |
| | 3 | 33 | | sb.AppendLine("`"); |
| | | 34 | | } |
| | 0 | 35 | | else if (!string.IsNullOrWhiteSpace(informational)) |
| | | 36 | | { |
| | 0 | 37 | | sb.Append("**Version:** `"); |
| | 0 | 38 | | sb.Append(informational); |
| | 0 | 39 | | sb.AppendLine("`"); |
| | | 40 | | } |
| | | 41 | | |
| | 3 | 42 | | if (!string.IsNullOrWhiteSpace(commit)) |
| | | 43 | | { |
| | 3 | 44 | | sb.Append("**Git SHA:** `"); |
| | 3 | 45 | | sb.Append(commit); |
| | 3 | 46 | | sb.AppendLine("`"); |
| | | 47 | | } |
| | | 48 | | |
| | 3 | 49 | | return sb.ToString() |
| | 3 | 50 | | .Replace("\r\n", "\n") |
| | 3 | 51 | | .TrimEnd(); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | private static (string SemVer, string? Commit) ParseInformationalVersion(string? informational) |
| | | 55 | | { |
| | 3 | 56 | | if (string.IsNullOrWhiteSpace(informational)) |
| | 0 | 57 | | return ("", null); |
| | | 58 | | |
| | 3 | 59 | | var trimmed = informational.Trim(); |
| | 3 | 60 | | var plus = trimmed.IndexOf('+'); |
| | 3 | 61 | | if (plus < 0) |
| | 0 | 62 | | return (trimmed, null); |
| | | 63 | | |
| | 3 | 64 | | var semver = trimmed[..plus] |
| | 3 | 65 | | .Trim(); |
| | | 66 | | |
| | 3 | 67 | | var commit = trimmed[(plus + 1)..] |
| | 3 | 68 | | .Trim(); |
| | | 69 | | |
| | 3 | 70 | | return (semver, string.IsNullOrEmpty(commit) ? null : commit); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | private static string? GetSourceRevisionId(Assembly asm) |
| | | 74 | | { |
| | 0 | 75 | | foreach (var meta in asm.GetCustomAttributes<AssemblyMetadataAttribute>()) |
| | | 76 | | { |
| | 0 | 77 | | if (string.Equals(meta.Key, "SourceRevisionId", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 78 | | return string.IsNullOrWhiteSpace(meta.Value) ? null : meta.Value.Trim(); |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | return null; |
| | 0 | 82 | | } |
| | | 83 | | } |