Skip to content

Commit

Permalink
Allow to specify dir containing jars as a tool dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
gayaldassanayake committed Jun 8, 2023
1 parent 92f12b3 commit 07ecfd4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
5 changes: 5 additions & 0 deletions compiler/ballerina-lang/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@
<Method name="extractBala"/>
<Bug pattern="RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE"/>
</Match>
<Match>
<Class name="io.ballerina.projects.internal.model.BalToolDescriptor"/>
<Method name="getToolJarsMatchingPattern"/>
<Bug pattern="RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE"/>
</Match>
<Match>
<Class name="io.ballerina.projects.CodeAnalyzerManager$CompilationAnalysisTask"/>
<Bug pattern="BC_UNCONFIRMED_CAST_OF_RETURN_VALUE"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,18 @@
import io.ballerina.toml.semantic.ast.TomlValueNode;
import io.ballerina.toml.semantic.ast.TopLevelNode;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* {@code BalToolDescriptor} Model for `BalTool.toml` file.
Expand Down Expand Up @@ -130,7 +139,15 @@ private static List<BalToolDescriptor.Dependency> getDependencies(TomlTableNode

for (TomlTableNode dependencyNode : dependencyTableArray.children()) {
TopLevelNode pathNode = dependencyNode.entries().get(PATH);
dependencies.add(new BalToolDescriptor.Dependency(getStringFromTomlTableNode(pathNode)));
String path = getStringFromTomlTableNode(pathNode);
if (path == null) {
continue;
}
if (new File(path).isFile()) {
dependencies.add(new BalToolDescriptor.Dependency(path));
} else {
dependencies.addAll(getDependenciesInDir(path));
}
}
}
return dependencies;
Expand Down Expand Up @@ -158,4 +175,41 @@ private static String getStringFromTomlTableNode(TopLevelNode topLevelNode) {
}
return null;
}

private static List<BalToolDescriptor.Dependency> getDependenciesInDir(String path) {
// in the path provided, only the last part can be a pattern. the rest should be an existing directory path

List<BalToolDescriptor.Dependency> dependencies = new ArrayList<>();
Path dependencyPath = Path.of(path);
Optional<Path> patternPath = Optional.ofNullable(dependencyPath.getFileName());
if (patternPath.isEmpty()) {
return dependencies;
}
String pattern = patternPath.get().toString();
Optional<Path> parentPath = Optional.ofNullable(dependencyPath.getParent());
if (parentPath.isEmpty() || !parentPath.get().toFile().exists()) {
return dependencies;
}

return getToolJarsMatchingPattern(pattern, parentPath.get()).stream()
.map(path1 -> new BalToolDescriptor.Dependency(path1.toString()))
.collect(Collectors.toList());
}

private static List<Path> getToolJarsMatchingPattern(String pattern, Path parentPath) {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
List<Path> matchingPaths = new ArrayList<>();
try (Stream<Path> paths = Files.list(parentPath)) {
paths.filter(Files::isRegularFile)
.filter(path -> {
Path fileName = path.getFileName();
return fileName != null && fileName.toString().endsWith(".jar");
})
.filter(path -> pathMatcher.matches(path.getFileName()))
.forEach(matchingPaths::add);
} catch (IOException e) {
// ignore
}
return matchingPaths;
}
}

0 comments on commit 07ecfd4

Please sign in to comment.