| 31 | import org.openqa.selenium.Platform; |
| 32 | |
| 33 | public class ExecutableFinder { |
| 34 | private static final List<String> ENDINGS = |
| 35 | Platform.getCurrent().is(WINDOWS) |
| 36 | ? List.of("", ".cmd", ".exe", ".com", ".bat") |
| 37 | : singletonList(""); |
| 38 | |
| 39 | /** |
| 40 | * Find the executable by scanning the file system and the PATH. In the case of Windows this |
| 41 | * method allows common executable endings (".com", ".bat" and ".exe") to be omitted. |
| 42 | * |
| 43 | * @param named The name of the executable to find |
| 44 | * @return The absolute path to the executable, or null if no match is made. |
| 45 | */ |
| 46 | @Nullable |
| 47 | public String find(String named) { |
| 48 | File file = new File(named); |
| 49 | if (canExecute(file)) { |
| 50 | return named; |
| 51 | } |
| 52 | |
| 53 | if (Platform.getCurrent().is(Platform.WINDOWS)) { |
| 54 | file = new File(named + ".exe"); |
| 55 | if (canExecute(file)) { |
| 56 | return named + ".exe"; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | List<String> pathSegments = new ArrayList<>(fromEnvironment()); |
| 61 | if (Platform.getCurrent().is(Platform.MAC)) { |
| 62 | pathSegments.addAll(macSpecificPathSegments()); |
| 63 | } |
| 64 | |
| 65 | for (String pathSegment : pathSegments) { |
| 66 | for (String ending : ENDINGS) { |
| 67 | file = new File(pathSegment, named + ending); |
| 68 | if (canExecute(file)) { |
| 69 | return file.getAbsolutePath(); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | private List<String> fromEnvironment() { |
| 77 | String pathName = "PATH"; |
| 78 | Map<String, String> env = System.getenv(); |
| 79 | if (!env.containsKey(pathName)) { |
| 80 | for (String key : env.keySet()) { |
| 81 | if (pathName.equalsIgnoreCase(key)) { |
| 82 | pathName = key; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | String path = env.get(pathName); |
| 88 | return path != null ? List.of(path.split(File.pathSeparator)) : emptyList(); |
| 89 | } |
| 90 |
nothing calls this directly
no test coverage detected