2016-02-25 00:05:55 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2016-04-28 23:30:32 +00:00
|
|
|
using Microsoft.DotNet.InternalAbstractions;
|
2016-02-25 00:05:55 +00:00
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli.Utils
|
|
|
|
{
|
|
|
|
public class EnvironmentProvider : IEnvironmentProvider
|
|
|
|
{
|
|
|
|
private IEnumerable<string> _searchPaths;
|
|
|
|
private IEnumerable<string> _executableExtensions;
|
|
|
|
|
|
|
|
public IEnumerable<string> ExecutableExtensions
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (_executableExtensions == null)
|
|
|
|
{
|
|
|
|
|
2016-04-28 23:30:32 +00:00
|
|
|
_executableExtensions = RuntimeEnvironment.OperatingSystemPlatform == Platform.Windows
|
2016-02-25 00:05:55 +00:00
|
|
|
? Environment.GetEnvironmentVariable("PATHEXT")
|
|
|
|
.Split(';')
|
|
|
|
.Select(e => e.ToLower().Trim('"'))
|
|
|
|
: new [] { string.Empty };
|
|
|
|
}
|
|
|
|
|
|
|
|
return _executableExtensions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<string> SearchPaths
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (_searchPaths == null)
|
|
|
|
{
|
2016-04-28 23:30:32 +00:00
|
|
|
var searchPaths = new List<string> { ApplicationEnvironment.ApplicationBasePath };
|
2016-02-25 00:05:55 +00:00
|
|
|
|
|
|
|
searchPaths.AddRange(Environment
|
|
|
|
.GetEnvironmentVariable("PATH")
|
|
|
|
.Split(Path.PathSeparator)
|
|
|
|
.Select(p => p.Trim('"')));
|
|
|
|
|
|
|
|
_searchPaths = searchPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _searchPaths;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 19:50:52 +00:00
|
|
|
public EnvironmentProvider(
|
|
|
|
IEnumerable<string> extensionsOverride = null,
|
|
|
|
IEnumerable<string> searchPathsOverride = null)
|
2016-02-25 00:05:55 +00:00
|
|
|
{
|
2016-03-07 19:50:52 +00:00
|
|
|
_executableExtensions = extensionsOverride;
|
|
|
|
_searchPaths = searchPathsOverride;
|
2016-02-25 00:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public string GetCommandPath(string commandName, params string[] extensions)
|
|
|
|
{
|
|
|
|
if (!extensions.Any())
|
|
|
|
{
|
|
|
|
extensions = ExecutableExtensions.ToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
var commandPath = SearchPaths.Join(
|
|
|
|
extensions,
|
|
|
|
p => true, s => true,
|
|
|
|
(p, s) => Path.Combine(p, commandName + s))
|
|
|
|
.FirstOrDefault(File.Exists);
|
|
|
|
|
|
|
|
return commandPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetCommandPathFromRootPath(string rootPath, string commandName, params string[] extensions)
|
|
|
|
{
|
|
|
|
if (!extensions.Any())
|
|
|
|
{
|
|
|
|
extensions = ExecutableExtensions.ToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
var commandPath = extensions.Select(e => Path.Combine(rootPath, commandName + e))
|
|
|
|
.FirstOrDefault(File.Exists);
|
|
|
|
|
|
|
|
return commandPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetCommandPathFromRootPath(string rootPath, string commandName, IEnumerable<string> extensions)
|
|
|
|
{
|
2016-03-07 19:50:52 +00:00
|
|
|
var extensionsArr = extensions.OrEmptyIfNull().ToArray();
|
2016-02-25 00:05:55 +00:00
|
|
|
|
|
|
|
return GetCommandPathFromRootPath(rootPath, commandName, extensionsArr);
|
|
|
|
}
|
2016-03-29 00:17:21 +00:00
|
|
|
|
|
|
|
public bool GetEnvironmentVariableAsBool(string name, bool defaultValue)
|
|
|
|
{
|
|
|
|
var str = Environment.GetEnvironmentVariable(name);
|
|
|
|
if (string.IsNullOrEmpty(str))
|
|
|
|
{
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (str.ToLowerInvariant())
|
|
|
|
{
|
|
|
|
case "true":
|
|
|
|
case "1":
|
|
|
|
case "yes":
|
|
|
|
return true;
|
|
|
|
case "false":
|
|
|
|
case "0":
|
|
|
|
case "no":
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 00:05:55 +00:00
|
|
|
}
|
|
|
|
}
|