From 19ed5a558b14d7b542498b9541dd572cd18d10f2 Mon Sep 17 00:00:00 2001 From: Piotr Puszkiewicz Date: Tue, 24 Jan 2017 21:59:18 -0800 Subject: [PATCH] WiP --- build_projects/dotnet-cli-build/DotNetTool.cs | 5 ++ .../EnvironmentVariableFilter.cs | 54 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 build_projects/dotnet-cli-build/EnvironmentVariableFilter.cs diff --git a/build_projects/dotnet-cli-build/DotNetTool.cs b/build_projects/dotnet-cli-build/DotNetTool.cs index d074856b0..58f813cab 100644 --- a/build_projects/dotnet-cli-build/DotNetTool.cs +++ b/build_projects/dotnet-cli-build/DotNetTool.cs @@ -1,5 +1,6 @@ // Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.Linq; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; @@ -12,6 +13,10 @@ namespace Microsoft.DotNet.Cli.Build { public DotNetTool() { + EnvironmentVariables = new EnvironmentFilter() + .GetEnvironmentVariableNamesToRemove() + .Select(e => $"{e}=") + .ToArray(); } protected abstract string Command { get; } diff --git a/build_projects/dotnet-cli-build/EnvironmentVariableFilter.cs b/build_projects/dotnet-cli-build/EnvironmentVariableFilter.cs new file mode 100644 index 000000000..54f5272f7 --- /dev/null +++ b/build_projects/dotnet-cli-build/EnvironmentVariableFilter.cs @@ -0,0 +1,54 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Microsoft.DotNet.Cli.Build +{ + public class EnvironmentFilter + { + private const string _MSBuildEnvironmentVariablePrefix = "MSBuild"; + private const string _DotNetEnvironmentVariablePrefix = "DOTNET"; + private const string _NugetEnvironmentVariablePrefix = "NUGET"; + + private IEnumerable _prefixesOfEnvironmentVariablesToRemove = new string [] + { + _MSBuildEnvironmentVariablePrefix, + _DotNetEnvironmentVariablePrefix, + _NugetEnvironmentVariablePrefix + }; + + private IEnumerable _environmentVariablesToRemove = new string [] + { + "CscToolExe" + }; + + private IEnumerable _environmentVariablesToKeep = new string [] + { + "DOTNET_CLI_TELEMETRY_SESSIONID" + }; + + public IEnumerable GetEnvironmentVariableNamesToRemove() + { + var allEnvironmentVariableNames = (IEnumerable)Environment + .GetEnvironmentVariables() + .Keys + .Cast(); + + var environmentVariablesToRemoveByPrefix = allEnvironmentVariableNames + .Where(e => _prefixesOfEnvironmentVariablesToRemove.Any(p => e.StartsWith(p))); + + var environmentVariablesToRemoveByName = allEnvironmentVariableNames + .Where(e => _environmentVariablesToRemove.Contains(e)); + + var environmentVariablesToRemove = environmentVariablesToRemoveByName + .Concat(environmentVariablesToRemoveByPrefix) + .Distinct() + .Except(_environmentVariablesToKeep); + + return environmentVariablesToRemoveByName; + } + } +}