|
1 | | -using Microsoft.CodeAnalysis; |
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Globalization; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using Devlooped.Sponsors; |
| 6 | +using Microsoft.CodeAnalysis; |
2 | 7 | using Microsoft.Extensions.DependencyInjection; |
| 8 | +using static Devlooped.Sponsors.SponsorLink; |
3 | 9 |
|
4 | 10 | namespace Devlooped.Extensions.DependencyInjection; |
5 | 11 |
|
@@ -30,8 +36,93 @@ public void Execute(GeneratorExecutionContext context) |
30 | 36 | var className = context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.AddServicesClassName", out value) && !string.IsNullOrEmpty(value) ? |
31 | 37 | value : DefaultAddServicesClass; |
32 | 38 |
|
33 | | - context.AddSource(DefaultAddServicesClass + ".g", ThisAssembly.Resources.AddServicesNoReflectionExtension.Text |
34 | | - .Replace("namespace " + DefaultNamespace, "namespace " + rootNs) |
35 | | - .Replace(DefaultAddServicesClass, className)); |
| 39 | + var code = ThisAssembly.Resources.AddServicesNoReflectionExtension.Text |
| 40 | + .Replace("namespace " + DefaultNamespace, "namespace " + rootNs) |
| 41 | + .Replace(DefaultAddServicesClass, className); |
| 42 | + |
| 43 | + if (IsEditor) |
| 44 | + { |
| 45 | + var status = Diagnostics.GetOrSetStatus(context.GetStatusOptions()); |
| 46 | + string? remarks = default; |
| 47 | + string? warn = default; |
| 48 | + |
| 49 | + if (status == SponsorStatus.Unknown || status == SponsorStatus.Expired) |
| 50 | + { |
| 51 | + warn = |
| 52 | + $""" |
| 53 | + [Obsolete("{string.Format(CultureInfo.CurrentCulture, Resources.Editor_Disabled, Funding.Product, Funding.HelpUrl)}", false |
| 54 | + #if NET6_0_OR_GREATER |
| 55 | + , UrlFormat = "{Funding.HelpUrl}" |
| 56 | + #endif |
| 57 | + )] |
| 58 | + """; |
| 59 | + |
| 60 | + remarks = Resources.Editor_DisabledRemarks; |
| 61 | + } |
| 62 | + else if (status == SponsorStatus.Grace && Diagnostics.TryGet() is { } grace && grace.Properties.TryGetValue(nameof(SponsorStatus.Grace), out var days)) |
| 63 | + { |
| 64 | + remarks = string.Format(CultureInfo.CurrentCulture, Resources.Editor_GraceRemarks, days); |
| 65 | + } |
| 66 | + |
| 67 | + if (remarks != null) |
| 68 | + { |
| 69 | + // Remove /// <remarks> and /// </remarks> LINES from the remarks string |
| 70 | + var builder = new StringBuilder(); |
| 71 | + foreach (var line in ReadLines(remarks)) |
| 72 | + { |
| 73 | + if (line.EndsWith("/// <remarks>") || line.EndsWith("/// </remarks>")) |
| 74 | + continue; |
| 75 | + if (line.TrimStart() is { Length: > 0 } trimmed && trimmed.StartsWith("///")) |
| 76 | + builder.AppendLine(trimmed); |
| 77 | + } |
| 78 | + remarks = builder.AppendLine("///").ToString(); |
| 79 | + } |
| 80 | + |
| 81 | + if (remarks != null || warn != null) |
| 82 | + { |
| 83 | + var builder = new StringBuilder(); |
| 84 | + foreach (var line in ReadLines(code)) |
| 85 | + { |
| 86 | + if (remarks != null && line.EndsWith("/// <remarks>")) |
| 87 | + { |
| 88 | + builder.AppendLine(line); |
| 89 | + // trim the remarks line to remove leading spaces and |
| 90 | + // replace them with the indenting from the target code line |
| 91 | + var indent = line.IndexOf("/// <remarks>"); |
| 92 | + foreach (var rline in ReadLines(remarks)) |
| 93 | + { |
| 94 | + builder.Append(new string(' ', indent)).AppendLine(rline); |
| 95 | + } |
| 96 | + } |
| 97 | + else if (warn != null && line.EndsWith("[DDIAddServices]")) |
| 98 | + { |
| 99 | + builder.AppendLine(line); |
| 100 | + // trim the remarks line to remove leading spaces and |
| 101 | + // replace them with the indenting from the target code line |
| 102 | + var indent = line.IndexOf("[DDIAddServices]"); |
| 103 | + // append indentation and the warning, also splitting lines and trimming start |
| 104 | + foreach (var wline in ReadLines(warn)) |
| 105 | + { |
| 106 | + builder.Append(new string(' ', indent)).AppendLine(wline.TrimStart()); |
| 107 | + } |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + builder.AppendLine(line); |
| 112 | + } |
| 113 | + } |
| 114 | + code = builder.ToString(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + context.AddSource(DefaultAddServicesClass + ".g", code); |
| 119 | + } |
| 120 | + |
| 121 | + static IEnumerable<string> ReadLines(string text) |
| 122 | + { |
| 123 | + using var reader = new StringReader(text); |
| 124 | + string? line; |
| 125 | + while ((line = reader.ReadLine()) != null) |
| 126 | + yield return line; |
36 | 127 | } |
37 | 128 | } |
0 commit comments