From c879f25bf483086725c8a29f104555644e6ee542 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Tue, 18 Jun 2024 01:37:59 -0300 Subject: [PATCH] Remove dependency on ThisAssembly Simplifying the approach to leverage just SDK features makes it easier to consume and apply across projects that might not use ThisAssembly (or have version incompatibilities). --- src/SponsorLink/Analyzer/Analyzer.csproj | 4 + .../Analyzer/StatusReportingAnalyzer.cs | 1 - src/SponsorLink/SponsorLink.targets | 36 +++++-- .../SponsorLink/DiagnosticsManager.cs | 25 ++--- ...{SponsorLink.es.resx => Resources.es.resx} | 6 +- .../{SponsorLink.resx => Resources.resx} | 6 +- .../SponsorLink/SponsorLink.csproj | 38 ++++--- .../SponsorLink/SponsorLinkAnalyzer.cs | 9 +- src/SponsorLink/SponsorLink/ThisAssembly.cs | 31 ------ src/SponsorLink/Tests/Resources.Designer.cs | 63 +++++++++++ src/SponsorLink/Tests/Resources.resx | 101 ++++++++++++++++++ src/SponsorLink/Tests/Sample.cs | 2 +- src/SponsorLink/Tests/Tests.csproj | 15 +++ 13 files changed, 261 insertions(+), 76 deletions(-) rename src/SponsorLink/SponsorLink/{SponsorLink.es.resx => Resources.es.resx} (95%) rename src/SponsorLink/SponsorLink/{SponsorLink.resx => Resources.resx} (96%) delete mode 100644 src/SponsorLink/SponsorLink/ThisAssembly.cs create mode 100644 src/SponsorLink/Tests/Resources.Designer.cs create mode 100644 src/SponsorLink/Tests/Resources.resx diff --git a/src/SponsorLink/Analyzer/Analyzer.csproj b/src/SponsorLink/Analyzer/Analyzer.csproj index 963c77b..d1df1ad 100644 --- a/src/SponsorLink/Analyzer/Analyzer.csproj +++ b/src/SponsorLink/Analyzer/Analyzer.csproj @@ -29,4 +29,8 @@ + + + + \ No newline at end of file diff --git a/src/SponsorLink/Analyzer/StatusReportingAnalyzer.cs b/src/SponsorLink/Analyzer/StatusReportingAnalyzer.cs index e21acb7..ad82ed3 100644 --- a/src/SponsorLink/Analyzer/StatusReportingAnalyzer.cs +++ b/src/SponsorLink/Analyzer/StatusReportingAnalyzer.cs @@ -3,7 +3,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using static Devlooped.Sponsors.SponsorLink; -using static ThisAssembly.Constants; namespace Analyzer; diff --git a/src/SponsorLink/SponsorLink.targets b/src/SponsorLink/SponsorLink.targets index a604122..f2ab5c2 100644 --- a/src/SponsorLink/SponsorLink.targets +++ b/src/SponsorLink/SponsorLink.targets @@ -9,6 +9,8 @@ true true + + CoreResGen;$(CoreCompileDependsOn) $(Product) @@ -18,12 +20,6 @@ 21 - - - - - - + ManifestResourceName="Devlooped.Sponsors.%(Filename)"/> + @@ -81,6 +82,27 @@ + + + namespace Devlooped.Sponsors%3B + +partial class SponsorLink +{ + public partial class Funding + { + public const string Product = "$(FundingProduct)"%3B + public const string Prefix = "$(FundingPrefix)"%3B + public const int Grace = $(FundingGrace)%3B + } +} + + + + + + + + new( $"{prefix}100", - ThisAssembly.Strings.Sponsor.Title, - ThisAssembly.Strings.Sponsor.MessageFormat, + Resources.Sponsor_Title, + Resources.Sponsor_Message, "SponsorLink", DiagnosticSeverity.Info, isEnabledByDefault: true, - description: ThisAssembly.Strings.Sponsor.Description, + description: Resources.Sponsor_Description, helpLinkUri: "https://github.com/devlooped#sponsorlink", "DoesNotSupportF1Help"); static DiagnosticDescriptor CreateUnknown(string[] sponsorable, string product, string prefix) => new( $"{prefix}101", - ThisAssembly.Strings.Unknown.Title, - ThisAssembly.Strings.Unknown.MessageFormat, + Resources.Unknown_Title, + Resources.Unknown_Message, "SponsorLink", DiagnosticSeverity.Warning, isEnabledByDefault: true, - description: ThisAssembly.Strings.Unknown.Description( + description: string.Format(CultureInfo.CurrentCulture, Resources.Unknown_Description, sponsorable.Humanize(x => $"https://github.com/sponsors/{x}"), string.Join(" ", sponsorable)), helpLinkUri: "https://github.com/devlooped#sponsorlink", @@ -116,23 +117,23 @@ public Diagnostic Push(string product, Diagnostic diagnostic) static DiagnosticDescriptor CreateExpiring(string[] sponsorable, string prefix) => new( $"{prefix}103", - ThisAssembly.Strings.Expiring.Title, - ThisAssembly.Strings.Expiring.MessageFormat, + Resources.Expiring_Title, + Resources.Expiring_Message, "SponsorLink", DiagnosticSeverity.Warning, isEnabledByDefault: true, - description: ThisAssembly.Strings.Expiring.Description(string.Join(" ", sponsorable)), + description: string.Format(CultureInfo.CurrentCulture, Resources.Expiring_Description, string.Join(" ", sponsorable)), helpLinkUri: "https://github.com/devlooped#autosync", "DoesNotSupportF1Help", WellKnownDiagnosticTags.NotConfigurable); static DiagnosticDescriptor CreateExpired(string[] sponsorable, string prefix) => new( $"{prefix}104", - ThisAssembly.Strings.Expired.Title, - ThisAssembly.Strings.Expired.MessageFormat, + Resources.Expired_Title, + Resources.Expired_Message, "SponsorLink", DiagnosticSeverity.Warning, isEnabledByDefault: true, - description: ThisAssembly.Strings.Expired.Description(string.Join(" ", sponsorable)), + description: string.Format(CultureInfo.CurrentCulture, Resources.Expired_Description, string.Join(" ", sponsorable)), helpLinkUri: "https://github.com/devlooped#autosync", "DoesNotSupportF1Help", WellKnownDiagnosticTags.NotConfigurable); } diff --git a/src/SponsorLink/SponsorLink/SponsorLink.es.resx b/src/SponsorLink/SponsorLink/Resources.es.resx similarity index 95% rename from src/SponsorLink/SponsorLink/SponsorLink.es.resx rename to src/SponsorLink/SponsorLink/Resources.es.resx index 1266993..ec1b5c1 100644 --- a/src/SponsorLink/SponsorLink/SponsorLink.es.resx +++ b/src/SponsorLink/SponsorLink/Resources.es.resx @@ -119,7 +119,7 @@ Patrocinar los proyectos en que dependes asegura que se mantengan activos, y que recibas el apoyo que necesitas. También es muy económico y está disponible en todo el mundo! -Por favor considera apoyar el proyecto patrocinando en {links} y ejecutando posteriormente 'sponsor sync {spaced}'. +Por favor considera apoyar el proyecto patrocinando en {0} y ejecutando posteriormente 'sponsor sync {1}'. Por favor considere apoyar {0} patrocinando @{1} 🙏 @@ -128,7 +128,7 @@ Por favor considera apoyar el proyecto patrocinando en {links} y ejecutando post Estado de patrocinio desconocido - Funcionalidades exclusivas para patrocinadores pueden no estar disponibles. Ejecuta 'sponsor sync {spaced}' y, opcionalmente, habilita la sincronización automática. + Funcionalidades exclusivas para patrocinadores pueden no estar disponibles. Ejecuta 'sponsor sync {0}' y, opcionalmente, habilita la sincronización automática. El estado de patrocino ha expirado y la sincronización automática no está habilitada. @@ -146,7 +146,7 @@ Por favor considera apoyar el proyecto patrocinando en {links} y ejecutando post Eres un patrocinador del proyecto, eres lo máximo 💟! - El estado de patrocino ha expirado y estás en un período de gracia. Ejecuta 'sponsor sync {spaced}' y, opcionalmente, habilita la sincronización automática. + El estado de patrocino ha expirado y estás en un período de gracia. Ejecuta 'sponsor sync {0}' y, opcionalmente, habilita la sincronización automática. El estado de patrocino necesita actualización periódica y la sincronización automática no está habilitada. diff --git a/src/SponsorLink/SponsorLink/SponsorLink.resx b/src/SponsorLink/SponsorLink/Resources.resx similarity index 96% rename from src/SponsorLink/SponsorLink/SponsorLink.resx rename to src/SponsorLink/SponsorLink/Resources.resx index 67000f3..e12a0e5 100644 --- a/src/SponsorLink/SponsorLink/SponsorLink.resx +++ b/src/SponsorLink/SponsorLink/Resources.resx @@ -119,7 +119,7 @@ Sponsoring projects you depend on ensures they remain active, and that you get the support you need. It's also super affordable and available worldwide! -Please consider supporting the project by sponsoring at {links} and running 'sponsor sync {spaced}' afterwards. +Please consider supporting the project by sponsoring at {0} and running 'sponsor sync {1}' afterwards. Unknown sponsor description @@ -129,7 +129,7 @@ Please consider supporting the project by sponsoring at {links} and running 'spo Unknown sponsor status - Sponsor-only features may be disabled. Please run 'sponsor sync {spaced}' and optionally enable automatic sync. + Sponsor-only features may be disabled. Please run 'sponsor sync {0}' and optionally enable automatic sync. Sponsor status has expired and automatic sync has not been enabled. @@ -147,7 +147,7 @@ Please consider supporting the project by sponsoring at {links} and running 'spo You are a sponsor of the project, you rock 💟! - Sponsor status has expired and you are in the grace period. Please run 'sponsor sync {spaced}' and optionally enable automatic sync. + Sponsor status has expired and you are in the grace period. Please run 'sponsor sync {0}' and optionally enable automatic sync. Sponsor status needs periodic updating and automatic sync has not been enabled. diff --git a/src/SponsorLink/SponsorLink/SponsorLink.csproj b/src/SponsorLink/SponsorLink/SponsorLink.csproj index 6b6c11e..824353d 100644 --- a/src/SponsorLink/SponsorLink/SponsorLink.csproj +++ b/src/SponsorLink/SponsorLink/SponsorLink.csproj @@ -5,6 +5,7 @@ SponsorLink disable false + CoreResGen;$(CoreCompileDependsOn) @@ -24,30 +25,41 @@ - - - - - - - - - - - - + + + + + namespace Devlooped.Sponsors%3B + +partial class SponsorLink +{ + public partial class Funding + { + public const string Product = "$(FundingProduct)"%3B + public const string Prefix = "$(FundingPrefix)"%3B + public const int Grace = $(FundingGrace)%3B + } +} + + + + + + + + - + diff --git a/src/SponsorLink/SponsorLink/SponsorLinkAnalyzer.cs b/src/SponsorLink/SponsorLink/SponsorLinkAnalyzer.cs index 2e97528..3de4d3e 100644 --- a/src/SponsorLink/SponsorLink/SponsorLinkAnalyzer.cs +++ b/src/SponsorLink/SponsorLink/SponsorLinkAnalyzer.cs @@ -7,10 +7,10 @@ using System.IO; using System.Linq; using Humanizer; +using Humanizer.Localisation; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using static Devlooped.Sponsors.SponsorLink; -using static ThisAssembly.Constants; namespace Devlooped.Sponsors; @@ -20,7 +20,6 @@ namespace Devlooped.Sponsors; [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] public class SponsorLinkAnalyzer : DiagnosticAnalyzer { - static readonly int graceDays = int.Parse(Funding.Grace); static readonly Dictionary descriptors = new() { // Requires: @@ -76,7 +75,7 @@ public override void Initialize(AnalysisContext context) // We'll report it as unknown as a fallback for now. ctx.ReportDiagnostic(Diagnostic.Create(descriptors[SponsorStatus.Unknown], null, properties: ImmutableDictionary.Create().Add(nameof(SponsorStatus), nameof(SponsorStatus.Unknown)), - Funding.Product, Sponsorables.Keys.Humanize(ThisAssembly.Strings.Or))); + Funding.Product, Sponsorables.Keys.Humanize(Resources.Or))); } }); } @@ -93,13 +92,13 @@ SponsorStatus SetStatus(ImmutableArray manifests) // report unknown, either unparsed manifest or one with no expiration (which we never emit). Diagnostics.Push(Funding.Product, Diagnostic.Create(descriptors[SponsorStatus.Unknown], null, properties: ImmutableDictionary.Create().Add(nameof(SponsorStatus), nameof(SponsorStatus.Unknown)), - Funding.Product, Sponsorables.Keys.Humanize(ThisAssembly.Strings.Or))); + Funding.Product, Sponsorables.Keys.Humanize(Resources.Or))); return SponsorStatus.Unknown; } else if (exp < DateTime.Now) { // report expired or expiring soon if still within the configured days of grace period - if (exp.AddDays(graceDays) < DateTime.Now) + if (exp.AddDays(Funding.Grace) < DateTime.Now) { // report expiring soon Diagnostics.Push(Funding.Product, Diagnostic.Create(descriptors[SponsorStatus.Expiring], null, diff --git a/src/SponsorLink/SponsorLink/ThisAssembly.cs b/src/SponsorLink/SponsorLink/ThisAssembly.cs deleted file mode 100644 index 89f2316..0000000 --- a/src/SponsorLink/SponsorLink/ThisAssembly.cs +++ /dev/null @@ -1,31 +0,0 @@ -// -partial class ThisAssembly -{ - partial class Strings - { - partial class Unknown - { - public static string MessageFormat => GetResourceManager("Devlooped.SponsorLink").GetString("Unknown_Message"); - } - - partial class Expiring - { - public static string MessageFormat => GetResourceManager("Devlooped.SponsorLink").GetString("Expiring_Message"); - } - - partial class Expired - { - public static string MessageFormat => GetResourceManager("Devlooped.SponsorLink").GetString("Expired_Message"); - } - - partial class Grace - { - public static string MessageFormat => GetResourceManager("Devlooped.SponsorLink").GetString("Grace_Message"); - } - - partial class Sponsor - { - public static string MessageFormat => GetResourceManager("Devlooped.SponsorLink").GetString("Sponsor_Message"); - } - } -} \ No newline at end of file diff --git a/src/SponsorLink/Tests/Resources.Designer.cs b/src/SponsorLink/Tests/Resources.Designer.cs new file mode 100644 index 0000000..7824a60 --- /dev/null +++ b/src/SponsorLink/Tests/Resources.Designer.cs @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Tests { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Tests.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/src/SponsorLink/Tests/Resources.resx b/src/SponsorLink/Tests/Resources.resx new file mode 100644 index 0000000..4fdb1b6 --- /dev/null +++ b/src/SponsorLink/Tests/Resources.resx @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/src/SponsorLink/Tests/Sample.cs b/src/SponsorLink/Tests/Sample.cs index 6249e62..897c91c 100644 --- a/src/SponsorLink/Tests/Sample.cs +++ b/src/SponsorLink/Tests/Sample.cs @@ -40,7 +40,7 @@ public void Test(string culture, SponsorStatus kind) public void RenderSponsorables() { Assert.NotEmpty(SponsorLink.Sponsorables); - + foreach (var pair in SponsorLink.Sponsorables) { output.WriteLine($"{pair.Key} = {pair.Value}"); diff --git a/src/SponsorLink/Tests/Tests.csproj b/src/SponsorLink/Tests/Tests.csproj index f753aad..0585911 100644 --- a/src/SponsorLink/Tests/Tests.csproj +++ b/src/SponsorLink/Tests/Tests.csproj @@ -23,6 +23,21 @@ + + + True + True + Resources.resx + + + + + + ResXFileCodeGenerator + Resources.Designer.cs + + +