From 1fa8c7be4788ba126a09e5d4f4b872c471bdee6d Mon Sep 17 00:00:00 2001 From: Michael Pollmeier Date: Wed, 18 Jun 2025 14:54:18 +0200 Subject: [PATCH 1/2] allow to make ProjectRoot finding configurable --- .../scala/io/shiftleft/utils/ProjectRoot.scala | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/codepropertygraph/src/main/scala/io/shiftleft/utils/ProjectRoot.scala b/codepropertygraph/src/main/scala/io/shiftleft/utils/ProjectRoot.scala index ef15e506b..329d53c25 100644 --- a/codepropertygraph/src/main/scala/io/shiftleft/utils/ProjectRoot.scala +++ b/codepropertygraph/src/main/scala/io/shiftleft/utils/ProjectRoot.scala @@ -1,6 +1,6 @@ package io.shiftleft.utils -import better.files.File +import java.nio.file.{Files, Path, Paths} import scala.annotation.tailrec @@ -15,19 +15,23 @@ 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 } @@ -35,7 +39,7 @@ object ProjectRoot { loop(0) } - def find: File = - File(findRelativePath) + def find: Path = + Paths.get(findRelativePath) } From dfde96a749760634fe78ad0d3b1ab32cbff57995 Mon Sep 17 00:00:00 2001 From: Michael Pollmeier Date: Wed, 18 Jun 2025 17:39:25 +0200 Subject: [PATCH 2/2] fmt --- .../src/main/scala/io/shiftleft/utils/ProjectRoot.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codepropertygraph/src/main/scala/io/shiftleft/utils/ProjectRoot.scala b/codepropertygraph/src/main/scala/io/shiftleft/utils/ProjectRoot.scala index 329d53c25..0d2d8f433 100644 --- a/codepropertygraph/src/main/scala/io/shiftleft/utils/ProjectRoot.scala +++ b/codepropertygraph/src/main/scala/io/shiftleft/utils/ProjectRoot.scala @@ -18,9 +18,9 @@ object ProjectRoot { val FileNameToSearchForEnvVar = "FILE_IN_PROJECT_ROOT" object SearchDepthExceededError extends Error - + private val SEARCH_DEPTH = 4 - private lazy val filenameToSearchFor = + private lazy val filenameToSearchFor = sys.env .get(FileNameToSearchForEnvVar) .getOrElse(".git")