ULSAlgorithms 1.1.0-g3e5595996d
High-performance exact and heuristic algorithms for uncapacitated lot sizing
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 requiring
5/// their SDKs at compile time.
6/// </summary>
8{
9 /// <summary>
10 /// Resolves an executable from explicit environment variables,
11 /// installation-home hints, optional direct candidates, and PATH.
12 /// </summary>
13 public static string Resolve(
14 IEnumerable<string> explicitExecutableEnvironmentVariables,
15 IEnumerable<string> homeEnvironmentVariables,
16 IEnumerable<string> relativeExecutablePaths,
17 IEnumerable<string> pathExecutableNames,
18 IEnumerable<string>? directCandidates = null)
19 {
20 ArgumentNullException.ThrowIfNull(
21 explicitExecutableEnvironmentVariables);
22 ArgumentNullException.ThrowIfNull(homeEnvironmentVariables);
23 ArgumentNullException.ThrowIfNull(relativeExecutablePaths);
24 ArgumentNullException.ThrowIfNull(pathExecutableNames);
25
26 foreach (string variableName in
27 explicitExecutableEnvironmentVariables)
28 {
29 if (string.IsNullOrWhiteSpace(variableName))
30 {
31 continue;
32 }
33
34 string configured =
35 Environment.GetEnvironmentVariable(
36 variableName)?.Trim() ??
37 string.Empty;
38
39 string existing =
40 NormalizeExistingFile(configured);
41
42 if (!string.IsNullOrEmpty(existing))
43 {
44 return existing;
45 }
46 }
47
48 string[] relativePaths =
49 relativeExecutablePaths
50 .Where(static path =>
51 !string.IsNullOrWhiteSpace(path))
52 .ToArray();
53
54 foreach (string variableName in homeEnvironmentVariables)
55 {
56 if (string.IsNullOrWhiteSpace(variableName))
57 {
58 continue;
59 }
60
61 string home =
62 Environment.GetEnvironmentVariable(
63 variableName)?.Trim() ??
64 string.Empty;
65
66 if (string.IsNullOrWhiteSpace(home))
67 {
68 continue;
69 }
70
71 foreach (string relativePath in relativePaths)
72 {
73 string existing =
74 NormalizeExistingFile(
75 Path.Combine(home, relativePath));
76
77 if (!string.IsNullOrEmpty(existing))
78 {
79 return existing;
80 }
81 }
82 }
83
84 if (directCandidates is not null)
85 {
86 foreach (string candidate in directCandidates)
87 {
88 string existing =
89 NormalizeExistingFile(candidate);
90
91 if (!string.IsNullOrEmpty(existing))
92 {
93 return existing;
94 }
95 }
96 }
97
98 return FindOnPath(pathExecutableNames);
99 }
100
101 /// <summary>
102 /// Searches PATH for one of the specified executable names.
103 /// </summary>
104 public static string FindOnPath(
105 IEnumerable<string> executableNames)
106 {
107 ArgumentNullException.ThrowIfNull(executableNames);
108
109 string pathValue =
110 Environment.GetEnvironmentVariable("PATH") ??
111 string.Empty;
112
113 string[] directories =
114 pathValue.Split(
115 Path.PathSeparator,
116 StringSplitOptions.RemoveEmptyEntries |
117 StringSplitOptions.TrimEntries);
118
119 foreach (string directory in directories)
120 {
121 foreach (string executableName in executableNames)
122 {
123 if (string.IsNullOrWhiteSpace(executableName))
124 {
125 continue;
126 }
127
128 try
129 {
130 string existing =
131 NormalizeExistingFile(
132 Path.Combine(
133 directory,
134 executableName.Trim()));
135
136 if (!string.IsNullOrEmpty(existing))
137 {
138 return existing;
139 }
140 }
141 catch
142 {
143 // Ignore malformed PATH entries and continue.
144 }
145 }
146 }
147
148 return string.Empty;
149 }
150
151 private static string NormalizeExistingFile(
152 string? path)
153 {
154 if (string.IsNullOrWhiteSpace(path))
155 {
156 return string.Empty;
157 }
158
159 try
160 {
161 string expanded =
162 Environment.ExpandEnvironmentVariables(
163 path.Trim().Trim('"'));
164
165 return File.Exists(expanded)
166 ? Path.GetFullPath(expanded)
167 : string.Empty;
168 }
169 catch
170 {
171 return string.Empty;
172 }
173 }
174}
Locates optional native-solver command-line executables without requiring their SDKs at compile time.
static string Resolve(IEnumerable< string > explicitExecutableEnvironmentVariables, IEnumerable< string > homeEnvironmentVariables, IEnumerable< string > relativeExecutablePaths, IEnumerable< string > pathExecutableNames, IEnumerable< string >? directCandidates=null)
Resolves an executable from explicit environment variables, installation-home hints,...
static string FindOnPath(IEnumerable< string > executableNames)
Searches PATH for one of the specified executable names.