|
12 | 12 | import java.nio.file.Paths;
|
13 | 13 | import java.nio.file.attribute.BasicFileAttributes;
|
14 | 14 | import java.util.ArrayList;
|
| 15 | +import java.util.Arrays; |
15 | 16 | import java.util.Collections;
|
16 | 17 | import java.util.EnumSet;
|
17 | 18 | import java.util.List;
|
| 19 | +import java.util.Map; |
18 | 20 | import java.util.Objects;
|
19 | 21 |
|
20 | 22 | /**
|
@@ -135,7 +137,7 @@ public static ReportsDirectory fromObject(Object obj) {
|
135 | 137 | public static Path getPathForObject(Object obj) {
|
136 | 138 | String[] subdirs = {};
|
137 | 139 | if (obj instanceof PathModifier) {
|
138 |
| - String message = String.format("Null path modifier returned by: %s", obj.getClass().getName()); |
| 140 | + String message = String.format("Null path modifier returned by: %s", obj.getClass().getName()); |
139 | 141 | subdirs = Objects.requireNonNull(((PathModifier) obj).getSubPath(), message);
|
140 | 142 | }
|
141 | 143 | return fromObject(obj).getPath(subdirs);
|
@@ -247,6 +249,77 @@ public String getNewName() {
|
247 | 249 | return newName;
|
248 | 250 | }
|
249 | 251 | }
|
| 252 | + |
| 253 | + /** |
| 254 | + * Get the system file path as a path-delimited string. |
| 255 | + * <p> |
| 256 | + * <b>NOTE</b>: The initial entries in the returned path string are derived from {@link System#getenv()}. |
| 257 | + * When running on {@code Mac OS X}, additional entries are acquired from {@code /etc/paths} |
| 258 | + * and the files found in the {@code /etc/paths.d} folder. |
| 259 | + * |
| 260 | + * @return system file path as a path-delimited string |
| 261 | + */ |
| 262 | + public static String getSystemPath() { |
| 263 | + List<String> pathList = new ArrayList<>(); |
| 264 | + addSystemPathList(pathList); |
| 265 | + addMacintoshPathList(pathList); |
| 266 | + return String.join(File.pathSeparator, pathList); |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * Append the system path entries to the specified list. |
| 271 | + * <p> |
| 272 | + * <b>NOTE</b>: Added entries are derived from {@link System#getenv()}. |
| 273 | + * |
| 274 | + * @param pathList existing list to receive system path entries |
| 275 | + * @return {@code true} if entries were appended; otherwise {@code false} |
| 276 | + */ |
| 277 | + public static boolean addSystemPathList(List<String> pathList) { |
| 278 | + String name = "PATH"; |
| 279 | + Map<String, String> env = System.getenv(); |
| 280 | + if (!env.containsKey(name)) { |
| 281 | + for (String key : env.keySet()) { |
| 282 | + if (name.equalsIgnoreCase(key)) { |
| 283 | + name = key; |
| 284 | + break; |
| 285 | + } |
| 286 | + } |
| 287 | + } |
| 288 | + String path = env.get(name); |
| 289 | + return (path != null) ? pathList.addAll(Arrays.asList(path.split(File.pathSeparator))) : false; |
| 290 | + } |
| 291 | + |
| 292 | + /** |
| 293 | + * Append Macintosh path entries to the specified list. |
| 294 | + * <p> |
| 295 | + * <b>NOTE</b>: When running on {@code Mac OS X}, added entries are acquired from {@code /etc/paths} |
| 296 | + * and the files found in the {@code /etc/paths.d} folder. |
| 297 | + * |
| 298 | + * @param pathList existing list to receive Macintosh path entries |
| 299 | + * @return {@code true} if entries were appended; otherwise {@code false} |
| 300 | + */ |
| 301 | + public static boolean addMacintoshPathList(List<String> pathList) { |
| 302 | + boolean didChange = false; |
| 303 | + if (System.getProperty("os.name").startsWith("Mac")) { |
| 304 | + List<String> pathFileList = new ArrayList<>(); |
| 305 | + pathFileList.add("/etc/paths"); |
| 306 | + String[] paths = new File("/etc/paths.d").list(); |
| 307 | + if (paths != null) { |
| 308 | + pathFileList.addAll(Arrays.asList(paths)); |
| 309 | + } |
| 310 | + for (String thisPathFile : pathFileList) { |
| 311 | + File pathFile = new File(thisPathFile); |
| 312 | + if (pathFile.exists()) { |
| 313 | + try { |
| 314 | + didChange |= pathList.addAll(Files.readAllLines(pathFile.toPath())); |
| 315 | + } catch (IOException eaten) { |
| 316 | + // nothing to do here |
| 317 | + } |
| 318 | + } |
| 319 | + } |
| 320 | + } |
| 321 | + return didChange; |
| 322 | + } |
250 | 323 |
|
251 | 324 | /**
|
252 | 325 | * Prepend the specified string to the indicated array.
|
|
0 commit comments