@@ -55,25 +55,77 @@ object RipgrepSearcher {
55
55
val osName = System .getProperty(" os.name" ).lowercase(Locale .getDefault())
56
56
val binName = if (osName.contains(" win" )) " rg.exe" else " rg"
57
57
58
- // try get from /usr/local/bin/rg if macos
59
- if (osName.contains(" mac" )) {
58
+ return when {
59
+ osName.contains(" win" ) -> findRipgrepBinaryOnWindows(binName)
60
+ else -> findRipgrepBinaryOnUnix(binName)
61
+ }
62
+ }
63
+
64
+ private fun findRipgrepBinaryOnWindows (binName : String ): Path ? {
65
+ try {
66
+ val pb = ProcessBuilder (" where" , binName)
67
+ val process = pb.start()
68
+ if (process.waitFor(1 , TimeUnit .SECONDS ) && process.exitValue() == 0 ) {
69
+ val path = String (process.inputStream.readAllBytes(), StandardCharsets .UTF_8 )
70
+ .lines().firstOrNull()?.trim()
71
+ if (path != null ) {
72
+ return Paths .get(path)
73
+ }
74
+ }
75
+ } catch (e: Exception ) {
76
+ LOG .debug(" Failed to locate rg using 'where' command" , e)
77
+ }
78
+
79
+ // Check common installation locations on Windows
80
+ val commonPaths = listOf (
81
+ Paths .get(System .getenv(" ProgramFiles" ), " ripgrep" , binName),
82
+ Paths .get(System .getenv(" ProgramFiles(x86)" ), " ripgrep" , binName),
83
+ Paths .get(System .getenv(" USERPROFILE" ), " .cargo" , " bin" , binName)
84
+ )
85
+
86
+ for (path in commonPaths) {
87
+ if (path.toFile().exists()) {
88
+ return path
89
+ }
90
+ }
91
+
92
+ return findInPath(binName)
93
+ }
94
+
95
+ private fun findRipgrepBinaryOnUnix (binName : String ): Path ? {
96
+ // Check macOS specific location
97
+ if (System .getProperty(" os.name" ).lowercase(Locale .getDefault()).contains(" mac" )) {
60
98
val path = Paths .get(" /usr/local/bin/rg" )
61
99
if (path.toFile().exists()) {
62
100
return path
63
101
}
64
102
}
65
103
66
- val pb = ProcessBuilder (" which" , binName)
67
- val process = pb.start()
68
104
try {
105
+ val pb = ProcessBuilder (" which" , binName)
106
+ val process = pb.start()
69
107
if (process.waitFor(1 , TimeUnit .SECONDS ) && process.exitValue() == 0 ) {
70
108
val path = String (process.inputStream.readAllBytes(), StandardCharsets .UTF_8 ).trim { it <= ' ' }
71
109
return Paths .get(path)
72
110
}
73
- } catch (_ : InterruptedException ) {
74
- return null
111
+ } catch (e : Exception ) {
112
+ LOG .debug( " Failed to locate rg using 'which' command " , e)
75
113
}
76
114
115
+ return findInPath(binName)
116
+ }
117
+
118
+ private fun findInPath (executable : String ): Path ? {
119
+ val pathEnv = System .getenv(" PATH" ) ? : return null
120
+ val pathSeparator = if (System .getProperty(" os.name" ).lowercase().contains(" win" )) " ;" else " :"
121
+
122
+ for (dir in pathEnv.split(pathSeparator)) {
123
+ val path = Paths .get(dir, executable)
124
+ if (path.toFile().exists() && path.toFile().canExecute()) {
125
+ return path
126
+ }
127
+ }
128
+
77
129
return null
78
130
}
79
131
@@ -170,3 +222,4 @@ object RipgrepSearcher {
170
222
return base.relativize(target).toString().replace(' \\ ' , ' /' )
171
223
}
172
224
}
225
+
0 commit comments