Skip to content

Commit

Permalink
Remove all code related to build pauses
Browse files Browse the repository at this point in the history
Closes #33
  • Loading branch information
kzu committed Aug 20, 2023
1 parent 82fb0a1 commit cbc52f5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 86 deletions.
7 changes: 2 additions & 5 deletions src/Package/Properties/Resources.es.resx
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,11 @@
<value>Instalar la app SponsorLink en GitHub garantiza que tu patrocinio se atribuya correctamente a tu usuario.</value>
</data>
<data name="AppNotInstalled_Message" xml:space="preserve">
<value>{0} utiliza SponsorLink para vincular tu patrocinio con {1}. Por favor, instale la app de GitHub en https://github.com/apps/sponsorlink. {2}</value>
<value>{0} utiliza SponsorLink para vincular tu patrocinio con {1}. Por favor, instale la app de GitHub en https://github.com/apps/sponsorlink.</value>
</data>
<data name="AppNotInstalled_Title" xml:space="preserve">
<value>Por favor, instale la app SponsorLink en GitHub 🙏</value>
</data>
<data name="BuildPaused" xml:space="preserve">
<value> Compilación pausada por {0}ms. Esto occurre solo una vez por sesion del editor/IDE.</value>
</data>
<data name="MissingDesignTimeBuild_Description" xml:space="preserve">
<value>Esto suele ser un error del autor del paquete que puede resolverse instalando el paquete Devlooped.SponsorLink o agregando el siguiente item de MSBuild al proyecto: &lt;CompilerVisibleProperty Include="DesignTimeBuild" /&gt;.</value>
</data>
Expand Down Expand Up @@ -160,7 +157,7 @@
<value>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!</value>
</data>
<data name="UserNotSponsoring_Message" xml:space="preserve">
<value>Por favor considera apoyar el desarrollo futuro de {0} patrocinando en https://github.com/sponsors/{1}. {2}</value>
<value>Por favor considera apoyar el desarrollo futuro de {0} patrocinando en https://github.com/sponsors/{1}.</value>
</data>
<data name="UserNotSponsoring_Title" xml:space="preserve">
<value>Por considera apoyar el desarrollo futuro del proyecto 🙏</value>
Expand Down
7 changes: 2 additions & 5 deletions src/Package/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,11 @@
<value>Installing the SponsorLink GitHub app ensures that your sponsorship is properly attributed to you.</value>
</data>
<data name="AppNotInstalled_Message" xml:space="preserve">
<value>{0} uses SponsorLink to properly attribute your sponsorship with {1}. Please install the GitHub app at https://github.com/apps/sponsorlink. {2}</value>
<value>{0} uses SponsorLink to properly attribute your sponsorship with {1}. Please install the GitHub app at https://github.com/apps/sponsorlink.</value>
</data>
<data name="AppNotInstalled_Title" xml:space="preserve">
<value>Please install the SponsorLink GitHub app 🙏</value>
</data>
<data name="BuildPaused" xml:space="preserve">
<value>Build paused for {0}ms. This happens only once per IDE/editor session.</value>
</data>
<data name="MissingDesignTimeBuild_Description" xml:space="preserve">
<value>This is typically a package authoring error which can be fixed by installing the Devlooped.SponsorLink package or adding the following MSBuild item to the project: &lt;CompilerVisibleProperty Include="DesignTimeBuild" /&gt;.</value>
</data>
Expand Down Expand Up @@ -160,7 +157,7 @@
<value>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!</value>
</data>
<data name="UserNotSponsoring_Message" xml:space="preserve">
<value>Please consider supporting {0} ongoing development by sponsoring at https://github.com/sponsors/{1}. {2}</value>
<value>Please consider supporting {0} ongoing development by sponsoring at https://github.com/sponsors/{1}.</value>
</data>
<data name="UserNotSponsoring_Title" xml:space="preserve">
<value>Please consider supporting the ongoing development of the project 🙏</value>
Expand Down
77 changes: 11 additions & 66 deletions src/Package/SponsorLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,13 @@ void IIncrementalGenerator.Initialize(IncrementalGeneratorInitializationContext
{
case DiagnosticKind.AppNotInstalled:
case DiagnosticKind.UserNotSponsoring:
// Add a random configurable pause in this case.
var (warn, pause, suffix) = GetPause();
if (!warn)
var show = ShouldShow();
if (!show)
return null;

// The Pause property is used by our default implementation to introduce the pause
// in an incremental-aware way.
var diag = Diagnostic.Create(descriptor, null,
properties: new Dictionary<string, string?>
{
{ "Pause", pause.ToString() },
}.ToImmutableDictionary(),
product, sponsorable, suffix);
properties: null,
product, sponsorable);

return WriteMessage(sponsorable, product, Path.GetDirectoryName(projectPath), diag);
case DiagnosticKind.Thanks:
Expand Down Expand Up @@ -271,10 +265,7 @@ void AnalyzeSponsors(CompilationAnalysisContext context)
// otherwise they go away as soon as they are reported by a real
// build. The IDE re-runs the analyzers at various points and we
// want the real build diagnostics to remain visible since otherwise
// the user can miss what happened. Granted, a message saying the
// build was paused might be misleading since upon reopening VS
// with no build performed yet, a previously created diagnostic
// might be reported again, but that's a small price to pay.
// the user can miss what happened.
// So: DO NOT TRY TO AVOID REPORTING AGAIN ON DTB

// If this particular build did not generate a new diagnostic (i.e. it was an
Expand All @@ -286,7 +277,7 @@ void AnalyzeSponsors(CompilationAnalysisContext context)
// communication "channel" between past runs of the generator check and the analyzer
// reporting live in VS.

// We never pause in DTB
// In DTB we report the diagnostic again, but we don't perform the check again.
if (info.DesignTimeBuild == true)
{
Trace($"ReportExisting {sponsorable}/{product}@{Path.GetFileNameWithoutExtension(projectFile)}", info.DesignTimeBuild == true);
Expand Down Expand Up @@ -399,19 +390,6 @@ void CheckAndReport(CompilationAnalysisContext context, BuildInfo info)
{
SessionManager.Set(sponsorable, product, info.ProjectPath, kind);

// Pause if configured so. Note we won't pause if the project is up to date.
if (diagnostic.Properties.TryGetValue("Pause", out var value) &&
int.TryParse(value, out var pause) &&
pause > 0)
{
Trace($"Pausing new check for {pause}ms");

#if !CI
Console.Beep(500, 500);
#endif
Thread.Sleep(pause);
}

// Note that we don't push even Thanks if they were already reported and the
// project is up to date. This means the Thanks won't become annoying either.
Diagnostics.ReportDiagnosticOnce(context,
Expand All @@ -420,30 +398,6 @@ void CheckAndReport(CompilationAnalysisContext context, BuildInfo info)
}
}

void ClearExisting(string projectFile)
{
// Clear a previous report in this case, to avoid giving the impression that we
// paused again when we haven't.
var cleared = false;
var objDir = Path.Combine(Path.GetDirectoryName(projectFile), "obj", "SponsorLink", sponsorable, product);
if (Directory.Exists(objDir))
{
foreach (var file in Directory.EnumerateFiles(objDir))
{
File.Delete(file);
cleared = true;
}
}

if (cleared)
{
Trace($"Cleared existing diagnostic files");
#if !CI
Console.Beep(800, 500);
#endif
}
}

/// <summary>
/// In DTB, we merely re-surface diagnostics that were previously generated in a
/// full build. This keeps the checks minimally impactful while still being visible.
Expand Down Expand Up @@ -486,29 +440,20 @@ void ReportExisting(CompilationAnalysisContext context, string? projectFile)
}
}

(bool warn, int pause, string suffix) GetPause()
bool ShouldShow()
{
if (settings.InstallTime == null)
return GetPaused(rnd.Next(settings.PauseMin, settings.PauseMax));
return true;

var daysOld = (int)DateTime.Now.Subtract(settings.InstallTime.Value).TotalDays;

// Never warn during the quiet days.
// Don't show during the quiet days.
if (daysOld < (settings.QuietDays ?? quietDays))
return (false, 0, "");
return false;

// At this point, daysOld is greater than quietDays and greater than 1.
var nonQuietDays = daysOld - (settings.QuietDays ?? quietDays);
// Turn days pause (starting at 1sec max pause into milliseconds, used for the pause.
var daysMaxPause = nonQuietDays * 1000;

// From second day, the max pause will increase from days old until the max pause.
return GetPaused(rnd.Next(settings.PauseMin, Math.Min(daysMaxPause, settings.PauseMax)));
return true;
}

static (bool warn, int pause, string suffix) GetPaused(int pause)
=> (true, pause, pause > 0 ? ThisAssembly.Strings.BuildPaused(pause) : "");

static Diagnostic WriteMessage(string sponsorable, string product, string projectDir, Diagnostic diag)
{
try
Expand Down
36 changes: 26 additions & 10 deletions src/Package/SponsorLinkSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ namespace Devlooped;
/// </remarks>
public class SponsorLinkSettings
{
const int DefaultMaxPause = 4000;

// Make this private for now. We may expose derived types if needed at some point?
SponsorLinkSettings(string sponsorable, string product)
{
Sponsorable = sponsorable;
Product = product;
}


/// <summary>
/// Creates the settings for <see cref="SponsorLink"/> with the given values.
/// </summary>
Expand All @@ -37,7 +34,7 @@ public static SponsorLinkSettings Create(string sponsorable, string product)
version: default,
diagnosticsIdPrefix: default,
pauseMin: default,
pauseMax: DefaultMaxPause,
pauseMax: default,
quietDays: default);

/// <summary>
Expand Down Expand Up @@ -112,13 +109,36 @@ public static SponsorLinkSettings Create(string sponsorable, string product,
/// <param name="pauseMax">Max random milliseconds to apply during build for non-sponsoring users. Use 0 for no pause.</param>
/// <param name="quietDays">Optional days to keep warnings quiet so the user has a chance to test the product undisturbed.</param>
/// <param name="transitive">Whether the check is transitive (enforced when dependency is indirect in a project).</param>
// <Previous release> BACKCOMPAT OVERLOAD -- DO NOT TOUCH
[EditorBrowsable(EditorBrowsableState.Never)]
public static SponsorLinkSettings Create(string sponsorable, string product,
string? packageId = default,
string? version = default,
string? diagnosticsIdPrefix = default,
int pauseMin = 0,
int pauseMax = DefaultMaxPause,
int? quietDays = default,
int pauseMax = 0,
int? quietDays = default,
bool transitive = default) => Create(sponsorable, product, packageId, version, diagnosticsIdPrefix, quietDays, transitive);

/// <summary>
/// Creates the settings for <see cref="SponsorLink"/> with the given values.
/// </summary>
/// <param name="sponsorable">The sponsor account to check for sponsorships.</param>
/// <param name="product">The product that uses SponsorLink. Used in diagnostics to clarify the product requesting the sponsor link check.</param>
/// <param name="packageId">Optional NuGet package identifier of the product performing the check. Defaults to <paramref name="product"/>.
/// Used to determine installation time of the product and avoid pausing builds or emitting warnings during the
/// <paramref name="quietDays"/> after install.</param>
/// <param name="version">Optional product or package version.</param>
/// <param name="diagnosticsIdPrefix">Prefix to use for diagnostics with numbers <c>02,03,04</c> reported by default. If not provided,
/// a default one is determined from the <paramref name="sponsorable"/> and <paramref name="product"/> values.</param>
/// <param name="quietDays">Optional days to keep diagnostic quiet so the user has a chance to test the product undisturbed.</param>
/// <param name="transitive">Whether the check is transitive (enforced when dependency is indirect in a project).</param>
// <Previous release> BACKCOMPAT OVERLOAD -- DO NOT TOUCH
public static SponsorLinkSettings Create(string sponsorable, string product,
string? packageId = default,
string? version = default,
string? diagnosticsIdPrefix = default,
int? quietDays = default,
bool transitive = default)
{
if (quietDays < 0)
Expand Down Expand Up @@ -161,8 +181,6 @@ public static SponsorLinkSettings Create(string sponsorable, string product,
{
PackageId = packageId ?? product,
Version = version,
PauseMin = pauseMin,
PauseMax = pauseMax,
QuietDays = quietDays,
SupportedDiagnostics = SponsorLink.Diagnostics.GetDescriptors(sponsorable, diagnosticsIdPrefix),
Transitive = transitive
Expand All @@ -186,8 +204,6 @@ public static SponsorLinkSettings Create(string sponsorable, string product,

internal string? PackageId { get; private set; }
internal string? Version { get; private set; }
internal int PauseMin { get; private set; }
internal int PauseMax { get; private set; }
internal DateTime? InstallTime { get; set; }
internal int? QuietDays { get; private set; }
internal bool Transitive { get; private set; }
Expand Down

0 comments on commit cbc52f5

Please sign in to comment.