Skip to content

Commit

Permalink
Various internal refactoring in TelemetryManager (#2764)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink committed Apr 25, 2024
1 parent 951ea5e commit 961334f
Showing 1 changed file with 14 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal sealed class TelemetryManager : ITelemetryManager, IOutputDeviceDataPro

public void AddTelemetryCollectorProvider(Func<IServiceProvider, ITelemetryCollector> telemetryFactory)
{
ApplicationStateGuard.Ensure(_telemetryFactory is null, PlatformResources.TelemetryProviderAlreadySetErrorMessage);
ArgumentGuard.IsNotNull(telemetryFactory);
_telemetryFactory = telemetryFactory;
}

Expand All @@ -51,34 +51,34 @@ public async Task<ITelemetryCollector> BuildAsync(ServiceProvider serviceProvide

// If the environment variable is not set or is set to 0, telemetry is opted in.
ICommandLineOptions commandLineOptions = serviceProvider.GetCommandLineOptions();
bool dontShowLogo = commandLineOptions.IsOptionSet(PlatformCommandLineProvider.NoBannerOptionKey);
bool doNotShowLogo = commandLineOptions.IsOptionSet(PlatformCommandLineProvider.NoBannerOptionKey);

string? noBanner = environment.GetEnvironmentVariable(EnvironmentVariableConstants.TESTINGPLATFORM_NOBANNER);
await logger.LogInformationAsync($"{EnvironmentVariableConstants.TESTINGPLATFORM_NOBANNER} environment variable: '{noBanner}'");
dontShowLogo = (noBanner is not null and ("1" or "true")) || dontShowLogo;
string? noBannerEnvVar = environment.GetEnvironmentVariable(EnvironmentVariableConstants.TESTINGPLATFORM_NOBANNER);
await logger.LogInformationAsync($"{EnvironmentVariableConstants.TESTINGPLATFORM_NOBANNER} environment variable: '{noBannerEnvVar}'");
doNotShowLogo = (noBannerEnvVar is not null and ("1" or "true")) || doNotShowLogo;

string? dotnet_noLogo = environment.GetEnvironmentVariable(EnvironmentVariableConstants.DOTNET_NOLOGO);
await logger.LogInformationAsync($"{EnvironmentVariableConstants.DOTNET_NOLOGO} environment variable: '{dotnet_noLogo}'");
dontShowLogo = (dotnet_noLogo is not null and ("1" or "true")) || dontShowLogo;
string? dotnetNoLogoEnvVar = environment.GetEnvironmentVariable(EnvironmentVariableConstants.DOTNET_NOLOGO);
await logger.LogInformationAsync($"{EnvironmentVariableConstants.DOTNET_NOLOGO} environment variable: '{dotnetNoLogoEnvVar}'");
doNotShowLogo = (dotnetNoLogoEnvVar is not null and ("1" or "true")) || doNotShowLogo;

await logger.LogInformationAsync($"Telemetry is '{(!isTelemetryOptedOut ? "ENABLED" : "DISABLED")}'");

if (!isTelemetryOptedOut && !dontShowLogo)
if (!isTelemetryOptedOut && !doNotShowLogo)
{
ITestApplicationModuleInfo testApplicationModuleInfo = serviceProvider.GetTestApplicationModuleInfo();
string fileName = Path.ChangeExtension(Path.GetFileName(testApplicationModuleInfo.GetCurrentTestApplicationFullPath()), "testingPlatformFirstTimeUseSentinel");
string? directory = environment.GetEnvironmentVariable("LOCALAPPDATA") ?? environment.GetEnvironmentVariable("HOME");
if (directory is not null)
{
directory = Path.Combine(directory, "Microsoft", "TestingPlatform");
}

IFileSystem fileSystem = serviceProvider.GetFileSystem();
string fileName = Path.ChangeExtension(Path.GetFileName(testApplicationModuleInfo.GetCurrentTestApplicationFullPath()), "testingPlatformFirstTimeUseSentinel");
bool sentinelIsNotPresent =
RoslynString.IsNullOrWhiteSpace(directory)
|| !fileSystem.Exists(Path.Combine(directory, fileName));

if (!dontShowLogo && sentinelIsNotPresent)
if (!doNotShowLogo && sentinelIsNotPresent)
{
IOutputDevice outputDevice = serviceProvider.GetOutputDevice();
await outputDevice.DisplayAsync(this, new TextOutputDeviceData(PlatformResources.TelemetryNotice));
Expand All @@ -105,18 +105,11 @@ public async Task<ITelemetryCollector> BuildAsync(ServiceProvider serviceProvide
}
}

if (!isTelemetryOptedOut)
{
serviceProvider.TryAddService(new TelemetryInformation(true, TelemetryProperties.VersionValue));
}
else
{
serviceProvider.TryAddService(new TelemetryInformation(false, TelemetryProperties.VersionValue));
}
serviceProvider.TryAddService(new TelemetryInformation(!isTelemetryOptedOut, TelemetryProperties.VersionValue));

ITelemetryCollector telemetryCollector = _telemetryFactory is null
ITelemetryCollector telemetryCollector = _telemetryFactory is null || isTelemetryOptedOut
? new NopTelemetryService(!isTelemetryOptedOut)
: !isTelemetryOptedOut ? _telemetryFactory(serviceProvider) : new NopTelemetryService(!isTelemetryOptedOut);
: _telemetryFactory(serviceProvider);

if (!isTelemetryOptedOut)
{
Expand Down

0 comments on commit 961334f

Please sign in to comment.