1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // Multi-Phasic Applications: SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc
.squirreljme
.plugin
.util
;
12 import java
.nio
.file
.Files
;
13 import java
.nio
.file
.Path
;
14 import java
.nio
.file
.Paths
;
15 import java
.util
.regex
.Pattern
;
16 import org
.gradle
.internal
.os
.OperatingSystem
;
19 * General path utilities.
23 public class PathUtils
25 /** Program files environment directories. */
26 private static final String
[] _PROGRAM_FILES_ENV
=
43 * Finds a file that is in {@code $PATH}.
45 * @param __name The name of the file
46 * @return The path to the file of the given name or {@code null}
48 * @throws NullPointerException On arguments.
51 public static Path
findPath(String __name
)
53 // Path should exist, but it might not
54 String pathEnv
= System
.getenv("PATH");
58 // Search each path piece for the given executable
59 for (String pathSegment
: pathEnv
.split(
60 Pattern
.quote(System
.getProperty("path.separator"))))
62 Path fullPath
= Paths
.get(pathSegment
).resolve(__name
);
64 // If we find it, cache it
65 if (Files
.isRegularFile(fullPath
) && Files
.isExecutable(fullPath
))
74 * Finds an executable that is in {@code $PATH}.
76 * @param __name The name of the executable
77 * @return The path to the executable of the given name or {@code null}
79 * @throws NullPointerException On arguments.
82 public static Path
findPathExecutable(String __name
)
83 throws NullPointerException
86 throw new NullPointerException("NARG");
88 // If we are on Windows there are multiple potential path executables
89 // for actual executables
90 if (OperatingSystem
.current() == OperatingSystem
.WINDOWS
)
92 // Get path extensions that exist
93 String pathExts
= System
.getenv("PathExt");
97 // Go through each extension and try to find it
98 for (String pathExt
: pathExts
.split(Pattern
.quote(";")))
100 Path found
= PathUtils
.findPath(__name
+ pathExt
);
106 // Otherwise just the exact name
107 return PathUtils
.findPath(__name
);
111 * Finds installed path of the given executable.
113 * @param __exe The executable to find.
114 * @param __programFiles The program files directory to look within.
115 * @return The found path or {@code null} if it was not found.
116 * @throws NullPointerException If {@code __exe} was not specified.
119 public static Path
findPathInstalled(String __exe
,
120 String __programFiles
)
121 throws NullPointerException
123 return PathUtils
.findPathInstalled(__exe
, Paths
.get(__programFiles
));
127 * Finds installed path of the given executable.
129 * @param __exe The executable to find.
130 * @param __programFiles The program files directory to look within.
131 * @return The found path or {@code null} if it was not found.
132 * @throws NullPointerException If {@code __exe} was not specified.
135 public static Path
findPathInstalled(String __exe
,
137 throws NullPointerException
140 throw new NullPointerException("NARG");
142 // Standard executable?
143 Path cmakePath
= PathUtils
.findPath(__exe
);
145 // Windows executable?
146 String exeName
= __exe
+ ".exe";
147 if (cmakePath
== null)
148 cmakePath
= PathUtils
.findPath(exeName
);
150 // Standard installation on Windows?
151 if (cmakePath
== null && __programFiles
!= null &&
152 OperatingSystem
.current() == OperatingSystem
.WINDOWS
)
154 for (String env
: PathUtils
._PROGRAM_FILES_ENV
)
156 if (cmakePath
!= null)
159 String programFiles
= System
.getenv(env
);
160 if (programFiles
!= null)
162 Path maybe
= Paths
.get(programFiles
)
163 .resolve(__programFiles
).resolve(exeName
);
164 if (Files
.exists(maybe
) && Files
.isExecutable(maybe
))
167 maybe
= Paths
.get(programFiles
)
168 .resolve(__programFiles
).resolve("bin")
170 if (Files
.exists(maybe
) && Files
.isExecutable(maybe
))
176 // Homebrew on macOS?
177 if (cmakePath
== null &&
178 OperatingSystem
.current() == OperatingSystem
.MAC_OS
)
180 Path maybe
= Paths
.get("/").resolve("opt")
181 .resolve("homebrew").resolve("bin")
183 if (Files
.exists(maybe
) && Files
.isExecutable(maybe
))