File size: 2,852 Bytes
18a519f | 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
namespace Unity.PlasticSCM.Editor.Tool
{
internal static class FindTool
{
internal static string ObtainToolCommand(
string toolName, List<string> installationPaths)
{
List<string> processPaths = GetPathsFromEnvVariable(
PATH_ENVIRONMENT_VARIABLE,
EnvironmentVariableTarget.Process);
List<string> machinePaths = GetPathsFromEnvVariable(
PATH_ENVIRONMENT_VARIABLE,
EnvironmentVariableTarget.Machine);
List<string> pathsToLookup = new List<string>();
pathsToLookup.AddRange(processPaths);
pathsToLookup.AddRange(machinePaths);
pathsToLookup.AddRange(installationPaths);
string toolPath = FindToolInPaths(toolName, pathsToLookup);
if (string.IsNullOrEmpty(toolPath))
return null;
EnsureIsInProcessPathEnvVariable(toolPath, processPaths);
return toolPath;
}
static string FindToolInPaths(
string toolName,
List<string> paths)
{
foreach (string path in paths)
{
if (path == null)
continue;
if (path.Trim() == string.Empty)
continue;
string filePath = CleanFolderPath(path);
filePath = Path.Combine(filePath, toolName);
if (File.Exists(filePath))
return Path.GetFullPath(filePath);
}
return null;
}
static string CleanFolderPath(string folderPath)
{
foreach (char c in Path.GetInvalidPathChars())
folderPath = folderPath.Replace(c.ToString(), string.Empty);
return folderPath;
}
static List<string> GetPathsFromEnvVariable(
string variableName,
EnvironmentVariableTarget target)
{
string value = Environment.GetEnvironmentVariable(variableName, target);
return new List<string>(value.Split(Path.PathSeparator));
}
static void EnsureIsInProcessPathEnvVariable(
string toolPath,
List<string> processPaths)
{
string plasticInstallDir = Path.GetDirectoryName(toolPath);
if (processPaths.Contains(plasticInstallDir, StringComparer.OrdinalIgnoreCase))
return;
Environment.SetEnvironmentVariable(
PATH_ENVIRONMENT_VARIABLE,
string.Concat(plasticInstallDir, Path.PathSeparator, processPaths),
EnvironmentVariableTarget.Process);
}
const string PATH_ENVIRONMENT_VARIABLE = "PATH";
}
}
|