-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathUserAgentStringBuilder.cs
More file actions
153 lines (132 loc) · 4.66 KB
/
UserAgentStringBuilder.cs
File metadata and controls
153 lines (132 loc) · 4.66 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if NETCOREAPP
using System;
#endif
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
using NuGet.Common;
using NuGet.Packaging;
namespace NuGet.Protocol.Core.Types
{
public class UserAgentStringBuilder
{
public static readonly string DefaultNuGetClientName = "NuGet Client V3";
private const string UserAgentWithMetadataTemplate = "{0}/{1} ({2})";
private const string UserAgentTemplate = "{0}/{1}";
private readonly string _clientName;
private string? _vsInfo;
private string? _osInfo;
private string? _ciInfo;
public UserAgentStringBuilder()
: this(DefaultNuGetClientName)
{
}
public UserAgentStringBuilder(string clientName)
: this(clientName, EnvironmentVariableWrapper.Instance)
{
}
/// <summary>
/// Internal constructor for testing purposes that allows injecting an environment variable reader.
/// </summary>
/// <param name="clientName">The client name to use in the user agent string.</param>
/// <param name="environmentVariableReader">The environment variable reader for CI detection.</param>
internal UserAgentStringBuilder(string clientName, IEnvironmentVariableReader environmentVariableReader)
{
_clientName = clientName;
// Read the client version from the assembly metadata and normalize it.
NuGetClientVersion = MinClientVersionUtility.GetNuGetClientVersion().ToNormalizedString();
_osInfo = GetOS();
_ciInfo = CIEnvironmentDetector.Detect(environmentVariableReader);
}
public string NuGetClientVersion { get; }
public UserAgentStringBuilder WithVisualStudioSKU(string? vsInfo)
{
_vsInfo = vsInfo;
return this;
}
public string Build()
{
var clientInfo = _clientName;
if (NuGetTestMode.Enabled)
{
clientInfo = NuGetTestMode.NuGetTestClientName;
}
else if (string.IsNullOrEmpty(clientInfo))
{
clientInfo = DefaultNuGetClientName;
}
string metadataString = BuildMetadataString();
if (string.IsNullOrEmpty(metadataString))
{
return string.Format(
CultureInfo.InvariantCulture,
UserAgentTemplate,
clientInfo,
NuGetClientVersion);
}
return string.Format(
CultureInfo.InvariantCulture,
UserAgentWithMetadataTemplate,
clientInfo,
NuGetClientVersion,
metadataString);
}
/// <summary>
/// Builds the metadata string for the parentheses section.
/// Items are collected in order (OS, CI, VS) and joined with ", ".
/// </summary>
internal string BuildMetadataString()
{
var sb = new StringBuilder();
// OS info
if (!string.IsNullOrEmpty(_osInfo))
{
sb.Append(_osInfo);
}
// CI info (formatted as "CI: {provider}")
if (!string.IsNullOrEmpty(_ciInfo))
{
if (sb.Length > 0) sb.Append(", ");
sb.Append("CI: ").Append(_ciInfo);
}
// VS info
if (!string.IsNullOrEmpty(_vsInfo))
{
if (sb.Length > 0) sb.Append(", ");
sb.Append(_vsInfo);
}
return sb.ToString();
}
internal static string GetOS()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return OSPlatform.Windows.ToString();
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return OSPlatform.Linux.ToString();
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return OSPlatform.OSX.ToString();
}
#if NETCOREAPP
else if (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
{
return OSPlatform.FreeBSD.ToString();
}
else if (OperatingSystem.IsBrowser())
{
return "BROWSER";
}
#endif
else
{
return "UnknownOS";
}
}
}
}