Skip to content

Commit

Permalink
optimization for the case where the m2 folder is not in the home dire…
Browse files Browse the repository at this point in the history
…ctory
  • Loading branch information
jurgenvinju committed Sep 24, 2024
1 parent e9fdfe0 commit 4221cb0
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/org/rascalmpl/uri/file/MavenRepositoryURIResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
* idea; but there is currently no way to register read-only logical schemes.
*/
public class MavenRepositoryURIResolver extends AliasedFileResolver {
private static String localRepoLocationCache;

private final Pattern authorityRegEx
= Pattern.compile("^([a-zA-Z0-9-_.]+?)[!]([a-zA-Z0-9-_.]+)([!][a-zA-Z0-9\\-_.]+)$");
// groupId ! artifactId ! optionAlComplexVersionString
Expand Down Expand Up @@ -113,7 +115,7 @@ private static String inferMavenRepositoryLocation() {
// note that since it does not exist this will make all downstream resolutions fail
// to "file does not exist"
}

return m2HomeFolder;
}

Expand All @@ -132,9 +134,14 @@ private static String computeMavenCommandName() {
/**
* This (slow) code runs only if the ~/.m2 folder does not exist and nobody -D'ed its location either.
* That is not necessarily how mvn prioritizes its configuration steps, but it is the way we can
* get a quick enough answer most of the time.
* get a quick enough answer most of the time. It caches its result to make sure repeated calls
* to here are faster than the first.
*/
private static String getLocalRepositoryLocationFromMavenCommand() {
if (localRepoLocationCache != null) {
return localRepoLocationCache;
}

try {
ProcessBuilder processBuilder = new ProcessBuilder(computeMavenCommandName(),
"-q",
Expand All @@ -150,7 +157,7 @@ private static String getLocalRepositoryLocationFromMavenCommand() {
}

try (var reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
return reader.lines().collect(Collectors.joining()).trim();
return (localRepoLocationCache = reader.lines().collect(Collectors.joining()).trim());
}
}
catch (IOException | InterruptedException e) {
Expand All @@ -159,7 +166,6 @@ private static String getLocalRepositoryLocationFromMavenCommand() {
}
}


@Override
public ISourceLocation resolve(ISourceLocation input) throws IOException {
String authority = input.getAuthority();
Expand Down

0 comments on commit 4221cb0

Please sign in to comment.