Set calling convention for sjme_scritchui_core_grabExternalThreadId.
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / util / PathUtils.java
blob59b3abe2f82754916d0b2a0d09df34f036a02a2d
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;
18 /**
19 * General path utilities.
21 * @since 2023/06/04
23 public class PathUtils
25 /** Program files environment directories. */
26 private static final String[] _PROGRAM_FILES_ENV =
28 "PROGRAMFILES",
29 "PROGRAMFILES(X86)",
30 "PROGRAMW6432"
33 /**
34 * Not used.
36 * @since 2023/06/04
38 private PathUtils()
42 /**
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}
47 * if not found.
48 * @throws NullPointerException On arguments.
49 * @since 2023/06/04
51 public static Path findPath(String __name)
53 // Path should exist, but it might not
54 String pathEnv = System.getenv("PATH");
55 if (pathEnv == null)
56 return null;
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))
66 return fullPath;
69 // Not found
70 return null;
73 /**
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}
78 * if not found.
79 * @throws NullPointerException On arguments.
80 * @since 2023/06/04
82 public static Path findPathExecutable(String __name)
83 throws NullPointerException
85 if (__name == null)
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");
94 if (pathExts == null)
95 pathExts = ".exe";
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);
101 if (found != null)
102 return found;
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.
117 * @since 2024/06/15
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.
133 * @since 2024/06/15
135 public static Path findPathInstalled(String __exe,
136 Path __programFiles)
137 throws NullPointerException
139 if (__exe == null)
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)
157 break;
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))
165 return maybe;
167 maybe = Paths.get(programFiles)
168 .resolve(__programFiles).resolve("bin")
169 .resolve(exeName);
170 if (Files.exists(maybe) && Files.isExecutable(maybe))
171 return 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")
182 .resolve(__exe);
183 if (Files.exists(maybe) && Files.isExecutable(maybe))
184 return maybe;
187 return cmakePath;