// 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.IO; using System.Linq; using System.Runtime.InteropServices; namespace Microsoft.DotNet.MSBuildSdkResolver { internal class EnvironmentProvider { private IEnumerable _searchPaths; private readonly Func _getEnvironmentVariable; public EnvironmentProvider(Func getEnvironmentVariable) { _getEnvironmentVariable = getEnvironmentVariable; } public string ExecutableExtension { get { return Interop.RunningOnWindows ? ".exe" : string.Empty; } } private IEnumerable SearchPaths { get { if (_searchPaths == null) { var searchPaths = new List(); searchPaths.AddRange( _getEnvironmentVariable("PATH") .Split(Path.PathSeparator) .Select(p => p.Trim('"'))); _searchPaths = searchPaths; } return _searchPaths; } } public string GetCommandPath(string commandName) { var commandNameWithExtension = commandName + ExecutableExtension; var commandPath = SearchPaths .Select(p => Path.Combine(p, commandNameWithExtension)) .FirstOrDefault(File.Exists); return commandPath; } } }