Skip to content

Commit 1fa8c7b

Browse files
committed
allow to make ProjectRoot finding configurable
1 parent 01e35ff commit 1fa8c7b

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.shiftleft.utils
22

3-
import better.files.File
3+
import java.nio.file.{Files, Path, Paths}
44

55
import scala.annotation.tailrec
66

@@ -15,27 +15,31 @@ import scala.annotation.tailrec
1515
* with this setting any more.
1616
*/
1717
object ProjectRoot {
18+
val FileNameToSearchForEnvVar = "FILE_IN_PROJECT_ROOT"
1819

19-
private val SEARCH_DEPTH = 4
2020
object SearchDepthExceededError extends Error
21+
22+
private val SEARCH_DEPTH = 4
23+
private lazy val filenameToSearchFor =
24+
sys.env
25+
.get(FileNameToSearchForEnvVar)
26+
.getOrElse(".git")
2127

2228
def relativise(path: String): String =
2329
s"$findRelativePath$path"
2430

2531
def findRelativePath: String = {
26-
val fileThatOnlyExistsInRoot = ".git"
27-
2832
@tailrec def loop(depth: Int): String = {
2933
val pathPrefix = "./" + "../" * depth
30-
if (File(s"$pathPrefix$fileThatOnlyExistsInRoot").exists) pathPrefix
34+
if (Files.exists(Paths.get(s"$pathPrefix$filenameToSearchFor"))) pathPrefix
3135
else if (depth < SEARCH_DEPTH) loop(depth + 1)
3236
else throw SearchDepthExceededError
3337
}
3438

3539
loop(0)
3640
}
3741

38-
def find: File =
39-
File(findRelativePath)
42+
def find: Path =
43+
Paths.get(findRelativePath)
4044

4145
}

0 commit comments

Comments
 (0)