-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathCliEnvironment.cs
55 lines (46 loc) · 1.85 KB
/
CliEnvironment.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.TemplateEngine.Abstractions;
namespace Microsoft.TemplateEngine.Cli
{
internal class CliEnvironment : IEnvironment
{
private const int DefaultBufferWidth = 160;
private readonly IReadOnlyDictionary<string, string> _environmentVariables;
public CliEnvironment()
{
Dictionary<string, string> variables = new(StringComparer.OrdinalIgnoreCase)
{
{ "TEMPLATE_ENGINE_DISABLE_FILEWATCHER", "1" }
};
var env = Environment.GetEnvironmentVariables();
foreach (string key in env.Keys.OfType<string>())
{
variables[key] = (env[key] as string) ?? string.Empty;
}
_environmentVariables = variables;
}
/// <inheritdoc/>
public string NewLine { get; } = Environment.NewLine;
/// <inheritdoc/>
// Console.BufferWidth can throw if there's no console, such as when output is redirected, so
// first check if it is redirected, and fall back to a default value if needed.
public int ConsoleBufferWidth => Console.IsOutputRedirected ? DefaultBufferWidth : Console.BufferWidth;
/// <inheritdoc/>
public string ExpandEnvironmentVariables(string name)
{
return Environment.ExpandEnvironmentVariables(name);
}
/// <inheritdoc/>
public string? GetEnvironmentVariable(string name)
{
_environmentVariables.TryGetValue(name, out string? result);
return result;
}
/// <inheritdoc/>
public IReadOnlyDictionary<string, string> GetEnvironmentVariables()
{
return _environmentVariables;
}
}
}