From f840487d4b9e5d71b6fb9a79da7435789ec27f75 Mon Sep 17 00:00:00 2001 From: Pavel Marek Date: Fri, 24 Nov 2023 10:46:31 +0100 Subject: [PATCH] BenchProcessor is able to locate root directory with different name than "enso" --- .../main/java/org/enso/benchmarks/Utils.java | 23 +++++++++++-------- .../benchmarks/processor/BenchProcessor.java | 21 ++++------------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/lib/scala/bench-processor/src/main/java/org/enso/benchmarks/Utils.java b/lib/scala/bench-processor/src/main/java/org/enso/benchmarks/Utils.java index cfcc579fce5c..0dcb8011760d 100644 --- a/lib/scala/bench-processor/src/main/java/org/enso/benchmarks/Utils.java +++ b/lib/scala/bench-processor/src/main/java/org/enso/benchmarks/Utils.java @@ -2,6 +2,7 @@ import java.io.File; import java.net.URISyntaxException; +import java.nio.file.Files; /** Utility methods used by the benchmark classes from the generated code */ public class Utils { @@ -21,26 +22,30 @@ public static File findLanguageHomeOverride() { } /** - * Returns the root directory of the Enso repository. + * Locates the root of the Enso repository. Heuristic: we just keep going up the directory tree + * until we are in a directory containing ".git" subdirectory. Note that we cannot use the "enso" + * name, as users are free to name their cloned directories however they like. * * @return Non-null file pointing to the root directory of the Enso repository. */ public static File findRepoRootDir() { - File ensoDir; + File rootDir = null; try { - ensoDir = new File(Utils.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + rootDir = new File(Utils.class.getProtectionDomain().getCodeSource().getLocation().toURI()); } catch (URISyntaxException e) { - throw new IllegalStateException("Unrecheable: ensoDir not found", e); + throw new IllegalStateException("repository root directory not found: " + e.getMessage()); } - for (; ensoDir != null; ensoDir = ensoDir.getParentFile()) { - if (ensoDir.getName().equals("enso")) { + for (; rootDir != null; rootDir = rootDir.getParentFile()) { + // Check if rootDir contains ".git" subdirectory + if (Files.exists(rootDir.toPath().resolve(".git"))) { break; } } - if (ensoDir == null || !ensoDir.exists() || !ensoDir.isDirectory() || !ensoDir.canRead()) { - throw new IllegalStateException("Unrecheable: ensoDir does not exist or is not readable"); + if (rootDir == null || !rootDir.exists() || !rootDir.isDirectory() || !rootDir.canRead()) { + throw new IllegalStateException( + "Unreachable: repository root directory does not exist or is not readable"); } - return ensoDir; + return rootDir; } public static BenchSpec findSpecByName(BenchGroup group, String specName) { diff --git a/lib/scala/bench-processor/src/main/java/org/enso/benchmarks/processor/BenchProcessor.java b/lib/scala/bench-processor/src/main/java/org/enso/benchmarks/processor/BenchProcessor.java index c37a9dbf4ea5..bf83c716e16e 100644 --- a/lib/scala/bench-processor/src/main/java/org/enso/benchmarks/processor/BenchProcessor.java +++ b/lib/scala/bench-processor/src/main/java/org/enso/benchmarks/processor/BenchProcessor.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.net.URISyntaxException; +import java.nio.file.Files; import java.util.List; import java.util.Set; import java.util.logging.Level; @@ -20,6 +21,7 @@ import org.enso.benchmarks.BenchGroup; import org.enso.benchmarks.BenchSpec; import org.enso.benchmarks.ModuleBenchSuite; +import org.enso.benchmarks.Utils; import org.enso.polyglot.LanguageInfo; import org.enso.polyglot.MethodNames.TopScope; import org.enso.polyglot.RuntimeOptions; @@ -71,28 +73,13 @@ public class BenchProcessor extends AbstractProcessor { "import org.enso.benchmarks.Utils;"); public BenchProcessor() { - File currentDir = null; - try { - currentDir = - new File( - BenchProcessor.class.getProtectionDomain().getCodeSource().getLocation().toURI()); - } catch (URISyntaxException e) { - failWithMessage("ensoDir not found: " + e.getMessage()); - } - for (; currentDir != null; currentDir = currentDir.getParentFile()) { - if (currentDir.getName().equals("enso")) { - break; - } - } - if (currentDir == null) { - failWithMessage("Unreachable: Could not find Enso root directory"); - } - ensoDir = currentDir; + ensoDir = Utils.findRepoRootDir(); // Note that ensoHomeOverride does not have to exist, only its parent directory ensoHomeOverride = ensoDir.toPath().resolve("distribution").resolve("component").toFile(); } + @Override public SourceVersion getSupportedSourceVersion() { return SourceVersion.latest();