2016-01-04 12:49:13 -08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2016-02-10 16:13:30 -08:00
|
|
|
|
using Microsoft.Extensions.PlatformAbstractions;
|
2016-01-04 12:49:13 -08:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli.Utils
|
|
|
|
|
{
|
2016-01-05 23:48:50 -08:00
|
|
|
|
public static class Env
|
2016-01-04 12:49:13 -08:00
|
|
|
|
{
|
|
|
|
|
private static IEnumerable<string> _searchPaths;
|
|
|
|
|
private static IEnumerable<string> _executableExtensions;
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<string> ExecutableExtensions
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_executableExtensions == null)
|
|
|
|
|
{
|
|
|
|
|
|
2016-02-10 16:13:30 -08:00
|
|
|
|
_executableExtensions = PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows
|
2016-01-15 10:15:26 -08:00
|
|
|
|
? Environment.GetEnvironmentVariable("PATHEXT")
|
|
|
|
|
.Split(';')
|
|
|
|
|
.Select(e => e.ToLower().Trim('"'))
|
2016-01-04 12:49:13 -08:00
|
|
|
|
: new [] { string.Empty };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _executableExtensions;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<string> SearchPaths
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_searchPaths == null)
|
|
|
|
|
{
|
2016-02-10 16:13:30 -08:00
|
|
|
|
var searchPaths = new List<string> { PlatformServices.Default.Application.ApplicationBasePath };
|
2016-01-04 12:49:13 -08:00
|
|
|
|
|
2016-01-14 18:27:26 -08:00
|
|
|
|
searchPaths.AddRange(Environment
|
|
|
|
|
.GetEnvironmentVariable("PATH")
|
|
|
|
|
.Split(Path.PathSeparator)
|
|
|
|
|
.Select(p => p.Trim('"')));
|
2016-01-04 12:49:13 -08:00
|
|
|
|
|
|
|
|
|
_searchPaths = searchPaths;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _searchPaths;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetCommandPath(string commandName, params string[] extensions)
|
|
|
|
|
{
|
|
|
|
|
if (!extensions.Any())
|
2016-01-05 01:01:46 -08:00
|
|
|
|
{
|
2016-01-04 12:49:13 -08:00
|
|
|
|
extensions = Env.ExecutableExtensions.ToArray();
|
2016-01-05 01:01:46 -08:00
|
|
|
|
}
|
2016-01-04 12:49:13 -08:00
|
|
|
|
|
|
|
|
|
var commandPath = Env.SearchPaths.Join(
|
|
|
|
|
extensions,
|
|
|
|
|
p => true, s => true,
|
|
|
|
|
(p, s) => Path.Combine(p, commandName + s))
|
|
|
|
|
.FirstOrDefault(File.Exists);
|
|
|
|
|
|
|
|
|
|
return commandPath;
|
|
|
|
|
}
|
2016-01-11 14:54:02 -08:00
|
|
|
|
|
2016-02-09 15:30:04 -08:00
|
|
|
|
public static string GetCommandPathFromRootPath(string rootPath, string commandName, params string[] extensions)
|
2016-01-11 14:54:02 -08:00
|
|
|
|
{
|
|
|
|
|
if (!extensions.Any())
|
|
|
|
|
{
|
|
|
|
|
extensions = Env.ExecutableExtensions.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-09 15:30:04 -08:00
|
|
|
|
var commandPath = extensions.Select(e => Path.Combine(rootPath, commandName + e))
|
2016-01-11 14:54:02 -08:00
|
|
|
|
.FirstOrDefault(File.Exists);
|
|
|
|
|
|
|
|
|
|
return commandPath;
|
|
|
|
|
}
|
2016-01-04 12:49:13 -08:00
|
|
|
|
}
|
|
|
|
|
}
|