Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.shiftleft.utils

import better.files.File
import java.nio.file.{Files, Path, Paths}

import scala.annotation.tailrec

Expand All @@ -15,27 +15,31 @@ import scala.annotation.tailrec
* with this setting any more.
*/
object ProjectRoot {
val FileNameToSearchForEnvVar = "FILE_IN_PROJECT_ROOT"

private val SEARCH_DEPTH = 4
object SearchDepthExceededError extends Error

private val SEARCH_DEPTH = 4
private lazy val filenameToSearchFor =
sys.env
.get(FileNameToSearchForEnvVar)
.getOrElse(".git")

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

def findRelativePath: String = {
val fileThatOnlyExistsInRoot = ".git"

@tailrec def loop(depth: Int): String = {
val pathPrefix = "./" + "../" * depth
if (File(s"$pathPrefix$fileThatOnlyExistsInRoot").exists) pathPrefix
if (Files.exists(Paths.get(s"$pathPrefix$filenameToSearchFor"))) pathPrefix
else if (depth < SEARCH_DEPTH) loop(depth + 1)
else throw SearchDepthExceededError
}

loop(0)
}

def find: File =
File(findRelativePath)
def find: Path =
Paths.get(findRelativePath)

}