-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
DotnetFirstTimeUseConfigurer.cs
112 lines (99 loc) · 5 KB
/
DotnetFirstTimeUseConfigurer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.DotNet.Cli.Utils;
using NuGet.Versioning;
namespace Microsoft.DotNet.Configurer
{
public class DotnetFirstTimeUseConfigurer
{
private readonly IReporter _reporter;
private readonly DotnetFirstRunConfiguration _dotnetFirstRunConfiguration;
private readonly IFirstTimeUseNoticeSentinel _firstTimeUseNoticeSentinel;
private readonly IAspNetCertificateSentinel _aspNetCertificateSentinel;
private readonly IAspNetCoreCertificateGenerator _aspNetCoreCertificateGenerator;
private readonly IFileSentinel _toolPathSentinel;
private readonly IEnvironmentPath _pathAdder;
private readonly Dictionary<string, double> _performanceMeasurements;
public DotnetFirstTimeUseConfigurer(
IFirstTimeUseNoticeSentinel firstTimeUseNoticeSentinel,
IAspNetCertificateSentinel aspNetCertificateSentinel,
IAspNetCoreCertificateGenerator aspNetCoreCertificateGenerator,
IFileSentinel toolPathSentinel,
DotnetFirstRunConfiguration dotnetFirstRunConfiguration,
IReporter reporter,
IEnvironmentPath pathAdder,
Dictionary<string, double> performanceMeasurements = null)
{
_firstTimeUseNoticeSentinel = firstTimeUseNoticeSentinel;
_aspNetCertificateSentinel = aspNetCertificateSentinel;
_aspNetCoreCertificateGenerator = aspNetCoreCertificateGenerator;
_toolPathSentinel = toolPathSentinel;
_dotnetFirstRunConfiguration = dotnetFirstRunConfiguration;
_reporter = reporter;
_pathAdder = pathAdder ?? throw new ArgumentNullException(nameof(pathAdder));
_performanceMeasurements ??= performanceMeasurements;
}
public void Configure()
{
if (_dotnetFirstRunConfiguration.AddGlobalToolsToPath && !_toolPathSentinel.Exists())
{
using (new PerformanceMeasurement(_performanceMeasurements, "AddPackageExecutablePath Time"))
{
_pathAdder.AddPackageExecutablePathToUserPath();
_toolPathSentinel.Create();
}
}
var isFirstTimeUse = !_firstTimeUseNoticeSentinel.Exists();
var canShowFirstUseMessages = isFirstTimeUse && !_dotnetFirstRunConfiguration.NoLogo;
if (isFirstTimeUse)
{
using (new PerformanceMeasurement(_performanceMeasurements, "FirstTimeUseNotice Time"))
{
// Migrate the NuGet state from earlier SDKs
NuGet.Common.Migrations.MigrationRunner.Run();
if (canShowFirstUseMessages)
{
_reporter.WriteLine();
string productVersion = Product.Version;
_reporter.WriteLine(string.Format(LocalizableStrings.FirstTimeMessageWelcome, ParseDotNetVersion(productVersion), productVersion));
if (!_dotnetFirstRunConfiguration.TelemetryOptout)
{
_reporter.WriteLine();
_reporter.WriteLine(LocalizableStrings.TelemetryMessage);
}
}
_firstTimeUseNoticeSentinel.CreateIfNotExists();
}
}
if (CanGenerateAspNetCertificate())
{
using (new PerformanceMeasurement(_performanceMeasurements, "GenerateAspNetCertificate Time"))
{
_aspNetCoreCertificateGenerator.GenerateAspNetCoreDevelopmentCertificate();
_aspNetCertificateSentinel.CreateIfNotExists();
if (canShowFirstUseMessages)
{
// This message is slightly misleading for (e.g.) FreeBSD, which doesn't officially
// support `dotnet dev-certs https --trust`, but the link in the message should help
// users find the right steps for their platform.
_reporter.WriteLine();
_reporter.WriteLine(LocalizableStrings.FirstTimeMessageAspNetCertificate);
}
}
}
if (canShowFirstUseMessages)
{
_reporter.WriteLine();
_reporter.WriteLine(LocalizableStrings.FirstTimeMessageMoreInformation);
}
}
private bool CanGenerateAspNetCertificate() =>
#if EXCLUDE_ASPNETCORE
false;
#else
_dotnetFirstRunConfiguration.GenerateAspNetCertificate && !_aspNetCertificateSentinel.Exists();
#endif
internal static string ParseDotNetVersion(string productVersion) =>
NuGetVersion.TryParse(productVersion, out var parsedVersion) ? $"{parsedVersion.Major}.{parsedVersion.Minor}" : string.Empty;
}
}