LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
ExternalSolverExecutableLocator.cs
Go to the documentation of this file.
2
3/// <summary>
4/// Locates optional native-solver command-line executables without creating
5/// a compile-time dependency on their SDKs.
6/// </summary>
8{
9 /// <summary>
10 /// Resolves an executable from an explicit environment variable,
11 /// installation-home hints and the current <c>PATH</c>.
12 /// </summary>
13 /// <param name="explicitExecutableEnvironmentVariable">
14 /// Environment variable that may contain a complete executable path.
15 /// </param>
16 /// <param name="homeEnvironmentVariables">
17 /// Environment variables that may contain an installation root.
18 /// </param>
19 /// <param name="relativeExecutablePaths">
20 /// Relative executable paths tested under every installation root.
21 /// </param>
22 /// <param name="pathExecutableNames">
23 /// Executable names searched in the current <c>PATH</c>.
24 /// </param>
25 /// <returns>Resolved full path, or an empty string.</returns>
26 public static string Resolve(
27 string explicitExecutableEnvironmentVariable,
28 IEnumerable<string> homeEnvironmentVariables,
29 IEnumerable<string> relativeExecutablePaths,
30 IEnumerable<string> pathExecutableNames)
31 {
32 ArgumentException.ThrowIfNullOrWhiteSpace(
33 explicitExecutableEnvironmentVariable);
34
35 ArgumentNullException.ThrowIfNull(
36 homeEnvironmentVariables);
37 ArgumentNullException.ThrowIfNull(
38 relativeExecutablePaths);
39 ArgumentNullException.ThrowIfNull(
40 pathExecutableNames);
41
42 string explicitPath =
43 Environment.GetEnvironmentVariable(
44 explicitExecutableEnvironmentVariable)?.Trim() ??
45 string.Empty;
46
47 if (File.Exists(explicitPath))
48 {
49 return Path.GetFullPath(
50 explicitPath);
51 }
52
53 string[] relativePaths =
54 relativeExecutablePaths
55 .Where(path => !string.IsNullOrWhiteSpace(path))
56 .ToArray();
57
58 foreach (string environmentVariable in homeEnvironmentVariables)
59 {
60 if (string.IsNullOrWhiteSpace(
61 environmentVariable))
62 {
63 continue;
64 }
65
66 string home =
67 Environment.GetEnvironmentVariable(
68 environmentVariable)?.Trim() ??
69 string.Empty;
70
71 if (string.IsNullOrWhiteSpace(home))
72 {
73 continue;
74 }
75
76 foreach (string relativePath in relativePaths)
77 {
78 string candidate =
79 Path.Combine(
80 home,
81 relativePath);
82
83 if (File.Exists(candidate))
84 {
85 return Path.GetFullPath(
86 candidate);
87 }
88 }
89 }
90
91 return FindOnPath(
92 pathExecutableNames);
93 }
94
95 /// <summary>
96 /// Searches the current process <c>PATH</c> for one of the specified
97 /// executable names.
98 /// </summary>
99 /// <param name="executableNames">Executable names to test.</param>
100 /// <returns>Resolved full path, or an empty string.</returns>
101 public static string FindOnPath(
102 IEnumerable<string> executableNames)
103 {
104 ArgumentNullException.ThrowIfNull(
105 executableNames);
106
107 string pathValue =
108 Environment.GetEnvironmentVariable("PATH") ??
109 string.Empty;
110
111 string[] directories =
112 pathValue.Split(
113 Path.PathSeparator,
114 StringSplitOptions.RemoveEmptyEntries |
115 StringSplitOptions.TrimEntries);
116
117 foreach (string directory in directories)
118 {
119 foreach (string executableName in executableNames)
120 {
121 if (string.IsNullOrWhiteSpace(
122 executableName))
123 {
124 continue;
125 }
126
127 try
128 {
129 string candidate =
130 Path.Combine(
131 directory,
132 executableName.Trim());
133
134 if (File.Exists(candidate))
135 {
136 return Path.GetFullPath(
137 candidate);
138 }
139 }
140 catch
141 {
142 // Ignore malformed PATH entries and keep searching.
143 }
144 }
145 }
146
147 return string.Empty;
148 }
149}
Locates optional native-solver command-line executables without creating a compile-time dependency on...
static string FindOnPath(IEnumerable< string > executableNames)
Searches the current process PATH for one of the specified executable names.
static string Resolve(string explicitExecutableEnvironmentVariable, IEnumerable< string > homeEnvironmentVariables, IEnumerable< string > relativeExecutablePaths, IEnumerable< string > pathExecutableNames)
Resolves an executable from an explicit environment variable, installation-home hints and the current...